Skip to content

Commit

Permalink
[Package] Normalize build package options (#67)
Browse files Browse the repository at this point in the history
* refactor: prefer to always use the terminology 'source' when getting the source of a Go module, NPM package or Rust crate

Signed-off-by: jaudiger <jeremy.audiger@ioterop.com>

* refactor: rename goInstall method to goBuild

Signed-off-by: jaudiger <jeremy.audiger@ioterop.com>

* feat: add ability to set dependencies when building a Go module

Signed-off-by: jaudiger <jeremy.audiger@ioterop.com>

---------

Signed-off-by: jaudiger <jeremy.audiger@ioterop.com>
  • Loading branch information
jaudiger authored Jul 14, 2024
1 parent 6333fe1 commit 487de0b
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 64 deletions.
6 changes: 3 additions & 3 deletions examples/go_cli/project.bri
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { goInstall } from "go";
import { goBuild } from "go";

export default function () {
return goInstall({
goModule: Brioche.glob("**/*.go", "go.mod", "go.sum"),
return goBuild({
source: Brioche.glob("**/*.go", "go.mod", "go.sum"),
runnable: "bin/go_cli",
});
}
4 changes: 2 additions & 2 deletions examples/nodejs_frontend/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import miniserve from "miniserve";
// Build the static site into a plain directory
export function staticSite(): std.Recipe<std.Directory> {
// Get all the files needed for the build
let npmPackage = Brioche.glob(
const source = Brioche.glob(
"public",
"src",
".eslintrc.cjs",
Expand All @@ -16,7 +16,7 @@ export function staticSite(): std.Recipe<std.Directory> {
);

// Install the NPM dependencies
npmPackage = npmInstall({ npmPackage });
const npmPackage = npmInstall({ source });

// Run the build script, then save the output from the `dist/` directory
return std.runBash`
Expand Down
2 changes: 1 addition & 1 deletion examples/rust_backend/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { cargoBuild } from "rust";

export default function server() {
return cargoBuild({
crate: Brioche.glob("src", "Cargo.*"),
source: Brioche.glob("src", "Cargo.*"),
runnable: "bin/rust_backend",
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bat/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const project = {
version: "0.24.0",
};

const crate = std
const source = std
.download({
url: `https://github.com/sharkdp/bat/archive/refs/tags/v${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -18,7 +18,7 @@ const crate = std

export default () => {
return cargoBuild({
crate,
source,
runnable: "bin/bat",
});
};
4 changes: 2 additions & 2 deletions packages/broot/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const project = {
version: "1.39.0",
};

const crate = std
const source = std
.download({
url: `https://github.com/Canop/broot/archive/refs/tags/v${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -18,7 +18,7 @@ const crate = std

export default () => {
return cargoBuild({
crate,
source,
runnable: "bin/broot",
});
};
8 changes: 4 additions & 4 deletions packages/carapace/project.bri
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as std from "std";
import { goInstall } from "go";
import { goBuild } from "go";

export const project = {
name: "carapace",
version: "1.0.4",
};

const goModule = std
const source = std
.download({
url: `https://github.com/carapace-sh/carapace-bin/archive/refs/tags/v${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -17,8 +17,8 @@ const goModule = std
.peel();

export default () => {
return goInstall({
goModule,
return goBuild({
source,
buildParams: {
generate: true,
ldflags: ["-s", "-w", `-X main.version=${project.version}`],
Expand Down
4 changes: 2 additions & 2 deletions packages/dust/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const project = {
version: "1.0.0",
};

const crate = std
const source = std
.download({
url: `https://github.com/bootandy/dust/archive/refs/tags/v${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -18,7 +18,7 @@ const crate = std

export default () => {
return cargoBuild({
crate,
source,
runnable: "bin/dust",
});
};
4 changes: 2 additions & 2 deletions packages/eza/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const project = {
version: "0.18.20",
};

const crate = std
const source = std
.download({
url: `https://github.com/eza-community/eza/archive/refs/tags/v${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -18,7 +18,7 @@ const crate = std

export default () => {
return cargoBuild({
crate,
source,
runnable: "bin/eza",
});
};
22 changes: 13 additions & 9 deletions packages/go/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ interface GoBuildParameters {
}

interface GoBuildOptions {
goModule: std.AsyncRecipe<std.Directory>;
source: std.AsyncRecipe<std.Directory>;
dependencies?: std.AsyncRecipe<std.Directory>[];
env?: Record<string, std.ProcessTemplateLike>;
buildParams?: GoBuildParameters;
path?: string;
Expand All @@ -68,13 +69,14 @@ interface GoBuildOptions {
*
* ## Options
*
* - `goModule`: The Go module to build. Should include `go.mod`, as well as
* - `source`: The Go module to build. Should include `go.mod`, as well as
* `go.sum` if external dependencies are needed.
* - `buildParams`: Optional build parameters:
* - `generate`: Run `go generate` before building.
* - `ldflags`: An array of ldflags to pass to the `go install` command.
* - `trimpath`: Set the `-trimpath` flag.
* - `mod`: Set the `-mod` flag to one of `"readonly"`, `"vendor"`, or `"mod"`.
* - `dependencies`: Optionally add additional dependencies to the build.
* - `env`: Optionally set environment variables for the build.
* - `path`: Optionally set the package path to build (e.g. `./cmd/foo`).
* - `runnable`: Optionally set a path to the binary to run
Expand All @@ -83,11 +85,13 @@ interface GoBuildOptions {
* ## Example
*
* ```typescript
* import { goInstall } from "go";
* import { goBuild } from "go";
* import openssl from "openssl";
*
* export default function {
* return goInstall({
* goModule: Brioche.glob("**\/*.go", "go.mod", "go.sum"),
* return goBuild({
* source: Brioche.glob("**\/*.go", "go.mod", "go.sum"),
* dependencies: [openssl()],
* env: {
* CGO_ENABLED: "0",
* },
Expand All @@ -106,10 +110,10 @@ interface GoBuildOptions {
* };
* ```
*/
export async function goInstall(
export async function goBuild(
options: GoBuildOptions,
): Promise<std.Recipe<std.Directory>> {
const modules = goModDownload(options.goModule);
const modules = goModDownload(options.source);

let buildResult = std.runBash`
# Run generate if requested
Expand All @@ -133,8 +137,8 @@ export async function goInstall(
go install "\${goargs[@]}" "$package_path"
`
.workDir(options.goModule)
.dependencies(go())
.workDir(options.source)
.dependencies(go(), ...(options.dependencies ?? []))
.env({
GOMODCACHE: modules,
GOBIN: std.tpl`${std.outputPath}/bin`,
Expand Down
4 changes: 2 additions & 2 deletions packages/joshuto/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const project = {
version: "0.9.8",
};

const crate = std
const source = std
.download({
url: `https://github.com/kamiyaa/joshuto/archive/refs/tags/v${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -18,7 +18,7 @@ const crate = std

export default () => {
return cargoBuild({
crate,
source,
runnable: "bin/joshuto",
});
};
4 changes: 2 additions & 2 deletions packages/jwt_cli/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const project = {
version: "6.1.0",
};

const crate = std
const source = std
.download({
url: `https://github.com/mike-engel/jwt-cli/archive/refs/tags/${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -18,7 +18,7 @@ const crate = std

export default function () {
return cargoBuild({
crate,
source,
runnable: "bin/jwt",
});
}
8 changes: 4 additions & 4 deletions packages/k9s/project.bri
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as std from "std";
import { goInstall } from "go";
import { goBuild } from "go";

export const project = {
name: "k9s",
version: "0.32.5",
};
const gitCommit = "1440643e8d1a101a38d9be1933131ddf5c863940";

const goModule = std
const source = std
.download({
url: `https://github.com/derailed/k9s/archive/refs/tags/v${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -18,8 +18,8 @@ const goModule = std
.peel();

export default () => {
return goInstall({
goModule,
return goBuild({
source,
buildParams: {
ldflags: [
"-s",
Expand Down
4 changes: 2 additions & 2 deletions packages/lurk/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const project = {
version: "0.3.5",
};

const crate = std
const source = std
.download({
url: `https://github.com/JakWai01/lurk/archive/refs/tags/v${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -18,7 +18,7 @@ const crate = std

export default () => {
return cargoBuild({
crate,
source,
runnable: "bin/lurk",
});
};
4 changes: 2 additions & 2 deletions packages/miniserve/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const project = {
version: "0.27.1",
};

const crate = std
const source = std
.download({
url: `https://github.com/svenstaro/miniserve/archive/refs/tags/v${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -18,7 +18,7 @@ const crate = std

export default () => {
return cargoBuild({
crate,
source,
runnable: "bin/miniserve",
});
};
12 changes: 8 additions & 4 deletions packages/nodejs/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ function nodejs(): std.Recipe<std.Directory> {
export default nodejs;

interface NpmInstallOptions {
npmPackage: std.AsyncRecipe<std.Directory>;
source: std.AsyncRecipe<std.Directory>;
}

/**
* Install the dependencies from an NPM package. Returns a recipe containing
* everything from the package, plus a `node_modules` directory.
*
* ## Options
*
* - `source`: The NPM package dependencies to install.
*
* ## Example
*
* ```typescript
Expand All @@ -46,10 +50,10 @@ interface NpmInstallOptions {
*
* export default () => {
* // Get all the files for the NPM package
* let npmPackage = Brioche.glob("src", "package.lock", "package.json");
* const source = Brioche.glob("src", "package.lock", "package.json");
*
* // Install the dependencies
* npmPackage = npmInstall({ npmPackage });
* const npmPackage = npmInstall({ source });
*
* // Run the build script and save the output from `dist/`
* return std.runBash`
Expand All @@ -69,7 +73,7 @@ export function npmInstall(
npm clean-install
`
.dependencies(nodejs())
.outputScaffold(options.npmPackage)
.outputScaffold(options.source)
.unsafe({ networking: true })
.toDirectory();
}
4 changes: 2 additions & 2 deletions packages/nushell/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const project = {
version: "0.95.0",
};

const crate = std
const source = std
.download({
url: `https://github.com/nushell/nushell/archive/refs/tags/${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -19,7 +19,7 @@ const crate = std

export default () => {
return cargoBuild({
crate,
source,
runnable: "bin/nu",
dependencies: [openssl()],
});
Expand Down
4 changes: 2 additions & 2 deletions packages/oha/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const project = {
version: "1.4.5",
};

const crate = std
const source = std
.download({
url: `https://github.com/hatoo/oha/archive/refs/tags/v${project.version}.tar.gz`,
hash: std.sha256Hash(
Expand All @@ -18,7 +18,7 @@ const crate = std

export default () => {
return cargoBuild({
crate,
source,
runnable: "bin/oha",
});
};
Loading

0 comments on commit 487de0b

Please sign in to comment.