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
|
||||
|
||||
[features]
|
||||
zip = ["shulkerbox/zip"]
|
||||
default = ["zip", "lua"]
|
||||
lang-debug = []
|
||||
lua = ["shulkerscript-lang/lua"]
|
||||
zip = ["shulkerbox/zip"]
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.4", features = ["derive"] }
|
||||
|
@ -15,6 +17,6 @@ colored = "2.1.0"
|
|||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
thiserror = "1.0.58"
|
||||
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}
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
namespace "{namespace}";
|
||||
|
||||
#[tick]
|
||||
fn main() {
|
||||
fn main() {{
|
||||
// Change this
|
||||
/say Hello World!
|
||||
}
|
||||
}}
|
|
@ -31,7 +31,6 @@ pub struct InitArgs {
|
|||
|
||||
pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
|
||||
let path = args.path.as_path();
|
||||
let name = args.name.as_deref();
|
||||
let description = args.description.as_deref();
|
||||
let pack_format = args.pack_format;
|
||||
let force = args.force;
|
||||
|
@ -46,6 +45,11 @@ pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
|
|||
print_error("The specified directory is not empty.");
|
||||
Err(Error::NonEmptyDirectoryError(path.to_path_buf()))
|
||||
} 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...");
|
||||
|
||||
// Create the pack.toml file
|
||||
|
@ -62,7 +66,11 @@ pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
|
|||
create_dir(&src_path, verbose)?;
|
||||
|
||||
// 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.");
|
||||
|
||||
|
@ -77,14 +85,12 @@ fn create_pack_config(
|
|||
description: Option<&str>,
|
||||
pack_format: Option<u8>,
|
||||
) -> Result<()> {
|
||||
let pack_name = name.or_else(|| base_path.file_name().and_then(|os| os.to_str()));
|
||||
|
||||
let path = base_path.join("pack.toml");
|
||||
|
||||
// Load the default config
|
||||
let mut content = ProjectConfig::default();
|
||||
// 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();
|
||||
}
|
||||
if let Some(description) = description {
|
||||
|
@ -141,9 +147,15 @@ fn create_pack_png(path: &Path, verbose: bool) -> std::io::Result<()> {
|
|||
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");
|
||||
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 {
|
||||
print_info(&format!(
|
||||
"Created main.shu file at {}.",
|
||||
|
@ -152,3 +164,24 @@ fn create_main_file(path: &Path, verbose: bool) -> std::io::Result<()> {
|
|||
}
|
||||
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 std::path::PathBuf;
|
||||
|
||||
use super::BuildArgs;
|
||||
|
||||
#[derive(Debug, clap::Args, Clone)]
|
||||
pub struct PackageArgs {
|
||||
/// The path of the project to package.
|
||||
#[clap(default_value = ".")]
|
||||
path: PathBuf,
|
||||
#[clap(flatten)]
|
||||
build_args: BuildArgs,
|
||||
}
|
||||
|
||||
pub fn package(_verbose: bool, _args: &PackageArgs) -> Result<()> {
|
||||
pub fn package(_verbose: bool, args: &PackageArgs) -> Result<()> {
|
||||
println!("PACKAGE");
|
||||
println!(" - Args: {:?}", args);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue