diff --git a/src/cli/cmd/init/handlers/elm.rs b/src/cli/cmd/init/handlers/elm.rs index 79252a0a..ffaacf24 100644 --- a/src/cli/cmd/init/handlers/elm.rs +++ b/src/cli/cmd/init/handlers/elm.rs @@ -1,12 +1,12 @@ -use crate::cli::cmd::init::project::Project; +use crate::cli::cmd::init::{project::Project, prompt::Prompt}; -use super::{prompt_for_language, Handler}; +use super::Handler; pub(crate) struct Elm; impl Handler for Elm { fn handle(project: &Project, flake: &mut super::Flake) { - if project.has_file("elm.json") && prompt_for_language("Elm") { + if project.has_file("elm.json") && Prompt::for_language("Elm") { flake .dev_shell_packages .push(String::from("elmPackages.elm")); diff --git a/src/cli/cmd/init/handlers/go.rs b/src/cli/cmd/init/handlers/go.rs index 118bc032..0d9613ca 100644 --- a/src/cli/cmd/init/handlers/go.rs +++ b/src/cli/cmd/init/handlers/go.rs @@ -1,6 +1,6 @@ use crate::cli::cmd::init::prompt::Prompt; -use super::{prompt_for_language, Flake, Handler, Project}; +use super::{Flake, Handler, Project}; const GO_VERSIONS: &[&str] = &["20", "19", "18", "17"]; @@ -8,7 +8,7 @@ pub(crate) struct Go; impl Handler for Go { fn handle(project: &Project, flake: &mut Flake) { - if project.has_file("go.mod") && prompt_for_language("Go") { + if project.has_file("go.mod") && Prompt::for_language("Go") { let go_version = Prompt::select("Select a version of Go", GO_VERSIONS); flake.dev_shell_packages.push(format!("go_1_{go_version}")); } diff --git a/src/cli/cmd/init/handlers/java.rs b/src/cli/cmd/init/handlers/java.rs index e909ad08..eced1a16 100644 --- a/src/cli/cmd/init/handlers/java.rs +++ b/src/cli/cmd/init/handlers/java.rs @@ -1,6 +1,6 @@ use crate::cli::cmd::init::{project::Project, prompt::Prompt}; -use super::{prompt_for_language, prompt_for_tool, Flake, Handler}; +use super::{Flake, Handler}; const JAVA_VERSIONS: &[&str] = &["19", "18", "17", "16", "15"]; @@ -8,15 +8,15 @@ pub(crate) struct Java; impl Handler for Java { fn handle(project: &Project, flake: &mut Flake) { - if project.has_one_of(&["build.gradle", "pom.xml"]) && prompt_for_language("Java") { + if project.has_one_of(&["build.gradle", "pom.xml"]) && Prompt::for_language("Java") { let java_version = Prompt::select("Which JDK version?", JAVA_VERSIONS); flake.dev_shell_packages.push(format!("jdk{java_version}")); - if project.has_file("pom.xml") && prompt_for_tool("Maven") { + if project.has_file("pom.xml") && Prompt::for_tool("Maven") { flake.dev_shell_packages.push(String::from("maven")); } - if project.has_file("build.gradle") && prompt_for_tool("Gradle") { + if project.has_file("build.gradle") && Prompt::for_tool("Gradle") { flake.dev_shell_packages.push(String::from("gradle")); } } diff --git a/src/cli/cmd/init/handlers/javascript.rs b/src/cli/cmd/init/handlers/javascript.rs index d25be207..30e8ed6b 100644 --- a/src/cli/cmd/init/handlers/javascript.rs +++ b/src/cli/cmd/init/handlers/javascript.rs @@ -1,6 +1,6 @@ use crate::cli::cmd::init::{project::Project, prompt::Prompt}; -use super::{prompt_for_language, prompt_for_tool, Flake, Handler}; +use super::{Flake, Handler}; const NODE_VERSIONS: &[&str] = &["18", "16", "14"]; @@ -8,11 +8,11 @@ pub(crate) struct JavaScript; impl Handler for JavaScript { fn handle(project: &Project, flake: &mut Flake) { - if project.has_one_of(&["deno.json", "deno.jsonc"]) && prompt_for_tool("Deno") { - flake.dev_shell_packages.push(String::from("deno")) + if project.has_one_of(&["deno.json", "deno.jsonc"]) && Prompt::for_tool("Deno") { + flake.dev_shell_packages.push(String::from("deno")); } - if project.has_file("package.json") && prompt_for_language("JavaScript") { + if project.has_file("package.json") && Prompt::for_language("JavaScript") { if project.has_file("bunfig.toml") && Prompt::bool( "This seems to be a Bun project. Would you like to add it to your environment?", @@ -26,13 +26,13 @@ impl Handler for JavaScript { flake.dev_shell_packages.push(format!("nodejs-{version}_x")); } - if project.has_file("pnpm-lock.yaml") && prompt_for_tool("pnpm") { + if project.has_file("pnpm-lock.yaml") && Prompt::for_tool("pnpm") { flake .dev_shell_packages .push(String::from("nodePackages.pnpm")); } - if project.has_file("yarn.lock") && prompt_for_tool("Yarn") { + if project.has_file("yarn.lock") && Prompt::for_tool("Yarn") { flake .dev_shell_packages .push(String::from("nodePackages.yarn")); diff --git a/src/cli/cmd/init/handlers/mod.rs b/src/cli/cmd/init/handlers/mod.rs index 83388a34..8eea8e3b 100644 --- a/src/cli/cmd/init/handlers/mod.rs +++ b/src/cli/cmd/init/handlers/mod.rs @@ -25,7 +25,7 @@ pub(crate) use system::System; pub(crate) use tools::Tools; pub(crate) use zig::Zig; -use super::{dev_shell::DevShell, project::Project, prompt::Prompt}; +use super::{dev_shell::DevShell, project::Project}; #[derive(Debug, Serialize)] pub(crate) struct Input { @@ -55,13 +55,3 @@ pub(crate) trait Handler { fn version_as_attr(v: &str) -> String { v.replace('.', "") } - -fn prompt_for_language(lang: &str) -> bool { - Prompt::bool(&format!("This seems to be a {lang} project. Would you like to initialize your flake with built-in {lang} dependencies?")) -} - -fn prompt_for_tool(tool: &str) -> bool { - Prompt::bool(&format!( - "This seems to be a {tool} project. Would you like to add it to your environment?" - )) -} diff --git a/src/cli/cmd/init/handlers/php.rs b/src/cli/cmd/init/handlers/php.rs index b6379bae..996ec8c8 100644 --- a/src/cli/cmd/init/handlers/php.rs +++ b/src/cli/cmd/init/handlers/php.rs @@ -1,6 +1,6 @@ use crate::cli::cmd::init::{project::Project, prompt::Prompt}; -use super::{prompt_for_language, version_as_attr, Flake, Handler}; +use super::{version_as_attr, Flake, Handler}; const PHP_VERSIONS: &[&str] = &["8.3", "8.2", "8.1", "8.0", "7.4", "7.3"]; @@ -8,7 +8,7 @@ pub(crate) struct Php; impl Handler for Php { fn handle(project: &Project, flake: &mut Flake) { - if project.has_one_of(&["composer.json", "php.ini"]) && prompt_for_language("PHP") { + if project.has_one_of(&["composer.json", "php.ini"]) && Prompt::for_language("PHP") { flake.inputs.insert( String::from("loophp"), super::Input { diff --git a/src/cli/cmd/init/handlers/python.rs b/src/cli/cmd/init/handlers/python.rs index a8f78457..3f4f9493 100644 --- a/src/cli/cmd/init/handlers/python.rs +++ b/src/cli/cmd/init/handlers/python.rs @@ -1,6 +1,6 @@ use crate::cli::cmd::init::{project::Project, prompt::Prompt}; -use super::{prompt_for_language, version_as_attr, Flake, Handler}; +use super::{version_as_attr, Flake, Handler}; const PYTHON_VERSIONS: &[&str] = &["3.11", "3.10", "3.09"]; const PYTHON_TOOLS: &[&str] = &["pip", "virtualenv", "pipenv"]; @@ -9,7 +9,7 @@ pub(crate) struct Python; impl Handler for Python { fn handle(project: &Project, flake: &mut Flake) { - if project.has_one_of(&["setup.py", "requirements.txt"]) && prompt_for_language("Python") { + if project.has_one_of(&["setup.py", "requirements.txt"]) && Prompt::for_language("Python") { let python_version = Prompt::select("Select a version of Python", PYTHON_VERSIONS); let python_version_attr = version_as_attr(&python_version); flake diff --git a/src/cli/cmd/init/handlers/ruby.rs b/src/cli/cmd/init/handlers/ruby.rs index 9bea3715..a71d6c2b 100644 --- a/src/cli/cmd/init/handlers/ruby.rs +++ b/src/cli/cmd/init/handlers/ruby.rs @@ -1,6 +1,6 @@ use crate::cli::cmd::init::{project::Project, prompt::Prompt}; -use super::{prompt_for_language, version_as_attr, Flake, Handler}; +use super::{version_as_attr, Flake, Handler}; const RUBY_VERSIONS: &[&str] = &["3.2", "3.1"]; @@ -8,7 +8,7 @@ pub(crate) struct Ruby; impl Handler for Ruby { fn handle(project: &Project, flake: &mut Flake) { - if project.has_one_of(&["Gemfile", "config.ru", "Rakefile"]) && prompt_for_language("Ruby") + if project.has_one_of(&["Gemfile", "config.ru", "Rakefile"]) && Prompt::for_language("Ruby") { let ruby_version = Prompt::select("Select a version of Ruby", RUBY_VERSIONS); let ruby_version_attr = version_as_attr(&ruby_version); diff --git a/src/cli/cmd/init/handlers/rust.rs b/src/cli/cmd/init/handlers/rust.rs index 45183030..0878ef5e 100644 --- a/src/cli/cmd/init/handlers/rust.rs +++ b/src/cli/cmd/init/handlers/rust.rs @@ -1,6 +1,6 @@ use crate::cli::cmd::init::prompt::Prompt; -use super::{prompt_for_language, Flake, Handler, Input, Project}; +use super::{Flake, Handler, Input, Project}; const CARGO_TOOLS: &[&str] = &[ "audit", "bloat", "cross", "edit", "outdated", "udeps", "watch", @@ -10,7 +10,7 @@ pub(crate) struct Rust; impl Handler for Rust { fn handle(project: &Project, flake: &mut Flake) { - if project.has_file("Cargo.toml") && prompt_for_language("Rust") { + if project.has_file("Cargo.toml") && Prompt::for_language("Rust") { flake.inputs.insert( String::from("rust-overlay"), Input { diff --git a/src/cli/cmd/init/handlers/tools.rs b/src/cli/cmd/init/handlers/tools.rs index 7d4ccccd..d126f81b 100644 --- a/src/cli/cmd/init/handlers/tools.rs +++ b/src/cli/cmd/init/handlers/tools.rs @@ -7,7 +7,7 @@ const COMMON_TOOLS: &[&str] = &["curl", "git", "jq", "wget"]; pub(crate) struct Tools; impl Handler for Tools { - fn handle(_: &Project, flake: &mut Flake) { + fn handle(project: &Project, flake: &mut Flake) { for tool in Prompt::multi_select( "Add any of these standard utilities to your environment if you wish", COMMON_TOOLS, @@ -15,5 +15,11 @@ impl Handler for Tools { let attr = tool.to_lowercase(); flake.dev_shell_packages.push(attr); } + + if project.has_one_of(&["WORKSPACE", ".bazelrc", ".bazelversion", "BUILD.bazel"]) + && Prompt::for_tool("Bazel") + { + flake.dev_shell_packages.push(String::from("bazel")); + } } } diff --git a/src/cli/cmd/init/handlers/zig.rs b/src/cli/cmd/init/handlers/zig.rs index beaae482..70a3e70b 100644 --- a/src/cli/cmd/init/handlers/zig.rs +++ b/src/cli/cmd/init/handlers/zig.rs @@ -1,12 +1,12 @@ -use crate::cli::cmd::init::project::Project; +use crate::cli::cmd::init::{project::Project, prompt::Prompt}; -use super::{prompt_for_language, Flake, Handler}; +use super::{Flake, Handler}; pub(crate) struct Zig; impl Handler for Zig { fn handle(project: &Project, flake: &mut Flake) { - if project.has_file("build.zig") && prompt_for_language("Zig") { + if project.has_file("build.zig") && Prompt::for_language("Zig") { flake.dev_shell_packages.push(String::from("zig")); } } diff --git a/src/cli/cmd/init/prompt.rs b/src/cli/cmd/init/prompt.rs index b65bc43b..13ccb384 100644 --- a/src/cli/cmd/init/prompt.rs +++ b/src/cli/cmd/init/prompt.rs @@ -100,6 +100,16 @@ impl Prompt { Err(_) => exit(1), } } + + pub(super) fn for_language(lang: &str) -> bool { + Self::bool(&format!("This seems to be a {lang} project. Would you like to initialize your flake with built-in {lang} dependencies?")) + } + + pub(super) fn for_tool(tool: &str) -> bool { + Self::bool(&format!( + "This seems to be a {tool} project. Would you like to add it to your environment?" + )) + } } #[derive(Clone)]