Skip to content

Commit

Permalink
chore: make the build flexible
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mciurcio committed Sep 21, 2021
1 parent 408deeb commit e97f68a
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions cli/build.ts
Expand Up @@ -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",
Expand All @@ -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);
Expand All @@ -20,20 +37,20 @@ 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",
"--unstable",
"--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");
Expand All @@ -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}`,
],
});

Expand All @@ -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`,
],
});

Expand All @@ -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);

0 comments on commit e97f68a

Please sign in to comment.