Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added OS-specific overrides for hooks. #874

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trunk"
version = "0.20.3"
version = "0.20.4"
edition = "2021"
description = "Build, bundle & ship your Rust WASM application to the web."
license = "MIT/Apache-2.0"
Expand Down
8 changes: 8 additions & 0 deletions Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ stage = "build"
command = "sh"
command_arguments = ["-c", "echo Staging directory: $TRUNK_STAGING_DIR"]

[hooks.windows]
# This shows how to create OS-specific overrides for hooks.
# This will override the hook directly above it.
# For overrides, only the `command` and `command_arguments` keys must be specified.
# Valid values are `windows`, `macos`, and `linux`.
command = "cmd"
command_arguments = ["/c", "echo Staging directory: %TRUNK_STAGING_DIR%"]

[[hooks]]
# This hook example shows how command_arguments defaults to an empty list when absent. It also uses
# the post_build stage, meaning it executes after the rest of the build is complete, just before
Expand Down
4 changes: 4 additions & 0 deletions site/content/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ All hooks are executed using the same `stdin` and `stdout` as trunk. The executa
- `TRUNK_DIST_DIR`: the full path of the Trunk dist directory.
- `TRUNK_PUBLIC_URL`: the configured public URL for Trunk.

## OS-specific overrides

Often times you will want to perform the same build step on different OSes, requiring different commands. A typical example of this is using the `sh` command on Linux, but `cmd` on Windows. To accomodate this, you can optionally create OS-specific overrides for each hook. To do this, specify the default hook, then directly below it create a `[hooks.<os>]` entry where `<os>` can be one of `windows`, `macos`, or `linux`. Within this entry you must specify only the `command` and `command_argumnets` keys. You may provide multiple overrides for each hook. i.e. One for `windows`, one for `macos`, and one for `linux`.

# Auto-Reload

As of `v0.14.0`, Trunk now ships with the ability to automatically reload your web app as the Trunk build pipeline completes.
Expand Down
61 changes: 61 additions & 0 deletions src/config/models/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,67 @@ use serde::Deserialize;
pub struct ConfigOptsHook {
/// The stage in the build process to execute this hook.
pub stage: PipelineStage,
/// The command to run for this hook.
command: String,
/// Any arguments to pass to the command.
#[serde(default)]
command_arguments: Vec<String>,
/// Overrides
#[serde(default, flatten)]
overrides: ConfigOptsHookOverrride,
}

impl ConfigOptsHook {
pub fn command(&self) -> &String {
if cfg!(target_os = "windows") {
if let Some(cfg) = self.overrides.windows.as_ref() {
return &cfg.command;
}
} else if cfg!(target_os = "macos") {
if let Some(cfg) = self.overrides.macos.as_ref() {
return &cfg.command;
}
} else if cfg!(target_os = "linux") {
if let Some(cfg) = self.overrides.linux.as_ref() {
return &cfg.command;
}
}

&self.command
}

pub fn command_arguments(&self) -> &Vec<String> {
if cfg!(target_os = "windows") {
if let Some(cfg) = self.overrides.windows.as_ref() {
return &cfg.command_arguments;
}
} else if cfg!(target_os = "macos") {
if let Some(cfg) = self.overrides.macos.as_ref() {
return &cfg.command_arguments;
}
} else if cfg!(target_os = "linux") {
if let Some(cfg) = self.overrides.linux.as_ref() {
return &cfg.command_arguments;
}
}

&self.command_arguments
}
}

/// Config options for build system hooks.
#[derive(Clone, Debug, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub struct ConfigOptsHookOverrride {
pub windows: Option<ConfigOptsHookOs>,
pub macos: Option<ConfigOptsHookOs>,
pub linux: Option<ConfigOptsHookOs>,
}

/// OS-specific overrides.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct ConfigOptsHookOs {
/// The command to run for this hook.
pub command: String,
/// Any arguments to pass to the command.
Expand Down
8 changes: 4 additions & 4 deletions src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub fn spawn_hooks(cfg: Arc<RtcBuild>, stage: PipelineStage) -> HookHandles {
.iter()
.filter(|hook_cfg| hook_cfg.stage == stage)
.map(|hook_cfg| {
let mut command = Command::new(&hook_cfg.command);
let mut command = Command::new(hook_cfg.command());
command
.args(&hook_cfg.command_arguments)
.args(hook_cfg.command_arguments())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.env("TRUNK_PROFILE", if cfg.release { "release" } else { "debug" })
Expand All @@ -31,9 +31,9 @@ pub fn spawn_hooks(cfg: Arc<RtcBuild>, stage: PipelineStage) -> HookHandles {
.env("TRUNK_DIST_DIR", &cfg.final_dist)
.env("TRUNK_PUBLIC_URL", &cfg.public_url);

tracing::info!(command_arguments = ?hook_cfg.command_arguments, "spawned hook {}", hook_cfg.command);
tracing::info!(command_arguments = ?hook_cfg.command_arguments(), "spawned hook {}", hook_cfg.command());

let command_name = hook_cfg.command.clone();
let command_name = hook_cfg.command().clone();
tracing::info!(?stage, command = %command_name, "spawning hook");
tokio::spawn(async move {
let status = command
Expand Down