-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…8332) * fix #8305 * update version * changelog-temp should keeps for gen changelog.md * Revert "changelog-temp should keeps for gen changelog.md" This reverts commit 2f37ada. * cleanup * cleanup * WIP on wanl/version-extraction * part 1 * part 1 * fix no feature bug: specify wrong api.md path for MLC * cleanup * support mix api versions in operations * get api.md in a better way * rename sdktype --------- Co-authored-by: albertxavier100 <[email protected]>
- Loading branch information
1 parent
c59ab4e
commit a488537
Showing
11 changed files
with
612 additions
and
79 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ApiVersionType } from "./types"; | ||
|
||
export interface IApiVersionTypeExtractor { | ||
(packageRoot: string): ApiVersionType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export enum SDKType { | ||
HighLevelClient = 'HighLevelClient', | ||
RestLevelClient = 'RestLevelClient', | ||
ModularClient = 'ModularClient', | ||
}; | ||
|
||
export enum ApiVersionType { | ||
None = 'None', | ||
Stable = 'Stable', | ||
Preview = 'Preview', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import shell from 'shelljs'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
import { SDKType } from './types' | ||
import { logger } from "../utils/logger"; | ||
import { Project, ScriptTarget, SourceFile } from 'ts-morph'; | ||
|
||
export function getClassicClientParametersPath(packageRoot: string): string { | ||
return path.join(packageRoot, 'src', 'models', 'parameters.ts'); | ||
} | ||
|
||
export function getSDKType(packageRoot: string): SDKType { | ||
const paraPath = getClassicClientParametersPath(packageRoot); | ||
const exist = shell.test('-e', paraPath); | ||
const type = exist ? SDKType.HighLevelClient : SDKType.ModularClient; | ||
logger.logInfo(`SDK type: ${type} detected in ${packageRoot}`); | ||
return type; | ||
} | ||
|
||
export function getNpmPackageName(packageRoot: string): string { | ||
const packageJsonPath = path.join(packageRoot, 'package.json'); | ||
const packageJson = fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }); | ||
const packageName = JSON.parse(packageJson).name; | ||
return packageName; | ||
} | ||
|
||
export function getApiReviewPath(packageRoot: string): string { | ||
const sdkType = getSDKType(packageRoot); | ||
const reviewDir = path.join(packageRoot, 'review'); | ||
switch (sdkType) { | ||
case SDKType.ModularClient: | ||
const npmPackageName = getNpmPackageName(packageRoot); | ||
const packageName = npmPackageName.substring("@azure/".length); | ||
const apiViewFileName = `${packageName}.api.md`; | ||
return path.join(packageRoot, 'review', apiViewFileName); | ||
case SDKType.HighLevelClient: | ||
case SDKType.RestLevelClient: | ||
default: | ||
// only one xxx.api.md | ||
return path.join(packageRoot, 'review', fs.readdirSync(reviewDir)[0]); | ||
} | ||
} | ||
|
||
export function getTsSourceFile(filePath: string): SourceFile | undefined { | ||
const target = ScriptTarget.ES2015; | ||
const compilerOptions = { target }; | ||
const project = new Project({ compilerOptions }); | ||
project.addSourceFileAtPath(filePath); | ||
return project.getSourceFile(filePath); | ||
} |
24 changes: 24 additions & 0 deletions
24
tools/js-sdk-release-tools/src/hlc/apiVersion/apiVersionTypeExtractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ApiVersionType } from "../../common/types" | ||
import { IApiVersionTypeExtractor } from "../../common/interfaces"; | ||
import { getClassicClientParametersPath, getTsSourceFile } from "../../common/utils"; | ||
|
||
// TODO: add unit test | ||
export const getApiVersionType: IApiVersionTypeExtractor = (packageRoot: string): ApiVersionType => { | ||
const paraPath = getClassicClientParametersPath(packageRoot); | ||
const source = getTsSourceFile(paraPath); | ||
const variableDeclarations = source?.getVariableDeclarations(); | ||
if (!variableDeclarations) return ApiVersionType.Stable; | ||
for (const variableDeclaration of variableDeclarations) { | ||
const fullText = variableDeclaration.getFullText(); | ||
if (fullText.toLowerCase().includes('apiversion')) { | ||
const match = fullText.match(/defaultValue: "([0-9a-z-]+)"/); | ||
if (!match || match.length !== 2) { | ||
continue; | ||
} | ||
if (match[1].includes('preview')) { | ||
return ApiVersionType.Preview; | ||
} | ||
} | ||
} | ||
return ApiVersionType.Stable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
tools/js-sdk-release-tools/src/hlc/utils/isGeneratedCodeStable.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
tools/js-sdk-release-tools/src/mlc/apiVersion/apiVersionTypeExtractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { SourceFile, SyntaxKind } from "ts-morph"; | ||
import shell from 'shelljs'; | ||
import path from 'path'; | ||
|
||
import { ApiVersionType } from "../../common/types" | ||
import { IApiVersionTypeExtractor } from "../../common/interfaces"; | ||
import { getTsSourceFile } from "../../common/utils"; | ||
|
||
const findRestClientPath = (packageRoot: string): string => { | ||
const restPath = path.join(packageRoot, 'src/rest/'); | ||
const fileNames = shell.ls(restPath); | ||
const clientFiles = fileNames.filter(f => f.endsWith("Client.ts")); | ||
if (clientFiles.length !== 1) throw new Error(`Single client is supported, but found ${clientFiles}`); | ||
|
||
const clientPath = path.join(restPath, clientFiles[0]); | ||
return clientPath; | ||
}; | ||
|
||
const matchPattern = (text: string, pattern: RegExp): string | undefined => { | ||
const match = text.match(pattern); | ||
const found = match != null && match.length === 2; | ||
return found ? match?.at(1) : undefined; | ||
} | ||
|
||
const findApiVersionInRestClient = (clientPath: string): string | undefined => { | ||
const sourceFile = getTsSourceFile(clientPath); | ||
const createClientFunction = sourceFile?.getFunction("createClient"); | ||
if (!createClientFunction) throw new Error("Function 'createClient' not found."); | ||
|
||
const apiVersionStatements = createClientFunction.getStatements() | ||
.filter(s => | ||
s.getKind() === SyntaxKind.ExpressionStatement && | ||
s.getText().indexOf("options.apiVersion") > -1); | ||
if (apiVersionStatements.length === 0) return undefined; | ||
|
||
const text = apiVersionStatements[apiVersionStatements.length - 1].getText(); | ||
const pattern = /(\d{4}-\d{2}-\d{2}(?:-preview)?)/; | ||
const apiVersion = matchPattern(text, pattern); | ||
return apiVersion; | ||
}; | ||
|
||
const getApiVersionTypeFromRestClient: IApiVersionTypeExtractor = (packageRoot: string): ApiVersionType => { | ||
const clientPath = findRestClientPath(packageRoot); | ||
const apiVersion = findApiVersionInRestClient(clientPath); | ||
if (apiVersion && apiVersion.indexOf("-preview") >= 0) return ApiVersionType.Preview; | ||
if (apiVersion && apiVersion.indexOf("-preview") < 0) return ApiVersionType.Stable; | ||
return ApiVersionType.None; | ||
}; | ||
|
||
const findApiVersionsInOperations = (sourceFile: SourceFile | undefined): Array<string> | undefined => { | ||
const interfaces = sourceFile?.getInterfaces(); | ||
const interfacesWithApiVersion = interfaces?.filter(itf => itf.getProperty('"api-version"')); | ||
const apiVersions = interfacesWithApiVersion?.map(itf => { | ||
const property = itf.getMembers() | ||
.filter(m => { | ||
const defaultValue = m.getChildrenOfKind(SyntaxKind.StringLiteral)[0]; | ||
return defaultValue && defaultValue.getText() === '"api-version"'; | ||
})[0]; | ||
const apiVersion = property.getChildrenOfKind(SyntaxKind.LiteralType)[0].getText(); | ||
return apiVersion; | ||
}); | ||
return apiVersions; | ||
} | ||
|
||
const getApiVersionTypeFromOperations: IApiVersionTypeExtractor = (packageRoot: string): ApiVersionType => { | ||
const paraPath = path.join(packageRoot, 'src/rest/parameters.ts'); | ||
const sourceFile = getTsSourceFile(paraPath); | ||
const apiVersions = findApiVersionsInOperations(sourceFile); | ||
if (!apiVersions) return ApiVersionType.None; | ||
const previewVersions = apiVersions.filter(v => v.indexOf("-preview") >= 0); | ||
return previewVersions.length > 0 ? ApiVersionType.Preview : ApiVersionType.Stable; | ||
}; | ||
|
||
// TODO: add unit test | ||
export const getApiVersionType: IApiVersionTypeExtractor = (packageRoot: string): ApiVersionType => { | ||
const typeFromClient = getApiVersionTypeFromRestClient(packageRoot); | ||
if (typeFromClient !== ApiVersionType.None) return typeFromClient; | ||
const typeFromOperations = getApiVersionTypeFromOperations(packageRoot); | ||
if (typeFromOperations !== ApiVersionType.None) return typeFromOperations; | ||
return ApiVersionType.Stable; | ||
} |
19 changes: 19 additions & 0 deletions
19
tools/js-sdk-release-tools/src/xlc/apiVersion/apiVersionTypeExtractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getSDKType } from "../../common/utils"; | ||
import { ApiVersionType, SDKType } from "../../common/types"; | ||
import { IApiVersionTypeExtractor } from "../../common/interfaces"; | ||
import * as mlcApi from '../../mlc/apiVersion/apiVersionTypeExtractor' | ||
import * as hlcApi from '../../hlc/apiVersion/apiVersionTypeExtractor' | ||
|
||
// TODO: move to x-level-client folder | ||
export const getApiVersionType: IApiVersionTypeExtractor = (packageRoot: string): ApiVersionType => { | ||
const sdkType = getSDKType(packageRoot); | ||
switch (sdkType) { | ||
case SDKType.ModularClient: | ||
return mlcApi.getApiVersionType(packageRoot); | ||
case SDKType.HighLevelClient: | ||
return hlcApi.getApiVersionType(packageRoot); | ||
default: | ||
console.warn(`Unsupported SDK type ${sdkType} to get detact api version`); | ||
return ApiVersionType.None; | ||
} | ||
} |