Skip to content

build: rework package-builder script to node script #17758

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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ jobs:
- *yarn_install
- *setup_bazel_binary

- run: ./scripts/build-packages-dist.sh
- run: yarn build
- run: yarn check-release-output

# TODO(devversion): replace this with bazel tests that run Madge. This is
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"scripts": {
"postinstall": "node tools/bazel/postinstall-patches.js && ngcc --properties main --create-ivy-entry-points",
"build": "bash ./scripts/build-packages-dist.sh",
"build": "node ./scripts/build-packages-dist.js",
"bazel:buildifier": "find . -type f \\( -name \"*.bzl\" -or -name WORKSPACE -or -name BUILD -or -name BUILD.bazel \\) ! -path \"*/node_modules/*\" | xargs buildifier -v --warnings=attr-cfg,attr-license,attr-non-empty,attr-output-default,attr-single-file,constant-glob,ctx-args,depset-iteration,depset-union,dict-concatenation,duplicated-name,filetype,git-repository,http-archive,integer-division,load,load-on-top,native-build,native-package,output-group,package-name,package-on-top,redefined-variable,repository-name,same-origin-load,string-iteration,unused-variable,unsorted-dict-items,out-of-order-load",
"bazel:format-lint": "yarn -s bazel:buildifier --lint=warn --mode=check",
"dev-app": "ibazel run //src/dev-app:devserver",
Expand Down
124 changes: 124 additions & 0 deletions scripts/build-packages-dist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/env node

/**
* Script that builds the release output of all packages which have the "release-package
* bazel tag set. The script builds all those packages and copies the release output to the
* distribution folder within the project.
*/

const {execSync} = require('child_process');
const {join} = require('path');
const {chmod, cp, mkdir, rm, set, test} = require('shelljs');

// ShellJS should exit if a command fails.
set('-e');

/** Name of the Bazel tag that will be used to find release package targets. */
const releaseTargetTag = 'release-package';

/** Path to the project directory. */
const projectDir = join(__dirname, '../');

/** Command that runs Bazel. */
const bazelCmd = process.env.BAZEL_COMMAND || `yarn -s bazel`;

/** Command that queries Bazel for all release package targets. */
const queryPackagesCmd =
`${bazelCmd} query --output=label "attr('tags', '\\[.*${releaseTargetTag}.*\\]', //src/...) ` +
`intersect kind('.*_package', //src/...)"`;

// Export the methods for building the release packages. These
// can be consumed by the release tool.
exports.buildReleasePackages = buildReleasePackages;
exports.defaultBuildReleasePackages = defaultBuildReleasePackages;

if (module === require.main) {
defaultBuildReleasePackages();
}

/**
* Builds the release packages with the default compile mode and
* output directory.
*/
function defaultBuildReleasePackages() {
buildReleasePackages('legacy', join(projectDir, 'dist/releases'));
}

/**
* Builds the release packages with the given compile mode and copies
* the package output into the given directory.
*/
function buildReleasePackages(compileMode, distPath) {
console.log('######################################');
console.log(' Building release packages...');
console.log(` Compile mode: ${compileMode}`);
console.log('######################################');

// List of targets to build. e.g. "src/cdk:npm_package", or "src/material:npm_package".
const targets = exec(queryPackagesCmd, true).split(/\r?\n/);
const packageNames = getPackageNamesOfTargets(targets);
const bazelBinPath = exec(`${bazelCmd} info bazel-bin`, true);
const getOutputPath = pkgName => join(bazelBinPath, 'src', pkgName, 'npm_package');

// Walk through each release package and clear previous "npm_package" outputs. This is
// a workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1219. We need to
// do this to ensure that the version placeholders are properly populated.
packageNames.forEach(pkgName => {
const outputPath = getOutputPath(pkgName);
if (test('-d', outputPath)) {
chmod('-R', 'u+w', outputPath);
rm('-rf', outputPath);
}
});

// Build with "--config=release" so that Bazel runs the workspace stamping script. The
// stamping script ensures that the version placeholder is populated in the release output.
exec(`${bazelCmd} build --config=release --define=compile=${compileMode} ${targets.join(' ')}`);

// Delete the distribution directory so that the output is guaranteed to be clean. Re-create
// the empty directory so that we can copy the release packages into it later.
rm('-rf', distPath);
mkdir('-p', distPath);

// Copy the package output into the specified distribution folder.
packageNames.forEach(pkgName => {
const outputPath = getOutputPath(pkgName);
const targetFolder = join(distPath, pkgName);
console.log(`> Copying package output to "${targetFolder}"`);
cp('-R', outputPath, targetFolder);
chmod('-R', 'u+w', targetFolder);
});
}

/**
* Gets the package names of the specified Bazel targets.
* e.g. //src/material:npm_package -> material
*/
function getPackageNamesOfTargets(targets) {
return targets.map(targetName => {
const matches = targetName.match(/\/\/src\/(.*):npm_package/);
if (matches === null) {
throw Error(`Found Bazel target with "${releaseTargetTag}" tag, but could not ` +
`determine release output name: ${targetName}`);
}
return matches[1];
});
}

/**
* Executes the given command in the project directory.
* @param {string} command The command to run
* @param {boolean=} captureStdout Whether the stdout should be captured and
* returned.
*/
function exec(command, captureStdout) {
const stdout = execSync(command, {
cwd: projectDir,
stdio: ['inherit', captureStdout ? 'pipe' : 'inherit', 'inherit'],
});

if (captureStdout) {
process.stdout.write(stdout);
return stdout.toString().trim();
}
}
81 changes: 0 additions & 81 deletions scripts/build-packages-dist.sh

This file was deleted.

17 changes: 5 additions & 12 deletions tools/release/publish-release.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import chalk from 'chalk';
import {spawnSync} from 'child_process';
import {readFileSync, unlinkSync} from 'fs';
import {homedir} from 'os';
import {join} from 'path';
Expand All @@ -15,6 +14,10 @@ import {releasePackages} from './release-output/release-packages';
import {CHANGELOG_FILE_NAME} from './stage-release';
import {parseVersionName, Version} from './version-name/parse-version';

// The package builder script is not written in TypeScript and needs to
// be imported through a CommonJS import.
const {defaultBuildReleasePackages} = require('../../scripts/build-packages-dist');

/**
* Class that can be instantiated in order to create a new release. The tasks requires user
* interaction/input through command line prompts.
Expand Down Expand Up @@ -87,7 +90,7 @@ class PublishReleaseTask extends BaseReleaseTask {
await this._promptStableVersionForNextTag();
}

this._buildReleasePackages();
defaultBuildReleasePackages();
console.info(chalk.green(` ✓ Built the release output.`));

// Checks all release packages against release output validations before releasing.
Expand Down Expand Up @@ -149,16 +152,6 @@ class PublishReleaseTask extends BaseReleaseTask {
}
}

/** Builds all release packages that should be published. */
private _buildReleasePackages() {
const buildScript = join(this.projectDir, 'scripts/build-packages-dist.sh');

// TODO(devversion): I'd prefer disabling the output for those, but it might be only
// worth if we consider adding some terminal spinner library (like "ora").
return spawnSync('bash', [buildScript],
{cwd: this.projectDir, stdio: 'inherit', shell: true}).status === 0;
}

/**
* Prompts the user whether they are sure that the current stable version should be
* released to the "next" NPM dist-tag.
Expand Down