Skip to content

Commit

Permalink
new: Add bun command cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 6, 2024
1 parent dc3b77a commit f3de5b0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### 🚀 Updates

- Added caching around `bun bun.lockb` commands, instead of running them for every task.
- Updated environment variable substitution to support different outputs when a variable is missing,
based on a trailing flag syntax.
- `$FOO` or `${FOO}` - If variable is missing, keeps the original syntax (current default).
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion legacy/bun/lang/src/bun_lockb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use cached::proc_macro::cached;
use miette::IntoDiagnostic;
use moon_lang::LockfileDependencyVersions;
use rustc_hash::FxHashMap;
use std::sync::Arc;
use yarn_lock_parser::{parse_str, Entry};

#[cached(result)]
pub fn load_lockfile_dependencies(
lockfile_text: String,
lockfile_text: Arc<String>,
) -> miette::Result<LockfileDependencyVersions> {
let mut deps: LockfileDependencyVersions = FxHashMap::default();

Expand Down
1 change: 1 addition & 0 deletions legacy/bun/tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ moon_utils = { path = "../../core/utils" }
miette = { workspace = true }
proto_core = { workspace = true }
rustc-hash = { workspace = true }
scc = { workspace = true }
starbase_utils = { workspace = true }
tracing = { workspace = true }

Expand Down
49 changes: 38 additions & 11 deletions legacy/bun/tool/src/bun_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use moon_utils::get_workspace_root;
use proto_core::flow::install::InstallOptions;
use proto_core::{Id, ProtoEnvironment, Tool as ProtoTool, UnresolvedVersionSpec};
use rustc_hash::FxHashMap;
use scc::hash_cache::Entry;
use starbase_utils::fs;
use std::env;
use std::path::{Path, PathBuf};
Expand All @@ -34,6 +35,8 @@ pub struct BunTool {

console: Arc<Console>,

lockfile_cache: scc::HashCache<PathBuf, Arc<String>>,

proto_env: Arc<ProtoEnvironment>,
}

Expand All @@ -50,6 +53,7 @@ impl BunTool {
global: false,
tool: load_tool_plugin(&Id::raw("bun"), &proto, config.plugin.as_ref().unwrap())
.await?,
lockfile_cache: scc::HashCache::default(),
proto_env: proto,
};

Expand All @@ -62,6 +66,37 @@ impl BunTool {

Ok(bun)
}

// Bun lockfiles are binary, so we need to convert them to text first
// using Bun itself!
async fn load_lockfile(&self, cwd: &Path) -> miette::Result<Arc<String>> {
let key = cwd.to_path_buf();

let cache = match self.lockfile_cache.entry_async(key).await {
Entry::Occupied(o) => o.get().clone(),
Entry::Vacant(v) => {
let yarn_lock = cwd.join("yarn.lock");

let content = if yarn_lock.exists() {
Arc::new(fs::read_file(yarn_lock)?)
} else {
let mut cmd = self.create_command(&())?;
cmd.arg("bun.lockb");
cmd.cwd(cwd);

let output = cmd.create_async().exec_capture_output().await?;

Arc::new(output_to_string(&output.stdout))
};

v.put_entry(content.clone());

content
}
};

Ok(cache)
}
}

#[async_trait]
Expand Down Expand Up @@ -189,17 +224,9 @@ impl DependencyManager<()> for BunTool {
return Ok(FxHashMap::default());
};

// Bun lockfiles are binary, so we need to convert them to text first
// using Bun itself!
let mut cmd = self.create_command(&())?;
cmd.arg("bun.lockb");
cmd.cwd(lockfile_path.parent().unwrap());

let output = cmd.create_async().exec_capture_output().await?;

Ok(load_lockfile_dependencies(output_to_string(
&output.stdout,
))?)
Ok(load_lockfile_dependencies(
self.load_lockfile(lockfile_path.parent().unwrap()).await?,
)?)
}

#[instrument(skip_all)]
Expand Down
1 change: 1 addition & 0 deletions legacy/node/tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ moon_utils = { path = "../../core/utils" }
miette = { workspace = true }
proto_core = { workspace = true }
rustc-hash = { workspace = true }
scc = { workspace = true }
starbase_styles = { workspace = true }
starbase_utils = { workspace = true }
tracing = { workspace = true }
Expand Down
51 changes: 39 additions & 12 deletions legacy/node/tool/src/bun_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use moon_utils::get_workspace_root;
use proto_core::flow::install::InstallOptions;
use proto_core::{Id, ProtoEnvironment, Tool as ProtoTool, UnresolvedVersionSpec};
use rustc_hash::FxHashMap;
use scc::hash_cache::Entry;
use starbase_utils::fs;
use std::env;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tracing::instrument;

Expand All @@ -28,6 +29,8 @@ pub struct BunTool {

console: Arc<Console>,

lockfile_cache: scc::HashCache<PathBuf, Arc<String>>,

proto_env: Arc<ProtoEnvironment>,
}

Expand All @@ -45,6 +48,7 @@ impl BunTool {
.await?,
config,
proto_env,
lockfile_cache: scc::HashCache::default(),
console,
})
}
Expand All @@ -67,6 +71,37 @@ impl BunTool {

Ok(cmd)
}

// Bun lockfiles are binary, so we need to convert them to text first
// using Bun itself!
async fn load_lockfile(&self, cwd: &Path) -> miette::Result<Arc<String>> {
let key = cwd.to_path_buf();

let cache = match self.lockfile_cache.entry_async(key).await {
Entry::Occupied(o) => o.get().clone(),
Entry::Vacant(v) => {
let yarn_lock = cwd.join("yarn.lock");

let content = if yarn_lock.exists() {
Arc::new(fs::read_file(yarn_lock)?)
} else {
let mut cmd = self.internal_create_command()?;
cmd.arg("bun.lockb");
cmd.cwd(cwd);

let output = cmd.create_async().exec_capture_output().await?;

Arc::new(output_to_string(&output.stdout))
};

v.put_entry(content.clone());

content
}
};

Ok(cache)
}
}

#[async_trait]
Expand Down Expand Up @@ -196,17 +231,9 @@ impl DependencyManager<NodeTool> for BunTool {
return Ok(FxHashMap::default());
};

// Bun lockfiles are binary, so we need to convert them to text first
// using Bun itself!
let mut cmd = self.internal_create_command()?;
cmd.arg("bun.lockb");
cmd.cwd(lockfile_path.parent().unwrap());

let output = cmd.create_async().exec_capture_output().await?;

Ok(bun::load_lockfile_dependencies(output_to_string(
&output.stdout,
))?)
Ok(bun::load_lockfile_dependencies(
self.load_lockfile(lockfile_path.parent().unwrap()).await?,
)?)
}

#[instrument(skip_all)]
Expand Down

0 comments on commit f3de5b0

Please sign in to comment.