Skip to content

Commit

Permalink
refactor: used term 'Framework' instead of 'Template' in code
Browse files Browse the repository at this point in the history
  • Loading branch information
sifnoc committed Jan 9, 2025
1 parent b44a368 commit 02abf6a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
59 changes: 40 additions & 19 deletions cli/src/create.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::fmt::Display;
use std::path::PathBuf;

use anyhow::Error;
Expand Down Expand Up @@ -26,6 +27,7 @@ trait Create {
fn print_message();
}

#[derive(Clone, PartialEq, Eq)]
pub enum Framework {
Ios,
Android,
Expand All @@ -34,6 +36,19 @@ pub enum Framework {
ReactNative,
}

impl Display for Framework {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let as_str = match self {
Framework::Ios => "ios",
Framework::Android => "android",
Framework::Web => "web",
Framework::Flutter => "flutter",
Framework::ReactNative => "react-native",
};
write!(f, "{}", as_str)
}
}

impl From<String> for Framework {
fn from(app: String) -> Self {
match app.to_lowercase().as_str() {
Expand All @@ -59,23 +74,29 @@ impl From<Framework> for &str {
}
}

const TEMPLATES: [&str; 5] = ["ios", "android", "web", "flutter", "react-native"];

pub fn create_project(arg_platform: &Option<String>) -> anyhow::Result<()> {
let platform: String = match arg_platform.as_deref() {
None => select_template()?,
const FRAMEWORKS: [Framework; 5] = [
Framework::Ios,
Framework::Android,
Framework::Web,
Framework::Flutter,
Framework::ReactNative,
];

pub fn create_project(arg_framework: &Option<String>) -> anyhow::Result<()> {
let framework: String = match arg_framework.as_deref() {
None => select_framework()?,
Some(m) => {
if TEMPLATES.contains(&m) {
if FRAMEWORKS.contains(&Framework::from(m.to_string())) {
m.to_string()
} else {
style::print_yellow("Invalid template selected. Please choose a valid template (e.g., 'ios', 'android', 'web', 'react-native', 'flutter').".to_string());
select_template()?
select_framework()?
}
}
};

let project_dir = env::current_dir()?;
match platform.into() {
match framework.into() {
Framework::Ios => Ios::create(project_dir)?,
Framework::Android => Android::create(project_dir)?,
Framework::Web => Web::create(project_dir)?,
Expand All @@ -86,7 +107,7 @@ pub fn create_project(arg_platform: &Option<String>) -> anyhow::Result<()> {
Ok(())
}

fn select_template() -> anyhow::Result<String> {
fn select_framework() -> anyhow::Result<String> {
let (items, unselectable) = get_target_platforms_with_status()?;

let idx = Select::with_theme(&ColorfulTheme::default())
Expand All @@ -98,9 +119,9 @@ fn select_template() -> anyhow::Result<String> {
if unselectable[selected_idx] {
style::print_yellow(format!(
"Cannot create {} template - build binding first",
&TEMPLATES[selected_idx]
&FRAMEWORKS[selected_idx]
));
return select_template();
return select_framework();
}
Ok(items[selected_idx].to_owned()) // Only available items will be matched with 'platform'
} else {
Expand All @@ -115,9 +136,9 @@ fn get_target_platforms_with_status() -> anyhow::Result<(Vec<String>, Vec<bool>)
let mut items = Vec::new();
let mut unselectable = Vec::new();

for &template in TEMPLATES.iter() {
match template {
"flutter" | "react-native" => {
for framework in FRAMEWORKS.iter() {
match framework {
Framework::Flutter | Framework::ReactNative => {
// Adding more information to the list
let requires = ["ios", "android"];
let missing: Vec<&str> = requires
Expand All @@ -129,21 +150,21 @@ fn get_target_platforms_with_status() -> anyhow::Result<(Vec<String>, Vec<bool>)
if !missing.is_empty() {
items.push(format!(
"{:<12} - Requires {} binding(s)",
template,
framework,
missing.join("/")
));
unselectable.push(true);
} else {
items.push(template.to_string());
items.push(framework.to_string());
unselectable.push(false);
}
}
_ => {
if config.target_platforms.contains(template) {
items.push(template.to_string());
if config.target_platforms.contains(&framework.to_string()) {
items.push(framework.to_string());
unselectable.push(false);
} else {
items.push(format!("{:<12} - Require binding", template));
items.push(format!("{:<12} - Require binding", framework));
unselectable.push(true);
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum Commands {
/// Create templates for the specified platform
Create {
#[arg(long, help = "Specify the platform")]
template: Option<String>,
framework: Option<String>,
},
}

Expand All @@ -57,7 +57,7 @@ fn main() {
Ok(_) => {}
Err(e) => style::print_red_bold(format!("Failed to build project: {:?}", e)),
},
Commands::Create { template } => match create::create_project(template) {
Commands::Create { framework } => match create::create_project(framework) {
Ok(_) => {}
Err(e) => style::print_red_bold(format!("Failed to create template: {:?}", e)),
},
Expand Down

0 comments on commit 02abf6a

Please sign in to comment.