Skip to content

Commit

Permalink
feat: Added OS-specific hook overrides.
Browse files Browse the repository at this point in the history
  • Loading branch information
bicarlsen authored and ctron committed Sep 19, 2024
1 parent 0d0cbd3 commit 5f1911c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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
64 changes: 63 additions & 1 deletion src/config/models/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,70 @@ pub struct Hook {
/// The stage in the build process to execute this hook.
pub stage: PipelineStage,
/// The command to run for this hook.
pub command: String,
command: String,
/// Any arguments to pass to the command.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
command_arguments: Vec<String>,
/// Overrides
#[serde(default, flatten)]
overrides: HookOverrides,
}

impl Hook {
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
}
}

/// Hook override config.
#[derive(Clone, Debug, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub struct HookOverrides {
pub windows: Option<HookOverride>,
pub macos: Option<HookOverride>,
pub linux: Option<HookOverride>,
}

/// OS-specific overrides.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct HookOverride {
/// The command to run for this hook.
pub command: String,
/// Any arguments to pass to the command.
#[serde(default)]
pub command_arguments: Vec<String>,
}

Expand Down Expand Up @@ -40,6 +101,7 @@ mod test {
stage: PipelineStage::PreBuild,
command: "foo".to_string(),
command_arguments: vec![],
overrides: HookOverrides::default(),
}]),
})
.expect("must serialize");
Expand Down
8 changes: 4 additions & 4 deletions src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ 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
.current_dir(&cfg.core.working_directory)
.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 @@ -27,9 +27,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

0 comments on commit 5f1911c

Please sign in to comment.