Skip to content

Commit

Permalink
ability to specify project root dir for template-core
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Jun 3, 2024
1 parent 905f048 commit bb0783f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion project-manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ impl ProjectManager {

fn on_button_click(&mut self, button: Handle<UiNode>, ui: &mut UserInterface) {
if button == self.create {
fyrox_template_core::init_project("test", "3d", "git", true).unwrap();
fyrox_template_core::init_project(Path::new("./"), "test", "3d", "git", true).unwrap();
self.refresh(ui);
} else if button == self.import {
// TODO: Import project.
Expand Down
23 changes: 15 additions & 8 deletions template-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,11 @@ fn init_data(base_path: &Path, style: &str) -> Result<(), String> {
}
}

pub fn init_script(raw_name: &str) -> Result<(), String> {
let mut base_path = Path::new("game/src/");
pub fn init_script(root_path: &Path, raw_name: &str) -> Result<(), String> {
let mut base_path = root_path.join("game/src/");
if !base_path.exists() {
eprintln!("game/src directory does not exists! Fallback to root directory...");
base_path = Path::new("");
base_path = root_path.to_path_buf();
}

let script_file_stem = raw_name.to_case(Case::Snake);
Expand Down Expand Up @@ -682,7 +682,13 @@ impl ScriptTrait for {name} {{
)
}

pub fn init_project(name: &str, style: &str, vcs: &str, overwrite: bool) -> Result<(), String> {
pub fn init_project(
root_path: &Path,
name: &str,
style: &str,
vcs: &str,
overwrite: bool,
) -> Result<(), String> {
let name = check_name(name);
let name = match name {
Ok(s) => s,
Expand All @@ -692,7 +698,8 @@ pub fn init_project(name: &str, style: &str, vcs: &str, overwrite: bool) -> Resu
}
};

let base_path = Path::new(name);
let base_path = root_path.join(name);
let base_path = &base_path;

// Check the path is empty / doesn't already exist (To prevent overriding)
if !overwrite
Expand All @@ -718,7 +725,7 @@ pub fn init_project(name: &str, style: &str, vcs: &str, overwrite: bool) -> Resu
init_android_executor(base_path, name)
}

pub fn upgrade_project(version: &str, local: bool) -> Result<(), String> {
pub fn upgrade_project(root_path: &Path, version: &str, local: bool) -> Result<(), String> {
let semver_regex = Regex::new(include_str!("regex")).map_err(|e| e.to_string())?;

if version != "latest" && version != "nightly" && !semver_regex.is_match(version) {
Expand Down Expand Up @@ -756,8 +763,8 @@ pub fn upgrade_project(version: &str, local: bool) -> Result<(), String> {
.collect::<HashMap<_, _>>();

// Open workspace manifest.
let workspace_manifest_path = "Cargo.toml";
if let Ok(mut file) = File::open(workspace_manifest_path) {
let workspace_manifest_path = root_path.join("Cargo.toml");
if let Ok(mut file) = File::open(&workspace_manifest_path) {
let mut toml = String::new();
if file.read_to_string(&mut toml).is_ok() {
drop(file);
Expand Down
8 changes: 5 additions & 3 deletions template/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Fyrox Project Template Generator command line interface.

use clap::{Parser, Subcommand};
use std::path::Path;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
Expand Down Expand Up @@ -55,7 +56,8 @@ fn main() {
vcs,
overwrite,
} => {
fyrox_template_core::init_project(&name, &style, &vcs, overwrite).unwrap();
fyrox_template_core::init_project(Path::new("./"), &name, &style, &vcs, overwrite)
.unwrap();

println!("Project {} was generated successfully!", name);
println!(
Expand All @@ -72,14 +74,14 @@ fn main() {
);
}
Commands::Script { name } => {
fyrox_template_core::init_script(&name).unwrap();
fyrox_template_core::init_script(Path::new("./"), &name).unwrap();

println!(
"Script {name} was added successfully! Do not forget to add it to your module tree!",
);
}
Commands::Upgrade { version, local } => {
fyrox_template_core::upgrade_project(&version, local).unwrap();
fyrox_template_core::upgrade_project(Path::new("./"), &version, local).unwrap();

println!("Fyrox version was successfully set to '{}'!", version);
}
Expand Down

0 comments on commit bb0783f

Please sign in to comment.