From 3d7b6ecb8f3f86ade71c0111569eb2521700f94d Mon Sep 17 00:00:00 2001 From: Michael Rogenmoser Date: Wed, 27 Dec 2023 23:19:22 +0100 Subject: [PATCH 1/3] Bender init command --- src/cli.rs | 7 ++++- src/cmd/init.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ src/cmd/mod.rs | 1 + 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/cmd/init.rs diff --git a/src/cli.rs b/src/cli.rs index fd91c6f6..e7a5d079 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -76,7 +76,8 @@ pub fn main() -> Result<()> { .subcommand(cmd::script::new()) .subcommand(cmd::checkout::new()) .subcommand(cmd::vendor::new()) - .subcommand(cmd::fusesoc::new()); + .subcommand(cmd::fusesoc::new()) + .subcommand(cmd::init::new()); // Add the `--debug` option in debug builds. let app = if cfg!(debug_assertions) { @@ -100,6 +101,10 @@ pub fn main() -> Result<()> { ENABLE_DEBUG.store(true, std::sync::atomic::Ordering::Relaxed); } + if let Some(("init", matches)) = matches.subcommand() { + return cmd::init::run(matches); + } + let mut force_fetch = false; if let Some(("update", intern_matches)) = matches.subcommand() { force_fetch = intern_matches.get_flag("fetch"); diff --git a/src/cmd/init.rs b/src/cmd/init.rs new file mode 100644 index 00000000..c6ab3cf1 --- /dev/null +++ b/src/cmd/init.rs @@ -0,0 +1,81 @@ +// Copyright (c) 2021 ETH Zurich +// Michael Rogenmoser + +//! The `init` subcommand. + +use std::env::current_dir; +use std::ffi::OsStr; +use std::fs::File; +use std::io::Write; +use std::path::Path; +use std::process::Command as SysCommand; + +use clap::{ArgMatches, Command}; + +use crate::error::*; + +/// Assemble the `init` subcommand. +pub fn new() -> Command { + Command::new("init").about("Initialize a Bender package") +} + +/// Execute the `init` subcommand. +pub fn run(_matches: &ArgMatches) -> Result<()> { + // Get working directory name + let binding = current_dir()?; + let cwd = binding + .as_path() + .file_name() + .unwrap_or(OsStr::new("new_package")) + .to_str() + .unwrap_or("new_package"); + + // Get author from git config + let name = String::from_utf8( + SysCommand::new("git") + .args(["config", "user.name"]) + .output()? + .stdout, + ) + .unwrap_or("".to_string()); + let name = &name + .strip_suffix("\r\n") + .unwrap_or(name.strip_suffix('\n').unwrap_or(&name)); + let email = String::from_utf8( + SysCommand::new("git") + .args(["config", "user.email"]) + .output()? + .stdout, + ) + .unwrap_or("".to_string()); + let email = &email + .strip_suffix("\r\n") + .unwrap_or(email.strip_suffix('\n').unwrap_or(&email)); + + // Create Bender.yml + if Path::new("Bender.yml").exists() { + return Err(Error::new("Bender.yml already exists")); + } + + let mut file = File::create("Bender.yml")?; + + writeln!( + file, + "\ +package: + name: {} + authors: + - \"{} <{}>\" + +dependencies: + +sources: + # Source files grouped in levels. Files in level 0 have no dependencies on files in this + # package. Files in level 1 only depend on files in level 0, files in level 2 on files in + # levels 1 and 0, etc. Files within a level are ordered alphabetically. + # Level 0", + cwd, name, email + )?; + + Ok(()) +} diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 740d8c1f..3c383995 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -11,6 +11,7 @@ pub mod checkout; pub mod clone; pub mod config; pub mod fusesoc; +pub mod init; pub mod packages; pub mod parents; pub mod path; From e9d82ad7692be366b7f5dc155f1f5599141f92af Mon Sep 17 00:00:00 2001 From: Michael Rogenmoser Date: Wed, 3 Jan 2024 16:50:42 +0100 Subject: [PATCH 2/3] Expand default Bender.yml --- src/cmd/init.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cmd/init.rs b/src/cmd/init.rs index c6ab3cf1..aab7b5d0 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -37,7 +37,7 @@ pub fn run(_matches: &ArgMatches) -> Result<()> { .output()? .stdout, ) - .unwrap_or("".to_string()); + .unwrap_or("Your Name".to_string()); let name = &name .strip_suffix("\r\n") .unwrap_or(name.strip_suffix('\n').unwrap_or(&name)); @@ -47,7 +47,7 @@ pub fn run(_matches: &ArgMatches) -> Result<()> { .output()? .stdout, ) - .unwrap_or("".to_string()); + .unwrap_or("you@example.com".to_string()); let email = &email .strip_suffix("\r\n") .unwrap_or(email.strip_suffix('\n').unwrap_or(&email)); @@ -62,6 +62,9 @@ pub fn run(_matches: &ArgMatches) -> Result<()> { writeln!( file, "\ +# A more detailed description of the manifest format `Bender.yml` can be found here: +# https://github.com/pulp-platform/bender#manifest-format-benderyml + package: name: {} authors: From 33b3be2ebeb3525402578d1ce00c60a07734bd81 Mon Sep 17 00:00:00 2001 From: Michael Rogenmoser Date: Mon, 8 Jan 2024 11:47:04 +0100 Subject: [PATCH 3/3] Update Changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c51d5cf..61a503a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## Unreleased +### Added +- Add macOS binary for releases +- Add `init` command to initialize a Bender.yml file of an IP. ## 0.27.4 - 2023-11-14 ### Added