Skip to content

Commit

Permalink
Bender init command
Browse files Browse the repository at this point in the history
  • Loading branch information
micprog committed Jan 8, 2024
1 parent 83fa382 commit 3d7b6ec
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand Down
81 changes: 81 additions & 0 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2021 ETH Zurich
// Michael Rogenmoser <[email protected]>

//! 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(())
}
1 change: 1 addition & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3d7b6ec

Please sign in to comment.