Skip to content

Commit

Permalink
breaking: Split toolchain settings into .moon/toolchain.yml. (#472)
Browse files Browse the repository at this point in the history
* Move config.

* Update templates and init.

* Update workspace.

* Update call sites.

* Update schemas.

* Split tests.

* Fix tests and lints.

* Split fixtures.

* Add test.

* Get tests working.

* Update project toolchain config.

* Split config docs.

* Doc polish.

* Update blog post.

* Some fixes.

* Update label.

* Temp fix snapshots.
  • Loading branch information
milesj committed Nov 29, 2022
1 parent 70777cd commit 5a45796
Show file tree
Hide file tree
Showing 186 changed files with 2,380 additions and 1,718 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
paths:
- .github/workflows/moon.yml
- .moon/workspace.yml
- .moon/toolchain.yml
- .moon/project.yml
- crates/**
- packages/**
Expand Down Expand Up @@ -39,7 +40,7 @@ jobs:
path: ~/.moon/tools
key:
${{ runner.os }}-moon-node@${{ matrix.node-version }}-${{
hashFiles('.moon/workspace.yml') }}
hashFiles('.moon/toolchain.yml') }}
restore-keys: ${{ runner.os }}-moon-node@${{ matrix.node-version }}-
- uses: actions-rs/toolchain@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .moon/project.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$schema: '../schemas/global-project.json'
$schema: '../website/static/schemas/global-project.json'

fileGroups:
configs:
Expand Down
17 changes: 17 additions & 0 deletions .moon/toolchain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$schema: '../website/static/schemas/toolchain.json'

node:
version: '16.18.0'
packageManager: 'yarn'
yarn:
version: '3.2.3'
plugins:
- 'interactive-tools'
- 'version'
- 'workspace-tools'
addEnginesConstraint: false
inferTasksFromScripts: false

typescript:
routeOutDirToCache: true
syncProjectReferences: true
22 changes: 5 additions & 17 deletions .moon/workspace.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
# Trigger CI: 7

$schema: '../schemas/workspace.json'
$schema: '../website/static/schemas/workspace.json'

projects:
- 'packages/*'
- '!packages/cli'
- '!packages/core-*'
- 'website'

node:
version: '16.18.0'
packageManager: 'yarn'
yarn:
version: '3.2.3'
plugins:
- 'interactive-tools'
- 'version'
- 'workspace-tools'
addEnginesConstraint: false
inferTasksFromScripts: false

typescript:
routeOutDirToCache: true
syncProjectReferences: true

generator:
templates:
- './templates'
Expand All @@ -36,3 +20,7 @@ runner:

vcs:
defaultBranch: 'master'

# TEMP, remove after 0.20 lands
node:
version: '16.18.0'
8 changes: 4 additions & 4 deletions crates/cli/src/commands/docker/scaffold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async fn scaffold_workspace(workspace: &Workspace, docker_root: &Path) -> Result
for lang in ProjectLanguage::iter() {
match lang {
ProjectLanguage::JavaScript => {
if workspace.config.node.is_some() {
if workspace.toolchain.config.node.is_some() {
files.push(NPM.manifest_filename.to_owned());

for ext in NODE.file_exts {
Expand All @@ -71,7 +71,7 @@ async fn scaffold_workspace(workspace: &Workspace, docker_root: &Path) -> Result
}
}
ProjectLanguage::TypeScript => {
if let Some(typescript_config) = &workspace.config.typescript {
if let Some(typescript_config) = &workspace.toolchain.config.typescript {
files.push(typescript_config.project_config_file_name.to_owned());
}
}
Expand All @@ -93,7 +93,7 @@ async fn scaffold_workspace(workspace: &Workspace, docker_root: &Path) -> Result
for lang in ProjectLanguage::iter() {
match lang {
ProjectLanguage::JavaScript => {
if let Some(node_config) = &workspace.config.node {
if let Some(node_config) = &workspace.toolchain.config.node {
let package_manager = match &node_config.package_manager {
NodePackageManager::Npm => NPM,
NodePackageManager::Pnpm => PNPM,
Expand All @@ -106,7 +106,7 @@ async fn scaffold_workspace(workspace: &Workspace, docker_root: &Path) -> Result
}
}
ProjectLanguage::TypeScript => {
if let Some(typescript_config) = &workspace.config.typescript {
if let Some(typescript_config) = &workspace.toolchain.config.typescript {
files.push(&typescript_config.root_config_file_name);
files.push(&typescript_config.root_options_config_file_name);
}
Expand Down
44 changes: 30 additions & 14 deletions crates/cli/src/commands/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ use crate::helpers::AnyError;
use clap::ValueEnum;
use dialoguer::theme::ColorfulTheme;
use dialoguer::Confirm;
use moon_config::{load_global_project_config_template, load_workspace_config_template};
use moon_constants::{CONFIG_DIRNAME, CONFIG_GLOBAL_PROJECT_FILENAME, CONFIG_WORKSPACE_FILENAME};
use moon_config::{
load_global_project_config_template, load_toolchain_config_template,
load_workspace_config_template,
};
use moon_constants::{
CONFIG_DIRNAME, CONFIG_GLOBAL_PROJECT_FILENAME, CONFIG_TOOLCHAIN_FILENAME,
CONFIG_WORKSPACE_FILENAME,
};
use moon_logger::color;
use moon_node_lang::NPM;
use moon_terminal::{create_theme, safe_exit};
Expand All @@ -28,7 +34,11 @@ pub enum InitTool {
TypeScript,
}

fn render_template(context: &Context) -> Result<String, Error> {
fn render_toolchain_template(context: &Context) -> Result<String, Error> {
Tera::one_off(load_toolchain_config_template(), context, false)
}

fn render_workspace_template(context: &Context) -> Result<String, Error> {
Tera::one_off(load_workspace_config_template(), context, false)
}

Expand Down Expand Up @@ -148,15 +158,15 @@ pub async fn init(
context.insert("vcs_default_branch", &vcs.1);

// Initialize all tools
let mut workspace_config = VecDeque::new();
let mut toolchain_configs = VecDeque::new();

if options.yes
|| dest_dir.join(NPM.manifest_filename).exists()
|| Confirm::with_theme(&theme)
.with_prompt("Initialize Node.js?")
.interact()?
{
workspace_config
toolchain_configs
.push_back(init_node(&dest_dir, &options, &theme, Some(&mut context)).await?);

if options.yes
Expand All @@ -165,23 +175,29 @@ pub async fn init(
.with_prompt("Initialize TypeScript?")
.interact()?
{
workspace_config.push_back(init_typescript(&dest_dir, &options, &theme).await?);
toolchain_configs.push_back(init_typescript(&dest_dir, &options, &theme).await?);
}
}

workspace_config.push_front(render_template(&context)?);
toolchain_configs.push_front(render_toolchain_template(&context)?);

// Create config files
fs::write(
&moon_dir.join(CONFIG_WORKSPACE_FILENAME),
workspace_config
&moon_dir.join(CONFIG_TOOLCHAIN_FILENAME),
toolchain_configs
.into_iter()
.map(|c| c.trim().to_owned())
.collect::<Vec<String>>()
.join("\n\n"),
)
.await?;

fs::write(
&moon_dir.join(CONFIG_WORKSPACE_FILENAME),
render_workspace_template(&context)?,
)
.await?;

fs::write(
&moon_dir.join(CONFIG_GLOBAL_PROJECT_FILENAME),
Tera::one_off(load_global_project_config_template(), &context, false)?,
Expand Down Expand Up @@ -219,23 +235,23 @@ mod tests {
fn renders_default() {
let context = create_default_context();

assert_snapshot!(render_template(&context).unwrap());
assert_snapshot!(render_workspace_template(&context).unwrap());
}

#[test]
fn renders_glob_list() {
let mut context = create_default_context();
context.insert("project_globs", &vec!["apps/*", "packages/*"]);

assert_snapshot!(render_template(&context).unwrap());
assert_snapshot!(render_workspace_template(&context).unwrap());
}

#[test]
fn renders_projects_map() {
let mut context = create_default_context();
context.insert("projects", &BTreeMap::from([("example", "apps/example")]));

assert_snapshot!(render_template(&context).unwrap());
assert_snapshot!(render_workspace_template(&context).unwrap());
}

#[test]
Expand All @@ -244,7 +260,7 @@ mod tests {
context.insert("vcs_manager", &"git");
context.insert("vcs_default_branch", &"main");

assert_snapshot!(render_template(&context).unwrap());
assert_snapshot!(render_workspace_template(&context).unwrap());
}

#[test]
Expand All @@ -253,6 +269,6 @@ mod tests {
context.insert("vcs_manager", &"svn");
context.insert("vcs_default_branch", &"trunk");

assert_snapshot!(render_template(&context).unwrap());
assert_snapshot!(render_workspace_template(&context).unwrap());
}
}
6 changes: 3 additions & 3 deletions crates/cli/src/commands/init/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dialoguer::theme::ColorfulTheme;
use dialoguer::{Confirm, Select};
use moon_config::{
default_node_version, default_npm_version, default_pnpm_version, default_yarn_version,
load_workspace_node_config_template,
load_toolchain_node_config_template,
};
use moon_lang::{is_using_package_manager, is_using_version_manager};
use moon_logger::color;
Expand All @@ -18,8 +18,8 @@ use std::collections::BTreeMap;
use std::path::Path;
use tera::{Context, Error, Tera};

fn render_template(context: Context) -> Result<String, Error> {
Tera::one_off(load_workspace_node_config_template(), &context, false)
pub fn render_template(context: Context) -> Result<String, Error> {
Tera::one_off(load_toolchain_node_config_template(), &context, false)
}

/// Detect the Node.js version from local configuration files,
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/src/commands/init/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use super::InitOptions;
use crate::helpers::AnyError;
use dialoguer::theme::ColorfulTheme;
use dialoguer::Confirm;
use moon_config::load_workspace_typescript_config_template;
use moon_config::load_toolchain_typescript_config_template;
use moon_terminal::label_header;
use moon_typescript_lang::TsConfigJson;
use std::path::Path;
use tera::{Context, Error, Tera};

fn render_template(context: Context) -> Result<String, Error> {
Tera::one_off(load_workspace_typescript_config_template(), &context, false)
pub fn render_template(context: Context) -> Result<String, Error> {
Tera::one_off(load_toolchain_typescript_config_template(), &context, false)
}

pub async fn init_typescript(
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub async fn setup() -> Result<(), Box<dyn std::error::Error>> {
let workspace = load_workspace().await?;
let mut dep_graph = DepGraph::default();

if let Some(node) = &workspace.config.node {
if let Some(node) = &workspace.toolchain.config.node {
let runtime = Runtime::Node(Version(node.version.to_owned(), false));

if is_test_env() {
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn load_workspace() -> Result<Workspace, WorkspaceError> {
.projects
.register_platform(Box::new(SystemPlatform::default()))?;

if workspace.config.node.is_some() {
if workspace.toolchain.config.node.is_some() {
workspace
.projects
.register_platform(Box::new(NodePlatform::default()))?;
Expand All @@ -43,7 +43,7 @@ pub async fn load_workspace_with_toolchain() -> Result<Workspace, WorkspaceError
for platform in PlatformType::iter() {
match platform {
PlatformType::Node => {
if let Some(node_config) = &workspace.config.node {
if let Some(node_config) = &workspace.toolchain.config.node {
workspace
.toolchain
.node
Expand Down
5 changes: 2 additions & 3 deletions crates/cli/tests/docker_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod scaffold_workspace {
let docker = fixture.join(".moon/docker/workspace");

assert!(docker.join(".moon/project.yml").exists());
assert!(docker.join(".moon/toolchain.yml").exists());
assert!(docker.join(".moon/workspace.yml").exists());
}

Expand Down Expand Up @@ -186,7 +187,7 @@ mod scaffold_sources {
fn can_include_more_files() {
let fixture = create_sandbox_with_git("cases");

let assert = create_moon_command(fixture.path())
create_moon_command(fixture.path())
.arg("docker")
.arg("scaffold")
.arg("base")
Expand All @@ -199,8 +200,6 @@ mod scaffold_sources {

let docker = fixture.join(".moon/docker/sources");

moon_utils::test::debug_sandbox(&fixture, &assert);

assert!(docker.join("base").exists());
assert!(docker.join("outputs/generate.js").exists());
assert!(docker.join("passthrough-args/passthroughArgs.sh").exists());
Expand Down
Loading

0 comments on commit 5a45796

Please sign in to comment.