Skip to content

Commit a81314f

Browse files
authored
Merge pull request #1561 from openapi-env-test/master
Refactor sdkautomation scripts
2 parents 27fd4d8 + 40d8018 commit a81314f

File tree

15 files changed

+420
-1555
lines changed

15 files changed

+420
-1555
lines changed

.sdkauto/generateScript.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
PARAMS=$(cat $1 | jq '{localPath: .specFolder, readmeFiles: .relatedReadmeMdFiles, $outputPath}' -c --arg outputPath $2)
6+
7+
echo $PARAMS
8+
9+
pushd generator
10+
11+
npm run generate-all $PARAMS

.sdkauto/initScript.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
pushd generator
6+
7+
npm install
8+
9+
popd

.vscode/launch.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,20 @@
3434
"name": "Generate all",
3535
"runtimeArgs": ["-r", "ts-node/register"],
3636
"cwd": "${workspaceFolder}/generator",
37-
"args": ["${workspaceFolder}/generator/cmd/generateall.ts"]
37+
"args": [
38+
"${workspaceFolder}/generator/cmd/generateall.ts"
39+
]
40+
},
41+
{
42+
"type": "node",
43+
"request": "launch",
44+
"name": "Generate all - local",
45+
"runtimeArgs": ["-r", "ts-node/register"],
46+
"cwd": "${workspaceFolder}/generator",
47+
"args": [
48+
"${workspaceFolder}/generator/cmd/generateall.ts",
49+
"{\"localPath\":\"/home/me/workspace/azure-rest-api-specs\",\"readmeFiles\":[\"specification/signalr/resource-manager/readme.md\",\"specification/appplatform/resource-manager/readme.md\"],\"outputPath\":\"../generateOutput.json\"}"
50+
]
3851
}
3952
],
4053
"inputs": [

generator/cmd/generateall.ts

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import * as constants from '../constants';
2-
import { cloneAndGenerateBasePaths, validateAndReturnReadmePath } from '../specs';
2+
import { cloneAndGenerateBasePaths, getPackageString, resolveAbsolutePath, validateAndReturnReadmePath } from '../specs';
33
import { SchemaConfiguration, generateSchemas, clearAutogeneratedSchemaRefs, saveAutogeneratedSchemaRefs } from '../generate';
44
import { getAutogenlist } from '../autogenlist';
55
import chalk from 'chalk';
6-
import { flatten, chunk } from 'lodash';
7-
import { executeSynchronous, chunker } from '../utils';
6+
import { flatten } from 'lodash';
7+
import { executeSynchronous, chunker, writeJsonFile } from '../utils';
8+
import { Package } from '../models';
89

910
interface GenerateAllParams {
1011
batchCount?: number,
1112
batchIndex?: number,
13+
localPath?: string,
14+
readmeFiles?: string[],
15+
outputPath?: string,
1216
}
1317

1418
function parseParams(): GenerateAllParams {
@@ -27,29 +31,62 @@ executeSynchronous(async () => {
2731
filteredAutogenlist = chunker(filteredAutogenlist, params.batchCount)[params.batchIndex];
2832
}
2933

30-
await cloneAndGenerateBasePaths(constants.specsRepoPath, constants.specsRepoUri, constants.specsRepoCommitHash);
34+
let localPath = params.localPath;
35+
if (!localPath) {
36+
localPath = constants.specsRepoPath;
37+
await cloneAndGenerateBasePaths(localPath, constants.specsRepoUri, constants.specsRepoCommitHash);
38+
} else {
39+
localPath = await resolveAbsolutePath(localPath);
40+
}
41+
42+
if (!!params.readmeFiles) {
43+
filteredAutogenlist = filteredAutogenlist.filter(c => {
44+
let r = params.readmeFiles?.find(f => f.startsWith('specification/' + c.basePath));
45+
if (!!r) {
46+
c.readmeFile = r;
47+
return true;
48+
}
49+
return false;
50+
}
51+
);
52+
}
3153

3254
await clearAutogeneratedSchemaRefs(filteredAutogenlist);
3355

3456
const schemaConfigs: SchemaConfiguration[] = [];
3557
const errors = [];
58+
const packages: Package[] = [];
3659
for (const autogenlistConfig of filteredAutogenlist) {
60+
let pkg = {
61+
path: ['schemas']
62+
} as Package;
3763
try {
38-
const readme = await validateAndReturnReadmePath(autogenlistConfig.basePath);
64+
const readme = await validateAndReturnReadmePath(localPath, autogenlistConfig.readmeFile || autogenlistConfig.basePath);
65+
pkg.packageName = getPackageString(readme);
3966

4067
const newConfigs = await generateSchemas(readme, autogenlistConfig);
4168
schemaConfigs.push(...newConfigs);
69+
pkg.result = 'succeeded';
4270
} catch(error) {
71+
pkg.packageName = autogenlistConfig.basePath;
72+
pkg.result = 'failed';
4373
console.log(chalk.red(`Caught exception processing autogenlist entry ${autogenlistConfig.basePath}.`));
4474
console.log(chalk.red(error));
4575

4676
errors.push(error);
4777
}
78+
packages.push(pkg);
4879
}
4980

5081
await saveAutogeneratedSchemaRefs(flatten(schemaConfigs));
5182

52-
if (errors.length > 0) {
53-
throw new Error(`Autogeneration failed with ${errors.length} errors. See logs for detailed information.`);
83+
if (!!params.outputPath) {
84+
const outputPath = await resolveAbsolutePath(params.outputPath);
85+
await writeJsonFile(outputPath, { packages });
86+
} else {
87+
if (errors.length > 0) {
88+
throw new Error(`Autogeneration failed with ${errors.length} errors. See logs for detailed information.`);
89+
}
5490
}
91+
5592
});

generator/cmd/generatesingle.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as constants from '../constants';
2-
import { cloneAndGenerateBasePaths, validateAndReturnReadmePath } from '../specs';
2+
import { cloneAndGenerateBasePaths, resolveAbsolutePath, validateAndReturnReadmePath, getPackageString } from '../specs';
33
import { generateSchemas, saveAutogeneratedSchemaRefs } from '../generate';
44
import process from 'process';
55
import { findAutogenEntries } from '../autogenlist';
@@ -8,11 +8,17 @@ import { executeSynchronous } from '../utils';
88

99
executeSynchronous(async () => {
1010
const basePath = process.argv[2];
11-
await cloneAndGenerateBasePaths(constants.specsRepoPath, constants.specsRepoUri, constants.specsRepoCommitHash);
11+
let localPath = process.argv[3];
12+
if (!localPath) {
13+
localPath = constants.specsRepoPath;
14+
await cloneAndGenerateBasePaths(localPath, constants.specsRepoUri, constants.specsRepoCommitHash);
15+
} else {
16+
localPath = await resolveAbsolutePath(localPath);
17+
}
1218

1319
let readme = '';
1420
try {
15-
readme = await validateAndReturnReadmePath(basePath);
21+
readme = await validateAndReturnReadmePath(localPath, basePath);
1622
} catch {
1723
throw new Error(`Unable to find a readme under '${basePath}'. Please try running 'npm run list-basepaths' to find the list of valid paths.`);
1824
}

generator/cmd/listbasepaths.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ executeSynchronous(async () => {
1010
const basePaths = await cloneAndGenerateBasePaths(constants.specsRepoPath, constants.specsRepoUri, constants.specsRepoCommitHash);
1111

1212
for (const basePath of basePaths) {
13-
const readme = await validateAndReturnReadmePath(basePath);
13+
const readme = await validateAndReturnReadmePath(constants.specsRepoPath, basePath);
1414
const namespaces = keys(await getApiVersionsByNamespace(readme));
1515
const autogenlistEntries = findAutogenEntries(basePath);
1616

generator/cmd/postprocessor.ts

Lines changed: 0 additions & 89 deletions
This file was deleted.

generator/generate.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { findRecursive, findDirRecursive, executeCmd, rmdirRecursive, lowerCaseC
44
import * as constants from './constants';
55
import chalk from 'chalk';
66
import { ScopeType, AutogenlistConfig } from './models';
7-
import { resetFile } from './git';
8-
import { get, set, flatten, uniq, concat, Dictionary, groupBy, keys, difference, pickBy, flatMap, values, uniqBy } from 'lodash';
7+
import { get, set, flatten, uniq, concat, Dictionary, groupBy, keys, difference, pickBy } from 'lodash';
98

109
const autorestBinary = os.platform() === 'win32' ? 'autorest.cmd' : 'autorest';
1110
const apiVersionRegex = /^\d{4}-\d{2}-\d{2}(|-preview)$/;
@@ -76,31 +75,6 @@ export async function generateSchemas(readme: string, autogenlistConfig?: Autoge
7675
return schemaConfigs;
7776
}
7877

79-
export async function schemaPostProcess(schemaPath: string, isNew: boolean, autogenlistConfig?: AutogenlistConfig) {
80-
const namespace = path.basename(schemaPath.substring(0, schemaPath.lastIndexOf(path.extname(schemaPath))));
81-
const apiVersion = path.basename(path.resolve(`${schemaPath}/..`));
82-
const schemaConfig = await generateSchemaConfig(schemaPath, namespace, apiVersion, autogenlistConfig);
83-
const unknownScopeResources = schemaConfig.references.filter(x => x.scope & ScopeType.Unknown);
84-
if (autogenlistConfig && unknownScopeResources.length > 0) {
85-
throw new Error(`Unable to determine scope for resource types ${unknownScopeResources.map(x => x.type).join(', ')} for file ${schemaPath}`);
86-
}
87-
88-
await saveSchemaFile(schemaPath, schemaConfig);
89-
const schemaPathNew = path.join(constants.schemasBasePath, schemaConfig.relativePath);
90-
91-
if (schemaPathNew !== schemaPath) {
92-
if (isNew) {
93-
console.log('Delete file: ' + chalk.red(schemaPath));
94-
safeUnlink(schemaPath);
95-
} else {
96-
console.log('Reset file: ' + chalk.green(schemaPath));
97-
resetFile(path.dirname(schemaPath), path.basename(schemaPath));
98-
}
99-
}
100-
101-
return schemaConfig;
102-
}
103-
10478
async function handleGeneratedSchema(readme: string, schemaPath: string, autogenlistConfig?: AutogenlistConfig) {
10579
const namespace = path.basename(schemaPath.substring(0, schemaPath.lastIndexOf(path.extname(schemaPath))));
10680

generator/git.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,3 @@ export async function cloneGitRepo(localPath: string, remoteUri: string, commitH
3131
await executeCmd(localPath, 'git', ['fetch', '-q']);
3232
await executeCmd(localPath, 'git', ['checkout', '-q', commitHash]);
3333
}
34-
35-
export async function resetFile(localPath: string, fileName: string) {
36-
await executeCmd(localPath, 'git', ['checkout', '-q', '--', fileName]);
37-
}

generator/models.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export enum ScopeType {
1111
export interface AutogenlistConfig {
1212
basePath: string,
1313
namespace: string,
14+
readmeFile?: string,
1415
suffix?: string,
1516
resourceConfig?: AutogenlistResourceConfig[],
1617
postProcessor?: SchemaPostProcessor,
@@ -23,4 +24,10 @@ export interface AutogenlistResourceConfig {
2324

2425
export interface SchemaPostProcessor {
2526
(namespace: string, apiVersion: string, schema: any): void,
27+
}
28+
29+
export interface Package {
30+
packageName?: string,
31+
path: string[],
32+
result: 'succeeded' | 'failed' | 'warning'
2633
}

0 commit comments

Comments
 (0)