Skip to content

Commit 40ec49c

Browse files
authored
Provide workspace manifest path in Subcommand (#24)
Especially with the new `[workspace.package]` values, `cargo-apk` would like to know the path of the workspace that was found for the requested package, so that `cargo-apk` can scan it for values to inherit.
1 parent 70046ed commit 40ec49c

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

src/manifest.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::error::{Error, Result};
22
use serde::Deserialize;
33
use std::path::Path;
44

5-
#[derive(Debug, Deserialize)]
5+
#[derive(Clone, Debug, Deserialize)]
66
pub struct Manifest {
77
pub workspace: Option<Workspace>,
88
pub package: Option<Package>,
@@ -15,12 +15,12 @@ impl Manifest {
1515
}
1616
}
1717

18-
#[derive(Debug, Deserialize)]
18+
#[derive(Clone, Debug, Deserialize)]
1919
pub struct Workspace {
2020
pub members: Vec<String>,
2121
}
2222

23-
#[derive(Debug, Deserialize)]
23+
#[derive(Clone, Debug, Deserialize)]
2424
pub struct Package {
2525
pub name: String,
2626
}

src/subcommand.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::path::{Path, PathBuf};
1010
pub struct Subcommand {
1111
args: Args,
1212
package: String,
13+
workspace_manifest: Option<PathBuf>,
1314
manifest: PathBuf,
1415
target_dir: PathBuf,
1516
host_triple: String,
@@ -57,14 +58,18 @@ impl Subcommand {
5758
// or contains the given `--manifest-path` as member.
5859
let workspace_manifest = utils::find_workspace(&search_path)?;
5960

60-
let (manifest_path, package) =
61-
if let Some((workspace_manifest_path, workspace)) = &workspace_manifest {
62-
// If a workspace was found, find packages relative to it
63-
utils::find_package_in_workspace(workspace_manifest_path, workspace, package)?
64-
} else {
65-
// Otherwise scan up the directories
66-
utils::find_package(&search_path, package)?
67-
};
61+
let (manifest_path, manifest) = if let Some((workspace_manifest_path, workspace)) =
62+
&workspace_manifest
63+
{
64+
// If a workspace was found, find packages relative to it
65+
utils::find_package_manifest_in_workspace(workspace_manifest_path, workspace, package)?
66+
} else {
67+
// Otherwise scan up the directories
68+
utils::find_package_manifest(&search_path, package)?
69+
};
70+
71+
// The manifest is known to contain a package at this point
72+
let package = &manifest.package.as_ref().unwrap().name;
6873

6974
let root_dir = manifest_path.parent().unwrap();
7075

@@ -127,7 +132,8 @@ impl Subcommand {
127132
let profile = args.profile();
128133
Ok(Self {
129134
args,
130-
package,
135+
package: package.clone(),
136+
workspace_manifest: workspace_manifest.map(|(path, _)| path),
131137
manifest: manifest_path,
132138
target_dir,
133139
host_triple,
@@ -145,6 +151,10 @@ impl Subcommand {
145151
&self.package
146152
}
147153

154+
pub fn workspace_manifest(&self) -> Option<&Path> {
155+
self.workspace_manifest.as_deref()
156+
}
157+
148158
pub fn manifest(&self) -> &Path {
149159
&self.manifest
150160
}

src/utils.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ pub fn list_rust_files(dir: &Path) -> Result<Vec<String>> {
1919
Ok(files)
2020
}
2121

22-
fn match_package_name(manifest: &Manifest, name: Option<&str>) -> Option<String> {
22+
fn match_package_name(manifest: &Manifest, name: Option<&str>) -> bool {
2323
if let Some(p) = &manifest.package {
2424
if let Some(name) = name {
2525
if name == p.name {
26-
return Some(p.name.clone());
26+
return true;
2727
}
2828
} else {
29-
return Some(p.name.clone());
29+
return true;
3030
}
3131
}
3232

33-
None
33+
false
3434
}
3535

3636
/// Tries to find a package by the given `name` in the [workspace root] or member
3737
/// of the given [workspace] [`Manifest`].
3838
///
39-
/// When a workspace is not detected, call [`find_package()`] instead.
39+
/// When a workspace is not detected, call [`find_package_manifest()`] instead.
4040
///
4141
/// [workspace root]: https://doc.rust-lang.org/cargo/reference/workspaces.html#root-package
4242
/// [workspace]: https://doc.rust-lang.org/cargo/reference/workspaces.html#workspaces
43-
pub fn find_package_in_workspace(
43+
pub fn find_package_manifest_in_workspace(
4444
workspace_manifest_path: &Path,
4545
workspace_manifest: &Manifest,
4646
name: Option<&str>,
47-
) -> Result<(PathBuf, String)> {
47+
) -> Result<(PathBuf, Manifest)> {
4848
let workspace = workspace_manifest
4949
.workspace
5050
.as_ref()
@@ -57,8 +57,11 @@ pub fn find_package_in_workspace(
5757
let name = name.ok_or(Error::MultiplePackagesNotSupported)?;
5858

5959
// Check if the workspace manifest also contains a [package]
60-
if let Some(package) = match_package_name(workspace_manifest, Some(name)) {
61-
return Ok((workspace_manifest_path.to_owned(), package));
60+
if match_package_name(workspace_manifest, Some(name)) {
61+
return Ok((
62+
workspace_manifest_path.to_owned(),
63+
workspace_manifest.clone(),
64+
));
6265
}
6366

6467
// Check all member packages inside the workspace
@@ -73,8 +76,8 @@ pub fn find_package_in_workspace(
7376
return Err(Error::UnexpectedWorkspace(manifest_path));
7477
}
7578

76-
if let Some(package) = match_package_name(&manifest, Some(name)) {
77-
return Ok((manifest_path, package));
79+
if match_package_name(&manifest, Some(name)) {
80+
return Ok((manifest_path, manifest));
7881
}
7982
}
8083
}
@@ -84,9 +87,9 @@ pub fn find_package_in_workspace(
8487

8588
/// Recursively walk up the directories until finding a `Cargo.toml`
8689
///
87-
/// When a workspace has been detected, [`find_package_in_workspace()`] to find packages
90+
/// When a workspace has been detected, [`find_package_manifest_in_workspace()`] to find packages
8891
/// instead (that are members of the given workspace).
89-
pub fn find_package(path: &Path, name: Option<&str>) -> Result<(PathBuf, String)> {
92+
pub fn find_package_manifest(path: &Path, name: Option<&str>) -> Result<(PathBuf, Manifest)> {
9093
let path = dunce::canonicalize(path).map_err(|e| Error::Io(path.to_owned(), e))?;
9194
for manifest_path in path
9295
.ancestors()
@@ -100,8 +103,8 @@ pub fn find_package(path: &Path, name: Option<&str>) -> Result<(PathBuf, String)
100103
return Err(Error::UnexpectedWorkspace(manifest_path));
101104
}
102105

103-
if let Some(package) = match_package_name(&manifest, name) {
104-
return Ok((manifest_path, package));
106+
if match_package_name(&manifest, name) {
107+
return Ok((manifest_path, manifest));
105108
}
106109
}
107110
Err(Error::ManifestNotFound)

0 commit comments

Comments
 (0)