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 27baced commit 0da7c22
Showing 1 changed file with 66 additions and 56 deletions.
122 changes: 66 additions & 56 deletions automation_tasks_rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
//! automation_tasks_rs for cargo_crev_web
// automation_tasks_rs for cargo_crev_web

use cargo_auto_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 @@ -75,7 +73,7 @@ fn print_help() {
{YELLOW}(If you use SSH, it is easy to start the ssh-agent in the background and ssh-add your credentials for git.){RESET}
{GREEN}cargo auto publish_to_web - publish to web, git tag{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 @@ -116,10 +114,10 @@ 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");
run_shell_command("cp target/debug/cargo_crev_web ./web_server_folder");
cl::auto_version_increment_semver_or_date();
cl::run_shell_command("cargo fmt");
cl::run_shell_command("cargo build");
cl::run_shell_command("cp target/debug/cargo_crev_web ./web_server_folder");
println!(
r#"
{YELLOW}After `cargo auto build`, run the compiled binary, examples and/or tests{RESET}
Expand All @@ -138,17 +136,17 @@ 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()
));
run_shell_command("cp target/release/cargo_crev_web ./web_server_folder");
cl::run_shell_command("cp target/release/cargo_crev_web ./web_server_folder");
println!(
r#"
{YELLOW}After `cargo auto release`, run the compiled binary, examples and/or tests{RESET}
Expand All @@ -166,35 +164,37 @@ package_name = cargo_toml.package_name(),

/// 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();
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();

run_shell_command("cargo doc --no-deps --document-private-items");
cl::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/");
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#"
{YELLOW}After `cargo auto doc`, check `docs/index.html`. If ok then test the documentation code examples{RESET}
{GREEN}cargo auto test{RESET}
"#
"#
);
}

/// 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 @@ -204,18 +204,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 publish_to_web{RESET}
{GREEN}cargo auto publish_to_crates_io{RESET}
"#
);
}
);
}
}

Expand All @@ -229,31 +239,31 @@ fn task_publish_to_web() {
"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);

println!(r#" {YELLOW}1. upload sh scripts {RESET}"#);
let project_folder_to_publish = format!(r#"~/rustprojects/{package_name}/var_www_scripts/{package_name}/"#);
let ssh_user_and_server = "[email protected]";
let ssh_key_file = "/home/rustdevuser/.ssh/bestia_dev_ssh_1";
let remote_temp_folder = format!(r#"/tmp/bestia-dev/{package_name}/"# );
let web_folder_over_ssh = format!(r#"{ssh_user_and_server}:{remote_temp_folder}"# );
run_shell_command(&format!(r#"ssh -i {ssh_key_file} {ssh_user_and_server} mkdir -p {remote_temp_folder}"#));
run_shell_command(&format!(r#"rsync -e ssh -a --info=progress2 --delete-after {project_folder_to_publish} {web_folder_over_ssh}"#));
run_shell_command(&format!(r#"ssh -i {ssh_key_file} {ssh_user_and_server} chmod a+rx {remote_temp_folder}/cargo_crev_web_publish.sh"#));
cl::run_shell_command(&format!(r#"ssh -i {ssh_key_file} {ssh_user_and_server} mkdir -p {remote_temp_folder}"#));
cl::run_shell_command(&format!(r#"rsync -e ssh -a --info=progress2 --delete-after {project_folder_to_publish} {web_folder_over_ssh}"#));
cl::run_shell_command(&format!(r#"ssh -i {ssh_key_file} {ssh_user_and_server} chmod a+rx {remote_temp_folder}/cargo_crev_web_publish.sh"#));

// 2. rsync files from the Rust project to uploaded_web_server_folder on the remote server.
println!(r#" {YELLOW}2. upload files to uploaded_web_server_folder on the remote {RESET}"#);
let project_folder_to_publish = format!(r#"~/rustprojects/{package_name}/web_server_folder/"#);
let remote_temp_folder = format!(r#"/tmp/bestia-dev/{package_name}/uploaded_web_server_folder/"# );
let web_folder_over_ssh = format!(r#"{ssh_user_and_server}:{remote_temp_folder}"#);
run_shell_command(&format!(r#"ssh -i {ssh_key_file} {ssh_user_and_server} mkdir -p {remote_temp_folder}"#));
run_shell_command(&format!(r#"rsync -e ssh -a --info=progress2 --delete-after {project_folder_to_publish} {web_folder_over_ssh}"#));
cl::run_shell_command(&format!(r#"ssh -i {ssh_key_file} {ssh_user_and_server} mkdir -p {remote_temp_folder}"#));
cl::run_shell_command(&format!(r#"rsync -e ssh -a --info=progress2 --delete-after {project_folder_to_publish} {web_folder_over_ssh}"#));

// 3. run the publishing script that will stop the server, copy the transferred files and restart the server
// this script will --exclude 'blocklisted_repos.json' when publishing to the web server.
println!(r#" {YELLOW}3. run the publishing script on the remote{RESET}"#);
let script_for_publishing_on_remote = format!(r#"/tmp/bestia-dev/{package_name}/{package_name}_publish.sh"#);
run_shell_command(&format!(r#"ssh -tt -i {ssh_key_file} {ssh_user_and_server} {script_for_publishing_on_remote}"#));
cl::run_shell_command(&format!(r#"ssh -tt -i {ssh_key_file} {ssh_user_and_server} {script_for_publishing_on_remote}"#));

let web_app_url = format!(r#"https://web.crev.dev/rust-reviews/"#);
println!(
Expand Down

0 comments on commit 0da7c22

Please sign in to comment.