Skip to content

Commit

Permalink
Wrapped up first implementation of turbo_json loading
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Apr 25, 2023
1 parent 955da4b commit b5c1aea
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/turborepo-lib/src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use turbopath::{AbsoluteSystemPathBuf, AnchoredSystemPathBuf};
pub struct PackageJson {
name: String,
version: String,
pub scripts: BTreeMap<String, String>,
dependencies: BTreeMap<String, String>,
dev_dependencies: BTreeMap<String, String>,
optional_dependencies: BTreeMap<String, String>,
Expand Down
13 changes: 13 additions & 0 deletions crates/turborepo-lib/src/pipeline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::collections::{HashMap, HashSet};

pub type Pipeline = HashMap<String, BookkeepingTaskDefinition>;

pub struct BookkeepingTaskDefinition {
pub defined_fields: HashSet<String>,
pub experimental_fields: HashSet<String>,
pub task_definition: HashableTaskDefinition,
}

pub struct HashableTaskDefinition {
pub should_cache: bool,
}
83 changes: 83 additions & 0 deletions crates/turborepo-lib/src/turbo_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::collections::HashSet;

use anyhow::{anyhow, Result};
use log::warn;
use serde::Deserialize;
use turbopath::{AbsoluteSystemPathBuf, RelativeSystemPathBuf};

use crate::{
commands::CommandBase,
package_json::PackageJson,
pipeline::{BookkeepingTaskDefinition, HashableTaskDefinition, Pipeline},
task_utils::{is_package_task, root_task_id},
};

const CONFIG_FILE: &str = "turbo.json";

#[derive(Debug, Clone, Deserialize)]
struct TurboJson {
pipeline: Pipeline,
}

fn load_turbo_config_for_single_package(
base: &mut CommandBase,
root_package_json: &mut PackageJson,
) -> Result<TurboJson> {
if root_package_json.legacy_turbo_config.is_some() {
warn!(
"[WARNING] \"turbo\" in package.json is no longer supported. Migrate to {} by running \
\"npx @turbo/codemod create-turbo-config\"\n",
CONFIG_FILE
);

root_package_json.legacy_turbo_config = None;
}

let mut turbo_json = read_turbo_config(
&base
.repo_root
.join_relative(RelativeSystemPathBuf::new("turbo.json")?),
)?;

let mut pipeline = Pipeline::new();
for (task_id, task_definition) in turbo_from_files.pipeline {
if is_package_task(&task_id) {
return Err(anyhow!(
"Package tasks (<package>#<task>) are not allowed in single-package repositories: \
found {}",
task_id
));
}
pipeline.insert(root_task_id(&task_id).to_string(), task_definition);
}

turbo_json.pipeline = pipeline;

for (script_name, _) in &root_package_json.scripts {
if !turbo_from_files.pipeline.contains_key(&script_name) {
let task_name = root_task_id(script_name);
let mut defined_fields = HashSet::new();
defined_fields.insert("ShouldCache".to_string());
let task_definition = BookkeepingTaskDefinition {
defined_fields,
task_definition: HashableTaskDefinition {
should_cache: false,
},
..Default::default()
};
turbo_from_files.insert(task_name, task_definition);
}
}

Ok(turbo_from_files)
}

fn read_turbo_config(turbo_json_path: &AbsoluteSystemPathBuf) -> Result<TurboJson> {
if turbo_json_path.exists() {
let contents = std::fs::read_to_string(turbo_json_path)?;
let turbo_json: TurboJson = serde_json::from_str(&contents)?;
Ok(turbo_json)
} else {
Err!("No {} found", CONFIG_FILE)
}
}

0 comments on commit b5c1aea

Please sign in to comment.