8000 feat: revamp min init output by twitchyliquid64 · Pull Request #773 · gominimal/minimal · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
83 changes: 45 additions & 38 deletions crates/op/src/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::Error;

const DEFAULT_PKGS: &str = "https://github.com/gominimal/pkgs";
const DEFAULT_PKGS_BRANCH: &str = "main";
const FALLBACK_STACK: &str = "shell";

/// Detects the stack matching an existing codebase and produces the
/// `minimal.toml` it should be initialized with.
Expand Down Expand Up @@ -84,7 +83,7 @@ impl ProjectOp for InitProject {

let matched_stack = match_stack(graph.iter_stacks(), &repo_dir);
let matched = matched_stack.is_some();
let content = generate_mfile(matched_stack, &origin);
let content = generate_mfile(matched_stack, &origin, &repo_dir);

Ok(InitPlan {
toml_path,
Expand Down Expand Up @@ -146,11 +145,11 @@ fn match_stack<'a>(

/// Renders the `minimal.toml` contents for the (optional) matched stack and the
/// resolved upstream `origin`.
fn generate_mfile(matched_stack: Option<MatchedStack>, origin: &SpecOrigin) -> String {
// Fall back to the "shell" stack if no project-specific matcher hit.
let (stack_name, build_packages, runtime_packages) =
matched_stack.unwrap_or_else(|| (FALLBACK_STACK.to_string(), vec![], vec![]));

fn generate_mfile(
matched_stack: Option<MatchedStack>,
origin: &SpecOrigin,
repo_dir: &Path,
) -> String {
let mut content = String::new();
content.push_str("# Generated by `minimal init`\n\n");

Expand All @@ -171,38 +170,41 @@ fn generate_mfile(matched_stack: Option<MatchedStack>, origin: &SpecOrigin) -> S
}
content.push('\n');

content.push_str("[stack]\n");
content.push_str(&format!("use = \"{stack_name}\"\n"));
if !build_packages.is_empty() {
content.push_str(&format!(
"build_packages = [{}]\n",
build_packages
.into_iter()
.map(|s| format!("\"{s}\""))
.collect::<Vec<_>>()
.join(", ")
));
}
if !runtime_packages.is_empty() {
content.push_str(&format!(
"runtime_packages = [{}]\n",
runtime_packages
.into_iter()
.map(|s| format!("\"{s}\""))
.collect::<Vec<_>>()
.join(", ")
));
if let Some((stack_name, build_packages, runtime_packages)) = matched_stack {
content.push_str("[stack]\n");
content.push_str(&format!("use = \"{stack_name}\"\n"));
if !build_packages.is_empty() {
content.push_str(&format!(
"build_packages = [{}]\n",
build_packages
.into_iter()
.map(|s| format!("\"{s}\""))
.collect::<Vec<_>>()
.join(", ")
));
}
if !runtime_packages.is_empty() {
content.push_str(&format!(
"runtime_packages = [{}]\n",
runtime_packages
.into_iter()
.map(|s| format!("\"{s}\""))
.collect::<Vec<_>>()
.join(", ")
));
}
content.push('\n');
}

content.push('\n');

content.push_str("[defaults]\n");
content.push_str("state_key = \"dev\"\n");
content.push('\n');
content.push_str("[tasks.shell]\n");
content.push_str("interactive = true\n");
content.push_str("packages = [\"base\"]\n");
content.push_str("exec = \"bash --noprofile -l\"\n");
content.push_str("[session] # min attach\n");
content.push_str("packages = [\"base\", \"vim\"");
if std::fs::exists(repo_dir.join(".git")).unwrap_or_default() {
content.push_str(", \"git\"");
}
content.push_str("]\n");
content
}

Expand Down Expand Up @@ -311,8 +313,9 @@ mod tests {
vec!["cc".to_string()],
vec!["openssl".to_string()],
));
let cwd = TempDir::new().unwrap();

let out = generate_mfile(matched, &origin);
let out = generate_mfile(matched, &origin, cwd.path());

assert!(out.contains("repo = \"https://github.com/gominimal/pkgs\""));
assert!(out.contains("branch = \"main\""));
Expand All @@ -323,19 +326,23 @@ mod tests {
}

#[test]
fn generate_mfile_falls_back_to_shell_without_match() {
fn generate_mfile_falls_back_to_upstream_with_session() {
let origin = SpecOrigin::Repo(Repo::Git {
url: "u".to_string(),
rev: "r".to_string(),
tracking: None,
});
let cwd = TempDir::new().unwrap();

let out = generate_mfile(None, &origin);
let out = generate_mfile(None, &origin, cwd.path());

assert!(out.contains("use = \"shell\""));
assert!(!out.contains("[stack]"));
// No tracking branch and no detected packages.
assert!(!out.contains("branch ="));
assert!(!out.contains("build_packages"));
assert!(!out.contains("runtime_packages"));

// Has some session config
assert!(out.contains("[session]"));
}
}
Loading
0