From 1427284c2a378d29246a7b92d4a5c5d9601793d0 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 27 Jul 2024 08:11:52 +0100 Subject: [PATCH] refactor: merge server crate into cli crate --- crates/cli/Cargo.toml | 18 +++- crates/cli/README.md | 8 +- crates/cli/src/main.rs | 5 +- crates/cli/src/server/mod.rs | 92 ++++++++++++++++++ .../{ => cli/src}/server/server-default.toml | 0 .../{server/src => cli/src/server}/tests.rs | 0 crates/server/Cargo.toml | 23 +---- crates/server/README.md | 2 + crates/server/src/lib.rs | 93 +------------------ 9 files changed, 118 insertions(+), 123 deletions(-) create mode 100644 crates/cli/src/server/mod.rs rename crates/{ => cli/src}/server/server-default.toml (100%) rename crates/{server/src => cli/src/server}/tests.rs (100%) diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 7927c623..2f5cbc7b 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -13,7 +13,8 @@ categories = { workspace = true } [dependencies] git-next-core = { workspace = true } -git-next-server = { workspace = true } +git-next-file-watcher-actor = { workspace = true } +git-next-server-actor = { workspace = true } # CLI parsing clap = { workspace = true } @@ -21,6 +22,21 @@ clap = { workspace = true } # fs/network kxio = { workspace = true } +# logging +console-subscriber = { workspace = true } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } + +# Actors +actix = { workspace = true } +actix-rt = { workspace = true } + +[dev-dependencies] +# Testing +assert2 = { workspace = true } +secrecy = { workspace = true } +test-log = { workspace = true } + [lints.clippy] nursery = { level = "warn", priority = -1 } # pedantic = "warn" diff --git a/crates/cli/README.md b/crates/cli/README.md index e62276ad..c27d6463 100644 --- a/crates/cli/README.md +++ b/crates/cli/README.md @@ -520,8 +520,8 @@ The following diagram shows the dependency between the crates that make up `git- stateDiagram-v2 cli --> core - - cli --> server + cli --> server_actor + cli --> file_watcher_actor forge --> core forge --> forge_forgejo @@ -534,10 +534,6 @@ stateDiagram-v2 repo_actor --> core repo_actor --> forge - server --> core - server --> file_watcher_actor - server --> server_actor - server_actor --> core server_actor --> forge server_actor --> repo_actor diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 2afbbb04..ee3ee789 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1,5 +1,6 @@ // mod init; +mod server; #[cfg(test)] mod tests; @@ -41,11 +42,11 @@ fn main() { } Command::Server(server) => match server { Server::Init => { - git_next_server::init(fs); + server::init(fs); } Server::Start => { let sleep_duration = std::time::Duration::from_secs(10); - git_next_server::start(fs, net, repository_factory, sleep_duration); + server::start(fs, net, repository_factory, sleep_duration); } }, } diff --git a/crates/cli/src/server/mod.rs b/crates/cli/src/server/mod.rs new file mode 100644 index 00000000..91953faf --- /dev/null +++ b/crates/cli/src/server/mod.rs @@ -0,0 +1,92 @@ +// +#[cfg(test)] +mod tests; + +use actix::prelude::*; + +use git_next_core::git::RepositoryFactory; +use git_next_file_watcher_actor::{FileUpdated, FileWatcher}; +use git_next_server_actor::ServerActor; + +use kxio::{fs::FileSystem, network::Network}; +use tracing::{error, info, level_filters::LevelFilter}; + +use std::path::PathBuf; + +pub fn init(fs: FileSystem) { + let file_name = "git-next-server.toml"; + let pathbuf = PathBuf::from(file_name); + let Ok(exists) = fs.path_exists(&pathbuf) else { + eprintln!("Could not check if file exist: {}", file_name); + return; + }; + if exists { + eprintln!( + "The configuration file already exists at {} - not overwritting it.", + file_name + ); + } else { + match fs.file_write(&pathbuf, include_str!("server-default.toml")) { + Ok(_) => println!("Created a default configuration file at {}", file_name), + Err(e) => { + eprintln!("Failed to write to the configuration file: {}", e) + } + } + } +} + +pub fn start( + fs: FileSystem, + net: Network, + repo: Box, + sleep_duration: std::time::Duration, +) { + init_logging(); + + info!("Starting Server..."); + let execution = async move { + let server = ServerActor::new(fs.clone(), net.clone(), repo, sleep_duration).start(); + server.do_send(FileUpdated); + + info!("Starting File Watcher..."); + let fw = match FileWatcher::new("git-next-server.toml".into(), server.clone().recipient()) { + Ok(fw) => fw, + Err(err) => { + error!(?err, "Failed to start file watcher"); + return; + } + }; + fw.start(); + + info!("Server running - Press Ctrl-C to stop..."); + let _ = actix_rt::signal::ctrl_c().await; + info!("Ctrl-C received, shutting down..."); + server.do_send(git_next_server_actor::messages::Shutdown); + actix_rt::time::sleep(std::time::Duration::from_millis(200)).await; + System::current().stop(); + }; + let system = System::new(); + Arbiter::current().spawn(execution); + if let Err(err) = system.run() { + tracing::error!(?err, "") + }; +} + +pub fn init_logging() { + use tracing_subscriber::prelude::*; + use tracing_subscriber::EnvFilter; + + let subscriber = tracing_subscriber::fmt::layer() + .with_target(false) + .with_file(true) + .with_line_number(true) + .with_filter( + EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .from_env_lossy(), + ); + tracing_subscriber::registry() + .with(console_subscriber::ConsoleLayer::builder().spawn()) + .with(subscriber) + .init(); +} diff --git a/crates/server/server-default.toml b/crates/cli/src/server/server-default.toml similarity index 100% rename from crates/server/server-default.toml rename to crates/cli/src/server/server-default.toml diff --git a/crates/server/src/tests.rs b/crates/cli/src/server/tests.rs similarity index 100% rename from crates/server/src/tests.rs rename to crates/cli/src/server/tests.rs diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index 5eb6afc5..6541ce03 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -4,30 +4,9 @@ version = { workspace = true } edition = { workspace = true } license = { workspace = true } repository = { workspace = true } -description = "server for git-next, the trunk-based development manager" +description = "[deprecated crate] server for git-next, the trunk-based development manager" -[dependencies] -git-next-core = { workspace = true } -git-next-file-watcher-actor = { workspace = true } -git-next-server-actor = { workspace = true } -# logging -console-subscriber = { workspace = true } -tracing = { workspace = true } -tracing-subscriber = { workspace = true } - -# fs/network -kxio = { workspace = true } - -# Actors -actix = { workspace = true } -actix-rt = { workspace = true } - -[dev-dependencies] -# Testing -assert2 = { workspace = true } -secrecy = { workspace = true } -test-log = { workspace = true } [lints.clippy] nursery = { level = "warn", priority = -1 } diff --git a/crates/server/README.md b/crates/server/README.md index ddc70e2a..b90af458 100644 --- a/crates/server/README.md +++ b/crates/server/README.md @@ -7,3 +7,5 @@ development workflows where each commit must pass CI before being included in the main branch. See [git-next](https://crates.io/crates/git-next) for more information. + +N.B. this crate has been merged into [git-next](https://crates.io/git-next). diff --git a/crates/server/src/lib.rs b/crates/server/src/lib.rs index a4139061..23ddbadb 100644 --- a/crates/server/src/lib.rs +++ b/crates/server/src/lib.rs @@ -1,92 +1 @@ -// -#[cfg(test)] -mod tests; - -use actix::prelude::*; - -use git_next_core::git::RepositoryFactory; -use git_next_file_watcher_actor::{FileUpdated, FileWatcher}; -use git_next_server_actor::ServerActor; - -use kxio::{fs::FileSystem, network::Network}; -use tracing::{error, info, level_filters::LevelFilter}; - -use std::path::PathBuf; - -pub fn init(fs: FileSystem) { - let file_name = "git-next-server.toml"; - let pathbuf = PathBuf::from(file_name); - let Ok(exists) = fs.path_exists(&pathbuf) else { - eprintln!("Could not check if file exist: {}", file_name); - return; - }; - if exists { - eprintln!( - "The configuration file already exists at {} - not overwritting it.", - file_name - ); - } else { - match fs.file_write(&pathbuf, include_str!("../server-default.toml")) { - Ok(_) => println!("Created a default configuration file at {}", file_name), - Err(e) => { - eprintln!("Failed to write to the configuration file: {}", e) - } - } - } -} - -pub fn start( - fs: FileSystem, - net: Network, - repo: Box, - sleep_duration: std::time::Duration, -) { - init_logging(); - - info!("Starting Server..."); - let execution = async move { - let server = ServerActor::new(fs.clone(), net.clone(), repo, sleep_duration).start(); - server.do_send(FileUpdated); - - info!("Starting File Watcher..."); - let fw = match FileWatcher::new("git-next-server.toml".into(), server.clone().recipient()) { - Ok(fw) => fw, - Err(err) => { - error!(?err, "Failed to start file watcher"); - return; - } - }; - fw.start(); - - info!("Server running - Press Ctrl-C to stop..."); - let _ = actix_rt::signal::ctrl_c().await; - info!("Ctrl-C received, shutting down..."); - server.do_send(git_next_server_actor::messages::Shutdown); - actix_rt::time::sleep(std::time::Duration::from_millis(200)).await; - System::current().stop(); - }; - let system = System::new(); - Arbiter::current().spawn(execution); - if let Err(err) = system.run() { - tracing::error!(?err, "") - }; -} - -pub fn init_logging() { - use tracing_subscriber::prelude::*; - use tracing_subscriber::EnvFilter; - - let subscriber = tracing_subscriber::fmt::layer() - .with_target(false) - .with_file(true) - .with_line_number(true) - .with_filter( - EnvFilter::builder() - .with_default_directive(LevelFilter::INFO.into()) - .from_env_lossy(), - ); - tracing_subscriber::registry() - .with(console_subscriber::ConsoleLayer::builder().spawn()) - .with(subscriber) - .init(); -} +// moved to /crates/cli/src/server