mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-29 16:13:27 +00:00
Supporting running OSDK commands in workspace root
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
c7b7e2473f
commit
aaf101a53e
@ -1,9 +1,6 @@
|
||||
#![no_std]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate ktest;
|
||||
|
||||
use aster_frame::prelude::*;
|
||||
|
||||
#[aster_main]
|
||||
|
@ -1,12 +1,6 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use std::{
|
||||
ffi::OsStr,
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
process,
|
||||
str::FromStr,
|
||||
};
|
||||
use std::{fs, path::PathBuf, process, str::FromStr};
|
||||
|
||||
use crate::{
|
||||
cli::NewArgs,
|
||||
@ -20,6 +14,7 @@ pub fn execute_new_command(args: &NewArgs) {
|
||||
let cargo_metadata = get_cargo_metadata(Some(&args.crate_name), None::<&[&str]>).unwrap();
|
||||
add_manifest_dependencies(&cargo_metadata, &args.crate_name);
|
||||
create_osdk_manifest(&cargo_metadata);
|
||||
exclude_osdk_base(&cargo_metadata);
|
||||
if args.kernel {
|
||||
write_kernel_template(&cargo_metadata, &args.crate_name);
|
||||
} else {
|
||||
@ -50,15 +45,48 @@ fn add_manifest_dependencies(cargo_metadata: &serde_json::Value, crate_name: &st
|
||||
dependencies.as_table_mut().unwrap().extend(ktest_dep);
|
||||
|
||||
// If we created a workspace by `osdk new`, we should exclude the `base` crate from the workspace.
|
||||
if get_cargo_metadata::<&Path, &OsStr>(None, None).is_none() {
|
||||
let exclude = toml::Table::from_str(r#"exclude = ["target/osdk/base"]"#).unwrap();
|
||||
manifest.insert("workspace".to_string(), toml::Value::Table(exclude));
|
||||
}
|
||||
// let exclude = toml::Table::from_str(r#"exclude = ["target/osdk/base"]"#).unwrap();
|
||||
// manifest.insert("workspace".to_string(), toml::Value::Table(exclude));
|
||||
|
||||
let content = toml::to_string(&manifest).unwrap();
|
||||
fs::write(mainfest_path, content).unwrap();
|
||||
}
|
||||
|
||||
// Add `target/osdk/base` to `exclude` array of the workspace manifest
|
||||
fn exclude_osdk_base(metadata: &serde_json::Value) {
|
||||
let osdk_base_path = "target/osdk/base";
|
||||
|
||||
let workspace_manifest_path = {
|
||||
let workspace_root = metadata.get("workspace_root").unwrap().as_str().unwrap();
|
||||
format!("{}/Cargo.toml", workspace_root)
|
||||
};
|
||||
|
||||
let content = fs::read_to_string(&workspace_manifest_path).unwrap();
|
||||
let mut manifest_toml: toml::Table = toml::from_str(&content).unwrap();
|
||||
|
||||
if let Some(workspace) = manifest_toml.get_mut("workspace") {
|
||||
let workspace = workspace.as_table_mut().unwrap();
|
||||
|
||||
if let Some(exclude) = workspace.get_mut("exclude") {
|
||||
let exclude = exclude.as_array_mut().unwrap();
|
||||
if exclude.contains(&toml::Value::String(osdk_base_path.to_string())) {
|
||||
return;
|
||||
}
|
||||
|
||||
exclude.push(toml::Value::String(osdk_base_path.to_string()));
|
||||
} else {
|
||||
let exclude = vec![toml::Value::String(osdk_base_path.to_string())];
|
||||
workspace.insert("exclude".to_string(), toml::Value::Array(exclude));
|
||||
}
|
||||
} else {
|
||||
let exclude = toml::Table::from_str(r#"exclude = ["target/osdk/base"]"#).unwrap();
|
||||
manifest_toml.insert("workspace".to_string(), toml::Value::Table(exclude));
|
||||
}
|
||||
|
||||
let content = toml::to_string(&manifest_toml).unwrap();
|
||||
fs::write(workspace_manifest_path, content).unwrap();
|
||||
}
|
||||
|
||||
fn create_osdk_manifest(cargo_metadata: &serde_json::Value) {
|
||||
let osdk_manifest_path = {
|
||||
let workspace_root = get_workspace_root(cargo_metadata);
|
||||
@ -78,6 +106,7 @@ fn create_osdk_manifest(cargo_metadata: &serde_json::Value) {
|
||||
r#"
|
||||
[boot]
|
||||
ovmf = "/usr/share/OVMF"
|
||||
protocol = "multiboot"
|
||||
[qemu]
|
||||
machine = "q35"
|
||||
args = [
|
||||
|
@ -6,10 +6,18 @@ use super::{build::do_build, util::DEFAULT_TARGET_RELPATH};
|
||||
use crate::{
|
||||
base_crate::new_base_crate,
|
||||
config_manager::{BuildConfig, RunConfig, TestConfig},
|
||||
util::{get_current_crate_info, get_target_directory},
|
||||
util::{get_cargo_metadata, get_current_crate_info, get_target_directory},
|
||||
};
|
||||
|
||||
pub fn execute_test_command(config: &TestConfig) {
|
||||
let crates = get_workspace_default_members();
|
||||
for crate_path in crates {
|
||||
std::env::set_current_dir(crate_path).unwrap();
|
||||
test_current_crate(config);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_current_crate(config: &TestConfig) {
|
||||
let current_crate = get_current_crate_info();
|
||||
let ws_target_directory = get_target_directory();
|
||||
let osdk_target_directory = ws_target_directory.join(DEFAULT_TARGET_RELPATH);
|
||||
@ -69,3 +77,23 @@ pub static KTEST_CRATE_WHITELIST: Option<&[&str]> = Some(&{:#?});
|
||||
|
||||
bundle.run(&required_run_config);
|
||||
}
|
||||
|
||||
fn get_workspace_default_members() -> Vec<String> {
|
||||
let metadata = get_cargo_metadata(None::<&str>, None::<&[&str]>).unwrap();
|
||||
let default_members = metadata
|
||||
.get("workspace_default_members")
|
||||
.unwrap()
|
||||
.as_array()
|
||||
.unwrap();
|
||||
default_members
|
||||
.iter()
|
||||
.map(|value| {
|
||||
// The default member is in the form of "<crate_name> <crate_version> (path+file://<crate_path>)"
|
||||
let default_member = value.as_str().unwrap();
|
||||
let path = default_member.split(" ").nth(2).unwrap();
|
||||
path.trim_start_matches("(path+file://")
|
||||
.trim_end_matches(')')
|
||||
.to_string()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
Reference in New Issue
Block a user