diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 00000000..a4d42a29 --- /dev/null +++ b/.bazelrc @@ -0,0 +1 @@ +build:release -c opt --stamp --workspace_status_command="$PWD/stamp.sh" diff --git a/BUILD b/BUILD index 8ace31fb..d7fe0284 100644 --- a/BUILD +++ b/BUILD @@ -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") @@ -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", + ], +) diff --git a/WORKSPACE b/WORKSPACE index 2f018c1b..8b7d135c 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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() diff --git a/bazelisk.js b/bazelisk.js new file mode 100644 index 00000000..c439d95a --- /dev/null +++ b/bazelisk.js @@ -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, +}; diff --git a/build.sh b/build.sh index 9d438c63..9367593a 100755 --- a/build.sh +++ b/build.sh @@ -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 @@ -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 diff --git a/package.json b/package.json new file mode 100644 index 00000000..01691c7f --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "name": "@bazel/bazelisk", + "version": "0.0.0-PLACEHOLDER", + "license": "Apache-2.0", + "bin": { + "bazelisk": "bazelisk.js" + } +} diff --git a/rules_nodejs.pr1591.patch b/rules_nodejs.pr1591.patch new file mode 100644 index 00000000..69718239 --- /dev/null +++ b/rules_nodejs.pr1591.patch @@ -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]); diff --git a/stamp.sh b/stamp.sh index 773743c1..9aa8d0ff 100755 --- a/stamp.sh +++ b/stamp.sh @@ -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}"