Skip to content

Commit

Permalink
internal: Migrate json utils to starbase. (#781)
Browse files Browse the repository at this point in the history
* First pass.

* Remove test.

* Delete files.
  • Loading branch information
milesj committed Apr 17, 2023
1 parent 2e761ef commit e0cd97f
Show file tree
Hide file tree
Showing 33 changed files with 78 additions and 316 deletions.
8 changes: 6 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ serde = { version = "1.0.159", features = ["derive"] }
serde_json = "1.0.95"
serde_yaml = "0.9.19"
starbase_styles = "0.1.3"
starbase_utils = { version = "0.2.2", default-features = false, features = ["editor-config", "yaml"] }
starbase_utils = { version = "0.2.2", default-features = false, features = ["editor-config", "json", "yaml"] }
strum = { version = "0.24.1", features = ["derive"] }
thiserror = "1.0.40"
tokio = { version = "1.27.0", default-features = false, features = ["tracing"] }
5 changes: 3 additions & 2 deletions crates/cli/src/commands/docker/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use moon_node_lang::{PackageJson, NODE};
use moon_node_tool::NodeTool;
use moon_project_graph::ProjectGraph;
use moon_terminal::safe_exit;
use moon_utils::{fs, json};
use moon_utils::fs;
use rustc_hash::FxHashSet;
use starbase_utils::json;
use std::path::Path;

pub async fn prune_node(
Expand Down Expand Up @@ -56,7 +57,7 @@ pub async fn prune() -> Result<(), AnyError> {
}

let project_graph = generate_project_graph(&mut workspace).await?;
let manifest: DockerManifest = json::read(manifest_path)?;
let manifest: DockerManifest = json::read_file(manifest_path)?;
let mut platforms = FxHashSet::<PlatformType>::default();

for project_id in &manifest.focused_projects {
Expand Down
9 changes: 5 additions & 4 deletions crates/cli/src/commands/docker/scaffold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use moon_constants::CONFIG_DIRNAME;
use moon_error::MoonError;
use moon_platform_detector::detect_language_files;
use moon_project_graph::{ProjectGraph, ProjectGraphError};
use moon_utils::{fs, glob, json, path};
use moon_utils::{fs, glob, path};
use moon_workspace::Workspace;
use rustc_hash::FxHashSet;
use serde::{Deserialize, Serialize};
use starbase_utils::json;
use std::path::Path;
use strum::IntoEnumIterator;

Expand Down Expand Up @@ -126,7 +127,7 @@ fn scaffold_sources(
docker_root: &Path,
project_ids: &[String],
include: &[String],
) -> Result<(), ProjectGraphError> {
) -> Result<(), AnyError> {
let docker_sources_root = docker_root.join("sources");
let mut manifest = DockerManifest {
focused_projects: FxHashSet::default(),
Expand Down Expand Up @@ -162,10 +163,10 @@ fn scaffold_sources(
copy_files(&files, &workspace.root, &docker_sources_root)?;
}

json::write(docker_sources_root.join(MANIFEST_NAME), &manifest, true)?;
json::write_file(docker_sources_root.join(MANIFEST_NAME), &manifest, true)?;

// Sync to the workspace scaffold for staged builds
json::write(
json::write_file(
docker_root.join("workspace").join(MANIFEST_NAME),
&manifest,
true,
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/commands/docker/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::helpers::AnyError;
use moon::{build_dep_graph, generate_project_graph, load_workspace_with_toolchain};
use moon_action_pipeline::Pipeline;
use moon_terminal::safe_exit;
use moon_utils::json;
use starbase_utils::json;

pub async fn setup() -> Result<(), AnyError> {
let mut workspace = load_workspace_with_toolchain().await?;
Expand All @@ -15,7 +15,7 @@ pub async fn setup() -> Result<(), AnyError> {
safe_exit(1);
}

let manifest: DockerManifest = json::read(manifest_path)?;
let manifest: DockerManifest = json::read_file(manifest_path)?;
let project_graph = generate_project_graph(&mut workspace).await?;
let mut dep_builder = build_dep_graph(&workspace, &project_graph);

Expand Down
6 changes: 3 additions & 3 deletions crates/cli/src/commands/migrate/from_turborepo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use moon_config::{InheritedTasksConfig, PlatformType, ProjectConfig, TaskCommand
use moon_constants as constants;
use moon_logger::{info, warn};
use moon_terminal::safe_exit;
use moon_utils::{fs, json};
use moon_utils::fs;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use starbase_utils::yaml;
use starbase_utils::{json, yaml};
use std::path::PathBuf;

const LOG_TARGET: &str = "moon:migrate:from-turborepo";
Expand Down Expand Up @@ -145,7 +145,7 @@ pub async fn from_turborepo(skip_touched_files_check: bool) -> Result<(), AnyErr
};

let project_graph = generate_project_graph(&mut workspace).await?;
let turbo_json: TurboJson = json::read(&turbo_file)?;
let turbo_json: TurboJson = json::read_file(&turbo_file)?;
let mut node_tasks_config = InheritedTasksConfig::default();
let mut has_modified_global_tasks = false;

Expand Down
1 change: 1 addition & 0 deletions crates/core/cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ moon_utils = { path = "../utils" }
rustc-hash = { workspace = true }
serde = { workspace = true }
starbase_styles = { workspace = true }
starbase_utils = { workspace = true }

[dev-dependencies]
moon_test_utils = { path = "../test-utils" }
Expand Down
7 changes: 4 additions & 3 deletions crates/core/cache/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use moon_constants::CONFIG_DIRNAME;
use moon_error::MoonError;
use moon_logger::{debug, trace};
use moon_platform_runtime::Runtime;
use moon_utils::{fs, json, time};
use moon_utils::{fs, time};
use serde::de::DeserializeOwned;
use serde::Serialize;
use starbase_styles::color;
use starbase_utils::json;
use std::path::{Path, PathBuf, MAIN_SEPARATOR_STR};

pub struct CacheEngine {
Expand Down Expand Up @@ -145,7 +146,7 @@ impl CacheEngine {
color::path(&path)
);

json::write(&path, &contents, true)?;
json::write_file(&path, &contents, true)?;

Ok(())
}
Expand All @@ -155,7 +156,7 @@ impl CacheEngine {

trace!(target: LOG_TARGET, "Writing report {}", color::path(&path));

json::write(path, &data, true)?;
json::write_file(path, &data, true)?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/core/cache/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ macro_rules! cache_item {
color::path(&path)
);

item = json::read(&path)?;
item = json::read_file(&path)?;
} else {
trace!(
target: log_target,
Expand All @@ -43,7 +43,7 @@ macro_rules! cache_item {
color::path(&self.path)
);

json::write(&self.path, &self, false)?;
json::write_file(&self.path, &self, false)?;
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion crates/core/cache/src/items/dependencies_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::cache_item;
use crate::helpers::get_cache_mode;
use moon_error::MoonError;
use moon_logger::trace;
use moon_utils::{fs, json};
use moon_utils::fs;
use serde::{Deserialize, Serialize};
use starbase_styles::color;
use starbase_utils::json;
use std::path::{Path, PathBuf};

#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
Expand Down
3 changes: 2 additions & 1 deletion crates/core/cache/src/items/projects_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use crate::cache_item;
use crate::helpers::get_cache_mode;
use moon_error::MoonError;
use moon_logger::trace;
use moon_utils::{fs, json};
use moon_utils::fs;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use starbase_styles::color;
use starbase_utils::json;
use std::path::{Path, PathBuf};

#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
Expand Down
3 changes: 2 additions & 1 deletion crates/core/cache/src/items/run_target_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use crate::helpers::get_cache_mode;
use moon_archive::{untar_with_diff, TarArchiver, TreeDiffer};
use moon_error::MoonError;
use moon_logger::trace;
use moon_utils::{fs, glob, json};
use moon_utils::{fs, glob};
use serde::{Deserialize, Serialize};
use starbase_styles::color;
use starbase_utils::json;
use std::path::{Path, PathBuf};

#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
Expand Down
3 changes: 2 additions & 1 deletion crates/core/cache/src/items/tool_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use crate::cache_item;
use crate::helpers::get_cache_mode;
use moon_error::MoonError;
use moon_logger::trace;
use moon_utils::{fs, json};
use moon_utils::fs;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use starbase_styles::color;
use starbase_utils::json;
use std::path::{Path, PathBuf};

#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
Expand Down
5 changes: 3 additions & 2 deletions crates/core/cache/src/runfiles.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use moon_error::MoonError;
use moon_logger::trace;
use moon_utils::{fs, json};
use moon_utils::fs;
use serde::de::DeserializeOwned;
use serde::Serialize;
use starbase_styles::color;
use starbase_utils::json;
use std::path::PathBuf;

pub struct Runfile {
Expand All @@ -21,7 +22,7 @@ impl Runfile {

// Always write a runfile, regardless of MOON_CACHE,
// since consumers expect this to exist at runtime
json::write(&path, data, true)?;
json::write_file(&path, data, true)?;

Ok(Runfile { path })
}
Expand Down
1 change: 1 addition & 0 deletions crates/core/error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ regex = { workspace = true }
serde_json = { workspace = true, default-features = false }
serde_yaml = { workspace = true, default-features = false }
starbase_styles = { workspace = true }
starbase_utils = { workspace = true }
thiserror = { workspace = true }
wax = "0.5.0"
6 changes: 6 additions & 0 deletions crates/core/error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ pub enum MoonError {

#[error("{0}")]
Unknown(#[source] IoError),

#[error(transparent)]
StarJson(#[from] starbase_utils::json::JsonError),

#[error(transparent)]
StarYaml(#[from] starbase_utils::yaml::YamlError),
}

pub fn map_io_to_fs_error(error: IoError, path: PathBuf) -> MoonError {
Expand Down
5 changes: 4 additions & 1 deletion crates/core/generator/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use moon_config::ConfigError;
use moon_constants as constants;
use moon_error::MoonError;
use starbase_styles::{Style, Stylize};
use starbase_utils::yaml::YamlError;
use starbase_utils::{json::JsonError, yaml::YamlError};
use std::path::PathBuf;
use tera::Error as TeraError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum GeneratorError {
#[error(transparent)]
Json(#[from] JsonError),

#[error(transparent)]
Yaml(#[from] YamlError),

Expand Down
10 changes: 5 additions & 5 deletions crates/core/generator/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use moon_config::{
};
use moon_constants::CONFIG_TEMPLATE_FILENAME;
use moon_logger::{debug, trace};
use moon_utils::{fs, json, lazy_static, path, regex};
use moon_utils::{fs, lazy_static, path, regex};
use starbase_styles::color;
use starbase_utils::yaml;
use starbase_utils::{json, yaml};
use std::path::{Path, PathBuf};
use tera::{Context, Tera};

Expand Down Expand Up @@ -305,10 +305,10 @@ impl Template {
if matches!(file.state, FileState::Merge) {
match file.is_mergeable() {
Some("json") => {
let prev: json::JsonValue = json::read(&file.dest_path)?;
let next: json::JsonValue = json::read(&file.source_path)?;
let prev: json::JsonValue = json::read_file(&file.dest_path)?;
let next: json::JsonValue = json::read_file(&file.source_path)?;

json::write(&file.dest_path, &json::merge(&prev, &next), true)?;
json::write_file(&file.dest_path, &json::merge(&prev, &next), true)?;
}
Some("yaml") => {
let prev: yaml::YamlValue = yaml::read_file(&file.dest_path)?;
Expand Down
1 change: 1 addition & 0 deletions crates/core/moon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ moon_project_graph = { path = "../project-graph" }
moon_system_platform = { path = "../../system/platform" }
moon_utils = { path = "../utils" }
moon_workspace = { path = "../workspace" }
starbase_utils = { workspace = true }
9 changes: 6 additions & 3 deletions crates/core/moon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use moon_error::MoonError;
use moon_node_platform::NodePlatform;
use moon_project_graph::{ProjectGraph, ProjectGraphBuilder, ProjectGraphError};
use moon_system_platform::SystemPlatform;
use moon_utils::{is_ci, is_test_env, json};
use moon_utils::{is_ci, is_test_env};
use moon_workspace::{Workspace, WorkspaceError};
use starbase_utils::json;
use std::env;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -116,7 +117,8 @@ pub async fn generate_project_graph(
let mut builder = build_project_graph(workspace).await?;

if builder.is_cached && cache_path.exists() {
let graph: ProjectGraph = json::read(&cache_path)?;
let graph: ProjectGraph = json::read_file(&cache_path)
.map_err(|e| ProjectGraphError::Moon(MoonError::StarJson(e)))?;

return Ok(graph);
}
Expand All @@ -126,7 +128,8 @@ pub async fn generate_project_graph(
let graph = builder.build()?;

if !builder.hash.is_empty() {
json::write(&cache_path, &graph, false)?;
json::write_file(&cache_path, &graph, false)
.map_err(|e| ProjectGraphError::Moon(MoonError::StarJson(e)))?;
}

Ok(graph)
Expand Down
Loading

0 comments on commit e0cd97f

Please sign in to comment.