fix: add missing run alias behaviour

The `run <alias>` behaviour was documented yet
missing. This commit adds it.
This commit is contained in:
daimond113 2025-03-09 14:51:19 +01:00
parent 12c62d315d
commit 0e73db2831
No known key found for this signature in database
GPG key ID: 640DC95EC1190354
2 changed files with 23 additions and 8 deletions

View file

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Fix path dependencies using project's workspace dependencies by @daimond113 - Fix path dependencies using project's workspace dependencies by @daimond113
- Fix binary linkers not being created for non-direct dependencies by @daimond113 - Fix binary linkers not being created for non-direct dependencies by @daimond113
- Add missing `run <alias>` behaviour by @daimond113
### Changed ### Changed
- Binary linkers are now done in Rust to simplify their implementation and cross-runtime portability by @daimond113 - Binary linkers are now done in Rust to simplify their implementation and cross-runtime portability by @daimond113

View file

@ -1,10 +1,11 @@
use crate::cli::up_to_date_lockfile; use crate::cli::{style::WARN_STYLE, up_to_date_lockfile};
use anyhow::Context as _; use anyhow::Context as _;
use clap::Args; use clap::Args;
use futures::{StreamExt as _, TryStreamExt as _}; use futures::{StreamExt as _, TryStreamExt as _};
use pesde::{ use pesde::{
errors::{ManifestReadError, WorkspaceMembersError}, errors::{ManifestReadError, WorkspaceMembersError},
linking::generator::generate_bin_linking_module, linking::generator::generate_bin_linking_module,
manifest::Alias,
names::{PackageName, PackageNames}, names::{PackageName, PackageNames},
source::traits::{GetTargetOptions, PackageRef as _, PackageSource as _, RefreshOptions}, source::traits::{GetTargetOptions, PackageRef as _, PackageSource as _, RefreshOptions},
Project, MANIFEST_FILE_NAME, Project, MANIFEST_FILE_NAME,
@ -28,7 +29,7 @@ pub struct RunCommand {
impl RunCommand { impl RunCommand {
pub async fn run(self, project: Project) -> anyhow::Result<()> { pub async fn run(self, project: Project) -> anyhow::Result<()> {
let run = |root: &Path, file_path: &Path| { let run = |root: &Path, file_path: &Path| -> ! {
let mut caller = tempfile::NamedTempFile::new().expect("failed to create tempfile"); let mut caller = tempfile::NamedTempFile::new().expect("failed to create tempfile");
caller caller
.write_all( .write_all(
@ -60,12 +61,13 @@ impl RunCommand {
project.package_dir(), project.package_dir(),
&script_path.to_path(project.package_dir()), &script_path.to_path(project.package_dir()),
); );
return Ok(());
} }
anyhow::bail!("no package or script specified, and no bin path found in manifest") anyhow::bail!("no package or script specified, and no bin path found in manifest")
}; };
let mut package_info = None;
if let Ok(pkg_name) = package_or_script.parse::<PackageName>() { if let Ok(pkg_name) = package_or_script.parse::<PackageName>() {
let graph = if let Some(lockfile) = up_to_date_lockfile(&project).await? { let graph = if let Some(lockfile) = up_to_date_lockfile(&project).await? {
lockfile.graph lockfile.graph
@ -80,12 +82,28 @@ impl RunCommand {
.filter(|(id, node)| *id.name() == pkg_name && node.direct.is_some()) .filter(|(id, node)| *id.name() == pkg_name && node.direct.is_some())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let (id, node) = match versions.len() { package_info = Some(match versions.len() {
0 => anyhow::bail!("package not found"), 0 => anyhow::bail!("package not found"),
1 => versions.pop().unwrap(), 1 => versions.pop().unwrap(),
_ => anyhow::bail!("multiple versions found. use the package's alias instead."), _ => anyhow::bail!("multiple versions found. use the package's alias instead."),
});
} else if let Ok(alias) = package_or_script.parse::<Alias>() {
if let Some(lockfile) = up_to_date_lockfile(&project).await? {
package_info = lockfile
.graph
.into_iter()
.find(|(_, node)| node.direct.as_ref().is_some_and(|(a, _, _)| alias == *a));
} else {
eprintln!(
"{}",
WARN_STYLE.apply_to(
"outdated lockfile, please run the install command first to use an alias"
)
);
}; };
}
if let Some((id, node)) = package_info {
let container_folder = node.container_folder_from_project( let container_folder = node.container_folder_from_project(
&id, &id,
&project, &project,
@ -122,7 +140,6 @@ impl RunCommand {
let path = bin_path.to_path(&container_folder); let path = bin_path.to_path(&container_folder);
run(&path, &path); run(&path, &path);
return Ok(());
} }
if let Ok(manifest) = project.deser_manifest().await { if let Ok(manifest) = project.deser_manifest().await {
@ -131,7 +148,6 @@ impl RunCommand {
project.package_dir(), project.package_dir(),
&script_path.to_path(project.package_dir()), &script_path.to_path(project.package_dir()),
); );
return Ok(());
} }
} }
@ -194,7 +210,5 @@ impl RunCommand {
}; };
run(&root, &path); run(&root, &path);
Ok(())
} }
} }