Skip to content

Commit 0c15f3e

Browse files
committed
build script: move Command run logic to .run()
1 parent fb10e19 commit 0c15f3e

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

crates/cargo-gpu/src/main.rs

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -124,40 +124,8 @@ fn run() -> anyhow::Result<()> {
124124
})
125125
.collect::<Vec<_>>();
126126
log::trace!("CLI args: {env_args:#?}");
127-
let cli = Cli::parse_from(env_args.clone());
128-
129-
match cli.command {
130-
Command::Install(install) => {
131-
let shader_crate_path = install.shader_crate;
132-
let command =
133-
config::Config::clap_command_with_cargo_config(&shader_crate_path, env_args)?;
134-
log::debug!(
135-
"installing with final merged arguments: {:#?}",
136-
command.install
137-
);
138-
command.install.run()?;
139-
}
140-
Command::Build(build) => {
141-
let shader_crate_path = build.install.shader_crate;
142-
let mut command =
143-
config::Config::clap_command_with_cargo_config(&shader_crate_path, env_args)?;
144-
log::debug!("building with final merged arguments: {command:#?}");
145-
146-
if command.build.watch {
147-
// When watching, do one normal run to setup the `manifest.json` file.
148-
command.build.watch = false;
149-
command.run()?;
150-
command.build.watch = true;
151-
command.run()?;
152-
} else {
153-
command.run()?;
154-
}
155-
}
156-
Command::Show(show) => show.run()?,
157-
Command::DumpUsage => dump_full_usage_for_readme()?,
158-
}
159-
160-
Ok(())
127+
let cli = Cli::parse_from(&env_args);
128+
cli.command.run(env_args)
161129
}
162130

163131
/// All of the available subcommands for `cargo gpu`
@@ -179,6 +147,45 @@ enum Command {
179147
DumpUsage,
180148
}
181149

150+
impl Command {
151+
/// run the command
152+
pub fn run(&self, env_args: Vec<String>) -> anyhow::Result<()> {
153+
match &self {
154+
Self::Install(install) => {
155+
let shader_crate_path = &install.shader_crate;
156+
let command =
157+
config::Config::clap_command_with_cargo_config(shader_crate_path, env_args)?;
158+
log::debug!(
159+
"installing with final merged arguments: {:#?}",
160+
command.install
161+
);
162+
command.install.run()?;
163+
}
164+
Self::Build(build) => {
165+
let shader_crate_path = &build.install.shader_crate;
166+
let mut command =
167+
config::Config::clap_command_with_cargo_config(shader_crate_path, env_args)?;
168+
log::debug!("building with final merged arguments: {command:#?}");
169+
170+
if command.build.watch {
171+
// When watching, do one normal run to setup the `manifest.json` file.
172+
command.build.watch = false;
173+
command.run()?;
174+
command.build.watch = true;
175+
command.run()?;
176+
} else {
177+
command.run()?;
178+
}
179+
}
180+
Self::Show(show) => show.run()?,
181+
Self::DumpUsage => dump_full_usage_for_readme()?,
182+
}
183+
184+
Ok(())
185+
}
186+
}
187+
188+
/// the Cli struct representing the main cli
182189
#[derive(clap::Parser)]
183190
#[clap(author, version, about, subcommand_required = true)]
184191
pub(crate) struct Cli {

crates/cargo-gpu/src/show.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ pub struct Show {
3333

3434
impl Show {
3535
/// Entrypoint
36-
pub fn run(self) -> anyhow::Result<()> {
36+
pub fn run(&self) -> anyhow::Result<()> {
3737
log::info!("{:?}: ", self.command);
3838

3939
#[expect(
4040
clippy::print_stdout,
4141
reason = "The output of this command could potentially be used in a script, \
4242
so we _don't_ want to use `crate::user_output`, as that prefixes a crab."
4343
)]
44-
match self.command {
44+
match &self.command {
4545
Info::CacheDirectory => {
4646
println!("{}\n", cache_dir()?.display());
4747
}
4848
Info::SpirvSource(SpirvSourceDep { shader_crate }) => {
4949
let rust_gpu_source =
50-
crate::spirv_source::SpirvSource::get_rust_gpu_deps_from_shader(&shader_crate)?;
50+
crate::spirv_source::SpirvSource::get_rust_gpu_deps_from_shader(shader_crate)?;
5151
println!("{rust_gpu_source}\n");
5252
}
5353
Info::Commitsh => {
54-
println!("{}", std::env!("GIT_HASH"));
54+
println!("{}", env!("GIT_HASH"));
5555
}
5656
Info::Capabilities => {
5757
println!("All available options to the `cargo gpu build --capabilities` argument:");

0 commit comments

Comments
 (0)