-
Notifications
You must be signed in to change notification settings - Fork 11.9k
build: use bazel to perform release builds #24483
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,7 @@ genrule( | |
|
|
||
| pkg_npm( | ||
| name = "npm_package", | ||
| tags = ["release-package"], | ||
| deps = [ | ||
| ":README.md", | ||
| ":core", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.io/license | ||
| */ | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
|
|
||
| import { BuiltPackage } from '@angular/ng-dev'; | ||
| import { execSync } from 'node:child_process'; | ||
| import { chmodSync, copyFileSync, mkdirSync, rmSync } from 'node:fs'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import sh from 'shelljs'; | ||
|
|
||
| /** 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(fileURLToPath(import.meta.url)), '../'); | ||
|
|
||
| /** Command that runs Bazel. */ | ||
| const bazelCmd = process.env.BAZEL || `yarn -s bazel`; | ||
|
|
||
| /** Command that queries Bazel for all release package targets. */ | ||
| const queryPackagesCmd = | ||
| `${bazelCmd} query --output=label "attr('tags', '\\[.*${releaseTargetTag}.*\\]', //packages/...) ` + | ||
| `intersect kind('pkg_npm', //packages/...)"`; | ||
|
|
||
| /** Path for the default distribution output directory. */ | ||
| const defaultDistPath = join(projectDir, 'dist/releases'); | ||
|
|
||
| /** Builds the release packages for NPM. */ | ||
| export function performNpmReleaseBuild(): BuiltPackage[] { | ||
| return buildReleasePackages(defaultDistPath, /* isSnapshotBuild */ false); | ||
| } | ||
|
|
||
| /** | ||
| * Builds the release packages as snapshot build. This means that the current | ||
| * Git HEAD SHA is included in the version (for easier debugging and back tracing). | ||
| */ | ||
| export function performDefaultSnapshotBuild(): BuiltPackage[] { | ||
| return buildReleasePackages(defaultDistPath, /* isSnapshotBuild */ true); | ||
| } | ||
|
|
||
| /** | ||
| * Builds the release packages with the given compile mode and copies | ||
| * the package output into the given directory. | ||
| */ | ||
| function buildReleasePackages(distPath: string, isSnapshotBuild: boolean): BuiltPackage[] { | ||
| console.log('######################################'); | ||
| console.log(' Building release packages...'); | ||
| console.log('######################################'); | ||
|
|
||
| // List of targets to build. e.g. "packages/angular/cli:npm_package" | ||
| const targets = exec(queryPackagesCmd, true).split(/\r?\n/); | ||
| const packageNames = getPackageNamesOfTargets(targets); | ||
| const bazelBinPath = exec(`${bazelCmd} info bazel-bin`, true); | ||
| const getBazelOutputPath = (pkgName: string) => | ||
| join(bazelBinPath, 'packages', pkgName, 'npm_package'); | ||
| const getDistPath = (pkgName: string) => join(distPath, pkgName); | ||
|
|
||
| // Build with "--config=release" or `--config=snapshot` so that Bazel | ||
| // runs the workspace stamping script. The stamping script ensures that the | ||
| // version placeholder is populated in the release output. | ||
| const stampConfigArg = `--config=${isSnapshotBuild ? 'snapshot' : 'release'}`; | ||
|
|
||
| // 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) => { | ||
| // Directory output is created by the npm_package target | ||
| const directoryOutputPath = getBazelOutputPath(pkgName); | ||
| // Archive output is created by the npm_package_archive target | ||
| const archiveOutputPath = directoryOutputPath + '_archive.tgz'; | ||
|
|
||
| if (sh.test('-d', directoryOutputPath)) { | ||
| sh.chmod('-R', 'u+w', directoryOutputPath); | ||
| sh.rm('-rf', directoryOutputPath); | ||
| } | ||
| try { | ||
| chmodSync(archiveOutputPath, '0755'); | ||
| rmSync(archiveOutputPath, { force: true }); | ||
| } catch {} | ||
| }); | ||
|
|
||
| // Build both the npm_package and npm_package_archive targets for each package | ||
| // TODO: Consider switching to only using the archive for publishing | ||
| const buildTargets = targets.flatMap((target) => [target, target + '_archive']); | ||
| exec(`${bazelCmd} build ${stampConfigArg} ${buildTargets.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. | ||
| rmSync(distPath, { force: true, recursive: true, maxRetries: 3 }); | ||
| mkdirSync(distPath, { recursive: true }); | ||
|
|
||
| // Copy the package output into the specified distribution folder. | ||
| packageNames.forEach((pkgName) => { | ||
| // Directory output is created by the npm_package target | ||
| const directoryOutputPath = getBazelOutputPath(pkgName); | ||
| // Archive output is created by the npm_package_archive target | ||
| const archiveOutputPath = directoryOutputPath + '_archive.tgz'; | ||
|
|
||
| const targetFolder = getDistPath(pkgName); | ||
| console.log(`> Copying package output to "${targetFolder}"`); | ||
|
|
||
| // Ensure package scope directory exists prior to copying | ||
| mkdirSync(dirname(targetFolder), { recursive: true }); | ||
|
|
||
| // Copy package contents to target directory | ||
| sh.cp('-R', directoryOutputPath, targetFolder); | ||
| sh.chmod('-R', 'u+w', targetFolder); | ||
|
|
||
| // Copy archive of package to target directory | ||
| const archiveTargetPath = join(distPath, `${pkgName.replace('/', '_')}.tgz`); | ||
| copyFileSync(archiveOutputPath, archiveTargetPath); | ||
| chmodSync(archiveTargetPath, '0755'); | ||
| }); | ||
|
|
||
| return packageNames.map((pkg) => { | ||
| return { | ||
| // Package names on disk do not have the @ scope prefix and use underscores instead of dashes | ||
| name: `@${pkg.replace(/_/g, '-')}`, | ||
| outputPath: getDistPath(pkg), | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the package names of the specified Bazel targets. | ||
| * e.g. //packages/angular/cli:npm_package = angular/cli | ||
| */ | ||
| function getPackageNamesOfTargets(targets: string[]): string[] { | ||
| return targets.map((targetName) => { | ||
| const matches = targetName.match(/\/\/packages\/(.*?):/); | ||
| 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. */ | ||
| function exec(command: string): void; | ||
| /** Executes the given command in the project directory and returns its stdout. */ | ||
| function exec(command: string, captureStdout: true): string; | ||
| function exec(command: string, captureStdout?: true) { | ||
| const stdout = execSync(command, { | ||
| cwd: projectDir, | ||
| stdio: ['inherit', captureStdout ? 'pipe' : 'inherit', 'inherit'], | ||
| }); | ||
|
|
||
| if (captureStdout) { | ||
| process.stdout.write(stdout); | ||
|
|
||
| return stdout.toString().trim(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| "tests/**/*", | ||
| "tools/**/*", | ||
| ".ng-dev/**/*", | ||
| "**/*_spec.ts" | ||
| "**/*_spec.ts", | ||
| "scripts/**/*.mts" | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.