1
+ use std:: fmt:: Display ;
1
2
use std:: io:: { stdout, stdin, Write } ;
2
3
use std:: path:: { PathBuf , Path } ;
3
4
use crate :: config:: Config ;
4
5
use clap:: Subcommand ;
5
6
use git2:: build:: RepoBuilder ;
6
7
use git2:: { FetchOptions , RemoteCallbacks , Repository } ;
7
8
use colored:: Colorize ;
9
+ use std:: fs;
8
10
9
11
use crate :: { fail, warn, info, done} ;
10
12
use crate :: NiceUnwrap ;
11
13
14
+ #[ derive( Debug , Clone , PartialEq ) ]
15
+ pub struct Version {
16
+ pub major : u32 ,
17
+ pub minor : u32 ,
18
+ pub patch : u32 ,
19
+ }
20
+
21
+ impl Version {
22
+ pub fn to_string ( & self ) -> String {
23
+ self . into ( )
24
+ }
25
+ }
26
+
27
+ impl From < String > for Version {
28
+ fn from ( str : String ) -> Self {
29
+ let mut iter = str. split ( "." ) ;
30
+ let ( major, minor, patch) = (
31
+ iter. next ( ) . and_then ( |n| n. parse :: < u32 > ( ) . ok ( ) ) . nice_unwrap ( "Invalid major part in version" ) ,
32
+ iter. next ( ) . and_then ( |n| n. parse :: < u32 > ( ) . ok ( ) ) . nice_unwrap ( "Invalid minor part in version" ) ,
33
+ iter. next ( ) . and_then ( |n| n. parse :: < u32 > ( ) . ok ( ) ) . nice_unwrap ( "Invalid patch part in version" )
34
+ ) ;
35
+ Version { major, minor, patch }
36
+ }
37
+ }
38
+
39
+ impl From < & Version > for String {
40
+ fn from ( ver : & Version ) -> Self {
41
+ format ! ( "v{}.{}.{}" , ver. major, ver. minor, ver. patch)
42
+ }
43
+ }
44
+
45
+ impl Display for Version {
46
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
47
+ f. write_fmt ( format_args ! ( "v{}.{}.{}" , self . major, self . minor, self . patch) )
48
+ }
49
+ }
50
+
12
51
#[ derive( Subcommand , Debug ) ]
13
52
pub enum Sdk {
14
53
/// Install SDK
@@ -32,7 +71,10 @@ pub enum Sdk {
32
71
/// Set update branch to stable
33
72
#[ clap( conflicts_with( "nightly" ) ) ]
34
73
stable : bool
35
- }
74
+ } ,
75
+
76
+ /// Get SDK version
77
+ Version ,
36
78
}
37
79
38
80
fn uninstall ( config : & mut Config ) -> bool {
@@ -161,6 +203,14 @@ fn update(config: &mut Config, nightly: bool, stable: bool) {
161
203
}
162
204
}
163
205
206
+ pub fn get_version ( config : & mut Config ) -> Version {
207
+ Version :: from (
208
+ fs:: read_to_string (
209
+ config. sdk_path . as_ref ( ) . nice_unwrap ( "SDK not installed!" ) . join ( "VERSION" )
210
+ ) . nice_unwrap ( "Unable to read SDK version, make sure you are using SDK v0.4.2 or later" )
211
+ )
212
+ }
213
+
164
214
pub fn subcommand ( config : & mut Config , cmd : Sdk ) {
165
215
match cmd {
166
216
Sdk :: Install { reinstall, path } => {
@@ -179,6 +229,7 @@ pub fn subcommand(config: &mut Config, cmd: Sdk) {
179
229
install ( config, path. unwrap_or ( default_path) ) ;
180
230
} ,
181
231
Sdk :: Uninstall => { uninstall ( config) ; } ,
182
- Sdk :: Update { nightly, stable } => update ( config, nightly, stable)
232
+ Sdk :: Update { nightly, stable } => update ( config, nightly, stable) ,
233
+ Sdk :: Version => info ! ( "Geode SDK version: {}" , get_version( config) )
183
234
}
184
- }
235
+ }
0 commit comments