diff --git a/CHANGELOG.md b/CHANGELOG.md index e41a60d9..996f4dc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * Added support for syncing in `.toml` files ([#633]) * Add `plugin` flag to the `build` command that outputs to the local plugins folder ([#735]) * Added better support for `Font` properties ([#731]) +* Add new plugin template to the `init` command ([#738]) [#668]: https://github.com/rojo-rbx/rojo/pull/668 [#674]: https://github.com/rojo-rbx/rojo/pull/674 @@ -38,6 +39,7 @@ [#633]: https://github.com/rojo-rbx/rojo/pull/633 [#735]: https://github.com/rojo-rbx/rojo/pull/735 [#731]: https://github.com/rojo-rbx/rojo/pull/731 +[#738]: https://github.com/rojo-rbx/rojo/pull/738 ## [7.3.0] - April 22, 2023 * Added `$attributes` to project format. ([#574]) diff --git a/assets/default-model-project/README.md b/assets/default-model-project/README.md index 30dfc436..1c1ad23c 100644 --- a/assets/default-model-project/README.md +++ b/assets/default-model-project/README.md @@ -2,7 +2,7 @@ Generated by [Rojo](https://github.com/rojo-rbx/rojo) {rojo_version}. ## Getting Started -To build this library or plugin, use: +To build this library, use: ```bash rojo build -o "{project_name}.rbxmx" diff --git a/assets/default-plugin-project/README.md b/assets/default-plugin-project/README.md new file mode 100644 index 00000000..a02eac72 --- /dev/null +++ b/assets/default-plugin-project/README.md @@ -0,0 +1,17 @@ +# {project_name} +Generated by [Rojo](https://github.com/rojo-rbx/rojo) {rojo_version}. + +## Getting Started +To build this plugin to your local plugins folder, use: + +```bash +rojo build -p "{project_name}.rbxm" +``` + +You can include the `watch` flag to re-build it on save: + +```bash +rojo build -p "{project_name}.rbxm" --watch +``` + +For more help, check out [the Rojo documentation](https://rojo.space/docs). diff --git a/assets/default-plugin-project/default.project.json b/assets/default-plugin-project/default.project.json new file mode 100644 index 00000000..e7bb5891 --- /dev/null +++ b/assets/default-plugin-project/default.project.json @@ -0,0 +1,6 @@ +{ + "name": "{project_name}", + "tree": { + "$path": "src" + } +} \ No newline at end of file diff --git a/assets/default-plugin-project/gitignore.txt b/assets/default-plugin-project/gitignore.txt new file mode 100644 index 00000000..608ce35a --- /dev/null +++ b/assets/default-plugin-project/gitignore.txt @@ -0,0 +1,3 @@ +# Plugin model files +/{project_name}.rbxmx +/{project_name}.rbxm diff --git a/src/cli/init.rs b/src/cli/init.rs index b770f55d..48d513b1 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -21,6 +21,11 @@ static PLACE_PROJECT: &str = static PLACE_README: &str = include_str!("../../assets/default-place-project/README.md"); static PLACE_GIT_IGNORE: &str = include_str!("../../assets/default-place-project/gitignore.txt"); +static PLUGIN_PROJECT: &str = + include_str!("../../assets/default-plugin-project/default.project.json"); +static PLUGIN_README: &str = include_str!("../../assets/default-plugin-project/README.md"); +static PLUGIN_GIT_IGNORE: &str = include_str!("../../assets/default-plugin-project/gitignore.txt"); + /// Initializes a new Rojo project. #[derive(Debug, Parser)] pub struct InitCommand { @@ -28,7 +33,7 @@ pub struct InitCommand { #[clap(default_value = "")] pub path: PathBuf, - /// The kind of project to create, 'place' or 'model'. Defaults to place. + /// The kind of project to create, 'place', 'plugin', or 'model'. Defaults to place. #[clap(long, default_value = "place")] pub kind: InitKind, } @@ -51,6 +56,7 @@ impl InitCommand { match self.kind { InitKind::Place => init_place(&base_path, project_params)?, InitKind::Model => init_model(&base_path, project_params)?, + InitKind::Plugin => init_plugin(&base_path, project_params)?, } println!("Created project successfully."); @@ -65,8 +71,11 @@ pub enum InitKind { /// A place that contains a baseplate. Place, - /// An empty model, suitable for a library or plugin. + /// An empty model, suitable for a library. Model, + + /// An empty plugin. + Plugin, } impl FromStr for InitKind { @@ -76,8 +85,9 @@ impl FromStr for InitKind { match source { "place" => Ok(InitKind::Place), "model" => Ok(InitKind::Model), + "plugin" => Ok(InitKind::Plugin), _ => Err(format_err!( - "Invalid init kind '{}'. Valid kinds are: place, model", + "Invalid init kind '{}'. Valid kinds are: place, model, plugin", source )), } @@ -147,6 +157,29 @@ fn init_model(base_path: &Path, project_params: ProjectParams) -> anyhow::Result Ok(()) } +fn init_plugin(base_path: &Path, project_params: ProjectParams) -> anyhow::Result<()> { + println!("Creating new plugin project '{}'", project_params.name); + + let project_file = project_params.render_template(PLUGIN_PROJECT); + try_create_project(base_path, &project_file)?; + + let readme = project_params.render_template(PLUGIN_README); + write_if_not_exists(&base_path.join("README.md"), &readme)?; + + let src = base_path.join("src"); + fs::create_dir_all(&src)?; + + write_if_not_exists( + &src.join("init.server.lua"), + "print(\"Hello world, from plugin!\")\n", + )?; + + let git_ignore = project_params.render_template(PLUGIN_GIT_IGNORE); + try_git_init(base_path, &git_ignore)?; + + Ok(()) +} + /// Contains parameters used in templates to create a project. struct ProjectParams { name: String,