Skip to content

Commit

Permalink
new: Add --updateCache option. (#490)
Browse files Browse the repository at this point in the history
* Add new enum.

* Add display for enums.

* Update tests.

* Add cli args.

* Add blog post.

* Polish.

* Fix windows tests.
  • Loading branch information
milesj committed Dec 19, 2022
1 parent 3eafb1b commit 6d68562
Show file tree
Hide file tree
Showing 42 changed files with 431 additions and 155 deletions.
17 changes: 2 additions & 15 deletions .github/workflows/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,9 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/cache@v3
name: Cache node modules
- uses: actions/setup-node@v3
with:
path: |
~/.yarn
.yarn/install-state.gz
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: ${{ runner.os }}-yarn-
- uses: actions/cache@v3
name: Cache moon toolchain
with:
path: ~/.moon/tools
key:
${{ runner.os }}-moon-node@${{ matrix.node-version }}-${{
hashFiles('.moon/toolchain.yml') }}
restore-keys: ${{ runner.os }}-moon-node@${{ matrix.node-version }}-
cache: yarn
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.65.0
Expand Down
26 changes: 21 additions & 5 deletions crates/cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,27 @@ pub enum Commands {
#[command(
name = "check",
about = "Run all build and test related tasks for the current project.",
alias = "c"
alias = "c",
rename_all = "camelCase"
)]
Check {
#[arg(help = "List of project IDs to explicitly check")]
#[clap(group = "projects")]
ids: Vec<ProjectID>,

#[arg(long, help = "Generate a run report for the current actions")]
report: bool,

#[arg(long, help = "Run check for all projects in the workspace")]
#[clap(group = "projects")]
all: bool,

#[arg(long, help = "Generate a run report for the current actions")]
report: bool,

#[arg(
long,
short = 'u',
help = "Bypass cache and force update any existing items"
)]
update_cache: bool,
},

// moon ci
Expand All @@ -317,7 +325,8 @@ pub enum Commands {
#[command(
name = "run",
about = "Run one or many project tasks and their dependent tasks.",
alias = "r"
alias = "r",
rename_all = "camelCase"
)]
Run {
#[arg(required = true, help = "List of targets (project:task) to run")]
Expand All @@ -329,6 +338,13 @@ pub enum Commands {
)]
dependents: bool,

#[arg(
long,
short = 'u',
help = "Bypass cache and force update any existing items"
)]
update_cache: bool,

// Debugging
#[arg(
value_enum,
Expand Down
4 changes: 3 additions & 1 deletion crates/cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use moon_project::Project;
use std::env;

pub struct CheckOptions {
pub report: bool,
pub all: bool,
pub report: bool,
pub update_cache: bool,
}

const LOG_TARGET: &str = "moon:check";
Expand Down Expand Up @@ -54,6 +55,7 @@ pub async fn check(project_ids: &Vec<String>, options: CheckOptions) -> Result<(
&targets,
RunOptions {
report: options.report,
update_cache: options.update_cache,
..RunOptions::default()
},
workspace,
Expand Down
11 changes: 9 additions & 2 deletions crates/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::enums::TouchedStatus;
use crate::enums::{CacheMode, TouchedStatus};
use crate::helpers::AnyError;
use crate::queries::touched_files::{query_touched_files, QueryTouchedFilesOptions};
use moon::{build_dep_graph, generate_project_graph, load_workspace};
Expand All @@ -9,6 +9,7 @@ use moon_runner_context::{ProfileType, RunnerContext};
use moon_utils::is_ci;
use moon_workspace::Workspace;
use rustc_hash::{FxHashMap, FxHashSet};
use std::env;
use std::string::ToString;

#[derive(Default)]
Expand All @@ -19,6 +20,7 @@ pub struct RunOptions {
pub passthrough: Vec<String>,
pub profile: Option<ProfileType>,
pub report: bool,
pub update_cache: bool,
pub upstream: bool,
}

Expand All @@ -36,6 +38,11 @@ pub async fn run_target(
workspace: Workspace,
project_graph: ProjectGraph,
) -> Result<(), AnyError> {
// Force cache to update using write-only mode
if options.update_cache {
env::set_var("MOON_CACHE", CacheMode::Write.to_string());
}

// Always query for a touched files list as it'll be used by many actions
let touched_files = if options.affected || workspace.vcs.is_enabled() {
query_touched_files(
Expand Down Expand Up @@ -74,7 +81,7 @@ pub async fn run_target(
println!(
"Target(s) {} not affected by touched files (using status {})",
targets_list,
color::symbol(&options.status.to_string().to_lowercase())
color::symbol(&options.status.to_string())
);
}
} else {
Expand Down
67 changes: 61 additions & 6 deletions crates/cli/src/enums.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use strum::Display;
use std::fmt::{Display, Error, Formatter};

#[derive(ValueEnum, Clone, Debug, Default, Display)]
#[derive(ValueEnum, Clone, Debug, Default)]
pub enum CacheMode {
Off,
Read,
#[default]
ReadWrite,
Write,
}

#[derive(ValueEnum, Clone, Debug, Default, Display)]
impl Display for CacheMode {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(
f,
"{}",
match self {
CacheMode::Off => "off",
CacheMode::Read => "read",
CacheMode::ReadWrite => "read-write",
CacheMode::Write => "write",
}
)?;

Ok(())
}
}

#[derive(ValueEnum, Clone, Debug, Default)]
pub enum LogLevel {
Off,
Error,
Expand All @@ -21,9 +39,26 @@ pub enum LogLevel {
Trace,
}

#[derive(
ValueEnum, Clone, Copy, Debug, Deserialize, Display, Default, Eq, PartialEq, Serialize,
)]
impl Display for LogLevel {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(
f,
"{}",
match self {
LogLevel::Off => "off",
LogLevel::Error => "error",
LogLevel::Warn => "warn",
LogLevel::Info => "info",
LogLevel::Debug => "debug",
LogLevel::Trace => "trace",
}
)?;

Ok(())
}
}

#[derive(ValueEnum, Clone, Copy, Debug, Deserialize, Default, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum TouchedStatus {
Added,
Expand All @@ -35,3 +70,23 @@ pub enum TouchedStatus {
Unstaged,
Untracked,
}

impl Display for TouchedStatus {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(
f,
"{}",
match self {
TouchedStatus::Added => "added",
TouchedStatus::All => "all",
TouchedStatus::Deleted => "deleted",
TouchedStatus::Modified => "modified",
TouchedStatus::Staged => "staged",
TouchedStatus::Unstaged => "unstaged",
TouchedStatus::Untracked => "untracked",
}
)?;

Ok(())
}
}
16 changes: 12 additions & 4 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ pub async fn run_cli() {

// Setup logging
if env::var("MOON_LOG").is_err() {
env::set_var("MOON_LOG", args.log.to_string().to_lowercase());
env::set_var("MOON_LOG", args.log.to_string());
}

Logger::init(map_log_level(args.log), args.log_file);

// Setup caching
if env::var("MOON_CACHE").is_err() {
env::set_var("MOON_CACHE", args.cache.to_string().to_lowercase());
env::set_var("MOON_CACHE", args.cache.to_string());
}

// Match and run subcommand
Expand All @@ -79,12 +79,18 @@ pub async fn run_cli() {
})
.await
}
Commands::Check { ids, report, all } => {
Commands::Check {
ids,
all,
report,
update_cache,
} => {
check(
ids,
CheckOptions {
report: *report,
all: *all,
report: *report,
update_cache: *update_cache,
},
)
.await
Expand Down Expand Up @@ -195,6 +201,7 @@ pub async fn run_cli() {
targets,
affected,
dependents,
update_cache,
status,
passthrough,
profile,
Expand All @@ -210,6 +217,7 @@ pub async fn run_cli() {
passthrough: passthrough.clone(),
profile: profile.clone(),
report: *report,
update_cache: *update_cache,
upstream: *upstream,
},
)
Expand Down
Loading

0 comments on commit 6d68562

Please sign in to comment.