Skip to content
Prev Previous commit
Next Next commit
WIP on wanl/version-extraction
  • Loading branch information
wanlwanl committed May 23, 2024
commit 44a4abf1454ca8f4e42d08222ea313590a87a6f9
1 change: 1 addition & 0 deletions tools/js-sdk-release-tools/src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_API_VERSION = "1.0.0"
4 changes: 4 additions & 0 deletions tools/js-sdk-release-tools/src/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum CodeGenLevel {
Classic = 'Classic',
Modular = 'Modular',
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ import {
import {isGeneratedCodeStable} from "./isGeneratedCodeStable";
import {execSync} from "child_process";
import { getversionDate } from "../../utils/version";
import { CodeGenLevel } from "../../common/types"

import fs from 'fs';
import path from 'path';
import shell from 'shelljs';

enum CodeGenLevel {
Classic = 'Classic',
Modular = 'Modular',
};

function getCodeGenLevel(parametersPath: string) : CodeGenLevel {
const exist = shell.test('-e', parametersPath);
const level = exist ? CodeGenLevel.Classic : CodeGenLevel.Modular;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,61 @@
import {Project, ScriptTarget} from "ts-morph";
import shell from 'shelljs';
import path from 'path';

import {CodeGenLevel} from "../../common/types"
import {DEFAULT_API_VERSION} from "../../common/constants"

function getClassicClientParametersPath(packageRoot: string): string {
return path.join(packageRoot, 'src', 'models', 'parameters.ts');
}

function getCodeGenLevel(parametersPath: string) : CodeGenLevel {
const exist = shell.test('-e', parametersPath);
const level = exist ? CodeGenLevel.Classic : CodeGenLevel.Modular;
console.log(`CodeGen Level: ${level} detected`);
return level;
}

// TODO
function findApiVersionFromRestClient(packageRoot: string): string | null {return null;}

// TODO
function findApiVersionFromOperations(packageRoot: string): string | null {
const paraPath = path.join(packageRoot, 'src/rest/parameters.ts');
return null;
}

function findModularClientApiVersion(packageRoot: string): string | null {
const clientApiVersion = findApiVersionFromRestClient(packageRoot);
if (clientApiVersion) return clientApiVersion;

const operationApiVersion = findApiVersionFromOperations(packageRoot);
return operationApiVersion;
}

function findClassicClientApiVersion(packageRoot: string): string {
const project = new Project({
compilerOptions: {
target: ScriptTarget.ES2015,
},
});
const paraPath = getClassicClientParametersPath(packageRoot);
project.addSourceFileAtPath(paraPath);
const source = project.getSourceFile(paraPath);
const variableDeclarations = source?.getVariableDeclarations();
if (!variableDeclarations) return DEFAULT_API_VERSION;
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;
}
return match[1];
}
}
return DEFAULT_API_VERSION;
}

export function isGeneratedCodeStable(filePath: string) {
const project = new Project({
Expand All @@ -23,4 +80,12 @@ export function isGeneratedCodeStable(filePath: string) {
}
}
return true;
}

function getClientApiVersion(packageRoot: string) {
const path = getClassicClientParametersPath(packageRoot);
const codeGenLevel = getCodeGenLevel(path);
return codeGenLevel == CodeGenLevel.Classic ?
findClassicClientApiVersion(packageRoot) :
findModularClientApiVersion(packageRoot);
}