From bc0ee3626a4642d8bc57b674f36479f018caddc3 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 1 Mar 2022 14:56:31 -0500 Subject: [PATCH 1/4] fail on regeneration diff --- eng/pipelines/ci.yml | 6 +- eng/scripts/check-for-changed-files.js | 29 +++++++++ eng/scripts/helpers.js | 83 ++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 eng/scripts/check-for-changed-files.js create mode 100644 eng/scripts/helpers.js diff --git a/eng/pipelines/ci.yml b/eng/pipelines/ci.yml index 7be82bebfb6..cdddcc923c0 100644 --- a/eng/pipelines/ci.yml +++ b/eng/pipelines/ci.yml @@ -74,10 +74,8 @@ jobs: - script: | inv regenerate displayName: 'Regenerate Code' - - script: | - git add -A # 'add' first so 'diff' includes untracked files - git diff --staged -w - displayName: 'Diff regeneration' + - script: node ./eng/scripts/check-for-changed-files.js + displayName: Fail on regeneration diff - task: UsePythonVersion@0 displayName: 'Use Python $(PythonVersion)' inputs: diff --git a/eng/scripts/check-for-changed-files.js b/eng/scripts/check-for-changed-files.js new file mode 100644 index 00000000000..dcf511ef828 --- /dev/null +++ b/eng/scripts/check-for-changed-files.js @@ -0,0 +1,29 @@ +// @ts-check + +const { run } = require("./helpers.js"); + +const proc = run("git", ["status", "--porcelain"], { + encoding: "utf-8", + stdio: [null, "pipe", "pipe"], +}); + +if (proc.stdout) { + console.log(proc.stdout); +} + +if (proc.stderr) { + console.error(proc.stderr); +} + +if (proc.stdout || proc.stderr) { + if (process.argv[2] !== "publish") { + console.error( + `ERROR: There are diffs in regeneration. Please run 'inv regenerate' and re-run.`, + ); + } else { + console.error( + `ERROR: There are diffs in regeneration. Please run 'inv regenerate' and re-run.`, + ); + } + process.exit(1); +} \ No newline at end of file diff --git a/eng/scripts/helpers.js b/eng/scripts/helpers.js new file mode 100644 index 00000000000..aac104cb2bd --- /dev/null +++ b/eng/scripts/helpers.js @@ -0,0 +1,83 @@ +// @ts-check +const { spawn, spawnSync } = require("child_process"); +const { resolve } = require("path"); + +const repoRoot = resolve(__dirname, "../.."); +const prettier = resolve( + repoRoot, + "packages/extensions/core/node_modules/.bin/prettier" +); +const tsc = resolve(repoRoot, "packages/extensions/core/node_modules/.bin/tsc"); + +const isCmdOnWindows = ["rush", "npm", "code", "code-insiders", tsc, prettier]; + +function run(command, args, options) { + console.log(); + console.log(`> ${command} ${args.join(" ")}`); + + options = { + stdio: "inherit", + sync: true, + throwOnNonZeroExit: true, + ...options, + }; + + if (process.platform === "win32" && isCmdOnWindows.includes(command)) { + command += ".cmd"; + } + + const proc = (options.sync ? spawnSync : spawn)(command, args, options); + if (proc.error) { + if (options.ignoreCommandNotFound && proc.error.code === "ENOENT") { + console.log(`Skipped: Command \`${command}\` not found.`); + } else { + throw proc.error; + } + } else if ( + options.throwOnNonZeroExit && + proc.status !== undefined && + proc.status !== 0 + ) { + throw new CommandFailedError( + `Command \`${command} ${args.join(" ")}\` failed with exit code ${ + proc.status + }`, + proc + ); + } + + return proc; +} + +class CommandFailedError extends Error { + constructor(msg, proc) { + super(msg); + this.proc = proc; + } +} + +function runPrettier(...args) { + run( + prettier, + [ + ...args, + "--config", + ".prettierrc.yml", + "--ignore-path", + ".prettierignore", + "**/*.{ts,js,cjs,mjs,json,yml,yaml,cadl,md}", + ], + { + cwd: repoRoot, + } + ); +} + +module.exports = { + repoRoot, + prettier, + tsc, + run, + runPrettier, + CommandFailedError, +}; From 2b73ec827ab0af5b9f0b286223095e8d101e9595 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 1 Mar 2022 15:11:32 -0500 Subject: [PATCH 2/4] add extra space in enum to fail --- autorest/codegen/templates/enum.py.jinja2 | 1 + 1 file changed, 1 insertion(+) diff --git a/autorest/codegen/templates/enum.py.jinja2 b/autorest/codegen/templates/enum.py.jinja2 index 3a8c98d7d0a..ae73d02d362 100644 --- a/autorest/codegen/templates/enum.py.jinja2 +++ b/autorest/codegen/templates/enum.py.jinja2 @@ -3,6 +3,7 @@ class {{ enum.name }}(with_metaclass(CaseInsensitiveEnumMeta, {{ enum.enum_type. {% if enum.description %} """{{ enum.description | wordwrap(width=95, break_long_words=False, break_on_hyphens=False, wrapstring='\n ') }} """ + {% endif %} {% for value in enum.values %} From d9745c0865e0e5f991e6b3c7f549616341b4f40c Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 1 Mar 2022 15:47:57 -0500 Subject: [PATCH 3/4] update error message --- eng/scripts/check-for-changed-files.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/eng/scripts/check-for-changed-files.js b/eng/scripts/check-for-changed-files.js index dcf511ef828..3fd1827da29 100644 --- a/eng/scripts/check-for-changed-files.js +++ b/eng/scripts/check-for-changed-files.js @@ -16,14 +16,8 @@ if (proc.stderr) { } if (proc.stdout || proc.stderr) { - if (process.argv[2] !== "publish") { - console.error( - `ERROR: There are diffs in regeneration. Please run 'inv regenerate' and re-run.`, - ); - } else { - console.error( - `ERROR: There are diffs in regeneration. Please run 'inv regenerate' and re-run.`, - ); - } + console.error( + `ERROR: There are diffs in regeneration. Please run 'inv regenerate' and re-run. You may also have to remove 'node_modules' and re-run 'npm install' to get the latest testserver.`, + ); process.exit(1); } \ No newline at end of file From a5efe7e82e1792814e7b28de7b29b5d75304960c Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 1 Mar 2022 16:40:20 -0500 Subject: [PATCH 4/4] revert enum change --- autorest/codegen/templates/enum.py.jinja2 | 1 - 1 file changed, 1 deletion(-) diff --git a/autorest/codegen/templates/enum.py.jinja2 b/autorest/codegen/templates/enum.py.jinja2 index ae73d02d362..3a8c98d7d0a 100644 --- a/autorest/codegen/templates/enum.py.jinja2 +++ b/autorest/codegen/templates/enum.py.jinja2 @@ -3,7 +3,6 @@ class {{ enum.name }}(with_metaclass(CaseInsensitiveEnumMeta, {{ enum.enum_type. {% if enum.description %} """{{ enum.description | wordwrap(width=95, break_long_words=False, break_on_hyphens=False, wrapstring='\n ') }} """ - {% endif %} {% for value in enum.values %}