Skip to content

Commit

Permalink
new: Use a PATH based approach for task execution. (#1208)
Browse files Browse the repository at this point in the history
* new: Rework `moon init` command. (#1204)

* Minor polish.

* Update init/node.

* Update ts/rust.

* Added bun.

* Update changelog.

* Update error.

* Final pass.

* Fix tests.

* Polish rust.

* Add paths.

* Add new methods.

* Update deno.

* Update bun.

* Update deno.

* Update rust.

* Update node.

* Add target paths.

* Add env vars.

* Update bin.

* Rework vars.

* Avoid latest version.

* Fix tests.

* Update docs.

* Install proto.

* Polish consts.

* Update lookup paths.

* Always use a shell.

* Remove shell from vcs.

* Copy bin.

* Polish.

* Fix workspace.

* Fixes.

* Disable proto for tests.

* Only run on linux.

* Start on blog post.

* Fix windows shell.

* Fix tests.

* More fixes.

* More fixes.

* Fix format.

* Update docs.

* Fallback to a version.

* Remove snapshot.

* Update proto config.
  • Loading branch information
milesj authored Dec 9, 2023
1 parent 0b7da73 commit f7bd6b1
Show file tree
Hide file tree
Showing 76 changed files with 893 additions and 1,461 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: PR
on:
pull_request:
env:
MOON_DEBUG: 'true'
PROTO_DEBUG: 'true'
# env:
# MOON_DEBUG: 'true'
# PROTO_DEBUG: 'true'
jobs:
version:
name: Version check
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ jobs:
bins: cargo-make, cargo-nextest, cargo-llvm-cov
cache-base: '^(master|develop-)'
components: llvm-tools-preview
# Required for "global bins" tests
- uses: moonrepo/setup-toolchain@v0
if: ${{ runner.os == 'Linux' }}
- run: proto install bun 1.0.0 --pin
if: ${{ runner.os == 'Linux' }}
with:
auto-install: true
proto-version: '0.24.2' # Keep in sync
- uses: mozilla-actions/[email protected]
- name: Run tests
if: ${{ env.WITH_COVERAGE == 'false' }}
Expand Down
2 changes: 1 addition & 1 deletion .moon/workspace.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Trigger CI: 23
# Trigger CI: 24

$schema: '../website/static/schemas/workspace.json'

Expand Down
4 changes: 4 additions & 0 deletions .prototools
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These are used by tests that require the tools
# to exist in the environment!
bun = "1.0.15"
node = "20.8.0"
21 changes: 12 additions & 9 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pathdiff = "0.2.1"
petgraph = { version = "0.6.4", default-features = false, features = [
"serde-1",
] }
proto_core = "0.24.2"
proto_core = "0.24.4"
relative-path = { version = "1.9.0", features = ["serde"] }
regex = "1.10.2"
reqwest = { version = "0.11.22", default-features = false, features = [
Expand All @@ -60,7 +60,7 @@ serde = { version = "1.0.193", features = ["derive", "rc"] }
serde_json = "1.0.108"
serde_yaml = "0.9.27"
starbase = "0.2.10"
starbase_archive = { version = "0.2.4", default-features = false, features = [
starbase_archive = { version = "0.2.5", default-features = false, features = [
"tar-gz",
] }
starbase_events = { version = "0.2.2" }
Expand Down
110 changes: 2 additions & 108 deletions crates/bun/platform/src/actions/run_target.rs
Original file line number Diff line number Diff line change
@@ -1,118 +1,12 @@
use crate::target_hash::BunTargetHash;
use moon_bun_tool::BunTool;
use moon_config::{HasherConfig, HasherOptimization};
use moon_node_lang::{
node::{self, BinFile},
PackageJson,
};
use moon_process::Command;
use moon_node_lang::PackageJson;
use moon_project::Project;
use moon_task::Task;
use moon_tool::{prepend_path_env_var, DependencyManager, Tool, ToolError};
use moon_utils::path;
use moon_tool::DependencyManager;
use rustc_hash::FxHashMap;
use std::path::Path;

fn find_package_bin(
command: &mut Command,
starting_dir: &Path,
working_dir: &Path,
bin_name: &str,
) -> miette::Result<Option<Command>> {
let possible_bin_path = match node::find_package_bin(starting_dir, bin_name)? {
Some(bin) => bin,
None => {
// moon isn't installed as a node module, but probably
// exists globally, so let's go with that instead of failing
if bin_name == "moon" {
return Ok(Some(Command::new(bin_name)));
}

return Err(ToolError::MissingBinary("node module".into(), bin_name.to_owned()).into());
}
};

match possible_bin_path {
// Rust, Go
BinFile::Binary(bin_path) => {
return Ok(Some(Command::new(bin_path)));
}
// JavaScript
BinFile::Script(bin_path) => {
command.arg(path::to_string(
path::relative_from(bin_path, working_dir).unwrap(),
)?);
}
// Other (Bash)
BinFile::Other(bin_path, parent_cmd) => {
let mut cmd = Command::new(parent_cmd);
cmd.arg(bin_path);

return Ok(Some(cmd));
}
};

Ok(None)
}

pub fn create_target_command(
bun: &BunTool,
project: &Project,
task: &Task,
working_dir: &Path,
) -> miette::Result<Command> {
let mut command = Command::new(bun.get_bin_path()?);

match task.command.as_str() {
"bun" | "bunx" => {
if task.command == "bunx" {
command.arg("x");
}
}
bin => {
if let Some(new_command) =
find_package_bin(&mut command, &project.root, working_dir, bin)?
{
command = new_command;
}
}
};

if !bun.global {
command.env(
"PATH",
prepend_path_env_var([bun.tool.get_exe_path()?.parent().unwrap()]),
);
}

command.args(&task.args).envs(&task.env);

Ok(command)
}

// This is like the function above, but is for situations where the tool
// has not been configured, and should default to the global "bun" found
// in the user's shell.
pub fn create_target_command_without_tool(
project: &Project,
task: &Task,
working_dir: &Path,
) -> miette::Result<Command> {
let mut command = Command::new(&task.command);

if task.command != "bun" && task.command != "bunx" {
if let Some(new_command) =
find_package_bin(&mut command, &project.root, working_dir, &task.command)?
{
command = new_command;
}
}

command.args(&task.args).envs(&task.env);

Ok(command)
}

pub async fn create_target_hasher(
bun: Option<&BunTool>,
project: &Project,
Expand Down
57 changes: 41 additions & 16 deletions crates/bun/platform/src/bun_platform.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::actions;
use moon_action_context::ActionContext;
use moon_bun_lang::BUNPM;
use moon_bun_tool::BunTool;
use moon_bun_tool::{get_bun_env_paths, BunTool};
use moon_common::Id;
use moon_config::{
BunConfig, DependencyConfig, DependencyScope, DependencySource, HasherConfig, PlatformType,
Expand All @@ -14,7 +14,7 @@ use moon_platform::{Platform, Runtime, RuntimeReq};
use moon_process::Command;
use moon_project::Project;
use moon_task::Task;
use moon_tool::{Tool, ToolManager};
use moon_tool::{get_proto_version_env, prepend_path_env_var, Tool, ToolManager};
use moon_typescript_platform::TypeScriptTargetHash;
use moon_utils::{async_trait, path};
use proto_core::ProtoEnvironment;
Expand Down Expand Up @@ -266,7 +266,7 @@ impl Platform for BunPlatform {
if !self.toolchain.has(&req) {
self.toolchain.register(
&req,
BunTool::new(&self.proto_env, &self.config, &req).await?,
BunTool::new(Arc::clone(&self.proto_env), &self.config, &req).await?,
);
}

Expand All @@ -292,8 +292,10 @@ impl Platform for BunPlatform {
let req = &runtime.requirement;

if !self.toolchain.has(req) {
self.toolchain
.register(req, BunTool::new(&self.proto_env, &self.config, req).await?);
self.toolchain.register(
req,
BunTool::new(Arc::clone(&self.proto_env), &self.config, req).await?,
);
}

Ok(self.toolchain.setup(req, last_versions).await?)
Expand Down Expand Up @@ -395,18 +397,41 @@ impl Platform for BunPlatform {
project: &Project,
task: &Task,
runtime: &Runtime,
working_dir: &Path,
_working_dir: &Path,
) -> miette::Result<Command> {
let command = if self.is_toolchain_enabled()? {
actions::create_target_command(
self.toolchain.get_for_version(&runtime.requirement)?,
project,
task,
working_dir,
)?
} else {
actions::create_target_command_without_tool(project, task, working_dir)?
};
let mut command = Command::new(&task.command);
command.args(&task.args);
command.envs(&task.env);

if let Ok(bun) = self.toolchain.get_for_version(&runtime.requirement) {
if let Some(version) = get_proto_version_env(&bun.tool) {
command.env("PROTO_BUN_VERSION", version);
}
}

let mut paths = vec![];
let mut current_dir = project.root.as_path();

loop {
paths.push(current_dir.join("node_modules").join(".bin"));

if current_dir == self.workspace_root {
break;
}

match current_dir.parent() {
Some(dir) => {
current_dir = dir;
}
None => break,
};
}

if !runtime.requirement.is_global() {
paths.extend(get_bun_env_paths(&self.proto_env));
}

command.env("PATH", prepend_path_env_var(paths));

Ok(command)
}
Expand Down
Loading

0 comments on commit f7bd6b1

Please sign in to comment.