Skip to content

Commit

Permalink
new: Add more token variables. (#1718)
Browse files Browse the repository at this point in the history
* Add graph context.

* Dont use vcs directly.

* Update expanders.

* Add new tokens.

* Add binstall version.

* Fix vcs context.
  • Loading branch information
milesj authored Nov 13, 2024
1 parent 3acf14b commit 3d77456
Show file tree
Hide file tree
Showing 21 changed files with 500 additions and 326 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
- Can now control the depth of upstream (dependencies) and downstream (dependents).
- Affected information now tracks based on dependent graph connections.
- Added `--upstream` and `--downstream` options to `moon query tasks`.
- Added 7 new token variables: `$arch`, `$os`, `$osFamily`, `$vcsBranch`, `$vcsRepository`,
`$vcsRevision`, `$workingDir`
- Added a `rust.binstallVersion` setting to `.moon/toolchain.yml`.

#### 🐞 Fixes

- Fixed `moon project-graph <id>` not including all dependencies/dependents. It was only showing direct relationships.
- Fixed `moon project-graph <id>` not including all dependencies/dependents. It was only showing
direct relationships.

## 1.29.4

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/config/src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pattern!(TOKEN_FUNC, "@([a-z]+)\\(([0-9A-Za-z_-]+)\\)");
pattern!(TOKEN_FUNC_DISTINCT, "^@([a-z]+)\\(([0-9A-Za-z_-]+)\\)$");
pattern!(
TOKEN_VAR,
"\\$(language|projectAlias|projectChannel|projectName|projectOwner|projectRoot|projectSource|projectStack|projectType|project|target|taskPlatform|taskType|task|workspaceRoot|timestamp|datetime|date|time)"
"\\$(arch|language|osFamily|os|projectAlias|projectChannel|projectName|projectOwner|projectRoot|projectSource|projectStack|projectType|project|target|taskPlatform|taskType|task|timestamp|datetime|date|time|vcsBranch|vcsRepository|vcsRevision|workingDir|workspaceRoot)"
);
pattern!(
TOKEN_VAR_DISTINCT,
"^\\$(language|projectAlias|projectChannel|projectName|projectOwner|projectRoot|projectSource|projectStack|projectType|project|target|taskPlatform|taskType|task|workspaceRoot|timestamp|datetime|date|time)$"
"^\\$(arch|language|osFamily|os|projectAlias|projectChannel|projectName|projectOwner|projectRoot|projectSource|projectStack|projectType|project|target|taskPlatform|taskType|task|timestamp|datetime|date|time|vcsBranch|vcsRepository|vcsRevision|workingDir|workspaceRoot)$"
);
6 changes: 5 additions & 1 deletion crates/config/src/toolchain/rust_config.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use super::bin_config::BinEntry;
use schematic::Config;
use semver::Version;
use version_spec::UnresolvedVersionSpec;
use warpgate_api::PluginLocator;

/// Configures and enables the Rust platform.
/// Docs: https://moonrepo.dev/docs/config/toolchain#rust
#[derive(Clone, Config, Debug)]
pub struct RustConfig {
/// List of binaries to install into the environment using `cargo install`.
/// List of binaries to install into the environment using `cargo binstall`.
#[setting(nested)]
pub bins: Vec<BinEntry>,

/// The version of `cargo-binstall` to install. Defaults to latest if not defined.
pub binstall_version: Option<Version>,

/// Rust components to automatically install.
pub components: Vec<String>,

Expand Down
19 changes: 19 additions & 0 deletions crates/graph-utils/src/graph_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::{path::PathBuf, sync::Arc};

#[derive(Clone, Default)]
pub struct GraphExpanderContext {
/// The current VCS branch.
pub vcs_branch: Arc<String>,

/// The VCS repository slug.
pub vcs_repository: Arc<String>,

/// The current VCS revision, commit, etc.
pub vcs_revision: Arc<String>,

/// The current working directory.
pub working_dir: PathBuf,

/// The workspace root.
pub workspace_root: PathBuf,
}
2 changes: 2 additions & 0 deletions crates/graph-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod graph_context;
mod graph_formats;
mod graph_traits;

pub use graph_context::*;
pub use graph_formats::*;
pub use graph_traits::*;
28 changes: 13 additions & 15 deletions crates/project-graph/src/project_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ impl ProjectMetadata {

#[derive(Default)]
pub struct ProjectGraph {
context: GraphExpanderContext,

/// Cache of file path lookups, mapped by starting path to project ID (as a string).
fs_cache: HashMap<PathBuf, Arc<String>>,

Expand All @@ -46,24 +48,21 @@ pub struct ProjectGraph {

/// Expanded projects, mapped by project ID.
projects: Arc<RwLock<ProjectsCache>>,

/// The current working directory.
pub working_dir: PathBuf,

/// Workspace root, required for expansion.
pub workspace_root: PathBuf,
}

impl ProjectGraph {
pub fn new(graph: ProjectGraphType, metadata: FxHashMap<Id, ProjectMetadata>) -> Self {
pub fn new(
graph: ProjectGraphType,
metadata: FxHashMap<Id, ProjectMetadata>,
context: GraphExpanderContext,
) -> Self {
debug!("Creating project graph");

Self {
context,
graph,
metadata,
projects: Arc::new(RwLock::new(FxHashMap::default())),
working_dir: PathBuf::new(),
workspace_root: PathBuf::new(),
fs_cache: HashMap::new(),
}
}
Expand Down Expand Up @@ -120,11 +119,11 @@ impl ProjectGraph {
/// This will attempt to find the closest matching project source.
#[instrument(name = "get_project_from_path", skip(self))]
pub fn get_from_path(&self, starting_file: Option<&Path>) -> miette::Result<Arc<Project>> {
let current_file = starting_file.unwrap_or(&self.working_dir);
let current_file = starting_file.unwrap_or(&self.context.working_dir);

let file = if current_file == self.workspace_root {
let file = if current_file == self.context.workspace_root {
Path::new(".")
} else if let Ok(rel_file) = current_file.strip_prefix(&self.workspace_root) {
} else if let Ok(rel_file) = current_file.strip_prefix(&self.context.workspace_root) {
rel_file
} else {
current_file
Expand Down Expand Up @@ -163,12 +162,11 @@ impl ProjectGraph {
}

Ok(Self {
context: self.context.clone(),
fs_cache: HashMap::new(),
graph,
metadata,
projects: self.projects.clone(),
working_dir: self.working_dir.clone(),
workspace_root: self.workspace_root.clone(),
})
}

Expand All @@ -183,7 +181,7 @@ impl ProjectGraph {

let expander = ProjectExpander::new(ProjectExpanderContext {
aliases: self.aliases(),
workspace_root: &self.workspace_root,
workspace_root: &self.context.workspace_root,
});

let project = Arc::new(expander.expand(self.get_unexpanded(&id)?)?);
Expand Down
1 change: 1 addition & 0 deletions crates/task-expander/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ publish = false
moon_args = { path = "../args" }
moon_common = { path = "../common" }
moon_config = { path = "../config" }
moon_graph_utils = { path = "../graph-utils" }
moon_project = { path = "../project" }
moon_task = { path = "../task" }
moon_task_args = { path = "../task-args" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
use moon_config::patterns;
use moon_project::Project;
use rustc_hash::FxHashMap;
use std::env;
use std::path::Path;
use tracing::debug;

pub struct TaskExpanderContext<'graph> {
/// The base unexpanded project.
pub project: &'graph Project,

/// Workspace root, of course.
pub workspace_root: &'graph Path,
}

pub fn substitute_env_vars(mut env: FxHashMap<String, String>) -> FxHashMap<String, String> {
let cloned_env = env.clone();

Expand Down
4 changes: 2 additions & 2 deletions crates/task-expander/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod expander_context;
mod expander_utils;
mod task_expander;
mod task_expander_error;
mod token_expander;
mod token_expander_error;

pub use expander_context::*;
pub use expander_utils::*;
pub use task_expander::*;
pub use task_expander_error::*;
pub use token_expander::*;
Expand Down
18 changes: 11 additions & 7 deletions crates/task-expander/src/task_expander.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
use crate::expander_context::*;
use crate::expander_utils::*;
use crate::task_expander_error::TasksExpanderError;
use crate::token_expander::TokenExpander;
use moon_common::color;
use moon_config::TaskArgs;
use moon_graph_utils::GraphExpanderContext;
use moon_project::Project;
use moon_task::Task;
use moon_task_args::parse_task_args;
use rustc_hash::FxHashMap;
use std::mem;
use tracing::{debug, instrument, trace, warn};

pub struct TaskExpander<'graph> {
// pub context: TaskExpanderContext<'graph>,
pub context: &'graph GraphExpanderContext,
pub token: TokenExpander<'graph>,
pub project: &'graph Project,
}

impl<'graph> TaskExpander<'graph> {
pub fn new(context: TaskExpanderContext<'graph>) -> Self {
pub fn new(project: &'graph Project, context: &'graph GraphExpanderContext) -> Self {
Self {
token: TokenExpander::new(context),
// context,
token: TokenExpander::new(project, context),
context,
project,
}
}

Expand Down Expand Up @@ -141,8 +145,8 @@ impl<'graph> TaskExpander<'graph> {
let env_paths = env_files
.iter()
.map(|file| {
file.to_workspace_relative(self.token.context.project.source.as_str())
.to_path(self.token.context.workspace_root)
file.to_workspace_relative(self.project.source.as_str())
.to_path(&self.context.workspace_root)
})
.collect::<Vec<_>>();

Expand Down
Loading

0 comments on commit 3d77456

Please sign in to comment.