Skip to content
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

Add an npm package output #114

Merged
merged 1 commit into from
Feb 4, 2020
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 .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build:release -c opt --stamp --workspace_status_command="$PWD/stamp.sh"
46 changes: 46 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")

# gazelle:prefix github.com/bazelbuild/bazelisk
gazelle(name = "gazelle")
Expand Down Expand Up @@ -54,3 +55,48 @@ go_binary(
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)


go_binary(
name = "bazelisk-darwin",
out = "bazelisk-darwin_amd64",
embed = [":go_default_library"],
goarch = "amd64",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know that one could create platform-specific go_binary targets - that's much better than my existing approach in build.sh! :) Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I figured you might want to refactor that script too. If nothing else, this allows you to build all the architectures in parallel, even if you still loop through them to place them in a bin directory

goos = "darwin",
pure = "on",
visibility = ["//visibility:public"],
)

go_binary(
name = "bazelisk-linux",
out = "bazelisk-linux_amd64",
embed = [":go_default_library"],
goarch = "amd64",
goos = "linux",
pure = "on",
visibility = ["//visibility:public"],
)

go_binary(
name = "bazelisk-windows",
out = "bazelisk-windows_amd64.exe",
embed = [":go_default_library"],
goarch = "amd64",
goos = "windows",
pure = "on",
visibility = ["//visibility:public"],
)

pkg_npm(
name = "npm_package",
srcs = [
"LICENSE",
"package.json",
"bazelisk.js",
],
deps = [
":bazelisk-darwin",
":bazelisk-linux",
":bazelisk-windows",
],
)
10 changes: 10 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ go_repository(
sum = "h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=",
version = "v1.1.0",
)

# We don't use any nodejs but this includes a rule for publishing releases to npm
http_archive(
name = "build_bazel_rules_nodejs",
patches = ["//:rules_nodejs.pr1591.patch"],
sha256 = "ecaa54955b314b5e33948bd8f39e35c35ee89e905d8de1c03868100293510573",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/1.2.1/rules_nodejs-1.2.1.tar.gz"],
)
load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories")
node_repositories()
73 changes: 73 additions & 0 deletions bazelisk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env node
// Copyright 2020 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';

// This package inspired by
// https://github.com/angular/clang-format/blob/master/index.js
const os = require('os');
const path = require('path');
const spawn = require('child_process').spawn;

function getNativeBinary() {
const arch = {
'x64' : 'amd64',
}[os.arch()];
// Filter the platform based on the platforms that are build/included.
const platform = {
'darwin' : 'darwin',
'linux' : 'linux',
'win32' : 'windows',
}[os.platform()];
const extension = {
'darwin' : '',
'linux' : '',
'win32' : '.exe',
}[os.platform()];

if (arch == undefined || platform == undefined) {
console.error(`FATAL: Your platform/architecture combination ${
os.platform()} - ${os.arch()} is not yet supported.
You may need to compile Bazelisk yourself, or use the Python version.
See instructions at https://github.com/bazelbuild/bazelisk/blob/master/README.md.`);
return Promise.resolve(1);
}

const binary =
path.join(__dirname, `bazelisk-${platform}_${arch}${extension}`);
return binary;
}

function main(args) {
const binary = getNativeBinary();
const ps = spawn(binary, args, {stdio : 'inherit'});

function shutdown() {
ps.kill("SIGTERM")
process.exit();
}

process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);

ps.on('close', e => process.exitCode = e);
}

if (require.main === module) {
main(process.argv.slice(2));
}

module.exports = {
getNativeBinary,
};
11 changes: 7 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ mkdir bin

go build
for platform in darwin linux windows; do
./bazelisk build \
-c opt \
--stamp \
--workspace_status_command="$PWD/stamp.sh" \
./bazelisk build --config=release \
--platforms=@io_bazel_rules_go//go/toolchain:${platform}_amd64 \
//:bazelisk
if [[ $platform == windows ]]; then
Expand All @@ -44,3 +41,9 @@ rm -f bazelisk
### Print some information about the generated binaries.
ls -lh bin/*
file bin/*

# Non-googlers: you should run this script with NPM_REGISTRY=https://registry.npmjs.org
readonly REGISTRY=${NPM_REGISTRY:-https://wombat-dressing-room.appspot.com}
# Googlers: you should npm login using the go/npm-publish service:
# $ npm login --registry https://wombat-dressing-room.appspot.com
./bazelisk run --config=release //:npm_package.publish -- --access=public --tag latest --registry $REGISTRY
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@bazel/bazelisk",
"description": "A user-friendly launcher for Bazel",
"version": "0.0.0-PLACEHOLDER",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is the "version" here replaced with the correct version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"license": "Apache-2.0",
"bin": {
"bazelisk": "bazelisk.js"
},
"keywords": [
"bazel"
],
"repository": {
"type": "git",
"url": "https://github.com/bazelbuild/bazelisk.git"
},
"bugs": {
"url": "https://github.com/bazelbuild/bazelisk/issues"
}
}
13 changes: 13 additions & 0 deletions rules_nodejs.pr1591.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff internal/pkg_npm/packager.js internal/pkg_npm/packager.js
index ac41b585..48167a19 100644
--- internal/pkg_npm/packager.js
+++ internal/pkg_npm/packager.js
@@ -89,7 +89,7 @@ function main(args) {
.find(s => s.startsWith('BUILD_SCM_VERSION'));
// Don't assume BUILD_SCM_VERSION exists
if (versionTag) {
- version = versionTag.split(' ')[1].trim();
+ version = versionTag.split(' ')[1].replace(/^v/, '').trim();
}
}
substitutions.push([new RegExp(replaceWithVersion, 'g'), version]);
2 changes: 2 additions & 0 deletions stamp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ CURRENT_TAG=$(git tag -l --points-at HEAD | head -n1)
CURRENT_COMMIT=$(git rev-parse HEAD)

echo "STABLE_VERSION ${CURRENT_TAG:-$CURRENT_COMMIT}"
# rules_nodejs expects to read from volatile-status.txt
echo "BUILD_SCM_VERSION ${CURRENT_TAG:-$CURRENT_COMMIT}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found it valuable to add a -dirty suffix when user changes happen. This saved me trying to debug someone's issue which would have been impossible without it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, for Angular we use a .with-local-changes suffix in a non-pristine clone
https://github.com/angular/angular/blob/master/tools/bazel_stamp_vars.js#L52-L53

But of course that change should be made for all the stamping here, not just npm, so I don't think it should go in this PR. @philwo do you want a separate PR for that?