Skip to content

Commit

Permalink
Merge pull request #33 from departure-inc/feature/add-rails-new-cmd
Browse files Browse the repository at this point in the history
feat: add rails new command
  • Loading branch information
runeleaf authored Jun 20, 2024
2 parents 319f3e0 + ec763e7 commit 9cafa0d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/tests
/tmp
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ fix:
build:
cargo build
cp target/debug/skeleton bin/skeleton
cp target/debug/skeleton tmp/skeleton
Binary file modified bin/skeleton
Binary file not shown.
2 changes: 2 additions & 0 deletions src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ pub fn help_command() {
eprintln!();
eprintln!("OPTIONS:");
eprintln!(" rails : Ruby on Rails");
eprintln!(" react : React+Vite");
eprintln!(" next : Next.js");
eprintln!(" rust : Rust, Axum and SQLx");
eprintln!(" python : Python, FastAPI and SQLAlchemy");
eprintln!(" flutter: Flutter");
eprintln!();
eprintln!("ARGS:");
eprintln!(" new : create new project.");
eprintln!(" init : initialize project.");
eprintln!(" help : help.");
}
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod help;
mod nextjs;
mod python;
mod rails;
mod react;
mod rust;

fn main() {
Expand All @@ -25,6 +26,7 @@ fn main() {
let _current_path = env::current_dir().unwrap();
let main_command = args[1].clone();
let sub_command = args[2].clone();
let options_command = ext_cmd(&args);

/*
println!("current path is {}", current_path.display());
Expand All @@ -34,10 +36,18 @@ fn main() {

match main_command.as_str() {
"rails" => match sub_command.as_str() {
"new" => rails::new_rails(&options_command),
"api" => rails::api_rails(&options_command),
"init" => rails::init_rails(),
"help" => rails::help_rails(),
_ => not_found(&sub_command),
},
"react" => match sub_command.as_str() {
"new" => react::new_react(),
"init" => react::init_react(),
"help" => react::help_react(),
_ => not_found(&sub_command),
},
"next" => match sub_command.as_str() {
"init" => nextjs::init_next(),
"help" => nextjs::help_next(),
Expand Down Expand Up @@ -67,3 +77,10 @@ fn not_found(text: &str) {
println!("! {} is not found.", text);
println!("! example: skeleton help");
}

fn ext_cmd(args: &[String]) -> String {
match args.get(3) {
Some(arg) => arg.to_string(),
None => "".to_string(),
}
}
6 changes: 1 addition & 5 deletions src/nextjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ pub fn init_next() {
// add .gitignore
println!("+ append .gitignore");
let path = Path::new(".gitignore");
let mut file = OpenOptions::new()
.write(true)
.append(true)
.open(path)
.unwrap();
let mut file = OpenOptions::new().append(true).open(path).unwrap();

writeln!(file, ".env").unwrap();

Expand Down
52 changes: 46 additions & 6 deletions src/rails.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs::OpenOptions;
use std::io::Write;
use std::io::{self, Write};
use std::path::Path;
use std::process::Command;

pub fn init_rails() {
println!("= init rails");
Expand All @@ -11,11 +12,7 @@ pub fn init_rails() {
return println!("! Gemfile is not found.");
}

let mut file = OpenOptions::new()
.write(true)
.append(true)
.open(path)
.unwrap();
let mut file = OpenOptions::new().append(true).open(path).unwrap();

writeln!(
file,
Expand All @@ -27,8 +24,51 @@ pub fn init_rails() {
println!("= init rails finished.");
}

//
// # 新しいRailsアプリの作成(WebApp)
// $ rails new {project-app-name} -d postgresql -T -B -a propshaft -c tailwind
pub fn new_rails(name: &str) {
println!("= rails new {}", name);

let mut bundle = Command::new("rails");
bundle
.arg("new")
.arg(name)
.args(["-d", "postgresql"])
.arg("-T")
.arg("-B")
.args(["-a", "propshaft"])
.args(["-c", "tailwind"]);
let output = bundle.output().expect("failed to execute rails command");

io::stdout().write_all(&output.stdout).unwrap();
println!("= rails new project created.");
}

//
// # 新しいRailsアプリの作成(APIモード)
// $ rails new {project-app-name} --api -d postgresql -T -B
pub fn api_rails(name: &str) {
println!("= rails new {} --api", name);

let mut bundle = Command::new("rails");
bundle
.arg("new")
.arg(name)
.arg("--api")
.args(["-d", "postgresql"])
.arg("-T")
.arg("-B");
let output = bundle.output().expect("failed to execute rails command");

io::stdout().write_all(&output.stdout).unwrap();
println!("= rails api project created.");
}

pub fn help_rails() {
println!("? help rails commands");
println!(" rails init");
println!(" rails help");
println!(" rails new project-name");
println!(" rails api project-name");
}
12 changes: 12 additions & 0 deletions src/react.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub fn new_react() {
println!("= new react");
}

pub fn init_react() {
println!("= init react");
}

pub fn help_react() {
println!(" react init");
println!(" react new");
}

0 comments on commit 9cafa0d

Please sign in to comment.