Skip to content

Commit

Permalink
fix(rescript): include rescript template in binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickard Natt och Dag committed Nov 16, 2020
1 parent 902db80 commit 458a495
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 111 deletions.
125 changes: 63 additions & 62 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ serde_json = "1.0"
structopt = "0.3.13"
handlebars = "3.5.1"
dialoguer = "0.7.1"
copy_dir = "0.1.2"
colored = "2.0.0"
include-dir-macro = "0.2"
39 changes: 18 additions & 21 deletions src/commands/rescript.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
use crate::utils::helpers;
use colored::*;
use copy_dir::copy_dir;
use handlebars::Handlebars;
use helpers::Result;
use include_dir_macro::include_dir;
use serde_json::json;
use std::fs;

fn update_file(dir: &str, filename: &str) -> Result<()> {
let reg = Handlebars::new();
let file = &fs::read(format!("{}/{}", dir, filename))?;
let file: String = String::from_utf8_lossy(file).parse()?;
let output = reg.render_template(&file, &json!({ "name": dir }))?;

fs::write(&format!("{}/{}", dir, filename), output)?;

Ok(())
}

pub fn run(name: String) -> Result<()> {
let rescript_template = helpers::application_dir("src/templates/rescript")?;

copy_dir(rescript_template, &name)?;

// Update files with template names
update_file(&name, "package.json")?;
update_file(&name, "bsconfig.json")?;
update_file(&name, "README.md")?;
update_file(&name, "public/index.html")?;
let reg = Handlebars::new();
fs::create_dir_all(&name)?;
fs::create_dir_all(format!("{}/public", &name))?;
fs::create_dir_all(format!("{}/src", &name))?;

let hashmap = include_dir!("src/templates/rescript");

for key in hashmap.keys() {
let file = std::path::Path::new(key);
let text = hashmap
.get(file)
.and_then(|entry| std::str::from_utf8(*entry).ok())
.unwrap();
let output = reg.render_template(&text, &json!({ "name": &name }))?;

fs::write(format!("{}/{}", &name, &key.to_string_lossy()), output)?;
}

println!(
"
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(proc_macro_hygiene)]

mod commands;
mod utils;

Expand Down
28 changes: 1 addition & 27 deletions src/utils/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, fs, io, path, process};
use std::process;

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

Expand All @@ -12,29 +12,3 @@ pub fn run_command(cmd: &str, arg: &[&str]) -> process::Output {
pub fn install_dev(pkg: &str) -> process::Output {
run_command("npm", &["install", "--save-exact", "--save-dev", pkg])
}

pub fn application_root_dir() -> std::result::Result<path::PathBuf, io::Error> {
if let Some(manifest_dir) = env::var_os("CARGO_MANIFEST_DIR") {
return Ok(path::PathBuf::from(manifest_dir));
}

let mut exe = fs::canonicalize(env::current_exe()?)?;

// Modify in-place to avoid an extra copy.
if exe.pop() {
return Ok(exe);
}

Err(io::Error::new(
io::ErrorKind::Other,
"Failed to find an application root",
))
}

/// Same as `application_root_dir`, but extends the root directory with the given path.
pub fn application_dir<P>(path: P) -> std::result::Result<path::PathBuf, io::Error>
where
P: AsRef<path::Path>,
{
Ok(application_root_dir()?.join(path))
}

0 comments on commit 458a495

Please sign in to comment.