From 972f6944487c12e2891440d8354a40f5103c05d5 Mon Sep 17 00:00:00 2001 From: nicholaslyang Date: Thu, 17 Aug 2023 12:06:00 -0400 Subject: [PATCH 1/3] Completed framework inference --- crates/turborepo-lib/src/framework.rs | 290 ++++++++++++++++++ crates/turborepo-lib/src/lib.rs | 1 + .../src/package_graph/builder.rs | 23 +- crates/turborepo-lib/src/package_graph/mod.rs | 18 +- 4 files changed, 312 insertions(+), 20 deletions(-) create mode 100644 crates/turborepo-lib/src/framework.rs diff --git a/crates/turborepo-lib/src/framework.rs b/crates/turborepo-lib/src/framework.rs new file mode 100644 index 0000000000000..67814143bb9f1 --- /dev/null +++ b/crates/turborepo-lib/src/framework.rs @@ -0,0 +1,290 @@ +use std::sync::OnceLock; + +use crate::package_graph::WorkspaceInfo; + +#[derive(Debug, PartialEq)] +enum Strategy { + All, + Some, +} + +#[derive(Debug, PartialEq)] +struct Matcher { + strategy: Strategy, + dependencies: Vec<&'static str>, +} + +#[derive(Debug, PartialEq)] +pub struct Framework { + slug: &'static str, + env_wildcards: Vec<&'static str>, + dependency_match: Matcher, +} + +static FRAMEWORKS: OnceLock<[Framework; 12]> = OnceLock::new(); + +fn get_frameworks() -> &'static [Framework] { + FRAMEWORKS.get_or_init(|| { + [ + Framework { + slug: "blitzjs", + env_wildcards: vec!["NEXT_PUBLIC_*"], + dependency_match: Matcher { + strategy: Strategy::All, + dependencies: vec!["blitz"], + }, + }, + Framework { + slug: "nextjs", + env_wildcards: vec!["NEXT_PUBLIC_*"], + dependency_match: Matcher { + strategy: Strategy::All, + dependencies: vec!["next"], + }, + }, + Framework { + slug: "gatsby", + env_wildcards: vec!["GATSBY_*"], + dependency_match: Matcher { + strategy: Strategy::All, + dependencies: vec!["gatsby"], + }, + }, + Framework { + slug: "astro", + env_wildcards: vec!["PUBLIC_*"], + dependency_match: Matcher { + strategy: Strategy::All, + dependencies: vec!["astro"], + }, + }, + Framework { + slug: "solidstart", + env_wildcards: vec!["VITE_*"], + dependency_match: Matcher { + strategy: Strategy::All, + dependencies: vec!["solid-js", "solid-start"], + }, + }, + Framework { + slug: "vue", + env_wildcards: vec!["VUE_APP_*"], + dependency_match: Matcher { + strategy: Strategy::All, + dependencies: vec!["@vue/cli-service"], + }, + }, + Framework { + slug: "sveltekit", + env_wildcards: vec!["VITE_*"], + dependency_match: Matcher { + strategy: Strategy::All, + dependencies: vec!["@sveltejs/kit"], + }, + }, + Framework { + slug: "create-react-app", + env_wildcards: vec!["REACT_APP_*"], + dependency_match: Matcher { + strategy: Strategy::Some, + dependencies: vec!["react-scripts", "react-dev-utils"], + }, + }, + Framework { + slug: "nuxtjs", + env_wildcards: vec!["NUXT_ENV_*"], + dependency_match: Matcher { + strategy: Strategy::Some, + dependencies: vec!["nuxt", "nuxt-edge", "nuxt3", "nuxt3-edge"], + }, + }, + Framework { + slug: "redwoodjs", + env_wildcards: vec!["REDWOOD_ENV_*"], + dependency_match: Matcher { + strategy: Strategy::All, + dependencies: vec!["@redwoodjs/core"], + }, + }, + Framework { + slug: "vite", + env_wildcards: vec!["VITE_*"], + dependency_match: Matcher { + strategy: Strategy::All, + dependencies: vec!["vite"], + }, + }, + Framework { + slug: "sanity", + env_wildcards: vec!["SANITY_STUDIO_*"], + dependency_match: Matcher { + strategy: Strategy::All, + dependencies: vec!["@sanity/cli"], + }, + }, + ] + }) +} + +impl Matcher { + pub fn test(&self, workspace: &WorkspaceInfo, is_monorepo: bool) -> bool { + // In the case where we're not in a monorepo, i.e. single package mode + // `unresolved_external_dependencies` is not populated. In which + // case we should check `dependencies` instead. + let deps = if is_monorepo { + workspace.unresolved_external_dependencies.as_ref() + } else { + workspace.package_json.dependencies.as_ref() + }; + + match self.strategy { + Strategy::All => self + .dependencies + .iter() + .all(|dep| deps.map_or(false, |deps| deps.contains_key(*dep))), + Strategy::Some => self + .dependencies + .iter() + .any(|dep| deps.map_or(false, |deps| deps.contains_key(*dep))), + } + } +} + +#[allow(dead_code)] +pub fn infer_framework(workspace: &WorkspaceInfo, is_monorepo: bool) -> Option<&'static Framework> { + let frameworks = get_frameworks(); + + for framework in frameworks { + if framework.dependency_match.test(workspace, is_monorepo) { + return Some(framework); + } + } + + None +} + +#[cfg(test)] +mod tests { + use test_case::test_case; + + use crate::{ + framework::{get_frameworks, infer_framework, Framework}, + package_graph::WorkspaceInfo, + package_json::PackageJson, + }; + + fn get_framework_by_slug(slug: &str) -> &'static Framework { + get_frameworks() + .iter() + .find(|framework| framework.slug == slug) + .expect("framework not found") + } + + #[test_case(WorkspaceInfo::default(), None, true; "empty dependencies")] + #[test_case( + WorkspaceInfo { + unresolved_external_dependencies: Some( + vec![("blitz".to_string(), "*".to_string())].into_iter().collect() + ), + ..Default::default() + }, + Some(get_framework_by_slug("blitzjs")), + true; + "blitz" + )] + #[test_case( + WorkspaceInfo { + unresolved_external_dependencies: Some( + vec![("blitz", "*"), ("next", "*")] + .into_iter() + .map(|(s1, s2)| (s1.to_string(), s2.to_string())) + .collect() + ), + ..Default::default() + }, + Some(get_framework_by_slug("blitzjs")), + true; + "Order is preserved (returns blitz, not next)" + )] + #[test_case( + WorkspaceInfo { + unresolved_external_dependencies: Some( + vec![("next", "*")] + .into_iter() + .map(|(s1, s2)| (s1.to_string(), s2.to_string())) + .collect() + ), + ..Default::default() + }, + Some(get_framework_by_slug("nextjs")), + true; + "Finds next without blitz" + )] + #[test_case( + WorkspaceInfo { + unresolved_external_dependencies: Some( + vec![("solid-js", "*"), ("solid-start", "*")] + .into_iter() + .map(|(s1, s2)| (s1.to_string(), s2.to_string())) + .collect() + ), + ..Default::default() + }, + Some(get_framework_by_slug("solidstart")), + true; + "match all strategy works (solid)" + )] + #[test_case( + WorkspaceInfo { + unresolved_external_dependencies: Some( + vec![("nuxt3", "*")] + .into_iter() + .map(|(s1, s2)| (s1.to_string(), s2.to_string())) + .collect() + ), + ..Default::default() + }, + Some(get_framework_by_slug("nuxtjs")), + true; + "match some strategy works (nuxt)" + )] + #[test_case( + WorkspaceInfo { + unresolved_external_dependencies: Some( + vec![("react-scripts", "*")] + .into_iter() + .map(|(s1, s2)| (s1.to_string(), s2.to_string())) + .collect() + ), + ..Default::default() + }, + Some(get_framework_by_slug("create-react-app")), + true; + "match some strategy works (create-react-app)" + )] + #[test_case( + WorkspaceInfo { + package_json: PackageJson { + dependencies: Some( + vec![("next", "*")] + .into_iter() + .map(|(s1, s2)| (s1.to_string(), s2.to_string())) + .collect() + ), + ..Default::default() + }, + ..Default::default() + }, + Some(get_framework_by_slug("nextjs")), + false; + "Finds next in non-monorepo" + )] + fn test_infer_framework( + workspace_info: WorkspaceInfo, + expected: Option<&'static Framework>, + is_monorepo: bool, + ) { + let framework = infer_framework(&workspace_info, is_monorepo); + assert_eq!(framework, expected); + } +} diff --git a/crates/turborepo-lib/src/lib.rs b/crates/turborepo-lib/src/lib.rs index 9de7fbf0a81cd..c1ae4ae10b02a 100644 --- a/crates/turborepo-lib/src/lib.rs +++ b/crates/turborepo-lib/src/lib.rs @@ -12,6 +12,7 @@ mod config; mod daemon; mod engine; mod execution_state; +mod framework; pub(crate) mod globwatcher; pub mod graph; mod manager; diff --git a/crates/turborepo-lib/src/package_graph/builder.rs b/crates/turborepo-lib/src/package_graph/builder.rs index 42f4e229e8353..a03f7b7965160 100644 --- a/crates/turborepo-lib/src/package_graph/builder.rs +++ b/crates/turborepo-lib/src/package_graph/builder.rs @@ -1,5 +1,5 @@ use std::{ - collections::{HashMap, HashSet}, + collections::{BTreeMap, HashMap, HashSet}, fmt, }; @@ -11,8 +11,12 @@ use turbopath::{ }; use turborepo_lockfiles::Lockfile; -use super::{Package, PackageGraph, WorkspaceInfo, WorkspaceName, WorkspaceNode}; -use crate::{package_json::PackageJson, package_manager::PackageManager}; +use super::{PackageGraph, WorkspaceInfo, WorkspaceName, WorkspaceNode}; +use crate::{ + package_graph::{PackageName, PackageVersion}, + package_json::PackageJson, + package_manager::PackageManager, +}; pub struct PackageGraphBuilder<'a> { repo_root: &'a AbsoluteSystemPath, @@ -397,9 +401,7 @@ impl<'a> BuildState<'a, ResolvedLockfile> { .as_ref() .map(|deps| { deps.iter() - .map(|Package { name, version }| { - (name.to_string(), version.to_string()) - }) + .map(|(name, version)| (name.to_string(), version.to_string())) .collect() }) .unwrap_or_default(); @@ -447,7 +449,7 @@ impl<'a> BuildState<'a, ResolvedLockfile> { struct Dependencies { internal: HashSet, - external: HashSet, + external: BTreeMap, } impl Dependencies { @@ -462,7 +464,7 @@ impl Dependencies { .parent() .expect("package.json path should have parent"); let mut internal = HashSet::new(); - let mut external = HashSet::new(); + let mut external = BTreeMap::new(); let splitter = DependencySplitter { repo_root, workspace_dir, @@ -472,10 +474,7 @@ impl Dependencies { if let Some(workspace) = splitter.is_internal(name, version) { internal.insert(workspace); } else { - external.insert(Package { - name: name.clone(), - version: version.clone(), - }); + external.insert(name.clone(), version.clone()); } } Self { internal, external } diff --git a/crates/turborepo-lib/src/package_graph/mod.rs b/crates/turborepo-lib/src/package_graph/mod.rs index 91d639081574e..e0859ae8d595a 100644 --- a/crates/turborepo-lib/src/package_graph/mod.rs +++ b/crates/turborepo-lib/src/package_graph/mod.rs @@ -1,5 +1,5 @@ use std::{ - collections::{HashMap, HashSet}, + collections::{BTreeMap, HashMap, HashSet}, fmt, }; @@ -28,7 +28,7 @@ pub struct PackageGraph { pub struct WorkspaceInfo { pub package_json: PackageJson, pub package_json_path: AnchoredSystemPathBuf, - pub unresolved_external_dependencies: Option>, + pub unresolved_external_dependencies: Option>, pub transitive_dependencies: Option>, } @@ -38,10 +38,13 @@ impl WorkspaceInfo { } } +type PackageName = String; +type PackageVersion = String; + #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] pub struct Package { - name: String, - version: String, + name: PackageName, + version: PackageVersion, } /// Name of workspaces with a special marker for the workspace root @@ -303,10 +306,9 @@ mod test { .unresolved_external_dependencies .as_ref() .unwrap(); - assert!(b_external.contains(&Package { - name: "c".into(), - version: "1.2.3".into() - })); + + let pkg_version = b_external.get("c").unwrap(); + assert_eq!(pkg_version, "1.2.3"); } struct MockLockfile {} From eadce54bb29d19e897f2543c27b2bcde939219e1 Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Fri, 18 Aug 2023 11:40:36 -0400 Subject: [PATCH 2/3] Update crates/turborepo-lib/src/framework.rs Co-authored-by: Chris Olszewski --- crates/turborepo-lib/src/framework.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/turborepo-lib/src/framework.rs b/crates/turborepo-lib/src/framework.rs index 67814143bb9f1..16149ef627e75 100644 --- a/crates/turborepo-lib/src/framework.rs +++ b/crates/turborepo-lib/src/framework.rs @@ -154,13 +154,9 @@ impl Matcher { pub fn infer_framework(workspace: &WorkspaceInfo, is_monorepo: bool) -> Option<&'static Framework> { let frameworks = get_frameworks(); - for framework in frameworks { - if framework.dependency_match.test(workspace, is_monorepo) { - return Some(framework); - } - } - - None + frameworks + .iter() + .find(|framework| framework.dependency_match.test(workspace, is_monorepo)) } #[cfg(test)] From e20c33465b59856a75e5c3adc7ccdff0d12d5e2f Mon Sep 17 00:00:00 2001 From: nicholaslyang Date: Fri, 18 Aug 2023 16:38:40 -0400 Subject: [PATCH 3/3] Removed package struct --- crates/turborepo-lib/src/package_graph/mod.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/crates/turborepo-lib/src/package_graph/mod.rs b/crates/turborepo-lib/src/package_graph/mod.rs index e0859ae8d595a..d332390993b55 100644 --- a/crates/turborepo-lib/src/package_graph/mod.rs +++ b/crates/turborepo-lib/src/package_graph/mod.rs @@ -41,12 +41,6 @@ impl WorkspaceInfo { type PackageName = String; type PackageVersion = String; -#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] -pub struct Package { - name: PackageName, - version: PackageVersion, -} - /// Name of workspaces with a special marker for the workspace root #[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)] pub enum WorkspaceName {