build: fix build scripts on macOS (#33854)
In #33823, `scripts/package-builds.sh` (which is used by both `build-packages-dist.sh` and `build-ivy-npm-packages.sh`) was updated to use `realpath`. It turns out that `realpath` does not exist on macOS, so the build scripts do not work there. In order to fix this (and also reduce the likelihood of introducing similar issues in the future), this commit changes these bash scripts to Node.js scripts (using [ShellJS](https://github.com/shelljs/shelljs) for a cross-platform implementation of Unix shell commands where necessary). PR Close #33854
This commit is contained in:

committed by
Alex Rickabaugh

parent
c89eed56b6
commit
7eb3e3bce6
8
scripts/build-ivy-npm-packages.js
Executable file
8
scripts/build-ivy-npm-packages.js
Executable file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict';
|
||||
|
||||
const {buildTargetPackages} = require('./package-builder');
|
||||
|
||||
|
||||
// Build the ivy packages.
|
||||
buildTargetPackages('dist/packages-dist-ivy-aot', 'aot', 'Ivy AOT');
|
@ -1,6 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
source $(dirname $0)/package-builder.sh
|
||||
|
||||
# Build the ivy packages
|
||||
buildTargetPackages "dist/packages-dist-ivy-aot" "aot" "Ivy AOT"
|
40
scripts/build-packages-dist.js
Executable file
40
scripts/build-packages-dist.js
Executable file
@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict';
|
||||
|
||||
const {chmod, cp, mkdir, rm} = require('shelljs');
|
||||
const {
|
||||
baseDir,
|
||||
bazelBin,
|
||||
bazelCmd,
|
||||
buildTargetPackages,
|
||||
exec,
|
||||
scriptPath,
|
||||
} = require('./package-builder');
|
||||
|
||||
|
||||
// Build the legacy (view engine) npm packages into `dist/packages-dist/`.
|
||||
buildTargetPackages('dist/packages-dist', 'legacy', 'Production');
|
||||
|
||||
// Build the `zone.js` npm package (into `dist/bin/packages/zone.js/npm_package/`), because it might
|
||||
// be needed by other scripts/tests.
|
||||
//
|
||||
// NOTE: The `zone.js` package is not built as part of `buildTargetPackages()` above, nor is it
|
||||
// copied into the `dist/packages-dist/` directory (despite its source's being inside
|
||||
// `packages/`), because it is not published to npm under the `@angular` scope (as happens for
|
||||
// the rest of the packages).
|
||||
console.log('');
|
||||
console.log('##############################');
|
||||
console.log(`${scriptPath}:`);
|
||||
console.log(' Building zone.js npm package');
|
||||
console.log('##############################');
|
||||
exec(`${bazelCmd} build //packages/zone.js:npm_package`);
|
||||
|
||||
// Copy artifacts to `dist/zone.js-dist/`, so they can be easier persisted on CI.
|
||||
const buildOutputDir = `${bazelBin}/packages/zone.js/npm_package`;
|
||||
const distTargetDir = `${baseDir}/dist/zone.js-dist/zone.js`;
|
||||
|
||||
console.log(`# Copy artifacts to ${distTargetDir}`);
|
||||
mkdir('-p', distTargetDir);
|
||||
rm('-rf', distTargetDir);
|
||||
cp('-R', buildOutputDir, distTargetDir);
|
||||
chmod('-R', 'u+w', distTargetDir);
|
@ -1,30 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
source $(dirname $0)/package-builder.sh
|
||||
|
||||
# Build the legacy (view engine) npm packages into dist/packages-dist
|
||||
buildTargetPackages "dist/packages-dist" "legacy" "Production"
|
||||
|
||||
# Build the `zone.js` npm package (into `dist/bin/packages/zone.js/npm_package/`), because it might be needed
|
||||
# by other scripts/tests.
|
||||
#
|
||||
# NOTE: The `zone.js` package is not built as part of `buildTargetPackages()` above, nor is it
|
||||
# copied into the `dist/packages-dist/` directory (despite its source's being in `packages/`),
|
||||
# because it is not published to npm under the `@angular` scope (as happens for the rest of
|
||||
# the packages).
|
||||
echo ""
|
||||
echo "##############################"
|
||||
echo "${script_path}:"
|
||||
echo " Building zone.js npm package"
|
||||
echo "##############################"
|
||||
yarn --silent bazel build //packages/zone.js:npm_package
|
||||
|
||||
# Copy artifacts to `dist/zone.js-dist/`, so they can be easier persisted on CI.
|
||||
readonly buildOutputDir="$base_dir/dist/bin/packages/zone.js/npm_package"
|
||||
readonly distTargetDir="$base_dir/dist/zone.js-dist/zone.js"
|
||||
|
||||
echo "# Copy artifacts to $distTargetDir"
|
||||
mkdir -p $distTargetDir
|
||||
rm -rf $distTargetDir
|
||||
cp -R $buildOutputDir $distTargetDir
|
||||
chmod -R u+w $distTargetDir
|
118
scripts/package-builder.js
Executable file
118
scripts/package-builder.js
Executable file
@ -0,0 +1,118 @@
|
||||
'use strict';
|
||||
|
||||
// Build the dist/packages-dist directory in the same fashion as the legacy
|
||||
// /build.sh script, by building the npm packages with Bazel and copying files.
|
||||
// This is needed for scripts and tests which are not updated to the Bazel output
|
||||
// layout (which always matches the input layout).
|
||||
// Do not add new dependencies on this script, instead adapt scripts to use the
|
||||
// new layout, and write new tests as Bazel targets.
|
||||
//
|
||||
// Ideally integration tests should run under bazel, and just consume the npm
|
||||
// packages via `deps`. Until that works, we manually build the npm packages and then
|
||||
// copy the results to the appropriate `dist` location.
|
||||
|
||||
const {execSync} = require('child_process');
|
||||
const {existsSync, statSync} = require('fs');
|
||||
const {resolve, relative} = require('path');
|
||||
const {chmod, cp, mkdir, rm, set} = require('shelljs');
|
||||
|
||||
set('-e');
|
||||
|
||||
|
||||
/** @type {string} The absolute path to the project root directory. */
|
||||
const baseDir = resolve(`${__dirname}/..`);
|
||||
|
||||
/** @type {string} The command to use for running bazel. */
|
||||
const bazelCmd = 'yarn --silent bazel';
|
||||
|
||||
/** @type {string} The absolute path to the bazel-bin directory. */
|
||||
const bazelBin = exec(`${bazelCmd} info bazel-bin`, true);
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
* The relative path to the entry script (i.e. the one loaded when the Node.js process launched).
|
||||
* It is relative to `baseDir`.
|
||||
*/
|
||||
const scriptPath = relative(baseDir, require.main.filename);
|
||||
|
||||
module.exports = {
|
||||
baseDir,
|
||||
bazelBin,
|
||||
bazelCmd,
|
||||
buildTargetPackages,
|
||||
exec,
|
||||
scriptPath,
|
||||
};
|
||||
|
||||
/**
|
||||
* Build the packages.
|
||||
*
|
||||
* @param {string} destPath Path to the output directory into which we copy the npm packages.
|
||||
* @param {'legacy' | 'aot'} compileMode Either `legacy` (view engine) or `aot` (ivy).
|
||||
* @param {string} description Human-readable description of the build.
|
||||
*/
|
||||
function buildTargetPackages(destPath, compileMode, description) {
|
||||
console.log('##################################');
|
||||
console.log(`${scriptPath}:`);
|
||||
console.log(' Building @angular/* npm packages');
|
||||
console.log(` Mode: ${description}`);
|
||||
console.log('##################################');
|
||||
|
||||
// List of targets to build, e.g. core, common, compiler, etc. Note that we want to also remove
|
||||
// all carriage return (`\r`) characters form the query output, because otherwise the carriage
|
||||
// return is part of the bazel target name and bazel will complain.
|
||||
const getTargetsCmd = `${bazelCmd} query --output=label "attr('tags', '\\[.*release-with-framework.*\\]', //packages/...) intersect kind('.*_package', //packages/...)"`;
|
||||
const targets = exec(getTargetsCmd, true).split(/\r?\n/);
|
||||
|
||||
// Use `--config=release` so that snapshot builds get published with embedded version info.
|
||||
exec(`${bazelCmd} build --config=release --define=compile=${compileMode} ${targets.join(' ')}`);
|
||||
|
||||
// Create the output directory.
|
||||
const absDestPath = `${baseDir}/${destPath}`;
|
||||
if (!existsSync(absDestPath)) mkdir('-p', absDestPath);
|
||||
|
||||
targets.forEach(target => {
|
||||
const pkg = target.replace(/\/\/packages\/(.*):npm_package/, '$1');
|
||||
|
||||
// Skip any that don't have an "npm_package" target.
|
||||
const srcDir = `${bazelBin}/packages/${pkg}/npm_package`;
|
||||
const destDir = `${absDestPath}/${pkg}`;
|
||||
|
||||
if (existsSync(srcDir) && statSync(srcDir).isDirectory()) {
|
||||
console.log(`# Copy artifacts to ${destDir}`);
|
||||
rm('-rf', destDir);
|
||||
cp('-R', srcDir, destDir);
|
||||
chmod('-R', 'u+w', destDir);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a command synchronously.
|
||||
*
|
||||
* By default, the current process' stdout is used (and thus the output is not captured and returned
|
||||
* to the caller). This is necessary for showing colors and modifying already printed output, for
|
||||
* example to show progress.
|
||||
*
|
||||
* If the caller requests the output (via `captureStdout: true`), the command is run without
|
||||
* printing anything to stdout and then (once the command has completed) the whole output is printed
|
||||
* to stdout and returned to the caller.
|
||||
*
|
||||
* @param {string} cmd The command to run.
|
||||
* @param {boolean} [captureStdout=false] Whether to return the output of the command.
|
||||
* @return {string | undefined} The captured stdout output if `captureStdout: true` or `undefined`.
|
||||
*/
|
||||
function exec(cmd, captureStdout) {
|
||||
const output = execSync(cmd, {
|
||||
stdio: [
|
||||
/* stdin */ 'inherit',
|
||||
/* stdout */ captureStdout ? 'pipe' : 'inherit',
|
||||
/* stderr */ 'inherit',
|
||||
],
|
||||
});
|
||||
|
||||
if (captureStdout) {
|
||||
process.stdout.write(output);
|
||||
return output.toString().trim();
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Build the dist/packages-dist directory in the same fashion as the legacy
|
||||
# /build.sh script, by building the npm packages with Bazel and copying files.
|
||||
# This is needed for scripts and tests which are not updated to the Bazel output
|
||||
# layout (which always matches the input layout).
|
||||
# Do not add new dependencies on this script, instead adapt scripts to use the
|
||||
# new layout, and write new tests as Bazel targets.
|
||||
#
|
||||
# Ideally integration tests should run under bazel, and just consume the npm
|
||||
# packages via `deps`. Until that works, we manually build the npm packages and then
|
||||
# copy the results to the appropriate `dist` location.
|
||||
|
||||
set -u -e -o pipefail
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
# basedir is the workspace root
|
||||
readonly base_dir="$(dirname "$(pwd)")"
|
||||
readonly bazel_bin="$(yarn run -s bazel info bazel-bin)"
|
||||
readonly script_path="$0"
|
||||
|
||||
function buildTargetPackages() {
|
||||
# Path to the output directory into which we copy the npm packages.
|
||||
dest_path="$1"
|
||||
|
||||
# Either "legacy" (view engine) or "aot" (ivy)
|
||||
compile_mode="$2"
|
||||
|
||||
# Human-readable description of the build.
|
||||
desc="$3"
|
||||
|
||||
echo "##################################"
|
||||
echo "${script_path}:"
|
||||
echo " Building @angular/* npm packages"
|
||||
echo " Mode: ${desc}"
|
||||
echo "##################################"
|
||||
|
||||
# List of targets to build, e.g. core, common, compiler, etc. Note that we want to
|
||||
# remove all carriage return ("\r") characters form the query output because otherwise
|
||||
# the carriage return is part of the bazel target name and bazel will complain.
|
||||
targets=$(yarn run -s bazel query --output=label 'attr("tags", "\[.*release-with-framework.*\]", //packages/...) intersect kind(".*_package", //packages/...)' | tr -d "\r")
|
||||
|
||||
# Use --config=release so that snapshot builds get published with embedded version info
|
||||
echo "$targets" | xargs yarn run -s bazel build --config=release --define=compile=${compile_mode}
|
||||
|
||||
[[ -d "${base_dir}/${dest_path}" ]] || mkdir -p ${base_dir}/${dest_path}
|
||||
|
||||
dirs=`echo "$targets" | sed -e 's/\/\/packages\/\(.*\):npm_package/\1/'`
|
||||
|
||||
for pkg in ${dirs}; do
|
||||
# Skip any that don't have an "npm_package" target
|
||||
src_dir="${bazel_bin}/packages/${pkg}/npm_package"
|
||||
dest_dir="${base_dir}/${dest_path}/${pkg}"
|
||||
if [[ -d ${src_dir} ]]; then
|
||||
echo "# Copy artifacts to ${dest_dir}"
|
||||
rm -rf ${dest_dir}
|
||||
cp -R ${src_dir} ${dest_dir}
|
||||
chmod -R u+w ${dest_dir}
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
Reference in New Issue
Block a user