Skip to content

Commit

Permalink
Merge branch 'dev' into mega
Browse files Browse the repository at this point in the history
  • Loading branch information
comfysage committed Nov 5, 2023
2 parents a6de04f + 277da3b commit 041becb
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name = "sk"
path = "src/main.rs"

[dependencies]
clap = "4.4.4"
clap = { version = "4.4.4", features = ["cargo", "default"] }
pretty_env_logger = "0.5.0"
saku-lib = { path = "./saku-lib" }
saku-cli = { path = "./saku-cli" }
70 changes: 70 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::io::Read;
use std::process::{Command, Stdio};

fn capture_command(command: &mut Command) -> String {
let command = command
.stdout(Stdio::piped()) // Capture stdout
.spawn();

// Check if the command was successfully created
let mut child = match command {
Ok(child) => child,
Err(e) => {
eprintln!("Failed to execute command: {}", e);
std::process::exit(1);
}
};

// Read the captured stdout
let mut output = String::new();
child
.stdout
.take()
.unwrap()
.read_to_string(&mut output)
.unwrap();

// Wait for the command to finish and check its exit status
let status = child.wait().expect("Failed to wait for child process");
if status.success() {
println!("Command executed successfully. Output:\n{}", output);
} else {
eprintln!("Command failed with exit code: {}", status);
}

output
}

struct Vars {
repo_branch: String,
commit_hash: String,
}

impl Vars {
pub fn new() -> Self {
let mut repo_branch_bind = Command::new("git");
let repo_branch_cmd = repo_branch_bind.arg("branch").arg("--show-current");
let mut commit_hash_bind = Command::new("git");
let commit_hash_cmd = commit_hash_bind.arg("rev-parse").arg("HEAD");
let mut repo_branch = capture_command(repo_branch_cmd);
repo_branch = repo_branch.replace('\n', "");
let mut commit_hash = capture_command(commit_hash_cmd);
commit_hash = commit_hash.chars().take(7).collect();

Self {
repo_branch,
commit_hash,
}
}
}

fn main() {
let vars = Vars::new();
println!("cargo:rustc-env=REPO_BRANCH={}", vars.repo_branch);
println!("cargo:rustc-env=COMMIT_HASH={}", vars.commit_hash);
let mut pkg_version = format!("{}-{}", vars.repo_branch, vars.commit_hash);
if std::env::var("PROFILE").unwrap() == "debug" {
pkg_version = format!("{pkg_version}-debug");
}
println!("cargo:rustc-env=PKG_VERSION={}", pkg_version);
}
1 change: 1 addition & 0 deletions saku-lib/pkg/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn get_pkg_from_path(path: &str) -> Result<Pkg> {
let fh = fs::read_to_string(fullpath)?;
let mut pkg = Pkg::from_string(fh)?;

pkg.path = Some(path.to_string());
pkg.safe_guard()?;
pkg.fill()?;

Expand Down
2 changes: 1 addition & 1 deletion saku-lib/pkg/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Pkg {
pub update: Vec<String>,
#[serde(default)]
pub root: Vec<Root>,
path: Option<String>,
pub path: Option<String>,
}

impl Pkg {
Expand Down
82 changes: 77 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
use clap::builder::styling;
use saku_lib as saku;
use saku_cli as cli;
use saku::pkg::config;
use saku::pkg::{config, data};
use saku::pkg::pkg::Pkg;
use saku::util::msg;
use saku::prelude::*;

use clap::{arg, Command, Arg};

fn get_commands() -> Command {
let effects = (styling::Effects::BOLD | styling::Effects::UNDERLINE).clear();
let styles = styling::Styles::styled()
.header(styling::AnsiColor::White.on_default() | effects)
.usage(styling::AnsiColor::White.on_default() | effects)
.literal(styling::AnsiColor::BrightWhite.on_default() | effects)
.placeholder(styling::AnsiColor::BrightWhite.on_default() | effects);

let version = env!("PKG_VERSION");

Command::new("saku")
.about("a tiny distro-independent package manager written in Go.")
.subcommand_required(true)
.arg_required_else_help(true)
.allow_external_subcommands(true)
.styles(styles)
.version(version)
.subcommand(Command::new("env").about("Show environment script"))
.subcommand(
Command::new("config")
Expand Down Expand Up @@ -90,6 +102,30 @@ fn get_commands() -> Command {
.help("List installed packages")
)
)
.subcommand(
Command::new("task")
.about("Run a task for a package")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
Command::new("clone")
.about("Clone a package")
.arg_required_else_help(true)
.arg(arg!(<NAME> ... "Package to clone")),
)
.subcommand(
Command::new("build")
.about("Build a package")
.arg_required_else_help(true)
.arg(arg!(<NAME> ... "Package to build")),
)
.subcommand(
Command::new("install")
.about("Install a package")
.arg_required_else_help(true)
.arg(arg!(<NAME> ... "Package to install")),
)
)
}

fn main() -> Result<()> {
Expand All @@ -104,16 +140,16 @@ fn main() -> Result<()> {
Ok(())
}
Some(("config", sub_matches)) => {
let stash_command = sub_matches.subcommand().unwrap_or(("init", sub_matches));
match stash_command {
let subcommand = sub_matches.subcommand().unwrap_or(("init", sub_matches));
match subcommand {
("init", _) => cli::config::init(),
("create", _) => cli::config::create(),
(&_, _) => Err(Error::Unexpected),
}
}
Some(("pkg", sub_matches)) => {
let stash_command = sub_matches.subcommand().unwrap_or(("show", sub_matches));
match stash_command {
let subcommand = sub_matches.subcommand().unwrap_or(("show", sub_matches));
match subcommand {
("add", sub_matches) => {
let name = sub_matches
.get_one::<String>("NAME")
Expand Down Expand Up @@ -237,6 +273,42 @@ fn main() -> Result<()> {

Ok(())
}
Some(("task", sub_matches)) => {
let subcommand = sub_matches.subcommand().ok_or(make_err!(Missing, "subcommand missing"))?;
match subcommand {
("clone", sub_matches) => {
let name = sub_matches
.get_one::<String>("NAME")
.ok_or(make_err!(Missing, "no package name specified."))?;

let pkg = data::get_pkg(name)?;
cli::install::clone_pkg(&pkg)?;

Ok(())
}
("build", sub_matches) => {
let name = sub_matches
.get_one::<String>("NAME")
.ok_or(make_err!(Missing, "no package name specified."))?;

let pkg = data::get_pkg(name)?;
cli::install::run_install(&pkg)?;

Ok(())
}
("install", sub_matches) => {
let name = sub_matches
.get_one::<String>("NAME")
.ok_or(make_err!(Missing, "no package name specified."))?;

let pkg = data::get_pkg(name)?;
pkg.install_root()?;

Ok(())
}
(&_, _) => Err(Error::Unexpected),
}
}
// If all subcommands are defined above, anything else is unreachable!()
_ => {
Err(make_err!(Missing, "missing command. run saku --help."))
Expand Down

0 comments on commit 041becb

Please sign in to comment.