|
7 | 7 | use anyhow::Context as _;
|
8 | 8 | use cargo_metadata::camino::{Utf8Path, Utf8PathBuf};
|
9 | 9 | use cargo_metadata::semver::Version;
|
10 |
| -use cargo_metadata::{MetadataCommand, Package}; |
| 10 | +use cargo_metadata::{Metadata, MetadataCommand, Package}; |
11 | 11 | use std::fs;
|
12 | 12 | use std::path::{Path, PathBuf};
|
13 | 13 |
|
@@ -96,8 +96,9 @@ impl SpirvSource {
|
96 | 96 |
|
97 | 97 | /// Look into the shader crate to get the version of `rust-gpu` it's using.
|
98 | 98 | pub fn get_rust_gpu_deps_from_shader(shader_crate_path: &Path) -> anyhow::Result<Self> {
|
99 |
| - let spirv_std_package = get_package_from_crate(shader_crate_path, "spirv-std")?; |
100 |
| - let spirv_source = Self::parse_spirv_std_source_and_version(&spirv_std_package)?; |
| 99 | + let crate_metadata = query_metadata(shader_crate_path)?; |
| 100 | + let spirv_std_package = crate_metadata.find_package("spirv-std")?; |
| 101 | + let spirv_source = Self::parse_spirv_std_source_and_version(spirv_std_package)?; |
101 | 102 | log::debug!(
|
102 | 103 | "Parsed `SpirvSource` from crate `{}`: \
|
103 | 104 | {spirv_source:?}",
|
@@ -175,49 +176,41 @@ impl SpirvSource {
|
175 | 176 | }
|
176 | 177 | }
|
177 | 178 |
|
178 |
| -/// Make sure shader crate path is absolute and canonical. |
179 |
| -fn crate_path_canonical(shader_crate_path: &Path) -> anyhow::Result<PathBuf> { |
180 |
| - let mut canonical_path = shader_crate_path.to_path_buf(); |
181 |
| - |
182 |
| - if !canonical_path.is_absolute() { |
183 |
| - let cwd = std::env::current_dir().context("no cwd")?; |
184 |
| - canonical_path = cwd.join(canonical_path); |
185 |
| - } |
186 |
| - canonical_path = canonical_path |
187 |
| - .canonicalize() |
188 |
| - .context("could not get absolute path to shader crate")?; |
189 |
| - |
190 |
| - if !canonical_path.is_dir() { |
191 |
| - log::error!( |
192 |
| - "{} is not a directory, aborting", |
193 |
| - shader_crate_path.display() |
194 |
| - ); |
195 |
| - anyhow::bail!("{} is not a directory", shader_crate_path.display()); |
196 |
| - } |
197 |
| - Ok(canonical_path) |
198 |
| -} |
199 |
| - |
200 | 179 | /// get the Package metadata from some crate
|
201 |
| -pub fn get_package_from_crate(crate_path: &Path, crate_name: &str) -> anyhow::Result<Package> { |
202 |
| - let canonical_crate_path = crate_path_canonical(crate_path)?; |
203 |
| - |
204 |
| - log::debug!( |
205 |
| - "Running `cargo metadata` on `{}` to query for package `{crate_name}`", |
206 |
| - canonical_crate_path.display() |
207 |
| - ); |
| 180 | +pub fn query_metadata(crate_path: &Path) -> anyhow::Result<Metadata> { |
| 181 | + log::debug!("Running `cargo metadata` on `{}`", crate_path.display()); |
208 | 182 | let metadata = MetadataCommand::new()
|
209 |
| - .current_dir(&canonical_crate_path) |
| 183 | + .current_dir( |
| 184 | + &crate_path |
| 185 | + .canonicalize() |
| 186 | + .context("could not get absolute path to shader crate")?, |
| 187 | + ) |
210 | 188 | .exec()?;
|
| 189 | + Ok(metadata) |
| 190 | +} |
| 191 | + |
| 192 | +/// implements [`Self::find_package`] |
| 193 | +pub trait FindPackage { |
| 194 | + /// Search for a package or return a nice error |
| 195 | + fn find_package(&self, crate_name: &str) -> anyhow::Result<&Package>; |
| 196 | +} |
211 | 197 |
|
212 |
| - let Some(package) = metadata |
213 |
| - .packages |
214 |
| - .into_iter() |
215 |
| - .find(|package| package.name.eq(crate_name)) |
216 |
| - else { |
217 |
| - anyhow::bail!("`{crate_name}` not found in `Cargo.toml` at `{canonical_crate_path:?}`"); |
218 |
| - }; |
219 |
| - log::trace!(" found `{}` version `{}`", package.name, package.version); |
220 |
| - Ok(package) |
| 198 | +impl FindPackage for Metadata { |
| 199 | + fn find_package(&self, crate_name: &str) -> anyhow::Result<&Package> { |
| 200 | + if let Some(package) = self |
| 201 | + .packages |
| 202 | + .iter() |
| 203 | + .find(|package| package.name.eq(crate_name)) |
| 204 | + { |
| 205 | + log::trace!(" found `{}` version `{}`", package.name, package.version); |
| 206 | + Ok(package) |
| 207 | + } else { |
| 208 | + anyhow::bail!( |
| 209 | + "`{crate_name}` not found in `Cargo.toml` at `{:?}`", |
| 210 | + self.workspace_root |
| 211 | + ); |
| 212 | + } |
| 213 | + } |
221 | 214 | }
|
222 | 215 |
|
223 | 216 | /// Parse the `rust-toolchain.toml` in the working tree of the checked-out version of the `rust-gpu` repo.
|
|
0 commit comments