Skip to content

Commit

Permalink
Merge pull request #1178 from sagiegurari/0.37.23
Browse files Browse the repository at this point in the history
0.37.23
  • Loading branch information
sagiegurari authored Oct 19, 2024
2 parents 9287fc2 + 257e9bb commit 2ba3955
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## CHANGELOG

### v0.37.23

* Enhancement: support relative keyword for makefile extending to enable easy git/crate/workspace root extending #1175
* Maintenance: critical bug fix upgrade for duckscript

### v0.37.22 (2024-10-12)

* Fix: set env CARGO_MAKE_CURRENT_TASK_NAME before condition invocation #1173
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ colored = "^2"
ctrlc = "^3"
dirs-next = "^2"
duckscript = "^0.10"
duckscriptsdk = { version = "^0.11", default-features = false }
duckscriptsdk = { version = "^0.11.1", default-features = false }
envmnt = "^0.10.4"
fern = "^0.6"
fsio = { version = "^0.4", features = ["temp-path"] }
Expand Down
21 changes: 20 additions & 1 deletion docs/_includes/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -1031,9 +1031,28 @@ All will be loaded in the order you define.<br>
For example:

```toml
extend = [ { path = "must_have_makefile.toml" }, { path = "optional_makefile.toml", optional = true }, { path = "another_must_have_makefile.toml" } ]
extend = [
{ path = "alias.toml" },
{ path = "optional_makefile.toml", optional = true },
{ path = "cwd.toml" },
]
```

You can also change the relative path from the current makefile location to the git root folder, crate root folder or workspace root folder by adding the relative keyword as follows:

```toml
extend = { path = "./examples/python.toml", relative = "crate" }
```

Where relative can have the following values:

* git - For nearest (up) .git folder location.
* crate - For crate root (based on first Cargo.toml file)
* workspace - For workspace root (based on second top Cargo.toml file)

Any other value defaults to the current makefile location.<br>
Important to mention, all paths are relative from the currently parsed makefile.

<a name="usage-workspace-extend"></a>
#### Automatically Extend Workspace Makefile
When running cargo make for modules which are part of a workspace, you can automatically have the member crates makefile (even if doesn't exist) extend the workspace level makefile.
Expand Down
9 changes: 9 additions & 0 deletions examples/extends_list.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ extend = [
{ path = "alias.toml" },
{ path = "optional_makefile.toml", optional = true },
{ path = "cwd.toml" },
{ path = "cwd.toml", relative = "bad" },
{ path = "./examples/python.toml", relative = "crate" },
{ path = "./examples/javascript.toml", relative = "git" },
]

[config]
skip_core_tasks = true
skip_git_env_info = true
skip_rust_env_info = true
skip_crate_env_info = true

[tasks.extended]
alias = "D2"
4 changes: 4 additions & 0 deletions examples/workspace/common.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

[tasks.common]
command = "echo"
args = ["common"]
2 changes: 2 additions & 0 deletions examples/workspace/member2/Makefile.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

extend = { path = "./common.toml", relative = "workspace" }

[tasks.echo]
script = ["echo hello from member2"]
91 changes: 78 additions & 13 deletions src/lib/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ pub(crate) mod descriptor_deserializer;
mod env;
mod makefiles;

use std::path::{Path, PathBuf};

use fsio::path::as_path::AsPath;
use fsio::path::from_path::FromPath;
use indexmap::IndexMap;

use crate::descriptor::env::{merge_env, merge_env_files, merge_env_scripts};
use crate::environment;
use crate::error::CargoMakeError;
use crate::plugin::descriptor::merge_plugins_config;
use crate::types::{
Config, ConfigSection, EnvFile, EnvFileInfo, EnvValue, Extend, ExternalConfig, ModifyConfig,
Task,
};
use crate::{io, scriptengine, version};
use fsio::path::as_path::AsPath;
use fsio::path::from_path::FromPath;
use indexmap::IndexMap;
use std::path::{Path, PathBuf};

#[derive(Debug)]
enum RelativeTo {
Makefile,
GitRoot,
CrateRoot,
WorkspaceRoot,
}

fn merge_tasks(
base: &mut IndexMap<String, Task>,
Expand Down Expand Up @@ -249,10 +256,29 @@ fn load_descriptor_extended_makefiles(
extend_struct: &Extend,
) -> Result<ExternalConfig, CargoMakeError> {
match extend_struct {
Extend::Path(base_file) => load_external_descriptor(parent_path, &base_file, true, false),
Extend::Path(base_file) => {
load_external_descriptor(parent_path, &base_file, true, false, RelativeTo::Makefile)
}
Extend::Options(extend_options) => {
let force = !extend_options.optional.unwrap_or(false);
load_external_descriptor(parent_path, &extend_options.path, force, false)
let relative_to_str = extend_options
.relative
.clone()
.unwrap_or("makefile".to_string());
let relative_to = match relative_to_str.as_str() {
"git" => RelativeTo::GitRoot,
"crate" => RelativeTo::CrateRoot,
"workspace" => RelativeTo::WorkspaceRoot,
"makefile" => RelativeTo::Makefile,
_ => {
warn!(
"Unknown relative-to value: {}, defaulting to makefile",
&relative_to_str
);
RelativeTo::Makefile
}
};
load_external_descriptor(parent_path, &extend_options.path, force, false, relative_to)
}
Extend::List(extend_list) => {
let mut ordered_list_config = ExternalConfig::new();
Expand Down Expand Up @@ -302,13 +328,50 @@ fn load_external_descriptor(
file_name: &str,
force: bool,
set_env: bool,
relative_to: RelativeTo,
) -> Result<ExternalConfig, CargoMakeError> {
debug!(
"Loading tasks from file: {} base directory: {}",
&file_name, &base_path
"Loading tasks from file: {} base directory: {}, relative to: {:#?}",
&file_name, &base_path, &relative_to,
);

let file_path = Path::new(base_path).join(file_name);
let descriptor_dir = match relative_to {
RelativeTo::Makefile => base_path.to_string(),
RelativeTo::GitRoot => {
let git_root = environment::find_git_root(&PathBuf::from(base_path));
debug!("git root: {:#?}", &git_root);
match git_root {
Some(git_root_dir) => git_root_dir.clone(),
None => base_path.to_string(),
}
}
RelativeTo::CrateRoot => {
let project_root = environment::get_project_root_for_path(&PathBuf::from(base_path));
debug!("project root: {:#?}", &project_root);
match project_root {
Some(crate_dir) => crate_dir.clone(),
None => base_path.to_string(),
}
}
RelativeTo::WorkspaceRoot => {
let base_path_buf = PathBuf::from(base_path);
let project_root = environment::get_project_root_for_path(&base_path_buf);
debug!("project root: {:#?}", &project_root);
match project_root {
Some(crate_dir) => {
let crate_parent_path = PathBuf::from(&crate_dir).join("..");
let workspace_root = environment::get_project_root_for_path(&crate_parent_path);
debug!("workspace root: {:#?}", &workspace_root);
match workspace_root {
Some(workspace_dir) => workspace_dir.clone(),
None => crate_dir.clone(),
}
}
None => base_path.to_string(),
}
}
};
let file_path = Path::new(&descriptor_dir).join(file_name);

if file_path.exists() && file_path.is_file() {
let file_path_string: String = FromPath::from_path(&file_path);
Expand All @@ -332,7 +395,7 @@ fn load_external_descriptor(

match file_config.extend {
Some(ref extend_struct) => {
let parent_path_buf = Path::new(base_path).join(file_name).join("..");
let parent_path_buf = Path::new(&file_path_string).join("..");
let parent_path = file_path
.parent()
.unwrap_or(&parent_path_buf)
Expand Down Expand Up @@ -515,7 +578,8 @@ fn load_descriptors(
) -> Result<Config, CargoMakeError> {
let default_config = load_internal_descriptors(stable, experimental, modify_core_tasks)?;

let mut external_config = load_external_descriptor(".", file_name, force, true)?;
let mut external_config =
load_external_descriptor(".", file_name, force, true, RelativeTo::Makefile)?;

external_config = match std::env::var("CARGO_MAKE_WORKSPACE_MAKEFILE") {
Ok(workspace_makefile) => {
Expand All @@ -532,6 +596,7 @@ fn load_descriptors(
workspace_file_name_str,
false,
false,
RelativeTo::Makefile,
)?;
merge_external_configs(external_config, workspace_config)?
}
Expand Down
Loading

0 comments on commit 2ba3955

Please sign in to comment.