Feature: Initialize autorest with the provided arguments in sequence (#16)

This commit is contained in:
Timothee Guerin 2021-02-09 20:26:38 -08:00 коммит произвёл GitHub
Родитель 07aa8d6060
Коммит 337166997d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 82 добавлений и 47 удалений

Просмотреть файл

@ -1,5 +1,8 @@
# Changelog
### 0.3.6
- **Fix** race condition issue with downloading extensions
### 0.3.5
- **Update** `@autorest/autorest` -> `autorest` as former is deprecated.

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "@autorest/compare",
"version": "0.3.5",
"version": "0.3.6",
"description": "Compares the output between two AutoRest runs to check for material differences.",
"main": "dist/index.js",
"bin": {

Просмотреть файл

@ -2,7 +2,7 @@
// Licensed under the MIT License.
import * as path from "path";
import { AutoRestResult } from "./runner";
import { AutoRestGenerateResult } from "./runner";
import * as Diff from "diff";
/**
@ -342,8 +342,8 @@ export function compareFile(
* Compares the set of output files for two AutoRest runs.
*/
export function compareOutputFiles(
baseResult: AutoRestResult,
nextResult: AutoRestResult,
baseResult: AutoRestGenerateResult,
nextResult: AutoRestGenerateResult,
options: CompareSourceOptions
): CompareResult {
return compareItems(
@ -366,8 +366,8 @@ export function compareOutputFiles(
* runtime has improved or degraded between the two runs.
*/
export async function compareDuration(
baseResult: AutoRestResult,
nextResult: AutoRestResult
baseResult: AutoRestGenerateResult,
nextResult: AutoRestGenerateResult
): Promise<void> {
// TODO: Write some logic for this
}

Просмотреть файл

@ -5,7 +5,12 @@ import * as fs from "fs";
import * as path from "path";
import * as chalk from "chalk";
import { RunConfiguration, LanguageConfiguration } from "./config";
import { runAutoRest, AutoRestResult, getBaseResult } from "./runner";
import {
generateWithAutoRest,
AutoRestGenerateResult,
getBaseResult,
runAutoRest,
} from "./runner";
import { compareOutputFiles, CompareResult } from "./comparers";
import { compareFile as compareTypeScriptFile } from "./languages/typescript";
import { compareFile as comparePythonFile } from "./languages/python";
@ -37,12 +42,12 @@ export class CompareOperation extends Operation {
console.log("Comparing output for spec:", chalk.yellowBright(specPath));
// Run two instances of AutoRest simultaneously
let oldRunPromise: Promise<AutoRestResult>;
let oldRunPromise: Promise<AutoRestGenerateResult>;
if (
languageConfig.useExistingOutput === undefined ||
languageConfig.useExistingOutput === "none"
) {
oldRunPromise = runAutoRest(
oldRunPromise = generateWithAutoRest(
languageConfig.language,
specPath,
oldOutputPath,
@ -58,9 +63,9 @@ export class CompareOperation extends Operation {
oldRunPromise = Promise.resolve(getBaseResult(oldOutputPath));
}
let newRunPromise: Promise<AutoRestResult>;
let newRunPromise: Promise<AutoRestGenerateResult>;
if (languageConfig.useExistingOutput !== "all") {
newRunPromise = runAutoRest(
newRunPromise = generateWithAutoRest(
languageConfig.language,
specPath,
newOutputPath,
@ -78,7 +83,7 @@ export class CompareOperation extends Operation {
const [oldResult, newResult] = await Promise.all([
oldRunPromise,
newRunPromise
newRunPromise,
]);
if (debug || languageConfig.oldArgs.indexOf("--debug") > -1) {
@ -94,8 +99,8 @@ export class CompareOperation extends Operation {
const compareResult = compareOutputFiles(oldResult, newResult, {
comparersByType: {
ts: compareTypeScriptFile,
py: comparePythonFile
}
py: comparePythonFile,
},
});
if (compareResult) {
@ -140,7 +145,7 @@ export class BaselineOperation extends Operation {
oldOutputPath
);
const runResult = await runAutoRest(
const runResult = await generateWithAutoRest(
languageConfig.language,
specPath,
oldOutputPath,
@ -161,13 +166,13 @@ export class BaselineOperation extends Operation {
}
}
function printAutoRestResult(runResult: AutoRestResult): void {
function printAutoRestResult(runResult: AutoRestGenerateResult): void {
if (runResult.processOutput) {
console.log("\nAutoRest Output:\n");
console.log(runResult.processOutput);
}
console.log(`Output files under ${runResult.outputPath}:
${runResult.outputFiles.map(f => " " + f).join("\n")}`);
${runResult.outputFiles.map((f) => " " + f).join("\n")}`);
}
export async function runOperation(
@ -179,6 +184,8 @@ export async function runOperation(
console.log(
`\n# Language Generator: ${chalk.greenBright(languageConfig.language)}\n`
);
await initAutoRest(languageConfig);
for (const specConfig of runConfig.specs) {
for (const specPath of specConfig.specPaths) {
const fullSpecPath = path.resolve(specConfig.specRootPath, specPath);
@ -190,7 +197,7 @@ export async function runOperation(
await operation.runForSpec(
{
...languageConfig,
outputPath: path.resolve(languageConfig.outputPath, specPath)
outputPath: path.resolve(languageConfig.outputPath, specPath),
},
fullSpecPath,
runConfig.debug
@ -201,3 +208,15 @@ export async function runOperation(
return operation.getExitCode();
}
/**
* Init autorest in sequence to prevent package installation race condition.
*/
const initAutoRest = async (languageConfig: LanguageConfiguration) => {
console.log(`Initializing autorest for language ${languageConfig.language}`);
await runAutoRest([...languageConfig.oldArgs, "--help"]);
await runAutoRest([...languageConfig.newArgs, "--help"]);
console.log(
`Completed autorest initialization for language ${languageConfig.language}`
);
};

Просмотреть файл

@ -25,15 +25,21 @@ export interface AutoRestOptions {
debug?: boolean;
}
/**
* Describes different aspects of the result of an AutoRest generation run.
*/
export interface AutoRestGenerateResult extends AutoRestResult {
outputPath: string;
outputFiles: string[];
}
/**
* Describes different aspects of the result of an AutoRest run.
*/
export interface AutoRestResult {
version?: string;
outputPath: string;
outputFiles: string[];
processOutput?: string;
timeElapsed?: number;
processOutput?: string;
}
/**
@ -63,7 +69,7 @@ export const AutoRestLanguages: AutoRestLanguage[] = [
* Gets the base AutoRestResult by gathering the output files under the
* given outputPath.
*/
export function getBaseResult(outputPath: string): AutoRestResult {
export function getBaseResult(outputPath: string): AutoRestGenerateResult {
return {
outputPath,
outputFiles: getPathsRecursively(outputPath).map((p) =>
@ -77,35 +83,43 @@ export function getBaseResult(outputPath: string): AutoRestResult {
* specified options. Returns a Promise that carries an AutoRestResult which
* describes the result of the run.
*/
export function runAutoRest(
export const generateWithAutoRest = async (
language: AutoRestLanguage,
specPath: string,
outputPath: string,
autoRestArgs: string[]
): Promise<AutoRestResult> {
): Promise<AutoRestGenerateResult> => {
const args = [
// The language generator to use
`--${language}`,
// The output-folder where generated files are written. Specify both
// styles of option due to inconsistencies between generators.
`--output-folder="${outputPath}"`,
`--${language}.output-folder="$(output-folder)"`,
// Clear the output folder before generating
`--${language}.clear-output-folder`,
// The path to the input spec
path.extname(specPath) !== "" ? `--input-file="${specPath}"` : specPath,
// Any additional arguments
...(autoRestArgs || []),
];
const result = await runAutoRest(args);
return {
...getBaseResult(outputPath),
...result,
};
};
export const runAutoRest = (args: string[]): Promise<AutoRestResult> => {
return new Promise((resolve, reject) => {
const autoRestCommand = getAutoRestCmd();
const args = [
// The language generator to use
`--${language}`,
// The output-folder where generated files are written. Specify both
// styles of option due to inconsistencies between generators.
`--output-folder="${outputPath}"`,
`--${language}.output-folder="$(output-folder)"`,
// Clear the output folder before generating
`--${language}.clear-output-folder`,
// The path to the input spec
path.extname(specPath) !== "" ? `--input-file="${specPath}"` : specPath,
// Any additional arguments
...(autoRestArgs || []),
];
if (autoRestArgs.indexOf("--debug") > -1) {
if (args.indexOf("--debug") > -1) {
console.log(`*** Invoking ${autoRestCommand} with args:`, args);
}
@ -122,7 +136,7 @@ export function runAutoRest(
errorOutput += data.toString();
});
let versionArg = autoRestArgs.find((arg) => arg.startsWith("--version"));
let versionArg = args.find((arg) => arg.startsWith("--version"));
let [_, version] = versionArg
? parseArgument(versionArg)
: [null, "unspecified"];
@ -138,7 +152,6 @@ export function runAutoRest(
const timeElapsed = Date.now() - startTime;
resolve({
...getBaseResult(outputPath),
version,
processOutput: normalOutput,
timeElapsed,
@ -146,7 +159,7 @@ export function runAutoRest(
}
});
});
}
};
const getAutoRestCmd = () => {
const exe = os.platform() == "win32" ? "autorest.cmd" : "autorest";