Skip to content

build: run publish sanity checks before building #13060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/gulp/gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 1 addition & 1 deletion tools/gulp/tasks/publish/branch-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 2 additions & 20 deletions tools/gulp/tasks/publish/publish-task.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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';
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 = [
Expand All @@ -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',
Expand All @@ -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'));
Expand All @@ -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();
Expand Down
57 changes: 57 additions & 0 deletions tools/gulp/tasks/publish/sanity-checks.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
26 changes: 5 additions & 21 deletions tools/gulp/tasks/publish/validate-release.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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
Expand Down