cleanup CargoSpec From trait

This commit is contained in:
bunnie 2022-10-05 02:54:12 +08:00
parent 9dcc0a3386
commit 9c5d773cf3
4 changed files with 34 additions and 28 deletions

View file

@ -22,7 +22,7 @@ impl BuildStream {
} }
} }
pub(crate) enum CrateSpec { pub enum CrateSpec {
/// name of the crate /// name of the crate
Local(String), Local(String),
/// crates.io: (name of crate, version) /// crates.io: (name of crate, version)
@ -45,8 +45,8 @@ impl Clone for CrateSpec {
} }
} }
} }
impl CrateSpec { impl From<&str> for CrateSpec {
pub fn from_str(spec: &str) -> CrateSpec { fn from(spec: &str) -> CrateSpec {
// remote crates are specified as "name@version", i.e. "xous-names@0.9.9" // remote crates are specified as "name@version", i.e. "xous-names@0.9.9"
if spec.contains('@') { if spec.contains('@') {
let (name, version) = spec.split_once('@').expect("couldn't parse crate specifier"); let (name, version) = spec.split_once('@').expect("couldn't parse crate specifier");
@ -209,26 +209,26 @@ impl Builder {
/// Override the default kernel. For example, to use the kernel from crates.io, specify as "xous-kernel@0.9.9" /// Override the default kernel. For example, to use the kernel from crates.io, specify as "xous-kernel@0.9.9"
#[allow(dead_code)] #[allow(dead_code)]
pub fn use_kernel<'a>(&'a mut self, spec: &str) -> &'a mut Builder { pub fn use_kernel<'a>(&'a mut self, spec: &str) -> &'a mut Builder {
self.kernel = CrateSpec::from_str(spec); self.kernel = spec.into();
self self
} }
/// Override the default loader /// Override the default loader
#[allow(dead_code)] #[allow(dead_code)]
pub fn use_loader<'a>(&'a mut self, spec: &str) -> &'a mut Builder { pub fn use_loader<'a>(&'a mut self, spec: &str) -> &'a mut Builder {
self.loader = CrateSpec::from_str(spec); self.loader = spec.into();
self self
} }
/// Add just one service /// Add just one service
#[allow(dead_code)] #[allow(dead_code)]
pub fn add_service<'a>(&'a mut self, service_spec: &str) -> &'a mut Builder { pub fn add_service<'a>(&'a mut self, service_spec: &str) -> &'a mut Builder {
self.services.push(CrateSpec::from_str(service_spec)); self.services.push(service_spec.into());
self self
} }
/// Add a list of services /// Add a list of services
pub fn add_services<'a>(&'a mut self, service_list: &Vec::<String>) -> &'a mut Builder { pub fn add_services<'a>(&'a mut self, service_list: &Vec::<String>) -> &'a mut Builder {
for service in service_list { for service in service_list {
self.services.push(CrateSpec::from_str(service)); self.services.push(service.as_str().into());
} }
self self
} }
@ -236,13 +236,13 @@ impl Builder {
/// Add just one app /// Add just one app
#[allow(dead_code)] #[allow(dead_code)]
pub fn add_app<'a>(&'a mut self, app_spec: &str) -> &'a mut Builder { pub fn add_app<'a>(&'a mut self, app_spec: &str) -> &'a mut Builder {
self.apps.push(CrateSpec::from_str(app_spec)); self.apps.push(app_spec.into());
self self
} }
/// Add a list of apps /// Add a list of apps
pub fn add_apps<'a>(&'a mut self, app_list: &Vec::<String>) -> &'a mut Builder { pub fn add_apps<'a>(&'a mut self, app_list: &Vec::<String>) -> &'a mut Builder {
for app in app_list { for app in app_list {
self.apps.push(CrateSpec::from_str(app)); self.apps.push(app.as_str().into());
} }
self self
} }
@ -833,11 +833,11 @@ impl Builder {
} }
} }
fn cargo() -> String { pub fn cargo() -> String {
env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()) env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())
} }
fn project_root() -> PathBuf { pub fn project_root() -> PathBuf {
Path::new(&env!("CARGO_MANIFEST_DIR")) Path::new(&env!("CARGO_MANIFEST_DIR"))
.ancestors() .ancestors()
.nth(1) .nth(1)

View file

@ -5,11 +5,10 @@ mod utils;
use utils::*; use utils::*;
mod builder; mod builder;
use builder::*; use builder::*;
mod verifier;
use verifier::*;
use std::{ use std::env;
env,
path::{Path, PathBuf},
};
/// gitrev of the current precursor SoC version targeted by this build. This must /// gitrev of the current precursor SoC version targeted by this build. This must
/// be manually updated every time the SoC version is bumped. /// be manually updated every time the SoC version is bumped.
@ -371,18 +370,6 @@ enum MemorySpec {
SvdFile(String), SvdFile(String),
} }
fn cargo() -> String {
env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())
}
fn project_root() -> PathBuf {
Path::new(&env!("CARGO_MANIFEST_DIR"))
.ancestors()
.nth(1)
.unwrap()
.to_path_buf()
}
/// [cratespecs] are positional arguments, and is a list of 0 to N tokens that immediately /// [cratespecs] are positional arguments, and is a list of 0 to N tokens that immediately
/// follow [verb] /// follow [verb]
fn get_cratespecs() -> Vec<String> { fn get_cratespecs() -> Vec<String> {

View file

@ -5,7 +5,8 @@ use std::{
process::Command, process::Command,
}; };
use crate::{cargo, project_root, TARGET_TRIPLE}; use crate::TARGET_TRIPLE;
use crate::{cargo, project_root};
const TOOLCHAIN_RELEASE_URL: &str = "https://api.github.com/repos/betrusted-io/rust/releases"; const TOOLCHAIN_RELEASE_URL: &str = "https://api.github.com/repos/betrusted-io/rust/releases";

18
xtask/src/verifier.rs Normal file
View file

@ -0,0 +1,18 @@
use std::env;
use crate::builder::CrateSpec;
use std::path::{Path, PathBuf};
use crate::DynError;
pub fn verify(spec: CrateSpec) -> Result<(), DynError> {
let mut cache_path = Path::new(&env::var("CARGO_HOME").unwrap()).to_path_buf();
cache_path.push("registry");
cache_path.push("src");
for entry in std::fs::read_dir(cache_path)? {
let entry = entry?;
let path = entry.path();
println!("entries: {}", path.to_str().unwrap());
}
Ok(())
}