Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bazel support #48

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/cli/cmd/init/handlers/elm.rs
Original file line number Diff line number Diff line change
@@ -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"));
Expand Down
4 changes: 2 additions & 2 deletions src/cli/cmd/init/handlers/go.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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"];

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}"));
}
Expand Down
8 changes: 4 additions & 4 deletions src/cli/cmd/init/handlers/java.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
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"];

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"));
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/cli/cmd/init/handlers/javascript.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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"];

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?",
Expand All @@ -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"));
Expand Down
12 changes: 1 addition & 11 deletions src/cli/cmd/init/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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?"
))
}
4 changes: 2 additions & 2 deletions src/cli/cmd/init/handlers/php.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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"];

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 {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/cmd/init/handlers/python.rs
Original file line number Diff line number Diff line change
@@ -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"];
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/cli/cmd/init/handlers/ruby.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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"];

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);
Expand Down
4 changes: 2 additions & 2 deletions src/cli/cmd/init/handlers/rust.rs
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion src/cli/cmd/init/handlers/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ 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,
) {
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"));
}
}
}
6 changes: 3 additions & 3 deletions src/cli/cmd/init/handlers/zig.rs
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/cli/cmd/init/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down