Skip to content

Commit 72c89a0

Browse files
committed
feat: exclude_dev_dependencies flag for .publish and .publish.diff. Default value is true
1 parent 1ce4ea6 commit 72c89a0

File tree

7 files changed

+128
-13
lines changed

7 files changed

+128
-13
lines changed

module/move/willbe/src/action/publish.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ mod private
109109
///
110110
/// # Arguments
111111
/// * `patterns` - A vector of patterns specifying the folders to search for packages.
112+
/// * `exclude_dev_dependencies` - A boolean value indicating whether to exclude dev dependencies from manifest before publish.
112113
/// * `dry` - A boolean value indicating whether to perform a dry run.
113114
/// * `temp` - A boolean value indicating whether to use a temporary directory.
114115
///
@@ -119,6 +120,7 @@ mod private
119120
(
120121
patterns : Vec< String >,
121122
channel : channel::Channel,
123+
exclude_dev_dependencies : bool,
122124
dry : bool,
123125
temp : bool
124126
)
@@ -233,6 +235,7 @@ mod private
233235
.channel( channel )
234236
.workspace_dir( CrateDir::try_from( workspace_root_dir ).unwrap() )
235237
.option_base_temp_dir( dir.clone() )
238+
.exclude_dev_dependencies( exclude_dev_dependencies )
236239
.dry( dry )
237240
.roots( roots )
238241
.packages( queue )

module/move/willbe/src/action/publish_diff.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod private
2222
pub struct PublishDiffOptions
2323
{
2424
path : PathBuf,
25+
exclude_dev_dependencies : bool,
2526
keep_archive : Option< PathBuf >,
2627
}
2728

@@ -141,16 +142,17 @@ mod private
141142
let name = &package.name()?;
142143
let version = &package.version()?;
143144

144-
_ = cargo::pack
145-
(
146-
cargo::PackOptions::former()
147-
.path( dir.as_ref() )
148-
.allow_dirty( true )
149-
.checking_consistency( false )
150-
.dry( false ).form()
151-
)?;
152-
let l = CrateArchive::read( packed_crate::local_path( name, version, dir )? )?;
153-
let r = CrateArchive::download_crates_io( name, version ).unwrap();
145+
_ = cargo::pack
146+
(
147+
cargo::PackOptions::former()
148+
.path( dir.as_ref() )
149+
.allow_dirty( true )
150+
.checking_consistency( false )
151+
.exclude_dev_dependencies( o.exclude_dev_dependencies)
152+
.dry( false ).form()
153+
)?;
154+
let l = CrateArchive::read( packed_crate::local_path( name, version, dir )? )?;
155+
let r = CrateArchive::download_crates_io( name, version ).unwrap();
154156

155157

156158
if let Some( out_path ) = &o.keep_archive

module/move/willbe/src/command/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ mod private
2525
.kind( Type::String )
2626
.optional( true )
2727
.end()
28+
.property( "exclude_dev_dependencies" )
29+
.hint( "Setting this option to true will temporarily remove development dependencies before executing the command, then restore them afterward. Default is `true`." )
30+
.kind( Type::Bool )
31+
.optional( true )
32+
.end()
2833
.property( "dry" )
2934
.hint( "Enables 'dry run'. Does not publish, only simulates. Default is `true`." )
3035
.kind( Type::Bool )
@@ -47,6 +52,11 @@ mod private
4752
.kind( Type::Path )
4853
.optional( true )
4954
.end()
55+
.property( "exclude_dev_dependencies" )
56+
.hint( "Setting this option to true will temporarily remove development dependencies before executing the command, then restore them afterward. Default is `true`." )
57+
.kind( Type::Bool )
58+
.optional( true )
59+
.end()
5060
.property( "keep_archive" )
5161
.hint( "Save remote package version to the specified path" )
5262
.kind( Type::Path )

module/move/willbe/src/command/publish.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ mod private
1616
#[ former( default = Channel::Stable ) ]
1717
channel : Channel,
1818
#[ former( default = true ) ]
19+
exclude_dev_dependencies : bool,
20+
#[ former( default = true ) ]
1921
dry : bool,
2022
#[ former( default = true ) ]
2123
temp : bool,
@@ -52,10 +54,11 @@ mod private
5254
let PublishProperties
5355
{
5456
channel,
57+
exclude_dev_dependencies,
5558
dry,
5659
temp
5760
} = o.props.try_into()?;
58-
let plan = action::publish_plan( patterns, channel, dry, temp )
61+
let plan = action::publish_plan( patterns, channel, exclude_dev_dependencies, dry, temp )
5962
.context( "Failed to plan the publication process" )?;
6063

6164
let mut formatted_plan = String::new();
@@ -110,6 +113,8 @@ mod private
110113
else
111114
{ this };
112115

116+
this = if let Some( v ) = value
117+
.get_owned( "exclude_dev_dependencies" ) { this.exclude_dev_dependencies::< bool >( v ) } else { this };
113118
this = if let Some( v ) = value
114119
.get_owned( "dry" ) { this.dry::< bool >( v ) } else { this };
115120
this = if let Some( v ) = value

module/move/willbe/src/command/publish_diff.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ mod private
1313
#[ derive( former::Former ) ]
1414
struct PublishDiffProperties
1515
{
16+
#[ former( default = true ) ]
17+
exclude_dev_dependencies : bool,
1618
keep_archive : Option< PathBuf >,
1719
}
1820

@@ -33,10 +35,11 @@ mod private
3335
pub fn publish_diff( o : VerifiedCommand ) -> error::untyped::Result< () > // qqq : use typed error
3436
{
3537
let path : PathBuf = o.args.get_owned( 0 ).unwrap_or( std::env::current_dir()? );
36-
let PublishDiffProperties { keep_archive } = o.props.try_into()?;
38+
let PublishDiffProperties { keep_archive, exclude_dev_dependencies } = o.props.try_into()?;
3739

3840
let mut o = action::PublishDiffOptions::former()
39-
.path( path );
41+
.path( path )
42+
.exclude_dev_dependencies( exclude_dev_dependencies );
4043
if let Some( k ) = keep_archive.clone() { o = o.keep_archive( k ); }
4144
let o = o.form();
4245

@@ -57,6 +60,12 @@ mod private
5760
{
5861
let mut this = Self::former();
5962

63+
this = if let Some( v ) = value
64+
.get_owned( "exclude_dev_dependencies" )
65+
{ this.exclude_dev_dependencies::< bool >( v ) }
66+
else
67+
{ this };
68+
6069
this = if let Some( v ) = value
6170
.get_owned( "keep_archive" )
6271
{ this.keep_archive::< PathBuf >( v ) }

module/move/willbe/src/entity/publish.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod private
4242
package : package::Package< 'a >,
4343
channel : channel::Channel,
4444
base_temp_dir : Option< path::PathBuf >,
45+
exclude_dev_dependencies : bool,
4546
#[ former( default = true ) ]
4647
dry : bool,
4748
}
@@ -58,6 +59,7 @@ mod private
5859
channel : self.channel,
5960
allow_dirty : self.dry,
6061
checking_consistency : !self.dry,
62+
exclude_dev_dependencies : self.exclude_dev_dependencies,
6163
temp_path : self.base_temp_dir.clone(),
6264
dry : self.dry,
6365
};
@@ -84,6 +86,7 @@ mod private
8486
{
8587
path : crate_dir.clone().absolute_path().inner(),
8688
temp_path : self.base_temp_dir.clone(),
89+
exclude_dev_dependencies : self.exclude_dev_dependencies,
8790
retry_count : 2,
8891
dry : self.dry,
8992
};
@@ -121,6 +124,10 @@ mod private
121124
/// Release channels for rust.
122125
pub channel : channel::Channel,
123126

127+
/// Setting this option to true will temporarily remove development dependencies before executing the command, then restore them afterward.
128+
#[ allow( dead_code ) ] // former related
129+
pub( crate ) exclude_dev_dependencies : bool,
130+
124131
/// `dry` - A boolean value indicating whether to do a dry run. If set to `true`, the application performs
125132
/// a simulated run without making any actual changes. If set to `false`, the operations are actually executed.
126133
/// This property is optional and defaults to `true`.
@@ -246,6 +253,10 @@ mod private
246253
{
247254
plan = plan.dry( dry );
248255
}
256+
if let Some( exclude_dev_dependencies ) = &self.storage.exclude_dev_dependencies
257+
{
258+
plan = plan.exclude_dev_dependencies( *exclude_dev_dependencies )
259+
}
249260
let plan = plan
250261
.channel( channel )
251262
.package( package )

module/move/willbe/src/tool/cargo.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/// Internal namespace.
22
mod private
33
{
4+
use crate::*;
5+
46
#[ allow( unused_imports ) ]
57
use crate::tool::*;
68

@@ -47,6 +49,9 @@ mod private
4749
// aaa : don't abuse negative form, rename to checking_consistency
4850
// renamed and changed logic
4951
pub( crate ) checking_consistency : bool,
52+
/// Setting this option to true will temporarily remove development dependencies before executing the command, then restore them afterward.
53+
#[ former( default = true ) ]
54+
pub( crate ) exclude_dev_dependencies : bool,
5055
/// An optional temporary path to be used during packaging.
5156
///
5257
/// This field may contain a path to a temporary directory that will be used during the packaging process.
@@ -79,6 +84,53 @@ mod private
7984
}
8085
}
8186

87+
#[ derive( Debug ) ]
88+
struct TemporaryManifestFile
89+
{
90+
original : PathBuf,
91+
temporary : PathBuf,
92+
}
93+
94+
impl TemporaryManifestFile
95+
{
96+
/// Creates a backup copy of the original file, allowing the original file location to serve as a temporary workspace.
97+
/// When the object is dropped, the temporary file at the original location is replaced by the backup, restoring the original file.
98+
fn new( path : impl Into< PathBuf > ) -> error::untyped::Result< Self >
99+
{
100+
let path = path.into();
101+
if !path.ends_with( "Cargo.toml" )
102+
{
103+
error::untyped::bail!( "Wrong path to temporary manifest" );
104+
}
105+
106+
let mut index = 0;
107+
let original = loop
108+
{
109+
let temp_path = PathBuf::from( format!( "{}.temp_{index}", path.display() ) );
110+
if !temp_path.exists()
111+
{
112+
_ = std::fs::copy( &path, &temp_path )?;
113+
break temp_path;
114+
}
115+
index += 1;
116+
};
117+
118+
Ok( Self
119+
{
120+
original,
121+
temporary : path,
122+
})
123+
}
124+
}
125+
126+
impl Drop for TemporaryManifestFile
127+
{
128+
fn drop( &mut self )
129+
{
130+
_ = std::fs::rename( &self.original, &self.temporary ).ok();
131+
}
132+
}
133+
82134
///
83135
/// Assemble the local package into a distributable tarball.
84136
///
@@ -96,6 +148,17 @@ mod private
96148
// qqq : use typed error
97149
pub fn pack( args : PackOptions ) -> error::untyped::Result< process::Report >
98150
{
151+
let _temp = if args.exclude_dev_dependencies
152+
{
153+
let manifest = TemporaryManifestFile::new( args.path.join( "Cargo.toml" ) )?;
154+
let mut file = Manifest::try_from( ManifestFile::try_from( &manifest.temporary )? )?;
155+
let data = file.data();
156+
157+
_ = data.remove( "dev-dependencies" );
158+
file.store()?;
159+
160+
Some( manifest )
161+
} else { None };
99162
let ( program, options ) = ( "rustup", args.to_pack_args() );
100163

101164
if args.dry
@@ -129,6 +192,7 @@ mod private
129192
{
130193
pub( crate ) path : PathBuf,
131194
pub( crate ) temp_path : Option< PathBuf >,
195+
pub( crate ) exclude_dev_dependencies : bool,
132196
#[ former( default = 0usize ) ]
133197
pub( crate ) retry_count : usize,
134198
pub( crate ) dry : bool,
@@ -162,6 +226,17 @@ mod private
162226
pub fn publish( args : PublishOptions ) -> error::untyped::Result< process::Report >
163227
// qqq : use typed error
164228
{
229+
let _temp = if args.exclude_dev_dependencies
230+
{
231+
let manifest = TemporaryManifestFile::new( args.path.join( "Cargo.toml" ) )?;
232+
let mut file = Manifest::try_from( ManifestFile::try_from( &manifest.temporary )? )?;
233+
let data = file.data();
234+
235+
_ = data.remove( "dev-dependencies" );
236+
file.store()?;
237+
238+
Some( manifest )
239+
} else { None };
165240
let ( program, arguments) = ( "cargo", args.as_publish_args() );
166241

167242
if args.dry

0 commit comments

Comments
 (0)