Skip to content

Commit

Permalink
fix: publish command
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsik-sus committed Nov 9, 2023
1 parent c6ed353 commit 258fd19
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"module/move/*",
# "module/step/*",
# "module/core/*/examples/*",
"module/willbe_asset/*",
]
exclude = [
"-*",
Expand Down
14 changes: 4 additions & 10 deletions module/move/willbe/src/command/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@ mod private

pub fn publish( ( args, properties ) : ( Args, Props ) ) -> Result< () >
{
let patterns : Vec< _ > = args.get_owned( 0 ).unwrap_or_default();
let dry = properties.get_owned( "dry" ).map( | dry : String | dry.to_bool_like() ).unwrap_or_else( || BoolLike::True ).into();
let patterns : Vec< _ > = args.get_owned( 0 ).unwrap_or_else( || vec![ "./".into() ] );
let dry : bool = properties.get_owned( "dry" ).map( | dry : String | dry.to_bool_like() ).unwrap_or_else( || BoolLike::True ).into();

match if patterns.is_empty()
{
endpoint::publish( [ "./".into() ].into(), dry )
}
else
{
endpoint::publish( patterns, dry )
}
println!( "`publish` command patterns: {patterns:?}, dry: {dry}" );
match endpoint::publish( patterns, dry )
{
core::result::Result::Ok( report ) =>
{
Expand Down
40 changes: 31 additions & 9 deletions module/move/willbe/src/endpoint/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ mod private
{
files,
manifest,
path,
};
use anyhow::Error;
use std::
{
env,
path::PathBuf,
collections::HashSet,
};
use core::fmt::Formatter;
use cargo_metadata::
Expand All @@ -31,6 +31,12 @@ mod private
{
fn fmt( &self, f : &mut Formatter< '_ > ) -> std::fmt::Result
{
if self.packages.is_empty()
{
f.write_fmt( format_args!( "Nothing to publish" ) )?;
return Ok( () );
}

for ( path, report ) in &self.packages
{
f.write_fmt( format_args!( "[ {} ]\n{report:#?}\n", path.display() ) )?;
Expand All @@ -49,26 +55,42 @@ mod private
let mut report = PublishReport::default();

let current_path = env::current_dir().map_err( | e | ( report.clone(), e.into() ) )?;

let paths = files::find( &current_path, &patterns );
let mut paths = paths.iter().filter_map( | s | if s.ends_with( "Cargo.toml" ) { Some( s.into() ) } else { None } ).collect::< Vec< PathBuf > >();

if !patterns.is_empty() && paths.is_empty() && path::valid_is( &patterns[ 0 ] )
let mut paths = HashSet::new();
for pattern in &patterns
{
paths.push( PathBuf::from( &patterns[ 0 ] ) );
let current_path = std::path::Path::new( pattern ).canonicalize().map_err( | e | ( report.clone(), e.into() ) )?;
#[ cfg( target_os = "windows" ) ] // canonicalization on windows adds `\\?\` prefix
let current_path =
{
const VERBATIM_PREFIX : &str = r#"\\?\"#;
let p = current_path.display().to_string();
if p.starts_with( VERBATIM_PREFIX )
{
PathBuf::from( &p[ VERBATIM_PREFIX.len() .. ] )
}
else
{
current_path
}
};
let current_paths = files::find( current_path, &[ "**/Cargo.toml" ] );
paths.extend( current_paths );
}

let paths = paths.iter().filter_map( | s | if s.ends_with( "Cargo.toml" ) { Some( s.into() ) } else { None } ).collect::< Vec< PathBuf > >();

for path in paths
{
package::publish( &current_path, &path, dry )
let current_report = package::publish( &current_path, &path, dry )
.map_err
(
| ( current_report, e ) |
{
report.packages.push(( path, current_report.clone() ));
report.packages.push(( path.clone(), current_report.clone() ));
( report.clone(), e.context( "Publish list of packages" ).into() )
}
)?;
report.packages.push(( path, current_report.clone() ));
}

Ok( report )
Expand Down
62 changes: 43 additions & 19 deletions module/move/willbe/src/package/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod private
fs,
path::PathBuf,
collections::HashMap,
fmt::Write,
};
use cargo_metadata::
{
Expand Down Expand Up @@ -33,6 +32,7 @@ mod private
pub struct PublishReport
{
get_info : Option< process::CmdReport >,
bump : Option< String >,
commit : Option< process::CmdReport >,
push : Option< process::CmdReport >,
publish : Option< process::CmdReport >,
Expand All @@ -57,6 +57,10 @@ mod private
package_dir.pop();

let output = process::start_sync( "cargo package", &package_dir ).context( "Take information about package" ).map_err( | e | ( report.clone(), e ) )?;
if output.err.contains( "not yet committed")
{
return Err(( report, anyhow!( "Some changes wasn't committed. Please, commit or stash that changes and try again." ) ));
}
report.get_info = Some( output );

let name = &data[ "package" ][ "name" ].clone();
Expand All @@ -73,37 +77,58 @@ mod private

if digest_of_local != digest_of_remote
{
data[ "package" ][ "version" ] = bump( version ).map_err( | e | ( report.clone(), e ) )?;
let version = &data[ "package" ][ "version" ].clone();
let version = version.as_str().ok_or( anyhow!( "Failed to take package version after bump" ) ).map_err( | e | ( report.clone(), e ) )?;
manifest.store().map_err( | e | ( report.clone(), e ) )?;

if dry
{
let buf = format!( "git commit --dry-run -am \"{} v{}\"", name, version );
let output = process::start_sync( &buf, current_path ).context( "Dry commit while publishing" ).map_err( | e | ( report.clone(), e ) )?;
report.bump = Some( "Bump package version".into() );

let buf = format!( "git commit -am {}-v{}", name, version );
let output = process::CmdReport
{
command : buf,
path : current_path.clone(),
out : String::new(),
err : String::new(),
};
report.commit = Some( output );

let output = process::start_sync( "git push --dry-run", current_path ).context( "Dry push while publishing" ).map_err( | e | ( report.clone(), e ) )?;
let buf = "git push".to_string();
let output = process::CmdReport
{
command : buf,
path : current_path.clone(),
out : String::new(),
err : String::new(),
};
report.push = Some( output );

let output = process::start_sync( "cargo publish --dry-run --allow-dirty", &package_dir ).context( "Dry publish" ).map_err( | e | ( report.clone(), e ) )?;
let buf = "cargo publish".to_string();
let output = process::CmdReport
{
command : buf,
path : package_dir.clone(),
out : String::new(),
err : String::new(),
};
report.publish = Some( output );

// ?
// let buf = format!( "git checkout {:?}", &package_dir );
// let output = process::start_sync( &buf, current_path )?;
}
else
{
let buf = format!( "git commit -am \"{} v{}\"", name, version );
data[ "package" ][ "version" ] = bump( version ).map_err( | e | ( report.clone(), e ) )?;
let version = &data[ "package" ][ "version" ].clone();
let version = version.as_str().ok_or( anyhow!( "Failed to take package version after bump" ) ).map_err( | e | ( report.clone(), e ) )?;
manifest.store().map_err( | e | ( report.clone(), e ) )?;
report.bump = Some( "Bump package version".into() );

let buf = format!( "git commit -am {}-v{}", name, version );
let output = process::start_sync( &buf, current_path ).context( "Commit changes while publishing" ).map_err( | e | ( report.clone(), e ) )?;
report.commit = Some( output );

let output = process::start_sync( "git push", current_path ).context( "Push while publishing" ).map_err( | e | ( report.clone(), e ) )?;
let buf = "git push".to_string();
let output = process::start_sync( &buf, current_path ).context( "Push while publishing" ).map_err( | e | ( report.clone(), e ) )?;
report.push = Some( output );

let output = process::start_sync( "cargo publish", &package_dir ).context( "Publish" ).map_err( | e | ( report.clone(), e ) )?;
let buf = "cargo publish".to_string();
let output = process::start_sync( &buf, &package_dir ).context( "Publish" ).map_err( | e | ( report.clone(), e ) )?;
report.publish = Some( output );
}
}
Expand Down Expand Up @@ -136,8 +161,7 @@ mod private

pub fn local_path_get< 'a >( name : &'a str, version : &'a str, manifest_path : &'a PathBuf ) -> PathBuf
{
let mut buf = String::new();
write!( &mut buf, "package/{0}-{1}.crate", name, version ).unwrap();
let buf = format!( "package/{0}-{1}.crate", name, version );

let package_metadata = MetadataCommand::new()
.manifest_path( manifest_path )
Expand Down

0 comments on commit 258fd19

Please sign in to comment.