Skip to content

Commit

Permalink
Add plugin template (rojo-rbx#738)
Browse files Browse the repository at this point in the history
Adds a new plugin template to the `init` command: `rojo init --kind
plugin`.
  • Loading branch information
nezuo authored Jul 24, 2023
1 parent 89b6666 commit f6fc559
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion assets/default-model-project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions assets/default-plugin-project/README.md
Original file line number Diff line number Diff line change
@@ -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).
6 changes: 6 additions & 0 deletions assets/default-plugin-project/default.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "{project_name}",
"tree": {
"$path": "src"
}
}
3 changes: 3 additions & 0 deletions assets/default-plugin-project/gitignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Plugin model files
/{project_name}.rbxmx
/{project_name}.rbxm
39 changes: 36 additions & 3 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ 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 {
/// Path to the place to create the project. Defaults to the current directory.
#[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,
}
Expand All @@ -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.");
Expand All @@ -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 {
Expand All @@ -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
)),
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f6fc559

Please sign in to comment.