Skip to content

Commit

Permalink
refactor: Comamnd func handle workspace instead of config directly
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Apr 1, 2024
1 parent a415441 commit 4693865
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 69 deletions.
37 changes: 0 additions & 37 deletions src/app.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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()));
}
Expand Down
23 changes: 16 additions & 7 deletions src/commands/info.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String> = 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);
Expand Down
13 changes: 7 additions & 6 deletions src/commands/major.rs
Original file line number Diff line number Diff line change
@@ -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(&current_version);
let context = make_context(&current_version, &new_version);
workspace.update_files(&context)
}
13 changes: 7 additions & 6 deletions src/commands/minor.rs
Original file line number Diff line number Diff line change
@@ -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(&current_version);
let context = make_context(&current_version, &new_version);
workspace.update_files(&context)
}
13 changes: 7 additions & 6 deletions src/commands/patch.rs
Original file line number Diff line number Diff line change
@@ -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(&current_version);
let context = make_context(&current_version, &new_version);
workspace.update_files(&context)
}
9 changes: 5 additions & 4 deletions src/commands/update.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(&current_version, &args.new_version);
workspace.update_files(&context)
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Entry point.
mod app;
mod commands;
mod config;
mod versioning;
mod workspace;
mod writer;
use crate::commands::run_command;

Expand Down
64 changes: 64 additions & 0 deletions src/workspace.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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;
}

0 comments on commit 4693865

Please sign in to comment.