Skip to content

Commit

Permalink
More small cleanups, hopefully last round
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-h-chamberlain committed Jun 16, 2024
1 parent a103806 commit 59000ef
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
41 changes: 16 additions & 25 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,24 +301,14 @@ impl CargoCmd {
///
/// - `cargo 3ds build` and other "build" commands will use their callbacks to build the final `.3dsx` file and link it.
/// - `cargo 3ds new` and other generic commands will use their callbacks to make 3ds-specific changes to the environment.
pub fn run_callbacks(&self, messages: &[Message], metadata: &Option<Metadata>) {
let max_artifact_count = if let Some(metadata) = metadata {
metadata.packages.iter().map(|pkg| pkg.targets.len()).sum()
} else {
0
};

let mut configs = Vec::with_capacity(max_artifact_count);

// Process the metadata only for commands that have it/use it
if self.should_build_3dsx() {
// unwrap: we should always have metadata if the command should compile something
configs = self.build_callbacks(messages, metadata.as_ref().unwrap());
}
pub fn run_callbacks(&self, messages: &[Message], metadata: Option<&Metadata>) {
let configs = metadata
.map(|metadata| self.build_callbacks(messages, metadata))
.unwrap_or_default();

let config = match self {
// If we produced one executable, we will attempt to run that one
_ if configs.len() == 1 => configs.remove(0),
_ if configs.len() == 1 => configs.into_iter().next().unwrap(),

// --no-run may produce any number of executables, and we skip the callback
Self::Test(Test { no_run: true, .. }) => return,
Expand Down Expand Up @@ -353,7 +343,8 @@ impl CargoCmd {
/// Generate a .3dsx for every executable artifact within the workspace that
/// was built by the cargo command.
fn build_callbacks(&self, messages: &[Message], metadata: &Metadata) -> Vec<CTRConfig> {
let mut configs = Vec::new();
let max_artifact_count = metadata.packages.iter().map(|pkg| pkg.targets.len()).sum();
let mut configs = Vec::with_capacity(max_artifact_count);

for message in messages {
let Message::CompilerArtifact(artifact) = message else {
Expand All @@ -377,25 +368,25 @@ impl CargoCmd {
configs
}

fn inner_callbacks(&self) -> Option<&dyn Callbacks> {
Some(match self {
Self::Build(cmd) => cmd,
Self::Run(cmd) => cmd,
Self::Test(cmd) => cmd,
_ => return None,
})
fn inner_callback(&self) -> Option<&dyn Callbacks> {
match self {
Self::Build(cmd) => Some(cmd),
Self::Run(cmd) => Some(cmd),
Self::Test(cmd) => Some(cmd),
_ => None,
}
}
}

impl Callbacks for CargoCmd {
fn build_callback(&self, config: &CTRConfig) {
if let Some(cb) = self.inner_callbacks() {
if let Some(cb) = self.inner_callback() {
cb.build_callback(config);
}
}

fn run_callback(&self, config: &CTRConfig) {
if let Some(cb) = self.inner_callbacks() {
if let Some(cb) = self.inner_callback() {
cb.run_callback(config);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub fn get_artifact_config(package: Package, artifact: Artifact) -> CTRConfig {
_ => artifact.target.name,
};

// TODO: need to break down by target kind and name, e.g.
// TODO(#62): need to break down by target kind and name, e.g.
// [package.metadata.cargo-3ds.example.hello-world]
// Probably fall back to top level as well.
let config = package
Expand Down Expand Up @@ -413,8 +413,8 @@ impl CTRConfig {
Self::DEFAULT_AUTHOR.to_string()
};

let icon_path = self.icon_path().unwrap_or_else(|err| {
eprintln!("Icon at {err} does not exist");
let icon_path = self.icon_path().unwrap_or_else(|err_path| {
eprintln!("Icon at {err_path} does not exist");
process::exit(1);
});

Expand Down
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ fn main() {
};

let metadata = if input.cmd.should_build_3dsx() {
cargo_metadata::MetadataCommand::new().no_deps().exec().ok()
match cargo_metadata::MetadataCommand::new().no_deps().exec() {
Ok(metadata) => Some(metadata),
Err(err) => {
eprintln!("Warning: failed to gather cargo metadata for the project: {err}");
None
}
}
} else {
None
};
Expand All @@ -30,5 +36,5 @@ fn main() {
process::exit(status.code().unwrap_or(1));
}

input.cmd.run_callbacks(&messages, &metadata);
input.cmd.run_callbacks(&messages, metadata.as_ref());
}

0 comments on commit 59000ef

Please sign in to comment.