Skip to content
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

new: Migrate to new starbase APIs. #1493

Merged
merged 33 commits into from
Jun 12, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move more.
milesj committed Jun 11, 2024
commit 7d2de6daf90f1a8a018659a75cc21da00a3140d7
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.

2 changes: 2 additions & 0 deletions crates/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ path = "src/main.rs"
moon_action_graph = { path = "../action-graph" }
moon_api = { path = "../api" }
moon_cache = { path = "../cache" }
moon_codegen = { path = "../codegen" }
moon_common = { path = "../common" }
moon_config = { path = "../config" }
moon_console = { path = "../console" }
@@ -28,6 +29,7 @@ moon_vcs = { path = "../vcs" }
moon_workspace = { path = "../workspace" }
async-trait = { workspace = true }
clap = { workspace = true }
clap_complete = { workspace = true }
miette = { workspace = true }
once_cell = { workspace = true }
proto_core = { workspace = true }
11 changes: 10 additions & 1 deletion crates/app/src/app_error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]

use miette::Diagnostic;
use moon_common::{consts, Style, Stylize};
use moon_common::{consts, Id, Style, Stylize};
use thiserror::Error;

#[derive(Debug, Diagnostic, Error)]
@@ -51,4 +51,13 @@ pub enum AppError {
#[diagnostic(code(app::missing_working_dir))]
#[error("Unable to determine your current working directory.")]
MissingWorkingDir,

#[diagnostic(code(app::extensions::unknown_id))]
#[error(
"The extension {} does not exist. Configure the {} setting in {} and try again.",
.id.style(Style::Id),
"extensions".style(Style::Property),
".moon/workspace.yml".style(Style::File),
)]
UnknownExtension { id: Id },
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
use crate::app::App;
use crate::session::CliSession;
use clap::{Args, CommandFactory};
use clap_complete::{generate, Shell};
use miette::miette;
use moon_app_components::Console;
use starbase::system;
use starbase::AppResult;
use tracing::instrument;

#[derive(Args, Clone, Debug)]
pub struct CompletionsArgs {
#[arg(long, help = "Shell to generate for")]
shell: Option<Shell>,
}

#[system]
pub async fn completions(args: ArgsRef<CompletionsArgs>, console: ResourceRef<Console>) {
#[instrument(skip_all)]
pub async fn completions(session: CliSession, args: CompletionsArgs) -> AppResult {
let Some(shell) = args.shell.or_else(Shell::from_env) else {
return Err(miette!(
code = "moon::completions",
"Could not determine your shell!"
));
};

console.quiet();
session.console.quiet();

let mut app = App::command();
let mut stdio = std::io::stdout();

generate(shell, &mut app, "moon", &mut stdio);

Ok(())
}
31 changes: 12 additions & 19 deletions legacy/cli/src/commands/ext.rs → crates/app/src/commands/ext.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::app_error::AppError;
use crate::session::CliSession;
use clap::Args;
use miette::miette;
use moon_app_components::ExtensionRegistry;
use moon_common::{color, Id};
use moon_common::Id;
use moon_plugin::{serialize_config, PluginId};
use moon_workspace::Workspace;
use starbase::system;
use starbase::AppResult;
use tracing::instrument;

#[derive(Args, Clone, Debug)]
pub struct ExtArgs {
@@ -16,23 +16,14 @@ pub struct ExtArgs {
pub passthrough: Vec<String>,
}

#[system]
pub async fn ext(
args: ArgsRef<ExtArgs>,
workspace: ResourceRef<Workspace>,
extensions: ResourceRef<ExtensionRegistry>,
) {
let Some(config) = workspace.config.extensions.get(&args.id) else {
return Err(miette!(
code = "moon::ext",
"The extension {} does not exist. Configure the {} setting in {} and try again.",
color::id(&args.id),
color::property("extensions"),
color::file(".moon/workspace.yml"),
));
#[instrument(skip_all)]
pub async fn ext(session: CliSession, args: ExtArgs) -> AppResult {
let Some(config) = session.workspace_config.extensions.get(&args.id) else {
return Err(AppError::UnknownExtension { id: args.id }.into());
};

let id = PluginId::raw(&args.id);
let extensions = session.get_extension_registry()?;

// Load and configure the plugin
extensions
@@ -50,4 +41,6 @@ pub async fn ext(
extensions.perform_sync(&id, |plugin, context| {
plugin.execute(args.passthrough.clone(), context)
})?;

Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
use crate::helpers::create_theme;
use crate::session::CliSession;
use dialoguer::{Confirm, Input, Select};
use miette::IntoDiagnostic;
use moon_app_components::{Console, MoonEnv};
use moon_codegen::{gather_variables, CodeGenerator, FileState};
use moon_workspace::Workspace;
use starbase::system;
use starbase::AppResult;
use starbase_styles::color;
use std::path::PathBuf;
use std::sync::Arc;
use tracing::debug;
use tracing::{debug, instrument};

pub use moon_codegen::GenerateArgs;

#[system]
pub async fn generate(
args: ArgsRef<GenerateArgs>,
workspace: ResourceRef<Workspace>,
console: ResourceRef<Console>,
moon_env: StateRef<MoonEnv>,
) {
#[instrument(skip_all)]
pub async fn generate(session: CliSession, args: GenerateArgs) -> AppResult {
let mut generator = CodeGenerator::new(
&workspace.root,
&workspace.config.generator,
Arc::clone(moon_env),
&session.workspace_root,
&session.workspace_config.generator,
Arc::clone(&session.moon_env),
);
let base_console = console;
let console = console.stdout();
let console = session.console.stdout();
let theme = create_theme();

// This is a special case for creating a new template with the generator itself!
@@ -65,7 +58,7 @@ pub async fn generate(
console.flush()?;

// Gather variables
let mut context = gather_variables(args, &template, base_console)?;
let mut context = gather_variables(&args, &template, &session.console)?;

// Determine the destination path
let relative_dest = PathBuf::from(match &args.dest {
@@ -87,15 +80,15 @@ pub async fn generate(
}
});
let relative_dest = template.interpolate_path(&relative_dest, &context)?;
let dest = relative_dest.to_logical_path(&workspace.working_dir);
let dest = relative_dest.to_logical_path(&session.working_dir);

debug!(dest = ?dest, "Destination path set");

// Inject built-in context variables
context.insert("dest_dir", &dest);
context.insert("dest_rel_dir", &relative_dest);
context.insert("working_dir", &workspace.working_dir);
context.insert("workspace_root", &workspace.root);
context.insert("working_dir", &session.working_dir);
context.insert("workspace_root", &session.workspace_root);

// Load template files and determine when to overwrite
template.load_files(&dest, &context)?;
@@ -176,7 +169,7 @@ pub async fn generate(
},
color::muted_light(
file.dest_path
.strip_prefix(&workspace.working_dir)
.strip_prefix(&session.working_dir)
.unwrap_or(&file.dest_path)
.to_string_lossy()
)
@@ -185,4 +178,6 @@ pub async fn generate(

console.write_newline()?;
console.flush()?;

Ok(())
}
4 changes: 2 additions & 2 deletions crates/app/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ pub mod bin;
pub mod clean;
// pub mod completions;
pub mod docker;
// pub mod ext;
// pub mod generate;
pub mod ext;
pub mod generate;
pub mod graph;
pub mod init;
pub mod migrate;