Include namespace when using init command
This commit is contained in:
parent
fd20fec53c
commit
fbb76bb3d2
|
@ -6,8 +6,10 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
zip = ["shulkerbox/zip"]
|
default = ["zip", "lua"]
|
||||||
lang-debug = []
|
lang-debug = []
|
||||||
|
lua = ["shulkerscript-lang/lua"]
|
||||||
|
zip = ["shulkerbox/zip"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.4", features = ["derive"] }
|
clap = { version = "4.5.4", features = ["derive"] }
|
||||||
|
@ -15,6 +17,6 @@ colored = "2.1.0"
|
||||||
serde = { version = "1.0.197", features = ["derive"] }
|
serde = { version = "1.0.197", features = ["derive"] }
|
||||||
thiserror = "1.0.58"
|
thiserror = "1.0.58"
|
||||||
toml = "0.8.12"
|
toml = "0.8.12"
|
||||||
shulkerscript-lang = {path = "../shulkerscript-lang"}
|
shulkerscript-lang = {path = "../shulkerscript-lang", features = ["shulkerbox"], default-features = false}
|
||||||
shulkerbox = {path = "../shulkerbox", default-features = false}
|
shulkerbox = {path = "../shulkerbox", default-features = false}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
|
namespace "{namespace}";
|
||||||
|
|
||||||
#[tick]
|
#[tick]
|
||||||
fn main() {
|
fn main() {{
|
||||||
// Change this
|
// Change this
|
||||||
/say Hello World!
|
/say Hello World!
|
||||||
}
|
}}
|
|
@ -31,7 +31,6 @@ pub struct InitArgs {
|
||||||
|
|
||||||
pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
|
pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
|
||||||
let path = args.path.as_path();
|
let path = args.path.as_path();
|
||||||
let name = args.name.as_deref();
|
|
||||||
let description = args.description.as_deref();
|
let description = args.description.as_deref();
|
||||||
let pack_format = args.pack_format;
|
let pack_format = args.pack_format;
|
||||||
let force = args.force;
|
let force = args.force;
|
||||||
|
@ -46,6 +45,11 @@ pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
|
||||||
print_error("The specified directory is not empty.");
|
print_error("The specified directory is not empty.");
|
||||||
Err(Error::NonEmptyDirectoryError(path.to_path_buf()))
|
Err(Error::NonEmptyDirectoryError(path.to_path_buf()))
|
||||||
} else {
|
} else {
|
||||||
|
let name = args
|
||||||
|
.name
|
||||||
|
.as_deref()
|
||||||
|
.or_else(|| path.file_name().and_then(|os| os.to_str()));
|
||||||
|
|
||||||
print_info("Initializing a new Shulkerscript project...");
|
print_info("Initializing a new Shulkerscript project...");
|
||||||
|
|
||||||
// Create the pack.toml file
|
// Create the pack.toml file
|
||||||
|
@ -62,7 +66,11 @@ pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
|
||||||
create_dir(&src_path, verbose)?;
|
create_dir(&src_path, verbose)?;
|
||||||
|
|
||||||
// Create the main.shu file
|
// Create the main.shu file
|
||||||
create_main_file(path, verbose)?;
|
create_main_file(
|
||||||
|
path,
|
||||||
|
&name_to_namespace(name.unwrap_or("shulkerscript-pack")),
|
||||||
|
verbose,
|
||||||
|
)?;
|
||||||
|
|
||||||
print_success("Project initialized successfully.");
|
print_success("Project initialized successfully.");
|
||||||
|
|
||||||
|
@ -77,14 +85,12 @@ fn create_pack_config(
|
||||||
description: Option<&str>,
|
description: Option<&str>,
|
||||||
pack_format: Option<u8>,
|
pack_format: Option<u8>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let pack_name = name.or_else(|| base_path.file_name().and_then(|os| os.to_str()));
|
|
||||||
|
|
||||||
let path = base_path.join("pack.toml");
|
let path = base_path.join("pack.toml");
|
||||||
|
|
||||||
// Load the default config
|
// Load the default config
|
||||||
let mut content = ProjectConfig::default();
|
let mut content = ProjectConfig::default();
|
||||||
// Override the default values with the provided ones
|
// Override the default values with the provided ones
|
||||||
if let Some(name) = pack_name {
|
if let Some(name) = name {
|
||||||
content.pack.name = name.to_string();
|
content.pack.name = name.to_string();
|
||||||
}
|
}
|
||||||
if let Some(description) = description {
|
if let Some(description) = description {
|
||||||
|
@ -141,9 +147,15 @@ fn create_pack_png(path: &Path, verbose: bool) -> std::io::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_main_file(path: &Path, verbose: bool) -> std::io::Result<()> {
|
fn create_main_file(path: &Path, namespace: &str, verbose: bool) -> std::io::Result<()> {
|
||||||
let main_file = path.join("src").join("main.shu");
|
let main_file = path.join("src").join("main.shu");
|
||||||
fs::write(&main_file, include_str!("../../assets/default-main.shu"))?;
|
fs::write(
|
||||||
|
&main_file,
|
||||||
|
format!(
|
||||||
|
include_str!("../../assets/default-main.shu"),
|
||||||
|
namespace = namespace
|
||||||
|
),
|
||||||
|
)?;
|
||||||
if verbose {
|
if verbose {
|
||||||
print_info(&format!(
|
print_info(&format!(
|
||||||
"Created main.shu file at {}.",
|
"Created main.shu file at {}.",
|
||||||
|
@ -152,3 +164,24 @@ fn create_main_file(path: &Path, verbose: bool) -> std::io::Result<()> {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn name_to_namespace(name: &str) -> String {
|
||||||
|
const VALID_CHARS: &str = "0123456789abcdefghijklmnopqrstuvwxyz_-.";
|
||||||
|
|
||||||
|
name.to_lowercase()
|
||||||
|
.chars()
|
||||||
|
.filter_map(|c| {
|
||||||
|
if VALID_CHARS.contains(c) {
|
||||||
|
Some(c)
|
||||||
|
} else if c.is_ascii_uppercase() {
|
||||||
|
Some(c.to_ascii_lowercase())
|
||||||
|
} else if c.is_ascii_punctuation() {
|
||||||
|
Some('-')
|
||||||
|
} else if c.is_ascii_whitespace() {
|
||||||
|
Some('_')
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use std::path::PathBuf;
|
|
||||||
|
use super::BuildArgs;
|
||||||
|
|
||||||
#[derive(Debug, clap::Args, Clone)]
|
#[derive(Debug, clap::Args, Clone)]
|
||||||
pub struct PackageArgs {
|
pub struct PackageArgs {
|
||||||
/// The path of the project to package.
|
#[clap(flatten)]
|
||||||
#[clap(default_value = ".")]
|
build_args: BuildArgs,
|
||||||
path: PathBuf,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn package(_verbose: bool, _args: &PackageArgs) -> Result<()> {
|
pub fn package(_verbose: bool, args: &PackageArgs) -> Result<()> {
|
||||||
println!("PACKAGE");
|
println!("PACKAGE");
|
||||||
|
println!(" - Args: {:?}", args);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue