Skip to content

Commit

Permalink
Merge pull request #1235 from Barsik-sus/willbe_publish_diff_keep
Browse files Browse the repository at this point in the history
READY : (willbe): Save remote version of the package on `publish.diff`
  • Loading branch information
Wandalen committed Mar 22, 2024
2 parents becb7ed + 87df523 commit 95ef442
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
28 changes: 26 additions & 2 deletions module/move/willbe/src/action/publish_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ mod private
use wtools::error::for_app::Result;
use diff::{ DiffReport, crate_diff };

/// Options for `publish_diff` command
#[ derive( Debug, former::Former ) ]
pub struct PublishDiffOptions
{
path : PathBuf,
keep_archive : Option< PathBuf >,
}

/// Return the differences between a local and remote package versions.
#[ cfg_attr( feature = "tracing", tracing::instrument ) ]
pub fn publish_diff( path : PathBuf ) -> Result< DiffReport >
pub fn publish_diff( o : PublishDiffOptions ) -> Result< DiffReport >
{
let path = AbsolutePath::try_from( path )?;
let path = AbsolutePath::try_from( o.path )?;
let dir = CrateDir::try_from( path )?;

let package = package::Package::try_from( dir.clone() )?;
Expand All @@ -25,6 +33,21 @@ mod private
let l = CrateArchive::read( packed_crate::local_path( name, version, dir )? )?;
let r = CrateArchive::download_crates_io( name, version ).unwrap();

if let Some( out_path ) = o.keep_archive
{
_ = std::fs::create_dir_all( &out_path );
for path in r.list()
{
let local_path = out_path.join( path );
let folder = local_path.parent().unwrap();
_ = std::fs::create_dir_all( folder );

let content = r.content_bytes( path ).unwrap();

std::fs::write( local_path, content )?;
}
}

Ok( crate_diff( &l, &r ) )
}
}
Expand All @@ -33,6 +56,7 @@ mod private

crate::mod_interface!
{
orphan use PublishDiffOptions;
/// Publishes the difference between the local and published versions of a package.
orphan use publish_diff;
}
5 changes: 5 additions & 0 deletions module/move/willbe/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ pub( crate ) mod private
.kind( Type::Path )
.optional( true )
.end()
.property( "keep_archive" )
.hint( "Save remote package version to the specified path" )
.kind( Type::Path )
.optional( true )
.end()
.routine( command::publish_diff )
.end()

Expand Down
37 changes: 34 additions & 3 deletions module/move/willbe/src/command/publish_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ mod private
use crate::*;

use std::path::PathBuf;
use wca::Args;
use wca::{ Args, Props };

use wtools::error::Result;
use _path::AbsolutePath;

#[ derive( former::Former ) ]
struct PublishDiffProperties
{
keep_archive : Option< PathBuf >,
}

/// Command to display the differences between a local and remote package versions.
///
Expand All @@ -20,14 +27,38 @@ mod private
/// # Errors
///
/// Returns an error if there is an issue with the command.
pub fn publish_diff( args : Args ) -> Result< () >
pub fn publish_diff( args : Args, props : Props ) -> Result< () >
{
let path : PathBuf = args.get_owned( 0 ).unwrap_or( std::env::current_dir()? );
let PublishDiffProperties { keep_archive } = props.try_into()?;

println!( "{}", action::publish_diff( path )? );
let mut o = action::PublishDiffOptions::former()
.path( path );
if let Some( k ) = keep_archive.clone() { o = o.keep_archive( k ); }
let o = o.form();

println!( "{}", action::publish_diff( o )? );
if let Some( keep ) = keep_archive
{
let keep = AbsolutePath::try_from( keep ).unwrap();
println!( "Remote version of the package was saved at `{}`", keep.as_ref().display() );
}

Ok( () )
}

impl TryFrom< Props > for PublishDiffProperties
{
type Error = wtools::error::for_app::Error;
fn try_from( value : Props ) -> Result< Self, Self::Error >
{
let mut this = Self::former();

this = if let Some( v ) = value.get_owned( "keep_archive" ) { this.keep_archive::< PathBuf >( v ) } else { this };

Ok( this.form() )
}
}
}

//
Expand Down

0 comments on commit 95ef442

Please sign in to comment.