Skip to content

Commit

Permalink
Merge pull request #49 from DeterminateSystems/init-ux-fixes
Browse files Browse the repository at this point in the history
Improve the UX around fh init
  • Loading branch information
lucperkins authored Oct 5, 2023
2 parents e843d7e + 3dfb3ed commit 9dff320
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
run: git ls-files '*.nix' | nix develop --command xargs nixpkgs-fmt --check

- name: "Nix Flake Check"
run: nix flake check --print-build-logs
run: nix flake check --print-build-logs --all-systems
2 changes: 1 addition & 1 deletion src/cli/cmd/init/handlers/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Handler for JavaScript {
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/TypeScript") {
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 Down
50 changes: 41 additions & 9 deletions src/cli/cmd/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
fs::write,
io::IsTerminal,
path::PathBuf,
process::{exit, ExitCode},
process::{exit, Command, ExitCode},
};
use url::Url;

Expand Down Expand Up @@ -182,9 +182,20 @@ impl CommandExecute for InitSubcommand {
}
}

flake.shell_hook = Prompt::maybe_string(
"If you'd like to add a shell hook that gets run every time you enter your Nix development environment, enter it here:",
);
if Prompt::bool("Would you like to add a shell hook that runs every time you enter your Nix development environment?") {
loop {
let hook = Prompt::maybe_string(
"Enter the hook here:",
);

if let Some(hook) = hook {
flake.shell_hook = Some(hook);
break;
} else if !Prompt::bool("You didn't enter a hook. Would you like to try again?") {
break;
}
}
}

// If the dev shell will be empty, prompt users to ensure that they still want a flake
if flake.dev_shell_packages.is_empty() {
Expand Down Expand Up @@ -219,21 +230,42 @@ impl CommandExecute for InitSubcommand {

write(self.output, flake_string)?;

if project.has_directory(".git")
&& command_exists("git")
&& Prompt::bool("Would you like to add your flake.nix file to Git?")
{
Command::new("git")
.args(["add", "--intent-to-add", "flake.nix"])
.output()?;
}

if !project.has_file(".envrc")
&& Prompt::bool("Would you like to add a .envrc file for use with direnv?")
&& Prompt::bool("Would you like to add a .envrc file so that you can use direnv in this project?")
{
write(PathBuf::from(".envrc"), String::from("use flake"))?;
} else {
println!(
"Your flake is ready to go! Run `nix flake show` to see which outputs it provides."
);

if Prompt::bool("You'll need to run `direnv allow` to activate direnv in this project. Would you like to do that now?") {
if command_exists("direnv") {
Command::new("direnv").arg("allow").output()?;
} else {
println!("It looks like direnv isn't installed.");
}
}
}

println!(
"Your flake is ready to go! Run `nix flake show` to see which outputs it provides."
);

Ok(ExitCode::SUCCESS)
}
}
}

fn command_exists(cmd: &str) -> bool {
Command::new(cmd).output().is_ok()
}

async fn select_nixpkgs(api_addr: &Url) -> Result<String, FhError> {
let client = &FlakeHubClient::new(api_addr)?;
let releases = client.releases("NixOS", "nixpkgs").await?;
Expand Down
6 changes: 5 additions & 1 deletion src/cli/cmd/init/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ impl Project {

// Helpers
pub(super) fn has_file(&self, file: &str) -> bool {
self.root.join(file).exists()
self.root.join(file).exists() && PathBuf::from(file).is_file()
}

pub(super) fn has_directory(&self, dir: &str) -> bool {
self.root.join(dir).exists() && PathBuf::from(dir).is_dir()
}

pub(super) fn has_one_of(&self, files: &[&str]) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cmd/init/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Prompt {
}

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?"))
Self::bool(&format!("This seems to be a {lang} project. Would you like to initialize your flake with some standard dependencies for {lang}?"))
}

pub(super) fn for_tool(tool: &str) -> bool {
Expand Down

0 comments on commit 9dff320

Please sign in to comment.