Multiple ways to specify OSDK new's type

This commit is contained in:
Zhang Junyang 2024-03-25 23:01:12 +08:00 committed by Tate, Hongliang Tian
parent dcab4e1039
commit 3dce753c86
12 changed files with 58 additions and 20 deletions

View File

@ -13,7 +13,7 @@ Creating a new kernel project is simple.
You only need to execute the following command: You only need to execute the following command:
```bash ```bash
cargo osdk new --type kernel myos cargo osdk new --kernel myos
``` ```
## Creating a new library project ## Creating a new library project

View File

@ -8,7 +8,7 @@ Suppose you have created a new kernel project named `myos`
and you are in the project directory: and you are in the project directory:
```bash ```bash
cargo osdk new --type kernel myos && cd myos cargo osdk new --kernel myos && cd myos
``` ```
## Build the project ## Build the project

View File

@ -24,7 +24,7 @@ Then, add the following content to `Cargo.toml`:
The two projects can be created using the following commands: The two projects can be created using the following commands:
```bash ```bash
cargo osdk new --type kernel myos cargo osdk new --kernel myos
cargo osdk new mymodule cargo osdk new mymodule
``` ```

View File

@ -17,7 +17,7 @@ cargo osdk new [OPTIONS] <name>
## Options ## Options
`--type kernel`: `--kernel`:
Use the kernel template. Use the kernel template.
If this option is not set, If this option is not set,
the library template will be used by default. the library template will be used by default.
@ -27,7 +27,7 @@ the library template will be used by default.
- Create a new kernel named `myos`: - Create a new kernel named `myos`:
```bash ```bash
cargo osdk new --type kernel myos cargo osdk new --kernel myos
``` ```
- Create a new library named `mymodule`: - Create a new library named `mymodule`:

View File

@ -56,7 +56,7 @@ Here we provide a simple demo to demonstrate how to create and run a simple kern
With `cargo-osdk`, a kernel project can be created by one command With `cargo-osdk`, a kernel project can be created by one command
```bash ```bash
cargo osdk new --type kernel my-first-os cargo osdk new --kernel my-first-os
``` ```
Then, you can run the kernel with Then, you can run the kernel with

View File

@ -97,16 +97,43 @@ pub struct ForwardedArguments {
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct NewArgs { pub struct NewArgs {
#[arg( #[arg(
id = "type",
long = "type", long = "type",
short = 't', short = 't',
default_value = "library", default_value = "library",
help = "The type of the project to create" help = "The type of the project to create",
conflicts_with_all = ["kernel", "library"],
)] )]
pub type_: ProjectType, pub type_: ProjectType,
#[arg(
long,
help = "Create a kernel package",
conflicts_with_all = ["library", "type"],
)]
pub kernel: bool,
#[arg(
long,
alias = "lib",
help = "Create a library package",
conflicts_with_all = ["kernel", "type"],
)]
pub library: bool,
#[arg(name = "name", required = true)] #[arg(name = "name", required = true)]
pub crate_name: String, pub crate_name: String,
} }
impl NewArgs {
pub fn project_type(&self) -> ProjectType {
if self.kernel {
ProjectType::Kernel
} else if self.library {
ProjectType::Library
} else {
self.type_
}
}
}
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct BuildArgs { pub struct BuildArgs {
#[command(flatten)] #[command(flatten)]

View File

@ -14,9 +14,9 @@ pub fn execute_new_command(args: &NewArgs) {
cargo_new_lib(&args.crate_name); cargo_new_lib(&args.crate_name);
let cargo_metadata = get_cargo_metadata(Some(&args.crate_name), None::<&[&str]>).unwrap(); let cargo_metadata = get_cargo_metadata(Some(&args.crate_name), None::<&[&str]>).unwrap();
add_manifest_dependencies(&cargo_metadata, &args.crate_name); add_manifest_dependencies(&cargo_metadata, &args.crate_name);
create_osdk_manifest(&cargo_metadata, &args.type_); create_osdk_manifest(&cargo_metadata, &args.project_type());
exclude_osdk_base(&cargo_metadata); exclude_osdk_base(&cargo_metadata);
write_src_template(&cargo_metadata, &args.crate_name, &args.type_); write_src_template(&cargo_metadata, &args.crate_name, &args.project_type());
add_rust_toolchain(&cargo_metadata); add_rust_toolchain(&cargo_metadata);
} }

View File

@ -24,7 +24,7 @@ pub struct Project {
pub type_: ProjectType, pub type_: ProjectType,
} }
#[derive(Debug, Clone, Serialize, Deserialize, ValueEnum, PartialEq, Eq)] #[derive(Debug, Clone, Copy, Serialize, Deserialize, ValueEnum, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub enum ProjectType { pub enum ProjectType {
Kernel, Kernel,

View File

@ -1,6 +1,9 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use std::{fs::remove_dir_all, path::Path}; use std::{
fs::remove_dir_all,
path::{Path, PathBuf},
};
use crate::util::*; use crate::util::*;
@ -14,10 +17,20 @@ fn create_kernel_in_workspace() {
remove_dir_all(WORKSPACE_NAME).unwrap(); remove_dir_all(WORKSPACE_NAME).unwrap();
} }
create_workspace(WORKSPACE_NAME, &[KERNEL_NAME]); create_workspace(WORKSPACE_NAME, &[KERNEL_NAME]);
let mut cargo_osdk = cargo_osdk(["new", "--type", "kernel", KERNEL_NAME]); let kernel_path = PathBuf::from(WORKSPACE_NAME).join(KERNEL_NAME);
cargo_osdk.current_dir(WORKSPACE_NAME);
let output = cargo_osdk.output().unwrap(); let mut cmd = cargo_osdk(["new", "--kernel", KERNEL_NAME]);
cmd.current_dir(WORKSPACE_NAME);
let output = cmd.output().unwrap();
assert_success(&output); assert_success(&output);
remove_dir_all(&kernel_path).unwrap();
let mut cmd = cargo_osdk(["new", "-t", "kernel", KERNEL_NAME]);
cmd.current_dir(WORKSPACE_NAME);
let output = cmd.output().unwrap();
assert_success(&output);
remove_dir_all(&kernel_path).unwrap();
remove_dir_all(WORKSPACE_NAME).unwrap(); remove_dir_all(WORKSPACE_NAME).unwrap();
} }
@ -51,7 +64,7 @@ fn create_two_crates_in_workspace() {
add_member_to_workspace(WORKSPACE_NAME, KERNEL_NAME); add_member_to_workspace(WORKSPACE_NAME, KERNEL_NAME);
// Create kernel crate // Create kernel crate
let mut command = cargo_osdk(["new", "--type", "kernel", KERNEL_NAME]); let mut command = cargo_osdk(["new", "--kernel", KERNEL_NAME]);
command.current_dir(WORKSPACE_NAME); command.current_dir(WORKSPACE_NAME);
let output = command.output().unwrap(); let output = command.output().unwrap();
assert_success(&output); assert_success(&output);

View File

@ -15,7 +15,7 @@ fn create_a_kernel_project() {
fs::remove_dir_all(&kernel_path).unwrap(); fs::remove_dir_all(&kernel_path).unwrap();
} }
cargo_osdk(&["new", "--type", "kernel", kernel]) cargo_osdk(&["new", "--kernel", kernel])
.current_dir(workdir) .current_dir(workdir)
.unwrap(); .unwrap();

View File

@ -15,7 +15,7 @@ fn create_and_run_kernel() {
fs::remove_dir_all(&os_dir).unwrap(); fs::remove_dir_all(&os_dir).unwrap();
} }
let mut command = cargo_osdk(&["new", "--type", "kernel", os_name]); let mut command = cargo_osdk(&["new", "--kernel", os_name]);
command.current_dir(work_dir); command.current_dir(work_dir);
command.ok().unwrap(); command.ok().unwrap();

View File

@ -29,9 +29,7 @@ fn work_in_workspace() {
// Create a kernel project and a library project // Create a kernel project and a library project
let kernel = "myos"; let kernel = "myos";
let module = "mymodule"; let module = "mymodule";
cargo_osdk(&["new", "--type", "kernel", kernel]) cargo_osdk(&["new", "--kernel", kernel]).ok().unwrap();
.ok()
.unwrap();
cargo_osdk(&["new", module]).ok().unwrap(); cargo_osdk(&["new", module]).ok().unwrap();
// Add a test function to mymodule/src/lib.rs // Add a test function to mymodule/src/lib.rs