70 lines
2.4 KiB
Rust
70 lines
2.4 KiB
Rust
use std::io::{self, BufRead};
|
|
use std::fs::{self, File};
|
|
use std::env;
|
|
use std::path::Path;
|
|
|
|
use git2::{Repository, Signature, ObjectType, Direction, RemoteCallbacks};
|
|
|
|
fn main() {
|
|
let args: Vec<String> = env::args().collect();
|
|
let version = args.into_iter().nth(1).unwrap();
|
|
let latest_changelog_path = Path::new("../LATEST_CHANGELOG");
|
|
let full_changelog_path = Path::new("../changelog.txt");
|
|
|
|
println!("Deploying version {}", version);
|
|
|
|
let changelog = io::BufReader::new(File::open(latest_changelog_path).unwrap()).lines();
|
|
let mut deploy_changelog = String::new();
|
|
let mut latest_changelog = String::from(version.clone() + "\n");
|
|
for line in changelog {
|
|
let line = line.unwrap();
|
|
deploy_changelog.push_str(&format!("* {}\n", line));
|
|
latest_changelog.push_str(&format!("-{}\n", line));
|
|
}
|
|
latest_changelog.push('\n');
|
|
|
|
println!("{}", deploy_changelog);
|
|
|
|
fs::write(latest_changelog_path, deploy_changelog).unwrap();
|
|
let old_changelog = fs::read_to_string(full_changelog_path).unwrap();
|
|
latest_changelog.push_str(&old_changelog);
|
|
fs::write(full_changelog_path, latest_changelog).unwrap();
|
|
|
|
let asmr_sig = Signature::now("AsmodeusRex", "dark.song2667@fastmail.com").unwrap();
|
|
let bot_sig = Signature::now("ASMR_Deploy_Bot", "dark.song2667@fastmail.com").unwrap();
|
|
|
|
let repo = Repository::open("..").unwrap();
|
|
let mut index = repo.index().unwrap();
|
|
|
|
index.add_path(Path::new("LATEST_CHANGELOG")).unwrap();
|
|
index.add_path(Path::new("changelog.txt")).unwrap();
|
|
let oid = index.write_tree().unwrap();
|
|
|
|
let obj = repo.head().unwrap().resolve().unwrap().peel(ObjectType::Commit).unwrap();
|
|
let parent_commit = obj.into_commit().unwrap();
|
|
let tree = repo.find_tree(oid).unwrap();
|
|
|
|
let oid = repo.commit(Some("HEAD"),
|
|
&asmr_sig, &bot_sig,
|
|
&format!("Update to {version}"),
|
|
&tree, &[&parent_commit]
|
|
).unwrap();
|
|
let target = repo.find_object(oid, Some(ObjectType::Commit)).unwrap();
|
|
|
|
repo.tag(&version, &target, &bot_sig, "", false).unwrap();
|
|
|
|
let mut callback = RemoteCallbacks::new();
|
|
callback.credentials(|_, uname, _| {
|
|
git2::Cred::ssh_key(
|
|
uname.unwrap(),
|
|
None,
|
|
Path::new(&format!("{}/.ssh/id_rsa", env::var("HOME").unwrap())),
|
|
None,
|
|
)
|
|
});
|
|
let mut rem = repo.find_remote("origin").unwrap();
|
|
let mut conn = rem.connect_auth(Direction::Push, Some(callback), None).unwrap();
|
|
let remote = conn.remote();
|
|
remote.push(&["refs/heads/master:refs/heads/master"], None).unwrap();
|
|
}
|