这是indexloc提供的服务,不要输入任何密码
Skip to content

chore(frameworks): move framework logic to new crate #10198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ turborepo-ci = { path = "crates/turborepo-ci" }
turborepo-env = { path = "crates/turborepo-env" }
turborepo-errors = { path = "crates/turborepo-errors" }
turborepo-fixed-map = { path = "crates/turborepo-fixed-map" }
turborepo-frameworks = { path = "crates/turborepo-frameworks" }
turborepo-fs = { path = "crates/turborepo-fs" }
turborepo-lib = { path = "crates/turborepo-lib", default-features = false }
turborepo-lockfiles = { path = "crates/turborepo-lockfiles" }
Expand Down
16 changes: 16 additions & 0 deletions crates/turborepo-frameworks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "turborepo-frameworks"
version = "0.1.0"
edition = "2024"
license = "MIT"

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
turborepo-repository = { workspace = true }

[dev-dependencies]
test-case = { workspace = true }

[lints]
workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ struct EnvConditional {
#[derive(Debug, PartialEq, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Framework {
slug: String,
slug: Slug,
env_wildcards: Vec<String>,
env_conditionals: Option<Vec<EnvConditional>>,
dependency_match: Matcher,
}

#[derive(Debug, PartialEq, Clone, Deserialize)]
#[serde(transparent)]
pub struct Slug(String);

impl Framework {
pub fn slug(&self) -> String {
pub fn slug(&self) -> Slug {
self.slug.clone()
}

Expand Down Expand Up @@ -99,6 +103,26 @@ impl Matcher {
}
}

impl Slug {
pub fn as_str(&self) -> &str {
&self.0
}

pub fn framework(&self) -> &Framework {
let frameworks = get_frameworks();
frameworks
.iter()
.find(|framework| framework.slug.as_str() == self.as_str())
.expect("slug is only constructed via deserialization")
}
}

impl std::fmt::Display for Slug {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}

pub fn infer_framework(workspace: &PackageInfo, is_monorepo: bool) -> Option<&Framework> {
let frameworks = get_frameworks();

Expand All @@ -114,12 +138,12 @@ mod tests {
use test_case::test_case;
use turborepo_repository::{package_graph::PackageInfo, package_json::PackageJson};

use crate::framework::{get_frameworks, infer_framework, Framework};
use super::*;

fn get_framework_by_slug(slug: &str) -> &Framework {
get_frameworks()
.iter()
.find(|framework| framework.slug == slug)
.find(|framework| framework.slug.as_str() == slug)
.expect("framework not found")
}

Expand Down Expand Up @@ -316,8 +340,8 @@ mod tests {
let mut framework = get_framework_by_slug("nextjs").clone();

if let Some(env_conditionals) = framework.env_conditionals.as_mut() {
env_conditionals.push(crate::framework::EnvConditional {
when: crate::framework::EnvConditionKey {
env_conditionals.push(EnvConditional {
when: EnvConditionKey {
key: "ANOTHER_CONDITION".to_string(),
value: Some("true".to_string()),
},
Expand All @@ -344,4 +368,11 @@ mod tests {
met"
);
}

#[test]
fn test_framework_slug_roundtrip() {
for framework in get_frameworks() {
assert_eq!(framework, framework.slug().framework());
}
}
}
1 change: 1 addition & 0 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ turborepo-env = { workspace = true }
turborepo-errors = { workspace = true }
turborepo-filewatch = { path = "../turborepo-filewatch" }
turborepo-fixed-map = { workspace = true }
turborepo-frameworks = { workspace = true }
turborepo-fs = { path = "../turborepo-fs" }
turborepo-graph-utils = { path = "../turborepo-graph-utils" }
turborepo-lockfiles = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion crates/turborepo-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ mod diagnostics;
mod engine;

mod boundaries;
mod framework;
mod gitignore;
mod hash;
mod microfrontends;
Expand Down
6 changes: 5 additions & 1 deletion crates/turborepo-lib/src/run/summary/task_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ impl<'a> TaskSummaryFactory<'a> {
.expanded_outputs(task_id)
.unwrap_or_default();

let framework = self.hash_tracker.framework(task_id).unwrap_or_default();
let framework = self
.hash_tracker
.framework(task_id)
.map(|framework| framework.to_string())
.unwrap_or_default();
let hash = self
.hash_tracker
.hash(task_id)
Expand Down
12 changes: 6 additions & 6 deletions crates/turborepo-lib/src/task_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tracing::{debug, Span};
use turbopath::{AbsoluteSystemPath, AnchoredSystemPath, AnchoredSystemPathBuf};
use turborepo_cache::CacheHitMetadata;
use turborepo_env::{BySource, DetailedMap, EnvironmentVariableMap};
use turborepo_frameworks::{infer_framework, Slug as FrameworkSlug};
use turborepo_repository::package_graph::{PackageInfo, PackageName};
use turborepo_scm::SCM;
use turborepo_telemetry::events::{
Expand All @@ -19,7 +20,6 @@ use turborepo_telemetry::events::{
use crate::{
cli::EnvMode,
engine::TaskNode,
framework::infer_framework,
hash::{FileHashes, LockFilePackages, TaskHashable, TurboHash},
opts::RunOpts,
run::task_id::TaskId,
Expand Down Expand Up @@ -228,7 +228,7 @@ pub struct TaskHashTrackerState {
package_task_env_vars: HashMap<TaskId<'static>, DetailedMap>,
package_task_hashes: HashMap<TaskId<'static>, String>,
#[serde(skip)]
package_task_framework: HashMap<TaskId<'static>, String>,
package_task_framework: HashMap<TaskId<'static>, FrameworkSlug>,
#[serde(skip)]
package_task_outputs: HashMap<TaskId<'static>, Vec<AnchoredSystemPathBuf>>,
#[serde(skip)]
Expand Down Expand Up @@ -297,9 +297,9 @@ impl<'a> TaskHasher<'a> {
framework.slug(),
framework.env(self.env_at_execution_start)
);
telemetry.track_framework(framework.slug());
telemetry.track_framework(framework.slug().to_string());
});
let framework_slug = framework.map(|f| f.slug().to_string());
let framework_slug = framework.map(|f| f.slug());

let env_vars = if let Some(framework) = framework {
let mut computed_wildcards = framework.env(self.env_at_execution_start);
Expand Down Expand Up @@ -570,7 +570,7 @@ impl TaskHashTracker {
task_id: TaskId<'static>,
env_vars: DetailedMap,
hash: String,
framework_slug: Option<String>,
framework_slug: Option<FrameworkSlug>,
) {
let mut state = self.state.lock().expect("hash tracker mutex poisoned");
state
Expand All @@ -589,7 +589,7 @@ impl TaskHashTracker {
state.package_task_env_vars.get(task_id).cloned()
}

pub fn framework(&self, task_id: &TaskId) -> Option<String> {
pub fn framework(&self, task_id: &TaskId) -> Option<FrameworkSlug> {
let state = self.state.lock().expect("hash tracker mutex poisoned");
state.package_task_framework.get(task_id).cloned()
}
Expand Down
Loading