Skip to content

Commit

Permalink
Upgrade rojo init to create README.md and create Git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed Mar 14, 2020
1 parent f7c0f33 commit fe87472
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 7 deletions.
9 changes: 9 additions & 0 deletions assets/default-model-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# {project_name}
Generated by [Rojo](https://github.com/rojo-rbx/rojo) {rojo_version}.

## Getting Started
To build this library or plugin, use:

```bash
rojo build -o "{project_name}.rbxmx"
```
File renamed without changes.
3 changes: 3 additions & 0 deletions assets/default-model-project/gitignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Roblox Studio lock files
/*.rbxlx.lock
/*.rbxl.lock
File renamed without changes.
17 changes: 17 additions & 0 deletions assets/default-place-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 the place from scratch, use:

```bash
rojo build -o Place.rbxlx
```

Next, open `Place.rbxlx` in Roblox Studio and start the Rojo server:

```bash
rojo serve
```

For more help, check out [the Rojo documentation](https://rojo.space/docs).
File renamed without changes.
6 changes: 6 additions & 0 deletions assets/default-place-project/gitignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Project place file
/{project_name}.rbxlx

# Roblox Studio lock files
/*.rbxlx.lock
/*.rbxl.lock
86 changes: 79 additions & 7 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ use std::{
fs::{self, OpenOptions},
io::{self, Write},
path::Path,
process::{Command, Stdio},
};

use snafu::Snafu;

use crate::cli::{InitCommand, InitKind};

static DEFAULT_PLACE_PROJECT: &str = include_str!("../../assets/place.project.json");
static MODEL_PROJECT: &str =
include_str!("../../assets/default-model-project/default.project.json");
static MODEL_README: &str = include_str!("../../assets/default-model-project/README.md");
static MODEL_INIT: &str = include_str!("../../assets/default-model-project/src-init.lua");
static MODEL_GIT_IGNORE: &str = include_str!("../../assets/default-model-project/gitignore.txt");

static DEFAULT_MODEL_PROJECT: &str = include_str!("../../assets/model.project.json");
static DEFAULT_MODEL_INIT: &str = include_str!("../../assets/default-model.lua");
static PLACE_PROJECT: &str =
include_str!("../../assets/default-place-project/default.project.json");
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");

#[derive(Debug, Snafu)]
pub struct InitError(Error);
Expand All @@ -21,6 +28,9 @@ enum Error {
#[snafu(display("A project file named default.project.json already exists in this folder"))]
AlreadyExists,

#[snafu(display("git init failed"))]
GitInit,

#[snafu(display("I/O error"))]
Io { source: io::Error },
}
Expand All @@ -37,6 +47,8 @@ pub fn init(options: InitCommand) -> Result<(), InitError> {

fn init_inner(options: InitCommand) -> Result<(), Error> {
let base_path = options.absolute_path();
fs::create_dir_all(&base_path)?;

let canonical = fs::canonicalize(&base_path)?;
let project_name = canonical
.file_name()
Expand All @@ -54,9 +66,14 @@ fn init_inner(options: InitCommand) -> Result<(), Error> {
}

fn init_place(base_path: &Path, project_params: ProjectParams) -> Result<(), Error> {
let project_file = project_params.render_template(DEFAULT_PLACE_PROJECT);
eprintln!("Creating new place project '{}'", project_params.name);

let project_file = project_params.render_template(PLACE_PROJECT);
try_create_project(base_path, &project_file)?;

let readme = project_params.render_template(PLACE_README);
write_if_not_exists(&base_path.join("README.md"), &readme)?;

let src = base_path.join("src");
fs::create_dir_all(&src)?;

Expand Down Expand Up @@ -84,19 +101,34 @@ fn init_place(base_path: &Path, project_params: ProjectParams) -> Result<(), Err
"print(\"Hello world, from client!\")",
)?;

let git_ignore = project_params.render_template(PLACE_GIT_IGNORE);
try_git_init(base_path, &git_ignore)?;

eprintln!("Created project successfully.");

Ok(())
}

fn init_model(base_path: &Path, project_params: ProjectParams) -> Result<(), Error> {
let project_file = project_params.render_template(DEFAULT_MODEL_PROJECT);
eprintln!("Creating new model project '{}'", project_params.name);

let project_file = project_params.render_template(MODEL_PROJECT);
try_create_project(base_path, &project_file)?;

let readme = project_params.render_template(MODEL_README);
write_if_not_exists(&base_path.join("README.md"), &readme)?;

let src = base_path.join("src");
fs::create_dir_all(&src)?;

let init = project_params.render_template(DEFAULT_MODEL_INIT);
let init = project_params.render_template(MODEL_INIT);
write_if_not_exists(&src.join("init.lua"), &init)?;

let git_ignore = project_params.render_template(MODEL_GIT_IGNORE);
try_git_init(base_path, &git_ignore)?;

eprintln!("Created project successfully.");

Ok(())
}

Expand All @@ -108,7 +140,47 @@ struct ProjectParams {
impl ProjectParams {
/// Render a template by replacing variables with project parameters.
fn render_template(&self, template: &str) -> String {
template.replace("{project_name}", &self.name)
template
.replace("{project_name}", &self.name)
.replace("{rojo_version}", env!("CARGO_PKG_VERSION"))
}
}

/// Attempt to initialize a Git repository if necessary, and create .gitignore.
fn try_git_init(path: &Path, git_ignore: &str) -> Result<(), Error> {
if should_git_init(path) {
log::debug!("Initializing Git repository...");

let status = Command::new("git").arg("init").current_dir(path).status()?;

if !status.success() {
return Err(Error::GitInit);
}
}

write_if_not_exists(&path.join(".gitignore"), git_ignore)?;

Ok(())
}

/// Tells whether we should initialize a Git repository inside the given path.
///
/// Will return false if the user doesn't have Git installed or if the path is
/// already inside a Git repository.
fn should_git_init(path: &Path) -> bool {
let result = Command::new("git")
.args(&["rev-parse", "--is-inside-work-tree"])
.stdout(Stdio::null())
.current_dir(path)
.status();

match result {
// If the command ran, but returned a non-zero exit code, we are not in
// a Git repo and we should initialize one.
Ok(status) => !status.success(),

// If the command failed to run, we probably don't have Git installed.
Err(_) => false,
}
}

Expand Down

0 comments on commit fe87472

Please sign in to comment.