|
| 1 | +use serde::Deserialize; |
1 | 2 | use std::path::{Path, PathBuf}; |
2 | 3 | use std::process::{Command, ExitStatus}; |
3 | | -use std::{env, fs}; |
4 | 4 |
|
| 5 | +use crate::cli::Args; |
5 | 6 | use crate::errors::*; |
6 | 7 | use crate::extensions::CommandExt; |
7 | 8 |
|
@@ -52,38 +53,90 @@ impl<'a> From<&'a str> for Subcommand { |
52 | 53 | } |
53 | 54 | } |
54 | 55 |
|
55 | | -#[derive(Debug)] |
56 | | -pub struct Root { |
57 | | - path: PathBuf, |
| 56 | +#[derive(Debug, Deserialize)] |
| 57 | +pub struct CargoMetadata { |
| 58 | + pub workspace_root: PathBuf, |
| 59 | + pub target_directory: PathBuf, |
| 60 | + pub packages: Vec<Package>, |
| 61 | + pub workspace_members: Vec<String>, |
58 | 62 | } |
59 | 63 |
|
60 | | -impl Root { |
61 | | - pub fn path(&self) -> &Path { |
62 | | - &self.path |
| 64 | +impl CargoMetadata { |
| 65 | + fn non_workspace_members(&self) -> impl Iterator<Item = &Package> { |
| 66 | + self.packages |
| 67 | + .iter() |
| 68 | + .filter(|p| !self.workspace_members.iter().any(|m| m == &p.id)) |
63 | 69 | } |
64 | | -} |
65 | 70 |
|
66 | | -/// Cargo project root |
67 | | -pub fn root() -> Result<Option<Root>> { |
68 | | - let cd = env::current_dir().wrap_err("couldn't get current directory")?; |
| 71 | + pub fn path_dependencies(&self) -> impl Iterator<Item = &Path> { |
| 72 | + // TODO: Also filter out things that are in workspace, but not a workspace member |
| 73 | + self.non_workspace_members().filter_map(|p| p.crate_path()) |
| 74 | + } |
| 75 | +} |
69 | 76 |
|
70 | | - let mut dir = &*cd; |
71 | | - loop { |
72 | | - let toml = dir.join("Cargo.toml"); |
| 77 | +#[derive(Debug, Deserialize)] |
| 78 | +pub struct Package { |
| 79 | + id: String, |
| 80 | + manifest_path: PathBuf, |
| 81 | + source: Option<String>, |
| 82 | +} |
73 | 83 |
|
74 | | - if fs::metadata(&toml).is_ok() { |
75 | | - return Ok(Some(Root { |
76 | | - path: dir.to_owned(), |
77 | | - })); |
| 84 | +impl Package { |
| 85 | + /// Returns the absolute path to the packages manifest "folder" |
| 86 | + fn crate_path(&self) -> Option<&Path> { |
| 87 | + // when source is none, this package is a path dependency or a workspace member |
| 88 | + if self.source.is_none() { |
| 89 | + self.manifest_path.parent() |
| 90 | + } else { |
| 91 | + None |
78 | 92 | } |
| 93 | + } |
| 94 | +} |
79 | 95 |
|
80 | | - match dir.parent() { |
81 | | - Some(p) => dir = p, |
82 | | - None => break, |
| 96 | +/// Cargo metadata with specific invocation |
| 97 | +pub fn cargo_metadata_with_args( |
| 98 | + cd: Option<&Path>, |
| 99 | + args: Option<&Args>, |
| 100 | + verbose: bool, |
| 101 | +) -> Result<Option<CargoMetadata>> { |
| 102 | + let mut command = std::process::Command::new( |
| 103 | + std::env::var("CARGO") |
| 104 | + .ok() |
| 105 | + .unwrap_or_else(|| "cargo".to_string()), |
| 106 | + ); |
| 107 | + command.arg("metadata").arg("--format-version=1"); |
| 108 | + if let Some(cd) = cd { |
| 109 | + command.current_dir(cd); |
| 110 | + } |
| 111 | + if let Some(config) = args { |
| 112 | + if let Some(ref manifest_path) = config.manifest_path { |
| 113 | + command.args(["--manifest-path".as_ref(), manifest_path.as_os_str()]); |
83 | 114 | } |
| 115 | + } else { |
| 116 | + command.arg("--no-deps"); |
84 | 117 | } |
85 | | - |
86 | | - Ok(None) |
| 118 | + if let Some(target) = args.and_then(|a| a.target.as_ref()) { |
| 119 | + command.args(["--filter-platform", target.triple()]); |
| 120 | + } |
| 121 | + if let Some(features) = args.map(|a| &a.features).filter(|v| !v.is_empty()) { |
| 122 | + command.args([String::from("--features"), features.join(",")]); |
| 123 | + } |
| 124 | + let output = command.run_and_get_output(verbose)?; |
| 125 | + if !output.status.success() { |
| 126 | + // TODO: logging |
| 127 | + return Ok(None); |
| 128 | + } |
| 129 | + let manifest: Option<CargoMetadata> = serde_json::from_slice(&output.stdout)?; |
| 130 | + manifest |
| 131 | + .map(|m| -> Result<_> { |
| 132 | + Ok(CargoMetadata { |
| 133 | + target_directory: args |
| 134 | + .and_then(|a| a.target_dir.clone()) |
| 135 | + .unwrap_or(m.target_directory), |
| 136 | + ..m |
| 137 | + }) |
| 138 | + }) |
| 139 | + .transpose() |
87 | 140 | } |
88 | 141 |
|
89 | 142 | /// Pass-through mode |
|
0 commit comments