Skip to content

Commit

Permalink
Rework vars.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 27, 2023
1 parent 3d65e6a commit 8e04a92
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 122 deletions.
16 changes: 0 additions & 16 deletions crates/bun/platform/src/actions/run_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,11 @@ use crate::target_hash::BunTargetHash;
use moon_bun_tool::BunTool;
use moon_config::{HasherConfig, HasherOptimization};
use moon_node_lang::PackageJson;
use moon_process::Command;
use moon_project::Project;
use moon_task::Task;
use moon_tool::DependencyManager;
use rustc_hash::FxHashMap;
use std::path::Path;

// 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);
command.args(&task.args).envs(&task.env);

Ok(command)
}

pub async fn create_target_hasher(
bun: Option<&BunTool>,
project: &Project,
Expand Down
32 changes: 21 additions & 11 deletions crates/bun/platform/src/bun_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{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 @@ -382,17 +382,22 @@ impl Platform for BunPlatform {
_context: &ActionContext,
project: &Project,
task: &Task,
_runtime: &Runtime,
working_dir: &Path,
runtime: &Runtime,
_working_dir: &Path,
) -> miette::Result<Command> {
let command = actions::create_target_command_without_tool(project, task, working_dir)?;

Ok(command)
}
let mut command = Command::new(&task.command);
command.args(&task.args);
command.envs(&task.env);

if let Ok(tool) = self.toolchain.get_for_version(&runtime.requirement) {
command.env(
"PROTO_BUN_VERSION",
tool.tool.get_resolved_version().to_string(),
);
}

fn get_run_target_paths(&self, working_dir: &Path) -> Vec<PathBuf> {
let mut paths = vec![];
let mut current_dir = working_dir;
let mut current_dir = project.root.as_path();

loop {
paths.push(current_dir.join("node_modules").join(".bin"));
Expand All @@ -409,7 +414,12 @@ impl Platform for BunPlatform {
};
}

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

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

Ok(command)
}
}
14 changes: 7 additions & 7 deletions crates/bun/tool/src/bun_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ impl DependencyManager<()> for BunTool {
fn create_command(&self, _parent: &()) -> miette::Result<Command> {
let mut cmd = Command::new("bun");

cmd.env(
"PATH",
prepend_path_env_var(get_bun_env_paths(&self.proto_env)),
);

if !self.global {
cmd.env(
"PROTO_BUN_VERSION",
self.tool.get_resolved_version().to_string(),
"PATH",
prepend_path_env_var(get_bun_env_paths(&self.proto_env)),
);
}

cmd.env(
"PROTO_BUN_VERSION",
self.tool.get_resolved_version().to_string(),
);

Ok(cmd)
}

Expand Down
4 changes: 1 addition & 3 deletions crates/core/platform/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use moon_task::Task;
use moon_tool::Tool;
use rustc_hash::FxHashMap;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::sync::Arc;

#[async_trait]
Expand Down Expand Up @@ -162,6 +162,4 @@ pub trait Platform: Send + Sync {
runtime: &Runtime,
working_dir: &Path,
) -> miette::Result<Command>;

fn get_run_target_paths(&self, working_dir: &Path) -> Vec<PathBuf>;
}
9 changes: 2 additions & 7 deletions crates/core/runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use moon_project::Project;
use moon_target::{TargetError, TargetScope};
use moon_task::Task;
use moon_terminal::{label_checkpoint, Checkpoint};
use moon_tool::prepend_path_env_var;
use moon_utils::{is_ci, is_test_env, path, time};
use moon_workspace::Workspace;
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -244,18 +243,14 @@ impl<'a> Runner<'a> {
color::path(working_dir)
);

let platform = PlatformManager::read().get(task.platform)?;
let mut command = platform
let mut command = PlatformManager::read()
.get(task.platform)?
.create_run_target_command(context, project, task, runtime, working_dir)
.await?;

command
.cwd(working_dir)
.envs(self.create_env_vars().await?)
.env(
"PATH",
prepend_path_env_var(platform.get_run_target_paths(&project.root)),
)
// We need to handle non-zero's manually
.set_error_on_nonzero(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,20 @@ impl Platform for DenoPlatform {
_context: &ActionContext,
_project: &Project,
task: &Task,
_runtime: &Runtime,
working_dir: &Path,
runtime: &Runtime,
_working_dir: &Path,
) -> miette::Result<Command> {
let mut command = Command::new(&task.command);
command.args(&task.args);
command.envs(&task.env);

command.args(&task.args).envs(&task.env).cwd(working_dir);
if !runtime.requirement.is_global() {
command.env(
"PATH",
prepend_path_env_var(get_deno_env_paths(&self.proto_env)),
);
}

Ok(command)
}

fn get_run_target_paths(&self, _working_dir: &Path) -> Vec<PathBuf> {
get_deno_env_paths(&self.proto_env)
}
}
4 changes: 2 additions & 2 deletions crates/deno/platform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod bins_hash;
mod deno_platform;
mod deps_hash;
mod platform;
mod target_hash;

pub use platform::*;
pub use deno_platform::*;
53 changes: 45 additions & 8 deletions crates/node/platform/src/node_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use moon_platform::{Platform, Runtime, RuntimeReq};
use moon_process::Command;
use moon_project::Project;
use moon_task::Task;
use moon_tool::prepend_path_env_var;
use moon_tool::{Tool, ToolManager};
use moon_typescript_platform::TypeScriptTargetHash;
use moon_utils::{async_trait, path};
Expand Down Expand Up @@ -430,23 +431,54 @@ impl Platform for NodePlatform {
context: &ActionContext,
project: &Project,
task: &Task,
_runtime: &Runtime,
runtime: &Runtime,
working_dir: &Path,
) -> miette::Result<Command> {
let command = actions::create_target_command_without_tool(
let mut command = actions::create_target_command_without_tool(
&self.config,
context,
project,
task,
working_dir,
)?;

Ok(command)
}
if let Ok(node) = self.toolchain.get_for_version(&runtime.requirement) {
command.env(
"PROTO_NODE_VERSION",
node.tool.get_resolved_version().to_string(),
);

if let Ok(npm) = node.get_npm() {
command.env(
"PROTO_NPM_VERSION",
npm.tool.get_resolved_version().to_string(),
);
}

if let Ok(pnpm) = node.get_pnpm() {
command.env(
"PROTO_PNPM_VERSION",
pnpm.tool.get_resolved_version().to_string(),
);
}

if let Ok(yarn) = node.get_yarn() {
command.env(
"PROTO_YARN_VERSION",
yarn.tool.get_resolved_version().to_string(),
);
}

if let Ok(bun) = node.get_bun() {
command.env(
"PROTO_BUN_VERSION",
bun.tool.get_resolved_version().to_string(),
);
}
}

fn get_run_target_paths(&self, working_dir: &Path) -> Vec<PathBuf> {
let mut paths = vec![];
let mut current_dir = working_dir;
let mut current_dir = project.root.as_path();

loop {
paths.push(current_dir.join("node_modules").join(".bin"));
Expand All @@ -463,7 +495,12 @@ impl Platform for NodePlatform {
};
}

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

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

Ok(command)
}
}
14 changes: 7 additions & 7 deletions crates/node/tool/src/bun_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ impl BunTool {
fn internal_create_command(&self) -> miette::Result<Command> {
let mut cmd = Command::new("bun");

cmd.env(
"PATH",
prepend_path_env_var(get_node_env_paths(&self.proto_env)),
);

if !self.global {
cmd.env(
"PROTO_BUN_VERSION",
self.tool.get_resolved_version().to_string(),
"PATH",
prepend_path_env_var(get_node_env_paths(&self.proto_env)),
);
}

cmd.env(
"PROTO_BUN_VERSION",
self.tool.get_resolved_version().to_string(),
);

Ok(cmd)
}
}
Expand Down
10 changes: 6 additions & 4 deletions crates/node/tool/src/node_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ impl NodeTool {
_ => {
let mut cmd = Command::new(self.get_npx_path()?);
cmd.args(["--silent", "--", package]);
cmd.env(
"PATH",
prepend_path_env_var(get_node_env_paths(&self.proto_env)),
);
cmd.env(
"PROTO_NODE_VERSION",
self.tool.get_resolved_version().to_string(),
);
if !self.global {
cmd.env(
"PATH",
prepend_path_env_var(get_node_env_paths(&self.proto_env)),
);
}
cmd
}
};
Expand Down
22 changes: 11 additions & 11 deletions crates/node/tool/src/npm_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,22 @@ impl DependencyManager<NodeTool> for NpmTool {
fn create_command(&self, node: &NodeTool) -> miette::Result<Command> {
let mut cmd = Command::new("npm");

cmd.env(
"PATH",
prepend_path_env_var(get_node_env_paths(&self.proto_env)),
);

if !self.global {
cmd.env(
"PROTO_NPM_VERSION",
self.tool.get_resolved_version().to_string(),
);
cmd.env(
"PROTO_NODE_VERSION",
node.tool.get_resolved_version().to_string(),
"PATH",
prepend_path_env_var(get_node_env_paths(&self.proto_env)),
);
}

cmd.env(
"PROTO_NPM_VERSION",
self.tool.get_resolved_version().to_string(),
);
cmd.env(
"PROTO_NODE_VERSION",
node.tool.get_resolved_version().to_string(),
);

Ok(cmd)
}

Expand Down
22 changes: 11 additions & 11 deletions crates/node/tool/src/pnpm_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,22 @@ impl DependencyManager<NodeTool> for PnpmTool {
fn create_command(&self, node: &NodeTool) -> miette::Result<Command> {
let mut cmd = Command::new("pnpm");

cmd.env(
"PATH",
prepend_path_env_var(get_node_env_paths(&self.proto_env)),
);

if !self.global {
cmd.env(
"PROTO_PNPM_VERSION",
self.tool.get_resolved_version().to_string(),
);
cmd.env(
"PROTO_NODE_VERSION",
node.tool.get_resolved_version().to_string(),
"PATH",
prepend_path_env_var(get_node_env_paths(&self.proto_env)),
);
}

cmd.env(
"PROTO_PNPM_VERSION",
self.tool.get_resolved_version().to_string(),
);
cmd.env(
"PROTO_NODE_VERSION",
node.tool.get_resolved_version().to_string(),
);

Ok(cmd)
}

Expand Down
Loading

0 comments on commit 8e04a92

Please sign in to comment.