#[ allow( clippy ::std_instead_of_alloc, clippy ::std_instead_of_core ) ]
mod private
{
#[ allow( unused_imports, clippy ::wildcard_imports ) ]
use crate ::tool :: *;
use std ::ffi ::OsString;
use std ::path ::PathBuf;
use former ::Former;
use process_tools ::process;
use crate ::channel ::Channel;
use std ::result ::Result :: { Ok, Err };
#[ derive( Debug, Former, Clone ) ]
#[ allow( clippy ::struct_excessive_bools ) ]
pub struct PackOptions
{
pub( crate ) path: PathBuf,
pub( crate ) channel: Channel,
#[ former( default = true ) ]
pub( crate ) allow_dirty: bool,
#[ former( default = false ) ]
pub( crate ) checking_consistency: bool,
pub( crate ) temp_path: Option< PathBuf >,
pub( crate ) dry: bool,
}
impl PackOptionsFormer
{
pub fn option_temp_path( mut self, value: impl Into< Option< PathBuf > > ) -> Self
{
self.storage.temp_path = value.into();
self
}
}
impl PackOptions
{
#[ allow( clippy ::if_not_else ) ]
fn to_pack_args( &self ) -> Vec< String >
{
let manifest_path = self.path.join( "Cargo.toml" );
let normalized_manifest_path = manifest_path.to_string_lossy().replace( '\\', "/" );
[ "run".to_string(), self.channel.to_string(), "cargo".into(), "package".into() ]
.into_iter()
.chain( Some( "--manifest-path".to_string() ) )
.chain( Some( normalized_manifest_path ) )
.chain( if self.allow_dirty { Some( "--allow-dirty".to_string() ) } else { None } )
.chain( if !self.checking_consistency { Some( "--no-verify".to_string() ) } else { None } )
.chain( self.temp_path.clone().map( | p | vec![ "--target-dir".to_string(), p.to_string_lossy().into() ] ).into_iter().flatten() )
.collect()
}
}
#[ cfg_attr
(
feature = "tracing",
track_caller,
tracing ::instrument( fields( caller = ?{ let x = std ::panic ::Location ::caller(); ( x.file(), x.line() ) } ) )
)]
pub fn pack( args: PackOptions ) -> error ::untyped ::Result< process ::Report >
{
let ( program, options ) = ( "rustup", args.to_pack_args() );
if args.dry
{
Ok
(
process ::Report
{
command: format!( "{program} {}", options.join( " " ) ),
out: String ::new(),
err: String ::new(),
current_path: args.path.clone(),
error: Ok( () ),
}
)
}
else
{
process ::Run ::former()
.bin_path( program )
.args( options.into_iter().map( OsString ::from ).collect :: < Vec< _ > >() )
.current_path( args.path )
.run().map_err( | report | error ::untyped ::format_err!( report.to_string() ) )
}
}
#[ derive( Debug, Former, Clone, Default ) ]
pub struct PublishOptions
{
pub( crate ) path: PathBuf,
pub( crate ) temp_path: Option< PathBuf >,
#[ former( default = 0usize ) ]
pub( crate ) retry_count: usize,
pub( crate ) dry: bool,
}
impl PublishOptionsFormer
{
pub fn option_temp_path( mut self, value: impl Into< Option< PathBuf > > ) -> Self
{
self.storage.temp_path = value.into();
self
}
}
impl PublishOptions
{
fn as_publish_args( &self ) -> Vec< String >
{
let target_dir = self.temp_path.clone().map( | p | vec![ "--target-dir".to_string(), p.to_string_lossy().into() ] );
[ "publish".to_string() ].into_iter().chain( target_dir.into_iter().flatten() ).collect()
}
}
#[ cfg_attr
(
feature = "tracing",
track_caller,
tracing ::instrument( fields( caller = ?{ let x = std ::panic ::Location ::caller(); ( x.file(), x.line() ) } ) )
)]
pub fn publish( args: &PublishOptions ) -> error ::untyped ::Result< process ::Report >
{
let ( program, arguments) = ( "cargo", args.as_publish_args() );
if args.dry
{
Ok
(
process ::Report
{
command: format!( "{program} {}", arguments.join( " " ) ),
out: String ::new(),
err: String ::new(),
current_path: args.path.clone(),
error: Ok( () ),
}
)
}
else
{
let mut results = Vec ::with_capacity( args.retry_count + 1 );
let run_args: Vec< _ > = arguments.into_iter().map( OsString ::from ).collect();
for _ in 0 ..=args.retry_count
{
let result = process ::Run ::former()
.bin_path( program )
.args( run_args.clone() )
.current_path( &args.path )
.run();
match result
{
Ok( report ) => return Ok( report ),
Err( e ) => results.push( e ),
}
}
if args.retry_count > 0
{
Err( error ::untyped ::format_err!
(
"It took {} attempts, but still failed. Here are the errors: \n{}",
args.retry_count + 1,
results
.into_iter()
.map( | r | format!( "- {r}" ) )
.collect :: < Vec< _ > >()
.join( "\n" )
))
}
else
{
Err( results.remove( 0 ) ).map_err( | report | error ::untyped ::format_err!( report.to_string() ) )
}
}
}
}
crate ::mod_interface!
{
own use pack;
own use publish;
own use PublishOptions;
own use PackOptions;
}