git_explore/manage/
commit.rs1use std::process;
2
3use crate::*;
4pub fn get_version(opt: &VersonOpts, config: &mut Config) -> Result<()> {
6 let version = match opt.commit_version.to_owned() {
7 Some(version) => version,
8 None => {
9 let mut version = Version::parse(&config.version).unwrap();
10 match opt.kind {
11 CommitKind::Patch => version.patch += 1,
12 CommitKind::Minor => version.minor += 1,
13 CommitKind::Major => version.major += 1,
14 };
15 version.to_string()
16 }
17 };
18
19 config.version = version;
20
21 Ok(())
22}
23pub fn sed_cargo(version: &str, config: &Config) -> Result<()> {
24 let paths = &config.cargo_repos;
25 let binding = vec!["s/\nversion.*\n/\nversion = '", version, "'\n/"].join("");
26
27 sed(paths, binding.as_str())
28}
29pub fn sed_pubspec(version: &str, config: &Config) -> Result<()> {
30 let paths = &config.pubspec_repos;
31 let binding = vec!["s/\nversion.*\n/\nversion = ", version, "\n/"].join("");
32
33 sed(paths, binding.as_str())
34}
35pub fn sed(paths: &[String], binding: &str) -> Result<()> {
36 let cmd = ReplaceCommand::new(binding).unwrap();
37 paths.iter().for_each(|path| {
38 let file_path = path.to_path();
39 let contents = file_path.get_content();
40
41 let contents = cmd.execute(contents).into_owned();
42
43 std::fs::write(file_path, contents).unwrap();
44 });
45 Ok(())
46}
47pub fn git_commit(version: &str, config: &Config) -> Result<()> {
48 config.git_repos.iter().for_each(|git| {
49 process::Command::new("git")
50 .current_dir(git)
51 .args(vec!["commit", "-am", version])
52 .spawn()
53 .unwrap_or_else(|_| panic!("Failed to execute command git commit {}", git));
54 });
55 Ok(())
56}
57pub fn commit(opt: &mut CommitOpts) -> Result<()> {
58 let mut config = Config::get(opt.base.base_dir.to_config_path().to_str().unwrap());
59
60 get_version(&opt.version_opts, &mut config).unwrap();
61 let version = &config.version;
62 sed_cargo(version, &config).unwrap();
63 sed_pubspec(version, &config).unwrap();
64 config.set(opt.base.base_dir.to_config_path()).unwrap();
65 git_commit(version, &config).unwrap();
66
67 Ok(())
68}
69
70#[test]
71pub fn tt() {
72 let version = "0.1.2";
73 let binding = vec!["s/\nversion.*\n/\nversion = '", version, "'\n/"].join("");
75 let cmd = ReplaceCommand::new(binding.as_str()).unwrap();
76 println!("{:#?}", cmd.execute("version = \"0.1.0\""));
77}
78