From e97f68a044993d2a1e0692cb6841beec18e082ff Mon Sep 17 00:00:00 2001 From: Miguel Di Ciurcio Filho Date: Fri, 17 Sep 2021 10:44:41 -0300 Subject: [PATCH] chore: make the build flexible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes the behavior of `build.ts` by using two new environment variables: BUILD_TARGET and RELEASE_VERSION. BUILD_TARGET must be one of the targets supported by deno. RELEASE_VERSION string used for naming the resulting binaries. Example: BUILD_TARGET=x86_64-unknown-linux-gnu \ RELEASE_VERSION=v0.1.0 \ deno run ... build.ts If the variables don’t exist, `build.ts` builds a binary based on runtime in use. --- cli/build.ts | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/cli/build.ts b/cli/build.ts index 777844f..2428aaf 100644 --- a/cli/build.ts +++ b/cli/build.ts @@ -2,7 +2,6 @@ import { bundle } from "https://deno.land/x/buckets@0.1.0/mod.ts"; import * as esbuild from "https://deno.land/x/esbuild@v0.12.19/mod.js"; import conf from "./buckets.ts"; -import { PIPELINIT_VERSION } from "./deps.ts"; const TARGETS = [ "x86_64-unknown-linux-gnu", @@ -11,6 +10,24 @@ const TARGETS = [ "aarch64-apple-darwin", ]; +const target: string | undefined = Deno.env.get("BUILD_TARGET"); +const version: string | undefined = Deno.env.get("RELEASE_VERSION"); + +if (target && !TARGETS.includes(target)) { + console.error( + `Invalid build target '${target}'. Must be one of:`, + ); + console.error(TARGETS.join("\n")); + Deno.exit(1); +} + +if (target && !version) { + console.error( + "The environment variable RELEASE_VERSION must be defined if BUILD_TARGET is also defined", + ); + Deno.exit(1); +} + // Bundle and minify await bundle(conf); const beforeMinify = await Deno.readTextFile(conf.output); @@ -20,7 +37,7 @@ const result = await esbuild.transform(beforeMinify, { await Deno.writeTextFile(conf.output, result.code); esbuild.stop(); -const compile = async function (target?: string) { +const compile = async function (target?: string, version?: string) { const cmd = [ "deno", "compile", @@ -28,12 +45,12 @@ const compile = async function (target?: string) { "--allow-read=.", "--allow-write", ]; - if (target) { + if (target && version) { cmd.push( "--target", target, "--output", - `bin/pipelinit-${PIPELINIT_VERSION}-${target}`, + `bin/pipelinit-${version}-${target}`, ); } else { cmd.push("--output", "bin/pipelinit"); @@ -47,14 +64,14 @@ const compile = async function (target?: string) { } }; -const tar = async function (target: string) { +const tar = async function (target: string, version: string) { const p = Deno.run({ cwd: "bin", cmd: [ "tar", "-czf", - `pipelinit-${PIPELINIT_VERSION}-${target}.tar.gz`, - `pipelinit-${PIPELINIT_VERSION}-${target}`, + `pipelinit-${version}-${target}.tar.gz`, + `pipelinit-${version}-${target}`, ], }); @@ -64,14 +81,14 @@ const tar = async function (target: string) { } }; -const zip = async function (target: string) { +const zip = async function (target: string, version: string) { const p = Deno.run({ cwd: "bin", cmd: [ "zip", "-9", - `pipelinit-${PIPELINIT_VERSION}-${target}.zip`, - `pipelinit-${PIPELINIT_VERSION}-${target}.exe`, + `pipelinit-${version}-${target}.zip`, + `pipelinit-${version}-${target}.exe`, ], }); @@ -81,18 +98,18 @@ const zip = async function (target: string) { } }; -const compress = async function (target: string) { +const compress = async function (target: string, version: string) { if (target === "x86_64-pc-windows-msvc") { - await zip(target); + await zip(target, version); } else { - await tar(target); + await tar(target, version); } }; -await compile(); -for (const target of TARGETS) { - await compile(target); - await compress(target); +await compile(target, version); + +if (target && version) { + await compress(target, version); } await Deno.remove(conf.output);