Skip to content

Commit

Permalink
adding the option to choose starter template
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad committed Nov 22, 2023
1 parent 9e90b95 commit 22f7450
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 23 deletions.
50 changes: 46 additions & 4 deletions rustyrails-cli/Cargo.lock

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

6 changes: 6 additions & 0 deletions rustyrails-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ path = "src/bin/main.rs"
required-features = []

[dependencies]
serde = "1.0.193"
serde_derive = "1.0.193"
clap = { version = "4.4.7", features = ["derive"] }
cargo-generate = { version = "0.18.5", default-features = false }
eyre = "0.6.9"
rand = "0.8.5"
dialoguer = "~0.11"
serde_variant = "0.1.2"
strum = { version = "0.25", features = ["derive"] }


[dev-dependencies]
trycmd = "0.14.19"
33 changes: 20 additions & 13 deletions rustyrails-cli/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{path::PathBuf, process::exit};

use clap::{Parser, Subcommand};
use rand::{distributions::Alphanumeric, thread_rng, Rng};

use rustyrails_cli::{template::Starter, CmdExit};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
Expand All @@ -26,6 +26,10 @@ enum Commands {
/// Rust lib name in Cargo.toml.
#[arg(short, long)]
lib_name: Option<String>,

/// Rust lib name in Cargo.toml.
#[arg(short, long)]
template: Option<Starter>,
},
}

Expand All @@ -37,7 +41,11 @@ fn main() {
path,
folder_name,
lib_name,
template,
} => {
let selected_template =
template.unwrap_or_else(|| Starter::prompt_selection().unwrap());

let random_string: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(20)
Expand All @@ -48,18 +56,17 @@ fn main() {
if let Some(lib_name) = lib_name {
define.push(format!("lib_name={lib_name}"));
}
match rustyrails_cli::generate::demo_site(&path, &folder_name, Some(define)) {
Ok(path) => rustyrails_cli::CmdExit {
code: 0,
message: Some(format!(
"\n💥 Rustyrails generated successfully in path: {}",
path.display()
)),
},
Err(err) => rustyrails_cli::CmdExit {
code: 0,
message: Some(format!("{err}")),
},
match rustyrails_cli::generate::demo_site(
&selected_template,
&path,
&folder_name,
Some(define),
) {
Ok(path) => CmdExit::ok_with_message(&format!(
"\n💥 Rustyrails generated successfully in path: {}",
path.display()
)),
Err(err) => CmdExit::error_with_message(&format!("{err}")),
}
}
};
Expand Down
10 changes: 5 additions & 5 deletions rustyrails-cli/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use std::{fs, path::PathBuf};

use cargo_generate::{generate, GenerateArgs, TemplatePath, Vcs};

/// Generator github template
const RUSTYRAILS_DEMO_TEMPLATE: &str =
"https://github.com/rustyrails-rs/rustyrails-starter-template";
use crate::template::Starter;

/// A generator form the git repo
///
Expand All @@ -17,10 +15,12 @@ const RUSTYRAILS_DEMO_TEMPLATE: &str =
///
/// ```rust
/// use std::path::PathBuf;
/// use rustyrails_cli::template::Starter;
/// let path = PathBuf::from(".");
/// rustyrails_cli::generate::demo_site(&path, "demo-website", None);
/// rustyrails_cli::generate::demo_site(&Starter::Saas,&path, "demo-website", None);
/// ```
pub fn demo_site(
starter_template: &Starter,
path: &PathBuf,
folder_name: &str,
define: Option<Vec<String>>,
Expand All @@ -32,7 +32,7 @@ pub fn demo_site(
name: Some(folder_name.to_string()),
vcs: Some(Vcs::Git),
template_path: TemplatePath {
git: Some(RUSTYRAILS_DEMO_TEMPLATE.to_string()),
git: Some(starter_template.git_url()),
..TemplatePath::default()
},
define,
Expand Down
18 changes: 18 additions & 0 deletions rustyrails-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
pub mod generate;
pub mod template;

#[derive(Debug)]
pub struct CmdExit {
pub code: i32,
pub message: Option<String>,
}

impl CmdExit {
#[must_use]
pub fn error_with_message(message: &str) -> Self {
Self {
code: 1,
message: Some(message.to_string()),
}
}
#[must_use]
pub fn ok_with_message(message: &str) -> Self {
Self {
code: 0,
message: Some(message.to_string()),
}
}
}
59 changes: 59 additions & 0 deletions rustyrails-cli/src/template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::str::FromStr;

use dialoguer::{theme::ColorfulTheme, Select};
use serde_derive::{Deserialize, Serialize};
use serde_variant::to_variant_name;
use strum::{EnumIter, EnumString, IntoEnumIterator};

#[derive(clap::ValueEnum, Clone, Serialize, Deserialize, Debug, EnumIter, EnumString)]
pub enum Starter {
#[serde(rename = "Saas")]
#[strum(serialize = "Saas")]
Saas,
#[serde(rename = "Stateless (not db)")]
#[strum(serialize = "Stateless (not db)")]
Stateless,
}

impl std::fmt::Display for Starter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
to_variant_name(self).expect("only enum supported").fmt(f)
}
}

impl Starter {
#[must_use]
pub fn to_list() -> Vec<String> {
Self::iter().map(|provider| provider.to_string()).collect()
}

/// Show interactive starter selection
///
/// # Errors
/// When could not show the prompt or could not convert selection the
/// [`Starter`]
pub fn prompt_selection() -> eyre::Result<Self> {
let selections = Self::to_list();
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Choose starter template")
.default(0)
.items(&selections[..])
.interact()?;

println!("{}", &selections[selection]);
Ok(Self::from_str(&selections[selection])?)
}

#[must_use]
pub fn git_url(&self) -> String {
match self {
Self::Saas => {
"https://github.com/rustyrails-rs/rustyrails-starter-template".to_string()
}
Self::Stateless => {
"https://github.com/rustyrails-rs/rustyrails-starter-without-db-template"
.to_string()
}
}
}
}
2 changes: 1 addition & 1 deletion rustyrails-cli/tests/cmd/generate-website.trycmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```console
$ rustyrails new --lib-name test
$ rustyrails new --lib-name test --template saas

💥 Rustyrails generated successfully in path: [CWD]/rustyrails-site

Expand Down

0 comments on commit 22f7450

Please sign in to comment.