Skip to content

Commit

Permalink
2024-02-23
Browse files Browse the repository at this point in the history
  • Loading branch information
luciano_bestia committed Feb 23, 2024
1 parent 18438c3 commit 456af01
Showing 1 changed file with 62 additions and 53 deletions.
115 changes: 62 additions & 53 deletions automation_tasks_rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
//! automation_tasks_rs for github_readme_copy
// automation_tasks_rs for github_readme_copy

use cargo_auto_lib::*;
use cargo_auto_github_lib::*;
// region: library with basic automation tasks
use cargo_auto_lib as cl;
// traits must be in scope (Rust strangeness)
use cl::CargoTomlPublicApiMethods;

// ANSI colors for Linux terminal
// https://github.com/shiena/ansicolor/blob/master/README.md
#[allow(dead_code)]
pub const RED: &str = "\x1b[31m";
#[allow(dead_code)]
pub const YELLOW: &str = "\x1b[33m";
#[allow(dead_code)]
pub const GREEN: &str = "\x1b[32m";
#[allow(dead_code)]
pub const RESET: &str = "\x1b[0m";
use cargo_auto_lib::GREEN;
use cargo_auto_lib::RED;
use cargo_auto_lib::RESET;
use cargo_auto_lib::YELLOW;

// region: library with basic automation tasks

fn main() {
exit_if_not_run_in_rust_project_root_directory();
cl::exit_if_not_run_in_rust_project_root_directory();

// get CLI arguments
let mut args = std::env::args();
Expand Down Expand Up @@ -78,7 +75,7 @@ fn print_help() {
This task needs PAT (personal access token from github) in the env variable:{RESET}
{GREEN} export GITHUB_TOKEN=paste_token_here{RESET}
{YELLOW}© 2023 bestia.dev MIT License github.com/bestia-dev/cargo-auto{RESET}
{YELLOW}© 2024 bestia.dev MIT License github.com/bestia-dev/cargo-auto{RESET}
"#
);
print_examples_cmd();
Expand Down Expand Up @@ -112,9 +109,9 @@ fn completion() {
/// cargo build
fn task_build() {
let cargo_toml = CargoToml::read();
auto_version_increment_semver_or_date();
run_shell_command("cargo fmt");
run_shell_command("cargo build");
cl::auto_version_increment_semver_or_date();
cl::run_shell_command("cargo fmt");
cl::run_shell_command("cargo build");
println!(
r#"
{YELLOW}Before connecting to GitHub, store once in env variable your personal token: export GITHUB_TOKEN=*****
Expand All @@ -138,13 +135,13 @@ package_name = cargo_toml.package_name(),
/// cargo build --release
fn task_release() {
let cargo_toml = CargoToml::read();
auto_version_increment_semver_or_date();
auto_cargo_toml_to_md();
auto_lines_of_code("");
cl::auto_version_increment_semver_or_date();
cl::auto_cargo_toml_to_md();
cl::auto_lines_of_code("");

run_shell_command("cargo fmt");
run_shell_command("cargo build --release");
run_shell_command(&format!(
cl::run_shell_command("cargo fmt");
cl::run_shell_command("cargo build --release");
cl::run_shell_command(&format!(
"strip target/release/{package_name}",
package_name = cargo_toml.package_name()
));
Expand All @@ -168,23 +165,25 @@ package_name = cargo_toml.package_name(),
print_examples_cmd();
}

/// cargo doc, then copies to /docs/ folder, because this is a GitHub standard folder
/// cargo doc, then copies to /docs/ folder, because this is a github standard folder
fn task_doc() {
let cargo_toml = CargoToml::read();
auto_cargo_toml_to_md();
auto_lines_of_code("");
auto_plantuml(&cargo_toml.package_repository().unwrap());
auto_md_to_doc_comments();

run_shell_command("cargo doc --no-deps --document-private-items");
// copy target/doc into docs/ because it is GitHub standard
run_shell_command("rsync -a --info=progress2 --delete-after target/doc/ docs/");
let cargo_toml = cl::CargoToml::read();
cl::auto_cargo_toml_to_md();
cl::auto_lines_of_code("");
cl::auto_plantuml(&cargo_toml.package_repository().unwrap());
cl::auto_md_to_doc_comments();

cl::run_shell_command("cargo doc --no-deps --document-private-items");
// copy target/doc into docs/ because it is github standard
cl::run_shell_command("rsync -a --info=progress2 --delete-after target/doc/ docs/");
// Create simple index.html file in docs directory
run_shell_command(&format!(
"echo \"<meta http-equiv=\\\"refresh\\\" content=\\\"0; url={}/index.html\\\" />\" > docs/index.html",
cargo_toml.package_name().replace("-","_")
cl::run_shell_command(&format!(
r#"echo "<meta http-equiv=\"refresh\" content=\"0; url={}/index.html\" />" > docs/index.html"#,
cargo_toml.package_name().replace("-", "_")
));
run_shell_command("cargo fmt");
// pretty html
cl::auto_doc_tidy_html().unwrap();
cl::run_shell_command("cargo fmt");
// message to help user with next move
println!(
r#"
Expand All @@ -196,9 +195,9 @@ fn task_doc() {

/// cargo test
fn task_test() {
run_shell_command("cargo test");
cl::run_shell_command("cargo test");
println!(
r#"
r#"
{YELLOW}After `cargo auto test`. If ok then {RESET}
{GREEN}cargo auto commit_and_push "message"{RESET}
{YELLOW}with mandatory commit message{RESET}
Expand All @@ -208,18 +207,28 @@ fn task_test() {

/// commit and push
fn task_commit_and_push(arg_2: Option<String>) {
match arg_2 {
None => println!("{RED}Error: Message for commit is mandatory.{RESET}"),
Some(message) => {
run_shell_command(&format!(r#"git add -A && git commit --allow-empty -m "{}""#, message));
run_shell_command("git push");
println!(
r#"
let Some(message) = arg_2 else {
eprintln!("{RED}Error: Message for commit is mandatory. Exiting.{RESET}");
// early exit
return;
};

// init repository if needed. If it is not init then normal commit and push.
if !cl::init_repository_if_needed(&message) {
// separate commit for docs if they changed, to not make a lot of noise in the real commit
if std::path::Path::new("docs").exists() {
cl::run_shell_command(r#"git add docs && git diff --staged --quiet || git commit -m "update docs" "#);
}
cl::add_message_to_unreleased(&message);
// the real commit of code
cl::run_shell_command(&format!( r#"git add -A && git diff --staged --quiet || git commit -m "{message}" "#));
cl::run_shell_command("git push");
println!(
r#"
{YELLOW}After `cargo auto commit_and_push "message"`{RESET}
{GREEN}cargo auto github_new_release{RESET}
{GREEN}cargo auto publish_to_crates_io{RESET}
"#
);
}
);
}
}

Expand All @@ -233,7 +242,7 @@ fn task_github_new_release() {
"git tag -f -a v{version} -m version_{version}",
version = cargo_toml.package_version()
);
run_shell_command(&shell_command);
cl::run_shell_command(&shell_command);

// async block inside sync code with tokio
use tokio::runtime::Runtime;
Expand All @@ -257,11 +266,11 @@ r#"## Changed

// compress files tar.gz
let tar_name = format!("{repo_name}-{tag_name_version}-x86_64-unknown-linux-gnu.tar.gz");
run_shell_command(&format!("tar -zcvf {tar_name} target/release/{repo_name}"));
cl::run_shell_command(&format!("tar -zcvf {tar_name} target/release/{repo_name}"));

// upload asset
auto_github_upload_asset_to_release(&owner, &repo_name, &release_id, &tar_name).await;
run_shell_command(&format!("rm {tar_name}"));
cl::run_shell_command(&format!("rm {tar_name}"));

println!(" {YELLOW}Asset uploaded. Open and edit the description on Github-Releases in the browser.{RESET}");
println!("{GREEN}https://github.com/{owner}/{repo_name}/releases{RESET}");
Expand Down

0 comments on commit 456af01

Please sign in to comment.