Skip to content

Commit

Permalink
feat: restore project.toml inside the app directory
Browse files Browse the repository at this point in the history
  • Loading branch information
callicles authored Feb 24, 2024
1 parent 6b5d2d2 commit 8de8002
Show file tree
Hide file tree
Showing 28 changed files with 397 additions and 312 deletions.
10 changes: 3 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{"rust-analyzer.linkedProjects": [
"apps/moose-cli/Cargo.toml",
"./apps/moose-cli/Cargo.toml",
"./apps/moose-cli/Cargo.toml",
"./apps/moose-cli/Cargo.toml"
],
{
"rust-analyzer.linkedProjects": ["apps/framework-cli/Cargo.toml"],
"rust-analyzer.showUnlinkedFileNotification": false
}
}
62 changes: 37 additions & 25 deletions apps/framework-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@ mod routines;
mod settings;
mod watcher;

use self::settings::setup_user_directory;
use std::path::Path;
use std::process::exit;
use std::sync::Arc;

use clap::Parser;
use commands::Commands;
use home::home_dir;
use log::{debug, info};
use logger::setup_logging;
use settings::{read_settings, Settings};

use self::{
use crate::cli::{
display::{Message, MessageType},
routines::{
clean::CleanProject, initialize::InitializeProject, start::RunLocalInfrastructure,
stop::StopLocalInfrastructure, validate::ValidateRedPandaCluster, RoutineController,
RunMode,
initialize::InitializeProject, start::RunLocalInfrastructure,
validate::ValidateRedPandaCluster, RoutineController, RunMode,
},
settings::{init_config_file, setup_user_directory},
};
use crate::cli::settings::init_config_file;
use crate::project::Project;
use crate::utilities::git::is_git_repo;
use clap::Parser;
use commands::Commands;
use home::home_dir;
use logger::setup_logging;
use settings::{read_settings, Settings};
use std::path::Path;
use std::process::exit;

use self::routines::{clean::CleanProject, stop::StopLocalInfrastructure};

#[derive(Parser)]
#[command(author, version, about, long_about = None, arg_required_else_help(true))]
Expand Down Expand Up @@ -76,25 +78,29 @@ async fn top_command_handler(settings: Settings, commands: &Commands) {
exit(1);
}

let project = Project::from_dir(dir_path, name.clone(), *language);
let project = Project::new(dir_path, name.clone(), *language);
let project_arc = Arc::new(project);

debug!("Project: {:?}", project);
debug!("Project: {:?}", project_arc);

let mut controller = RoutineController::new();
let run_mode = RunMode::Explicit {};

controller.add_routine(Box::new(InitializeProject::new(run_mode, project.clone())));
controller.add_routine(Box::new(InitializeProject::new(
run_mode,
project_arc.clone(),
)));

controller.run_routines(run_mode);

project
.write_to_file()
project_arc
.write_to_disk()
.expect("Failed to write project to file");

let is_git_repo =
is_git_repo(dir_path).expect("Failed to check if directory is a git repo");
if !is_git_repo {
crate::utilities::git::create_init_commit(&project, dir_path);
crate::utilities::git::create_init_commit(project_arc, dir_path);
show_message!(
MessageType::Success,
Message::new("Created".to_string(), "Git Repository".to_string())
Expand All @@ -107,17 +113,20 @@ async fn top_command_handler(settings: Settings, commands: &Commands) {
let project = Project::load_from_current_dir()
.expect("No project found, please run `moose init` to create a project");

let project_arc = Arc::new(project);

let mut controller = RoutineController::new();
let run_mode = RunMode::Explicit {};

controller.add_routine(Box::new(RunLocalInfrastructure::new(project.clone())));
controller.add_routine(Box::new(RunLocalInfrastructure::new(project_arc.clone())));

controller
.add_routine(Box::new(ValidateRedPandaCluster::new(project.name.clone())));
controller.add_routine(Box::new(ValidateRedPandaCluster::new(
project_arc.name().clone(),
)));

controller.run_routines(run_mode);

routines::start_development_mode(&project).await.unwrap();
routines::start_development_mode(project_arc).await.unwrap();
}
Commands::Update {} => {
// This command may not be needed if we have incredible automation
Expand All @@ -128,16 +137,19 @@ async fn top_command_handler(settings: Settings, commands: &Commands) {
let run_mode = RunMode::Explicit {};
let project = Project::load_from_current_dir()
.expect("No project found, please run `moose init` to create a project");
controller.add_routine(Box::new(StopLocalInfrastructure::new(project)));
let project_arc = Arc::new(project);

controller.add_routine(Box::new(StopLocalInfrastructure::new(project_arc)));
controller.run_routines(run_mode);
}
Commands::Clean {} => {
let run_mode = RunMode::Explicit {};
let project = Project::load_from_current_dir()
.expect("No project found, please run `moose init` to create a project");
let project_arc = Arc::new(project);

let mut controller = RoutineController::new();
controller.add_routine(Box::new(CleanProject::new(project, run_mode)));
controller.add_routine(Box::new(CleanProject::new(project_arc, run_mode)));
controller.run_routines(run_mode);
}
}
Expand Down
3 changes: 2 additions & 1 deletion apps/framework-cli/src/cli/local_webserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use std::future::Future;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use tokio::net::TcpListener;
use tokio::sync::RwLock;
Expand Down Expand Up @@ -245,7 +246,7 @@ impl Webserver {
pub async fn start(
&self,
route_table: &'static RwLock<HashMap<PathBuf, RouteMeta>>,
project: &Project,
project: Arc<Project>,
) {
//! Starts the local webserver
let socket = self.socket().await;
Expand Down
36 changes: 20 additions & 16 deletions apps/framework-cli/src/cli/routines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use std::{io::Error, path::PathBuf};

use log::debug;
Expand Down Expand Up @@ -224,7 +225,7 @@ impl RoutineController {
}

// Starts the file watcher and the webserver
pub async fn start_development_mode(project: &Project) -> anyhow::Result<()> {
pub async fn start_development_mode(project: Arc<Project>) -> anyhow::Result<()> {
show_message!(
MessageType::Success,
Message {
Expand All @@ -236,18 +237,16 @@ pub async fn start_development_mode(project: &Project) -> anyhow::Result<()> {
let mut route_table = HashMap::<PathBuf, RouteMeta>::new();

info!("Initializing project state");
initialize_project_state(project.schemas_dir(), project, &mut route_table).await?;

initialize_project_state(project.clone(), &mut route_table).await?;
let route_table: &'static RwLock<HashMap<PathBuf, RouteMeta>> =
Box::leak(Box::new(RwLock::new(route_table)));

let web_server = Webserver::new(
project.local_webserver_config.host.clone(),
project.local_webserver_config.port,
);
let server_config = project.http_server_config.clone();

let web_server = Webserver::new(server_config.host.clone(), server_config.port);
let file_watcher = FileWatcher::new();

file_watcher.start(project, route_table)?;
file_watcher.start(project.clone(), route_table)?;

info!("Starting web server...");

Expand All @@ -257,25 +256,29 @@ pub async fn start_development_mode(project: &Project) -> anyhow::Result<()> {
}

async fn initialize_project_state(
schema_dir: PathBuf,
project: &Project,
project: Arc<Project>,
route_table: &mut HashMap<PathBuf, RouteMeta>,
) -> anyhow::Result<()> {
let configured_client = olap::clickhouse::create_client(project.clickhouse_config.clone());
let producer = redpanda::create_producer(project.redpanda_config.clone());

let schema_dir = project.schemas_dir();
info!("Starting schema directory crawl...");

with_spinner_async("Processing schema file", async {
let crawl_result =
process_schemas_in_dir(&schema_dir, project, &configured_client, route_table).await;
let crawl_result = process_schemas_in_dir(
schema_dir.as_path(),
project.clone(),
&configured_client,
route_table,
)
.await;

let _ = post_current_state_to_console(
project,
&configured_client,
&producer,
route_table.clone(),
project.console_config.clone(),
)
.await;

Expand All @@ -298,7 +301,7 @@ async fn initialize_project_state(
#[async_recursion]
async fn process_schemas_in_dir(
schema_dir: &Path,
project: &Project,
project: Arc<Project>,
configured_client: &ConfiguredDBClient,
route_table: &mut HashMap<PathBuf, RouteMeta>,
) -> anyhow::Result<()> {
Expand All @@ -308,10 +311,11 @@ async fn process_schemas_in_dir(
let path = entry.path();
if path.is_dir() {
debug!("Processing directory: {:?}", path);
process_schemas_in_dir(&path, project, configured_client, route_table).await?;
process_schemas_in_dir(&path, project.clone(), configured_client, route_table)
.await?;
} else {
debug!("Processing file: {:?}", path);
process_schema_file(&path, project, configured_client, route_table).await?
process_schema_file(&path, project.clone(), configured_client, route_table).await?
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions apps/framework-cli/src/cli/routines/clean.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::Arc;
use std::{fs, path::PathBuf};

use crate::cli::routines::util::ensure_docker_running;
Expand All @@ -6,11 +7,11 @@ use crate::{cli::display::Message, project::Project};
use super::{stop::StopLocalInfrastructure, Routine, RoutineFailure, RoutineSuccess, RunMode};

pub struct CleanProject {
project: Project,
project: Arc<Project>,
run_mode: RunMode,
}
impl CleanProject {
pub fn new(project: Project, run_mode: RunMode) -> Self {
pub fn new(project: Arc<Project>, run_mode: RunMode) -> Self {
Self { project, run_mode }
}
}
Expand Down
25 changes: 13 additions & 12 deletions apps/framework-cli/src/cli/routines/initialize.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::Arc;
use std::{fs, path::PathBuf};

use crate::{
Expand All @@ -11,10 +12,10 @@ use super::{Routine, RoutineFailure, RoutineSuccess, RunMode};

pub struct InitializeProject {
run_mode: RunMode,
project: Project,
project: Arc<Project>,
}
impl InitializeProject {
pub fn new(run_mode: RunMode, project: Project) -> Self {
pub fn new(run_mode: RunMode, project: Arc<Project>) -> Self {
Self { run_mode, project }
}
}
Expand Down Expand Up @@ -103,10 +104,10 @@ impl Routine for ValidateMountVolumes {

pub struct CreateInternalTempDirectoryTree {
run_mode: RunMode,
project: Project,
project: Arc<Project>,
}
impl CreateInternalTempDirectoryTree {
pub fn new(run_mode: RunMode, project: Project) -> Self {
pub fn new(run_mode: RunMode, project: Arc<Project>) -> Self {
Self { run_mode, project }
}
}
Expand Down Expand Up @@ -135,11 +136,11 @@ impl Routine for CreateInternalTempDirectoryTree {

pub struct CreateTempDataVolumes {
run_mode: RunMode,
project: Project,
project: Arc<Project>,
}

impl CreateTempDataVolumes {
fn new(run_mode: RunMode, project: Project) -> Self {
fn new(run_mode: RunMode, project: Arc<Project>) -> Self {
Self { run_mode, project }
}
}
Expand Down Expand Up @@ -256,11 +257,11 @@ impl Routine for CreateClickhouseMountVolume {
}

pub struct CreateModelsVolume {
project: Project,
project: Arc<Project>,
}

impl CreateModelsVolume {
fn new(project: Project) -> Self {
fn new(project: Arc<Project>) -> Self {
Self { project }
}
}
Expand Down Expand Up @@ -295,11 +296,11 @@ impl Routine for CreateModelsVolume {
}

pub struct CreateDockerComposeFile {
project: Project,
project: Arc<Project>,
}

impl CreateDockerComposeFile {
pub fn new(project: Project) -> Self {
pub fn new(project: Arc<Project>) -> Self {
Self { project }
}
}
Expand All @@ -325,11 +326,11 @@ impl Routine for CreateDockerComposeFile {
}

pub struct CreateBaseAppFiles {
project: Project,
project: Arc<Project>,
}

impl CreateBaseAppFiles {
pub fn new(project: Project) -> Self {
pub fn new(project: Arc<Project>) -> Self {
Self { project }
}
}
Expand Down
9 changes: 5 additions & 4 deletions apps/framework-cli/src/cli/routines/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::cli::routines::util::ensure_docker_running;
use crate::utilities::constants::CLI_PROJECT_INTERNAL_DIR;
use crate::utilities::docker;
use crate::{cli::display::Message, project::Project};
use std::sync::Arc;

use super::{
initialize::ValidateMountVolumes,
Expand All @@ -14,10 +15,10 @@ use super::{
};

pub struct RunLocalInfrastructure {
project: Project,
project: Arc<Project>,
}
impl RunLocalInfrastructure {
pub fn new(project: Project) -> Self {
pub fn new(project: Arc<Project>) -> Self {
Self { project }
}
}
Expand Down Expand Up @@ -56,11 +57,11 @@ impl Routine for RunLocalInfrastructure {
}

struct RunContainers {
project: Project,
project: Arc<Project>,
}

impl RunContainers {
fn new(project: Project) -> Self {
fn new(project: Arc<Project>) -> Self {
Self { project }
}
}
Expand Down
Loading

0 comments on commit 8de8002

Please sign in to comment.