From 072c353e0b2ffb53d504cdc6b4130fc9afcd32c1 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Mon, 10 Sep 2018 17:30:00 +0200 Subject: [PATCH] build: run publish sanity checks before building * Runs the publish sanity checks before building the release output of Angular Material. Building the release output is very time consuming, and therefore the sanity checks should run before. Closes #12918 --- tools/gulp/gulpfile.ts | 1 + tools/gulp/tasks/publish/branch-check.ts | 2 +- tools/gulp/tasks/publish/publish-task.ts | 22 +------- tools/gulp/tasks/publish/sanity-checks.ts | 57 ++++++++++++++++++++ tools/gulp/tasks/publish/validate-release.ts | 26 ++------- 5 files changed, 66 insertions(+), 42 deletions(-) create mode 100644 tools/gulp/tasks/publish/sanity-checks.ts diff --git a/tools/gulp/gulpfile.ts b/tools/gulp/gulpfile.ts index ef56169bc9e5..267db813c5e5 100644 --- a/tools/gulp/gulpfile.ts +++ b/tools/gulp/gulpfile.ts @@ -33,4 +33,5 @@ import './tasks/unit-test'; import './tasks/universal'; import './tasks/publish/publish-task'; +import './tasks/publish/sanity-checks'; import './tasks/publish/validate-release'; diff --git a/tools/gulp/tasks/publish/branch-check.ts b/tools/gulp/tasks/publish/branch-check.ts index 1d41252187da..522601d64cbd 100644 --- a/tools/gulp/tasks/publish/branch-check.ts +++ b/tools/gulp/tasks/publish/branch-check.ts @@ -2,7 +2,7 @@ import {spawnSync} from 'child_process'; import {buildConfig} from 'material2-build-tools'; /** Regular expression that matches version names and the individual version segments. */ -export const versionNameRegex = /^(\d+)\.(\d+)\.(\d+)(?:-(alpha|beta|rc)\.(\d)+)?/; +export const versionNameRegex = /^(\d+)\.(\d+)\.(\d+)(?:-(alpha|beta|rc)\.(\d)+)?$/; /** Checks if the specified version can be released from the current Git branch. */ export function checkPublishBranch(version: string) { diff --git a/tools/gulp/tasks/publish/publish-task.ts b/tools/gulp/tasks/publish/publish-task.ts index 9b3faa5ba6fc..0e3cd1e6e1a3 100644 --- a/tools/gulp/tasks/publish/publish-task.ts +++ b/tools/gulp/tasks/publish/publish-task.ts @@ -1,4 +1,4 @@ -import {green, grey, red, yellow} from 'chalk'; +import {green, grey, yellow} from 'chalk'; import {spawn} from 'child_process'; import {existsSync, statSync} from 'fs-extra'; import {task} from 'gulp'; @@ -6,7 +6,6 @@ import {buildConfig, sequenceTask} from 'material2-build-tools'; import * as minimist from 'minimist'; import {join} from 'path'; import {execTask} from '../../util/task_helpers'; -import {checkPublishBranch, versionNameRegex} from './branch-check'; /** Packages that will be published to NPM by the release task. */ export const releasePackages = [ @@ -21,9 +20,9 @@ export const releasePackages = [ const argv = minimist(process.argv.slice(3)); task('publish', sequenceTask( + ':publish:sanity-checks', ':publish:whoami', ':publish:build-releases', - 'validate-release:check-remote-tag', 'validate-release:check-bundles', ':publish', ':publish:logout', @@ -48,14 +47,6 @@ task(':publish', async () => { const version = buildConfig.projectVersion; const currentDir = process.cwd(); - if (!version.match(versionNameRegex)) { - console.error(red(`Error: Cannot publish due to an invalid version name. Version ` + - `"${version}" is not following our semver format.`)); - console.error(yellow(`A version should follow this format: X.X.X, X.X.X-beta.X, ` + - `X.X.X-alpha.X, X.X.X-rc.X`)); - return; - } - console.log(); if (!tag) { console.log(grey('> You can specify the tag by passing --tag=labelName.\n')); @@ -65,21 +56,12 @@ task(':publish', async () => { } console.log(); - if (version.match(/(alpha|beta|rc)/) && (!tag || tag === 'latest')) { - console.error(red(`Publishing ${version} to the "latest" tag is not allowed.`)); - console.error(red(`Alpha, Beta or RC versions shouldn't be published to "latest".`)); - console.error(); - return; - } - if (releasePackages.length > 1) { console.warn(yellow('Warning: Multiple packages will be released.')); console.warn(yellow('Warning: Packages to be released:', releasePackages.join(', '))); console.warn(); } - checkPublishBranch(version); - console.log(yellow('> Make sure to check the "requiredAngularVersion" in the package.json.')); console.log(yellow('> The version in the config defines the peer dependency of Angular.')); console.log(); diff --git a/tools/gulp/tasks/publish/sanity-checks.ts b/tools/gulp/tasks/publish/sanity-checks.ts new file mode 100644 index 000000000000..0ed4f3c75808 --- /dev/null +++ b/tools/gulp/tasks/publish/sanity-checks.ts @@ -0,0 +1,57 @@ +import {red} from 'chalk'; +import {spawnSync} from 'child_process'; +import {task} from 'gulp'; +import {buildConfig} from 'material2-build-tools'; +import * as minimist from 'minimist'; +import {checkPublishBranch, versionNameRegex} from './branch-check'; + +const {projectDir, projectVersion} = buildConfig; + +/** Git repository URL that has been read out from the project package.json file. */ +const repositoryGitUrl = require('../../../../package.json').repository.url; + +/** Parse command-line arguments for release task. */ +const argv = minimist(process.argv.slice(3)); + +/** Task that runs various sanity checks before publishing. */ +task(':publish:sanity-checks', [ + ':publish:check-project-version', + ':publish:check-remote-tag', + ':publish:check-publish-branch', +]); + +/** Task that checks the new project version. */ +task(':publish:check-project-version', () => { + const tag = argv['tag']; + + if (!projectVersion.match(versionNameRegex)) { + console.error(red(`Error: Cannot publish due to an invalid version name. Version ` + + `"${projectVersion}" is not following our semver format.`)); + console.error(red(`A version should follow this format: X.X.X, X.X.X-beta.X, ` + + `X.X.X-alpha.X, X.X.X-rc.X`)); + process.exit(1); + } + + if (projectVersion.match(/(alpha|beta|rc)/) && (!tag || tag === 'latest')) { + console.error(red(`Publishing ${projectVersion} to the "latest" tag is not allowed.`)); + console.error(red(`Alpha, Beta or RC versions shouldn't be published to "latest".`)); + process.exit(1); + } +}); + +/** Task that verifies that the new version can be published from the current branch. */ +task(':publish:check-publish-branch', () => checkPublishBranch(projectVersion)); + +/** Task that ensures that the new release tagged on GitHub before publishing to NPM. */ +task(':publish:check-remote-tag', () => { + // Since we cannot assume that every developer uses `origin` as the default name for the upstream + // remote, we just pass in the Git URL that refers to angular/material2 repository on Github. + const tagCommitSha = spawnSync('git', ['ls-remote', '--tags', repositoryGitUrl, projectVersion], + {cwd: projectDir}).stdout.toString().trim(); + + if (!tagCommitSha) { + console.error(red(`Cannot publish v${projectVersion} because the release is not ` + + `tagged on upstream yet. Please tag the release before publishing to NPM.`)); + process.exit(1); + } +}); diff --git a/tools/gulp/tasks/publish/validate-release.ts b/tools/gulp/tasks/publish/validate-release.ts index 2857778058d2..996887d49612 100644 --- a/tools/gulp/tasks/publish/validate-release.ts +++ b/tools/gulp/tasks/publish/validate-release.ts @@ -1,16 +1,12 @@ -import {task} from 'gulp'; -import {readFileSync, existsSync} from 'fs'; -import {join} from 'path'; import {green, red} from 'chalk'; -import {releasePackages} from './publish-task'; +import {existsSync, readFileSync} from 'fs'; import {sync as glob} from 'glob'; -import {spawnSync} from 'child_process'; +import {task} from 'gulp'; import {buildConfig, sequenceTask} from 'material2-build-tools'; +import {join} from 'path'; +import {releasePackages} from './publish-task'; -const {projectDir, projectVersion, outputDir} = buildConfig; - -/** Git repository URL that has been read out from the project package.json file. */ -const repositoryGitUrl = require('../../../../package.json').repository.url; +const {outputDir} = buildConfig; /** Path to the directory where all releases are created. */ const releasesDir = join(outputDir, 'releases'); @@ -23,18 +19,6 @@ const externalReferencesRegex = /(templateUrl|styleUrls): *["'[]/; task('validate-release', sequenceTask(':publish:build-releases', 'validate-release:check-bundles')); -task('validate-release:check-remote-tag', () => { - // Since we cannot assume that every developer uses `origin` as the default name for the upstream - // remote, we just pass in the Git URL that refers to angular/material2 repository on Github. - const tagCommitSha = spawnSync('git', ['ls-remote', '--tags', repositoryGitUrl, projectVersion], - {cwd: projectDir}).stdout.toString().trim(); - - if (!tagCommitSha) { - throw Error(red(`Cannot publish v${projectVersion} because the release is not ` + - `tagged on upstream yet. Please tag the release before publishing to NPM.`)); - } -}); - /** Task that checks the release bundles for any common mistakes before releasing to the public. */ task('validate-release:check-bundles', () => { const releaseFailures = releasePackages