Skip to content

Commit

Permalink
0.1.0-alpha.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason5Lee committed Apr 24, 2022
1 parent 71f85b4 commit e228418
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 54 deletions.
8 changes: 8 additions & 0 deletions .idea/.idea.gradle-util-rs.dir/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions .idea/.idea.gradle-util-rs.dir/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 52 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gradle-util-rs"
version = "0.1.0-alpha.2"
version = "0.1.0-alpha.3"
edition = "2021"
description = "Gradle util written in Rust"
repository = "https://github.com/jason5lee/gradle-util-rs"
Expand All @@ -14,6 +14,8 @@ notify = "4.0"
env_logger = "0.9"
clap = { version = "3.0", features = ["derive"] }
humantime = "2.1"
termcolor = "1.1"
chrono = "0.4"

[[bin]]
name = "gur"
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Note that this project is still in the alpha stage. The functionalities and beha

## Install

You can find the pre-built binaries at the [release page](https://github.com/jason5lee/gradle-util-rs/releases). You can build and install it via `cargo install gradle-util-rs --version 0.1.0-alpha.2`.
You can find the pre-built binaries at the [release page](https://github.com/jason5lee/gradle-util-rs/releases). You can build and install it via `cargo install gradle-util-rs --version 0.1.0-alpha.3`.

## Usage

Expand All @@ -30,7 +30,7 @@ This is made as a workaround of [IDEA-177325](https://youtrack.jetbrains.com/iss

Example:

`gur set-new 7.3.3 --watch-dir path1 --watch-dir path2` : watch the gradle project creation under `path1` and `path2` recursively, and create the gradle wrapper properties for the new projects using gradle version `7.3.3`.
`gur set-new 7.3.3 path1 path2` : watch the gradle project creation under `path1` and `path2` recursively, and create the gradle wrapper properties for the new projects using gradle version `7.3.3`.

### `chver`

Expand All @@ -48,6 +48,7 @@ You might be surprised that a Gradle utility is written in Rust instead of Java
The major reason is that Gradle already requires a java instance to run. I don't want yet another java process.
Instead, just keep it as light as possible.

The reason I choose Rust instead of C/C++ or any others is that its mental model is surprisingly closed to Kotlin, the
The reason I choose Rust is that its mental model is surprisingly closed to Kotlin, the
major language I used with Gradle. Many building blocks in Kotlin like data class, sealed class and nullable type have their
corresponding in Rust like struct, enum and option.

17 changes: 8 additions & 9 deletions src/bin/gur.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::{Parser, Subcommand};
use gradle_util_rs::LoggedSideEffect;
use std::path::PathBuf;
use std::time::Duration;

#[derive(Parser)]
Expand All @@ -13,14 +14,17 @@ struct Cli {
enum Command {
#[clap(about = "Watch for the new Gradle project and set the gradle version")]
SetNew {
#[clap(help = "The gradle wrapper version to be set for the new projects.")]
#[clap(
required = true,
help = "The gradle wrapper version to be set for the new projects."
)]
version: String,
#[clap(
long,
required = true,
parse(from_os_str),
help = "Directories to be watched recursively for the new projects. You can have multiple watched directories."
)]
watch_dir: Vec<String>,
watch_dir: Vec<PathBuf>,
#[clap(long, default_value = "1s", parse(try_from_str = humantime::parse_duration), help = "Duration of file watching delay. Default to 1 second.")]
watch_duration: Duration,
},
Expand All @@ -31,18 +35,13 @@ enum Command {
version: String,
#[clap(
long,
help = "Enable the yolo mode. It will change the gradle-wrapper.properties file before running the wrapper task. With this flag, the gradle distribution of the old version won't be downloaded. But it may have potential problems."
help = "Enable the yolo mode. It will change the gradle-wrapper.properties file before running the wrapper task. With this flag, the gradle distribution of the old version won't be downloaded. But it may not work as expected."
)]
yolo: bool,
},
}

fn main() {
env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
);
log::debug!("DEBUG ON");

let cli = Cli::parse();

match cli.command {
Expand Down
10 changes: 5 additions & 5 deletions src/chver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{utils, Logged};
use crate::{log_error, utils, Logged};

#[cfg(windows)]
const GRADLEW: &str = "./gradlew.bat";
Expand All @@ -16,11 +16,11 @@ fn run_gradlew_wrapper(ver: &str) -> Result<(), Logged> {
std::process::Command::new(GRADLEW)
.args(&["wrapper", "--gradle-version", ver])
.spawn()
.map_err(|err| log_error!("Failed to run `gradlew`. {}.", err))
.map_err(|err| log_error(format_args!("failed to run `gradlew`, {}", err)))
.and_then(|mut child| {
child
.wait()
.map_err(|err| log_error!("Failed to run `gradlew`. {}.", err))
.map_err(|err| log_error(format_args!("failed to run `gradlew`, {}", err)))
.and_then(|status| {
if !status.success() {
Err(handle_gradlew_status(status))
Expand All @@ -33,7 +33,7 @@ fn run_gradlew_wrapper(ver: &str) -> Result<(), Logged> {

fn handle_gradlew_status(status: std::process::ExitStatus) -> Logged {
match status.code() {
Some(code) => log_error!("`gradlew` exited with status code: {}.", code),
None => log_error!("`gradlew` terminated by signal."),
Some(code) => log_error(format_args!("`gradlew` exited with status code: {}", code)),
None => log_error(format_args!("`gradlew` terminated by signal.")),
}
}
52 changes: 33 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
use std::io::Write;
use termcolor::WriteColor;

#[derive(Clone, Copy)]
pub struct Logged;

macro_rules! log_error {
(target: $target:expr, $($arg:tt)+) => ({
log::log!(target: $target, log::Level::Error, $($arg)+);
Logged
});
($($arg:tt)+) => ({
log::log!(log::Level::Error, $($arg)+);
Logged
});
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
log::log!(target: $target, $lvl, $($arg)+);
Logged
});
($lvl:expr, $($arg:tt)+) => ({
log::log!($lvl, $($arg)+);
Logged
})
}

pub trait LoggedSideEffect {
fn ignore_logged_error(self);
}
impl LoggedSideEffect for Result<(), Logged> {
fn ignore_logged_error(self) {}
}

pub fn log_error(err: std::fmt::Arguments) -> Logged {
let stderr = termcolor::StandardStream::stderr(termcolor::ColorChoice::Auto);
let mut stderr = stderr.lock();
let _ = stderr.set_color(termcolor::ColorSpec::new().set_fg(Some(termcolor::Color::Red)));
let _ = stderr.write(b"error");
let _ = stderr.reset();
let _ = writeln!(stderr, ": {}", err);
Logged
}

pub fn log_error_with_timestamp(err: std::fmt::Arguments) -> Logged {
let stderr = termcolor::StandardStream::stderr(termcolor::ColorChoice::Auto);
let mut stderr = stderr.lock();
let _ = write!(stderr, "[{}]", chrono::Local::now());
let _ = stderr.set_color(termcolor::ColorSpec::new().set_fg(Some(termcolor::Color::Red)));
let _ = stderr.write(b"error");
let _ = stderr.reset();
let _ = writeln!(stderr, ": {}", err);
Logged
}

pub fn log_warn(warn: std::fmt::Arguments) {
let stderr = termcolor::StandardStream::stderr(termcolor::ColorChoice::Auto);
let mut stderr = stderr.lock();
let _ = stderr.set_color(termcolor::ColorSpec::new().set_fg(Some(termcolor::Color::Yellow)));
let _ = stderr.write(b"warning");
let _ = stderr.reset();
let _ = writeln!(stderr, ": {}", warn);
}

pub mod chver;
pub mod set_new;
pub(crate) mod utils;
Loading

0 comments on commit e228418

Please sign in to comment.