11#![ allow( clippy:: disallowed_macros) ]
22
3- extern crate clap;
4- extern crate walkdir;
5-
63use clap:: { Arg , Command } ;
74use duct:: cmd;
85use regex:: Regex ;
96use std:: env;
10- use std:: ffi:: OsStr ;
117use std:: fs;
12- use std:: fs:: File ;
13- use std:: io:: BufRead ;
14- use std:: io:: BufReader ;
15- use std:: io:: Write ;
8+ use std:: path:: Path ;
169use std:: path:: PathBuf ;
17- use tempfile:: NamedTempFile ;
18- use walkdir:: WalkDir ;
19-
20- static IGNORE_FILES : [ & str ; 5 ] = [
21- "crates/sdk/tests/connect_disconnect_client/Cargo.toml" ,
22- "crates/sdk/tests/test-client/Cargo.toml" ,
23- "crates/sdk/tests/test-counter/Cargo.toml" ,
24- "crates/sqltest/Cargo.toml" ,
25- "crates/testing/Cargo.toml" ,
26- ] ;
27-
28- fn find_files ( start_dir : & str , name : & str ) -> Vec < String > {
29- let mut files = Vec :: new ( ) ;
30- for entry in WalkDir :: new ( start_dir) . into_iter ( ) . filter_map ( |e| e. ok ( ) ) {
31- if entry. file_type ( ) . is_file ( ) && entry. path ( ) . file_name ( ) == Some ( OsStr :: new ( name) ) {
32- if IGNORE_FILES . contains ( & entry. path ( ) . to_string_lossy ( ) . as_ref ( ) ) {
33- continue ;
34- }
35- files. push ( entry. path ( ) . to_string_lossy ( ) . to_string ( ) ) ;
36- }
37- }
38- files
39- }
40-
41- enum FileProcessState {
42- Package ,
43- Dependencies ,
44- }
45-
46- fn process_crate_toml ( path : & PathBuf , upgrade_version : & str , upgrade_package_version : bool ) {
47- println ! ( "Processing file: {}" , path. to_string_lossy( ) ) ;
48-
49- let file = File :: open ( path) . unwrap_or_else ( |_| panic ! ( "File not found: {}" , path. to_string_lossy( ) ) ) ;
50- let reader = BufReader :: new ( file) ;
51- let mut temp_file = NamedTempFile :: new ( ) . expect ( "Failed to create temporary file!" ) ;
52- let mut state = FileProcessState :: Package ;
53- let re = Regex :: new ( r#"(version = ")([^"]+)"# ) . unwrap ( ) ;
54-
55- for line_result in reader. lines ( ) {
56- match line_result {
57- Ok ( line) => {
58- let new_line = match state {
59- FileProcessState :: Package => {
60- if line. contains ( "version = " ) && upgrade_package_version {
61- re. replace ( & line, format ! ( "version = \" {}" , upgrade_version) . as_str ( ) )
62- . into ( )
63- } else if line. contains ( "[dependencies]" ) {
64- state = FileProcessState :: Dependencies ;
65- line
66- } else {
67- line
68- }
69- }
70- FileProcessState :: Dependencies => {
71- if line. starts_with ( "spacetimedb" ) {
72- if !line. contains ( '{' ) {
73- format ! ( "spacetimedb = \" {}\" " , upgrade_version)
74- } else {
75- // Match the version number and capture it
76- re. replace ( & line, format ! ( "version = \" {}" , upgrade_version) . as_str ( ) )
77- . into ( )
78- }
79- } else {
80- line
81- }
82- }
83- } ;
84-
85- writeln ! ( temp_file, "{}" , new_line) . unwrap ( ) ;
86- }
87- Err ( e) => eprintln ! ( "Error reading line: {}" , e) ,
88- }
89- }
90-
91- // Rename the temporary file to replace the original file
92- fs:: rename ( temp_file. path ( ) , path) . expect ( "Failed to overwrite source file." ) ;
93- }
9410
9511fn process_license_file ( upgrade_version : & str ) {
9612 let path = "LICENSE.txt" ;
97- let file = File :: open ( path) . unwrap_or_else ( |_| panic ! ( "File not found: {}" , path) ) ;
98- let reader = BufReader :: new ( file) ;
99- let mut temp_file = NamedTempFile :: new ( ) . expect ( "Failed to create temporary file!" ) ;
100- let re = Regex :: new ( r"(^Licensed Work:\s+SpacetimeDB )([\d\.]+)" ) . unwrap ( ) ;
101-
102- for line_result in reader. lines ( ) {
103- match line_result {
104- Ok ( line) => {
105- let new_line = if line. starts_with ( "Licensed Work" ) {
106- re. replace (
107- & line,
108- format ! ( "{}{}" , & re. captures( & line) . unwrap( ) [ 1 ] , upgrade_version) . as_str ( ) ,
109- )
110- . into ( )
111- } else {
112- line
113- } ;
114- writeln ! ( temp_file, "{}" , new_line) . unwrap ( ) ;
115- }
116- Err ( e) => eprintln ! ( "Error reading line: {}" , e) ,
117- }
118- }
13+ let file = fs:: read_to_string ( path) . unwrap ( ) ;
14+ let re = Regex :: new ( r"(?m)^(Licensed Work:\s+SpacetimeDB )([\d\.]+)$" ) . unwrap ( ) ;
15+ let file = re. replace_all ( & file, |caps : & regex:: Captures | {
16+ format ! ( "{}{}" , & caps[ 1 ] , upgrade_version)
17+ } ) ;
18+ fs:: write ( path, & * file) . unwrap ( ) ;
19+ }
11920
120- // Rename the temporary file to replace the original file
121- fs:: rename ( temp_file. path ( ) , path) . expect ( "Failed to overwrite source file." ) ;
21+ fn edit_toml ( path : impl AsRef < Path > , f : impl FnOnce ( & mut toml_edit:: Document ) ) -> anyhow:: Result < ( ) > {
22+ let path = path. as_ref ( ) ;
23+ let mut doc = fs:: read_to_string ( path) ?. parse :: < toml_edit:: Document > ( ) ?;
24+ f ( & mut doc) ;
25+ fs:: write ( path, doc. to_string ( ) ) ?;
26+ Ok ( ( ) )
12227}
12328
124- fn main ( ) {
29+ fn main ( ) -> anyhow :: Result < ( ) > {
12530 let matches = Command :: new ( "upgrade-version" )
12631 . version ( "1.0" )
12732 . about ( "Upgrades the version of the SpacetimeDB repository" )
@@ -144,22 +49,30 @@ fn main() {
14449
14550 let current_dir = env:: current_dir ( ) . expect ( "No current directory!" ) ;
14651 let dir_name = current_dir. file_name ( ) . expect ( "No current directory!" ) ;
147- if dir_name != "SpacetimeDB" {
148- println ! ( "You must execute this binary from inside of the SpacetimeDB directory, or use --spacetime-path" ) ;
149- return ;
52+ if dir_name != "SpacetimeDB" && dir_name != "public" {
53+ anyhow:: bail!( "You must execute this binary from inside of the SpacetimeDB directory, or use --spacetime-path" ) ;
15054 }
15155
152- for file in find_files ( "crates" , "Cargo.toml" ) {
153- process_crate_toml ( & PathBuf :: from ( file) , version, true ) ;
154- }
155- for file in find_files ( "modules" , "Cargo.toml" ) {
156- process_crate_toml ( & PathBuf :: from ( file) , version, false ) ;
157- }
158- for file in find_files ( "crates" , "Cargo._toml" ) {
159- process_crate_toml ( & PathBuf :: from ( file) , version, false ) ;
160- }
56+ // root Cargo.toml
57+ edit_toml ( "Cargo.toml" , |doc| {
58+ doc[ "workspace" ] [ "package" ] [ "version" ] = toml_edit:: value ( version) ;
59+ for ( key, dep) in doc[ "workspace" ] [ "dependencies" ]
60+ . as_table_like_mut ( )
61+ . expect ( "workspace.dependencies is not a table" )
62+ . iter_mut ( )
63+ {
64+ if key. get ( ) . starts_with ( "spacetime" ) {
65+ dep[ "version" ] = toml_edit:: value ( version)
66+ }
67+ }
68+ } ) ?;
69+
70+ edit_toml ( "crates/cli/src/subcommands/project/rust/Cargo._toml" , |doc| {
71+ doc[ "dependencies" ] [ "spacetimedb" ] = toml_edit:: value ( version) ;
72+ } ) ?;
16173
162- process_crate_toml ( & PathBuf :: from ( "crates/testing/Cargo.toml" ) , version, false ) ;
16374 process_license_file ( version) ;
16475 cmd ! ( "cargo" , "check" ) . run ( ) . expect ( "Cargo check failed!" ) ;
76+
77+ Ok ( ( ) )
16578}
0 commit comments