@@ -7,6 +7,7 @@ use crate::fs::Fs;
7
7
use anyhow:: { anyhow, bail, Context } ;
8
8
use clap:: Parser ;
9
9
use regex:: Regex ;
10
+ use std:: borrow:: Cow ;
10
11
use std:: path:: { Path , PathBuf } ;
11
12
12
13
#[ derive( Parser , Debug ) ]
@@ -27,35 +28,42 @@ pub async fn subcommand_upgrade_runtime_crates_version(
27
28
. with_context ( || format ! ( "{} is not a valid semver version" , & args. version) ) ?;
28
29
let fs = Fs :: Real ;
29
30
let gradle_properties = read_gradle_properties ( fs, & args. gradle_properties_path ) . await ?;
31
+ let updated_gradle_properties = update_gradle_properties ( & gradle_properties, & upgraded_version)
32
+ . with_context ( || {
33
+ format ! (
34
+ "Failed to extract the expected runtime crates version from `{:?}`" ,
35
+ & args. gradle_properties_path
36
+ )
37
+ } ) ?;
38
+ update_gradle_properties_file (
39
+ fs,
40
+ & args. gradle_properties_path ,
41
+ updated_gradle_properties. as_ref ( ) ,
42
+ )
43
+ . await ?;
44
+ Ok ( ( ) )
45
+ }
46
+
47
+ fn update_gradle_properties < ' a > (
48
+ gradle_properties : & ' a str ,
49
+ upgraded_version : & ' a semver:: Version ,
50
+ ) -> Result < Cow < ' a , str > , anyhow:: Error > {
30
51
let version_regex =
31
52
Regex :: new ( r"(?P<field>smithy\.rs\.runtime\.crate\.version=)(?P<version>\d+\.\d+\.\d+.*)" )
32
53
. unwrap ( ) ;
33
- let current_version = version_regex. captures ( & gradle_properties) . ok_or_else ( || {
34
- anyhow ! (
35
- "Failed to extract the expected runtime crates version from `{:?}`" ,
36
- & args. gradle_properties_path
37
- )
38
- } ) ?;
54
+ let current_version = version_regex
55
+ . captures ( gradle_properties)
56
+ . ok_or_else ( || anyhow ! ( "Failed to extract the expected runtime crates version" ) ) ?;
39
57
let current_version = current_version. name ( "version" ) . unwrap ( ) ;
40
58
let current_version = semver:: Version :: parse ( current_version. as_str ( ) )
41
59
. with_context ( || format ! ( "{} is not a valid semver version" , current_version. as_str( ) ) ) ?;
42
- if current_version > upgraded_version
60
+ if & current_version > upgraded_version
43
61
// Special version tag used on the `main` branch
44
62
&& current_version != semver:: Version :: parse ( "0.0.0-smithy-rs-head" ) . unwrap ( )
45
63
{
46
64
bail ! ( "Moving from {current_version} to {upgraded_version} would be a *downgrade*. This command doesn't allow it!" ) ;
47
65
}
48
- let updated_gradle_properties = version_regex. replace (
49
- & gradle_properties,
50
- format ! ( "${{field}}{}" , upgraded_version) ,
51
- ) ;
52
- update_gradle_properties (
53
- fs,
54
- & args. gradle_properties_path ,
55
- updated_gradle_properties. as_ref ( ) ,
56
- )
57
- . await ?;
58
- Ok ( ( ) )
66
+ Ok ( version_regex. replace ( gradle_properties, format ! ( "${{field}}{}" , upgraded_version) ) )
59
67
}
60
68
61
69
async fn read_gradle_properties ( fs : Fs , path : & Path ) -> Result < String , anyhow:: Error > {
@@ -65,11 +73,32 @@ async fn read_gradle_properties(fs: Fs, path: &Path) -> Result<String, anyhow::E
65
73
Ok ( contents)
66
74
}
67
75
68
- async fn update_gradle_properties (
76
+ async fn update_gradle_properties_file (
69
77
fs : Fs ,
70
78
path : & Path ,
71
79
contents : & str ,
72
80
) -> Result < ( ) , anyhow:: Error > {
73
81
fs. write_file ( path, contents. as_bytes ( ) ) . await ?;
74
82
Ok ( ( ) )
75
83
}
84
+
85
+ #[ cfg( test) ]
86
+ mod tests {
87
+ use crate :: subcommand:: upgrade_runtime_crates_version:: update_gradle_properties;
88
+
89
+ #[ test]
90
+ fn upgrading_works_with_actual_version ( ) {
91
+ let gradle_properties = "smithy.rs.runtime.crate.version=0.54.2" ;
92
+ let version = semver:: Version :: new ( 0 , 54 , 3 ) ;
93
+ let updated = update_gradle_properties ( gradle_properties, & version) . unwrap ( ) ;
94
+ assert_eq ! ( "smithy.rs.runtime.crate.version=0.54.3" , updated) ;
95
+ }
96
+
97
+ #[ test]
98
+ fn upgrading_works_with_dummy_version ( ) {
99
+ let gradle_properties = "smithy.rs.runtime.crate.version=0.0.0-smithy-rs-head" ;
100
+ let version = semver:: Version :: new ( 0 , 54 , 3 ) ;
101
+ let updated = update_gradle_properties ( gradle_properties, & version) . unwrap ( ) ;
102
+ assert_eq ! ( "smithy.rs.runtime.crate.version=0.54.3" , updated) ;
103
+ }
104
+ }
0 commit comments