diff --git a/src/app.rs b/src/app.rs deleted file mode 100644 index e2e8e64..0000000 --- a/src/app.rs +++ /dev/null @@ -1,37 +0,0 @@ -use anyhow::Result; -use chrono::Local; -use semver::Version; -use std::path::PathBuf; -use tera::Context; - -use crate::config::{Config, DEFAULT_FILENAME}; -use crate::writer::Writer; - -pub fn update(base_config: &Config, new_version: &Version) -> Result<()> { - let ctx = make_context(&base_config.current_version, new_version); - let mut writer = Writer::new(&ctx); - writer.add_target( - &PathBuf::from(DEFAULT_FILENAME), - &("current_version = \"{{current_version}}\"".to_string()), - &("current_version = \"{{new_version}}\"".to_string()), - ); - for f in &base_config.files { - writer.add_target(&f.path, &f.search, &f.replace); - } - - match writer.update_all() { - Ok(_) => { - println!("Updated!!"); - Ok(()) - } - Err(err) => Err(err), - } -} - -fn make_context(current_version: &Version, new_version: &Version) -> Context { - let mut ctx = Context::new(); - ctx.insert("current_version", current_version); - ctx.insert("new_version", new_version); - ctx.insert("now", &Local::now().to_rfc3339()); - return ctx; -} diff --git a/src/commands.rs b/src/commands.rs index 05e9533..992ea38 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -10,7 +10,7 @@ use std::env::current_dir; use anyhow::{anyhow, Result}; use clap::{Parser, Subcommand}; -use crate::config::resolve_config; +use crate::workspace::Workspace; #[derive(Parser)] #[command(version, about, long_about = None)] @@ -42,7 +42,7 @@ pub fn run_command() -> Result<()> { return init::execute(&args); }; let pwd = current_dir().unwrap(); - let resolved = resolve_config(&pwd); + let resolved = Workspace::try_new(pwd); if resolved.is_err() { return Err(anyhow!(resolved.unwrap_err())); } diff --git a/src/commands/info.rs b/src/commands/info.rs index 22d3dfd..ebe0f99 100644 --- a/src/commands/info.rs +++ b/src/commands/info.rs @@ -1,8 +1,8 @@ /* 'info' command displays data from config-file. */ -use crate::config::Config; use crate::config::DEFAULT_FILENAME; use crate::versioning; +use crate::workspace::Workspace; use anyhow::Result; use clap::Args; @@ -11,15 +11,24 @@ use tera::{Context, Tera}; #[derive(Args)] pub(crate) struct Arguments {} -pub(crate) fn execute(_args: &Arguments, config: &Config) -> Result<()> { +pub(crate) fn execute(_args: &Arguments, workspace: &Workspace) -> Result<()> { let mut ctx = Context::new(); let mut files: Vec = Vec::new(); files.push(DEFAULT_FILENAME.to_string()); - ctx.insert("current_version", &config.current_version); - ctx.insert("next_major", &versioning::up_major(&config.current_version)); - ctx.insert("next_minor", &versioning::up_minor(&config.current_version)); - ctx.insert("next_patch", &versioning::up_patch(&config.current_version)); - for f in &config.files { + ctx.insert("current_version", &workspace.config.current_version); + ctx.insert( + "next_major", + &versioning::up_major(&workspace.config.current_version), + ); + ctx.insert( + "next_minor", + &versioning::up_minor(&workspace.config.current_version), + ); + ctx.insert( + "next_patch", + &versioning::up_patch(&workspace.config.current_version), + ); + for f in &workspace.config.files { files.push(f.path.display().to_string()); } ctx.insert("files", &files); diff --git a/src/commands/major.rs b/src/commands/major.rs index 817fd33..76aa98f 100644 --- a/src/commands/major.rs +++ b/src/commands/major.rs @@ -1,13 +1,14 @@ -use crate::config::Config; -use crate::{app, versioning::up_major}; +use crate::versioning::up_major; +use crate::workspace::{make_context, Workspace}; use anyhow::Result; use clap::Args; #[derive(Args)] pub(crate) struct Arguments {} -pub(crate) fn execute(_args: &Arguments, config: &Config) -> Result<()> { - let new_version = up_major(&config.current_version); - - app::update(&config, &new_version) +pub(crate) fn execute(_args: &Arguments, workspace: &Workspace) -> Result<()> { + let current_version = &workspace.config.current_version; + let new_version = up_major(¤t_version); + let context = make_context(¤t_version, &new_version); + workspace.update_files(&context) } diff --git a/src/commands/minor.rs b/src/commands/minor.rs index df7cf41..ed1fccb 100644 --- a/src/commands/minor.rs +++ b/src/commands/minor.rs @@ -1,13 +1,14 @@ -use crate::config::Config; -use crate::{app, versioning::up_minor}; +use crate::versioning::up_minor; +use crate::workspace::{make_context, Workspace}; use anyhow::Result; use clap::Args; #[derive(Args)] pub(crate) struct Arguments {} -pub(crate) fn execute(_args: &Arguments, config: &Config) -> Result<()> { - let new_version = up_minor(&config.current_version); - - app::update(&config, &new_version) +pub(crate) fn execute(_args: &Arguments, workspace: &Workspace) -> Result<()> { + let current_version = &workspace.config.current_version; + let new_version = up_minor(¤t_version); + let context = make_context(¤t_version, &new_version); + workspace.update_files(&context) } diff --git a/src/commands/patch.rs b/src/commands/patch.rs index 7a8f804..0244cfd 100644 --- a/src/commands/patch.rs +++ b/src/commands/patch.rs @@ -1,13 +1,14 @@ -use crate::config::Config; -use crate::{app, versioning::up_patch}; +use crate::versioning::up_patch; +use crate::workspace::{make_context, Workspace}; use anyhow::Result; use clap::Args; #[derive(Args)] pub(crate) struct Arguments {} -pub(crate) fn execute(_args: &Arguments, config: &Config) -> Result<()> { - let new_version = up_patch(&config.current_version); - - app::update(&config, &new_version) +pub(crate) fn execute(_args: &Arguments, workspace: &Workspace) -> Result<()> { + let current_version = &workspace.config.current_version; + let new_version = up_patch(¤t_version); + let context = make_context(¤t_version, &new_version); + workspace.update_files(&context) } diff --git a/src/commands/update.rs b/src/commands/update.rs index b8de37d..584ac90 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,5 +1,4 @@ -use crate::app; -use crate::config::Config; +use crate::workspace::{make_context, Workspace}; use anyhow::Result; use clap::Args; use semver::Version; @@ -9,6 +8,8 @@ pub(crate) struct Arguments { new_version: Version, } -pub(crate) fn execute(args: &Arguments, config: &Config) -> Result<()> { - app::update(&config, &args.new_version) +pub(crate) fn execute(args: &Arguments, workspace: &Workspace) -> Result<()> { + let current_version = &workspace.config.current_version; + let context = make_context(¤t_version, &args.new_version); + workspace.update_files(&context) } diff --git a/src/main.rs b/src/main.rs index 7a522ed..0ba8a22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ // Entry point. -mod app; mod commands; mod config; mod versioning; +mod workspace; mod writer; use crate::commands::run_command; diff --git a/src/workspace.rs b/src/workspace.rs new file mode 100644 index 0000000..4bb067c --- /dev/null +++ b/src/workspace.rs @@ -0,0 +1,64 @@ +use anyhow::Result; +use chrono::Local; +use semver::Version; +use std::path::PathBuf; +use tera::Context; + +use crate::config::{resolve_config, Config, DEFAULT_FILENAME}; +use crate::writer::Writer; + +/** + * CLI workspace. + * + * .. note:: Under construction + */ +#[derive(Debug)] +pub struct Workspace { + pub config: Config, +} + +impl Workspace { + pub fn try_new(root: PathBuf) -> Result { + let resolved = resolve_config(&root); + if resolved.is_err() { + return Err(resolved.unwrap_err()); + } + Ok(Self { + config: resolved.unwrap(), + }) + } + + #[allow(dead_code)] + fn init_writer(&self, ctx: &Context) -> Writer { + let mut writer = Writer::new(ctx); + writer.add_target( + &PathBuf::from(DEFAULT_FILENAME), + &("current_version = \"{{current_version}}\"".to_string()), + &("current_version = \"{{new_version}}\"".to_string()), + ); + for f in &self.config.files { + writer.add_target(&f.path, &f.search, &f.replace); + } + writer + } + + #[allow(dead_code)] + pub fn update_files(&self, ctx: &Context) -> Result<()> { + let writer = self.init_writer(ctx); + match writer.update_all() { + Ok(_) => { + println!("Updated!"); + Ok(()) + } + Err(err) => Err(err), + } + } +} + +pub fn make_context(current_version: &Version, new_version: &Version) -> Context { + let mut ctx = Context::new(); + ctx.insert("current_version", current_version); + ctx.insert("new_version", new_version); + ctx.insert("now", &Local::now().to_rfc3339()); + return ctx; +}