Skip to content

Commit

Permalink
Add an npm package output
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Jan 31, 2020
1 parent 56a03d9 commit 8ee2faf
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 4 deletions.
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",
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",
"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}"

0 comments on commit 8ee2faf

Please sign in to comment.