Skip to content

Commit

Permalink
Merge pull request #1070 from Descatles/OnbroadAutorestSchema
Browse files Browse the repository at this point in the history
Onboard autorest.schema to sdkauto
  • Loading branch information
azcloudfarmer authored Aug 12, 2020
2 parents 030762e + 1cadf60 commit 7c5d745
Show file tree
Hide file tree
Showing 8 changed files with 1,441 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ npm-debug.log
node_modules
.DS_Store
test-results.xml
schemas/code-model-v1
70 changes: 70 additions & 0 deletions generator/cmd/postprocessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import path from 'path';
import chalk from 'chalk';
import { executeSynchronous, lowerCaseEquals, safeUnlink } from '../utils';
import { findAutogenEntries } from '../autogenlist';
import { saveAutogeneratedSchemaRefs, SchemaConfiguration, schemaPostProcess } from '../generate';
import * as constants from '../constants';

async function getChangedSchemas(repoPath: string) {
var git = require('nodegit-kit');
const repo = await git.open(repoPath);
const status = await git.status(repo);
const changedSchemas: { path: string, isNew: boolean }[] = [];
for (const stat of status) {
if (stat.path.toString().split(path.sep).indexOf('schemas') !== -1
&& path.extname(stat.path) === '.json') {
if (stat.status !== 'modified' && stat.status !== 'new') {
throw new Error(`Undefined file status for '${stat.path}'.`);
} else {
if (stat.status === 'modified') {
changedSchemas.push({
path: path.join(repoPath, stat.path),
isNew: false
});
} else {
changedSchemas.push({
path: path.join(repoPath, stat.path),
isNew: true
});
}
}
}
}
return changedSchemas;
}

async function postProcessor(basePath: string) {
const autogenEntries = findAutogenEntries(basePath);
let changedSchemas = await getChangedSchemas(constants.generatorRoot);
let schemaConfigs: SchemaConfiguration[] = [];
for (const changedSchema of changedSchemas) {
const changedSchemaPath = changedSchema.path;
console.log('Processing changed schema file: ' + chalk.green(changedSchemaPath));
const namespace = path.basename(changedSchemaPath.substring(0, changedSchemaPath.lastIndexOf(path.extname(changedSchemaPath))));
const autogenEntriesSameNamespace = autogenEntries
.filter(autogenEntry => lowerCaseEquals(autogenEntry.namespace, namespace));
if (autogenEntriesSameNamespace.length === 0) {
const localSchemaConfig = await schemaPostProcess(changedSchemaPath, changedSchema.isNew);
schemaConfigs.push(localSchemaConfig);
} else {
if (autogenEntriesSameNamespace.length > 1) {
throw new Error(`Multiple autogenEntries for '${changedSchemaPath}'`);
}
const autogenlistConfig = autogenEntriesSameNamespace[0];
const localSchemaConfig = await schemaPostProcess(changedSchemaPath, changedSchema.isNew, autogenlistConfig);
schemaConfigs.push(localSchemaConfig);
}
}
await saveAutogeneratedSchemaRefs(schemaConfigs);
await safeUnlink(path.join(constants.generatorRoot, "schemas", "code-model-v1"));
}

executeSynchronous(async () => {
const basePath = process.argv[2];
try {
await postProcessor(basePath);
console.log(`PostProcessor finished successfully.`);
} catch (e) {
console.error(`PostProcessor ${e}`);
}
});
28 changes: 27 additions & 1 deletion generator/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { findRecursive, findDirRecursive, executeCmd, rmdirRecursive, lowerCaseC
import * as constants from './constants';
import chalk from 'chalk';
import { ScopeType, AutogenlistConfig } from './models';
import { resetFile } from './git';
import { get, set, flatten, uniq, concat, Dictionary, groupBy, keys, difference, pickBy, flatMap, values, uniqBy } from 'lodash';

const autorestBinary = os.platform() === 'win32' ? 'autorest-beta.cmd' : 'autorest-beta';
Expand Down Expand Up @@ -78,6 +79,31 @@ export async function generateSchemas(readme: string, autogenlistConfig?: Autoge
return schemaConfigs;
}

export async function schemaPostProcess(schemaPath: string, isNew: boolean, autogenlistConfig?: AutogenlistConfig) {
const namespace = path.basename(schemaPath.substring(0, schemaPath.lastIndexOf(path.extname(schemaPath))));
const apiVersion = path.basename(path.resolve(`${schemaPath}/..`));
const schemaConfig = await generateSchemaConfig(schemaPath, namespace, apiVersion, autogenlistConfig);
const unknownScopeResources = schemaConfig.references.filter(x => x.scope & ScopeType.Unknown);
if (autogenlistConfig && unknownScopeResources.length > 0) {
throw new Error(`Unable to determine scope for resource types ${unknownScopeResources.map(x => x.type).join(', ')} for file ${schemaPath}`);
}

await saveSchemaFile(schemaPath, schemaConfig);
const schemaPathNew = path.join(constants.schemasBasePath, schemaConfig.relativePath);

if (schemaPathNew !== schemaPath) {
if (isNew) {
console.log('Delete file: ' + chalk.red(schemaPath));
safeUnlink(schemaPath);
} else {
console.log('Reset file: ' + chalk.green(schemaPath));
resetFile(path.dirname(schemaPath), path.basename(schemaPath));
}
}

return schemaConfig;
}

async function handleGeneratedSchema(readme: string, schemaPath: string, autogenlistConfig?: AutogenlistConfig) {
const namespace = path.basename(schemaPath.substring(0, schemaPath.lastIndexOf(path.extname(schemaPath))));

Expand Down Expand Up @@ -328,4 +354,4 @@ export async function saveAutogeneratedSchemaRefs(schemaConfigs: SchemaConfigura
set(template, rootSchemaConfig.jsonPath, newRefs.map(ref => ({ '$ref': ref })));
await writeJsonFile(rootSchemaConfig.file, template);
});
}
}
6 changes: 5 additions & 1 deletion generator/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ export async function cloneGitRepo(localPath: string, remoteUri: string, commitH

await executeCmd(localPath, 'git', ['fetch', '-q']);
await executeCmd(localPath, 'git', ['checkout', '-q', commitHash]);
}
}

export async function resetFile(localPath: string, fileName: string) {
await executeCmd(localPath, 'git', ['checkout', '-q', '--', fileName]);
}
Loading

0 comments on commit 7c5d745

Please sign in to comment.