From 87df523709b1cccdec7f4a73e163079d0c49fb1d Mon Sep 17 00:00:00 2001 From: Barsik-sus Date: Fri, 22 Mar 2024 11:27:29 +0200 Subject: [PATCH] Save remote version of the package --- module/move/willbe/src/action/publish_diff.rs | 28 +++++++++++++- module/move/willbe/src/command/mod.rs | 5 +++ .../move/willbe/src/command/publish_diff.rs | 37 +++++++++++++++++-- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/module/move/willbe/src/action/publish_diff.rs b/module/move/willbe/src/action/publish_diff.rs index d8f648aba7..06835dce1d 100644 --- a/module/move/willbe/src/action/publish_diff.rs +++ b/module/move/willbe/src/action/publish_diff.rs @@ -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() )?; @@ -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 ) ) } } @@ -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; } diff --git a/module/move/willbe/src/command/mod.rs b/module/move/willbe/src/command/mod.rs index 384d1540f4..7cd16ae169 100644 --- a/module/move/willbe/src/command/mod.rs +++ b/module/move/willbe/src/command/mod.rs @@ -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() diff --git a/module/move/willbe/src/command/publish_diff.rs b/module/move/willbe/src/command/publish_diff.rs index baedd83605..85f0c29dc8 100644 --- a/module/move/willbe/src/command/publish_diff.rs +++ b/module/move/willbe/src/command/publish_diff.rs @@ -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. /// @@ -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() ) + } + } } //