1- use anyhow:: { Context , Result , bail } ;
1+ use anyhow:: { Context , Result } ;
22use std:: {
33 fs:: { self } ,
44 path:: { Path , PathBuf } ,
5- time:: Duration ,
65} ;
76use version_compare:: Version ;
87
9- use crate :: { github, utils} ;
10-
11- const MIN_VERSION : & str = "0.0.0" ;
12-
13- #[ cfg( target_os = "windows" ) ]
14- const EXE_SUFFIX : & ' static str = ".exe" ;
8+ use crate :: { release_downloader, utils} ;
159
10+ const EXECUTABLE_NAME : & str = "defold-nvim-bridge" ;
11+ const OWNER : & str = "atomicptr" ;
12+ const REPOSITORY : & str = "defold.nvim" ;
1613#[ cfg( not( target_os = "windows" ) ) ]
1714const EXE_SUFFIX : & str = "" ;
1815
1916#[ cfg( target_os = "linux" ) ]
20- const NAME : & str = "linux-x86-defold-nvim-bridge" ;
17+ const ASSET_NAME : & str = "linux-x86-defold-nvim-bridge" ;
2118
2219#[ cfg( all( target_os = "macos" , target_arch = "x86_64" ) ) ]
23- const NAME : & str = "macos-x86-defold-nvim-bridge" ;
20+ const ASSET_NAME : & str = "macos-x86-defold-nvim-bridge" ;
2421
2522#[ cfg( all( target_os = "macos" , target_arch = "aarch64" ) ) ]
26- const NAME : & str = "macos-arm-defold-nvim-bridge" ;
23+ const ASSET_NAME : & str = "macos-arm-defold-nvim-bridge" ;
2724
2825#[ cfg( target_os = "windows" ) ]
29- const NAME : & str = "windows-x86-defold-nvim-bridge" ;
26+ const ASSET_NAME : & str = "windows-x86-defold-nvim-bridge" ;
3027
31- const OWNER : & str = "atomicptr" ;
32- const REPOSITORY : & str = "defold.nvim" ;
33-
34- pub fn path ( plugin_root : & Path ) -> Result < PathBuf > {
28+ pub fn path ( plugin_root : Option < & Path > ) -> Result < PathBuf > {
3529 let exe = exe_name ( ) ;
3630
37- if plugin_root. exists ( ) {
31+ if let Some ( plugin_root) = plugin_root
32+ && plugin_root. exists ( )
33+ {
3834 let candidates = [
39- plugin_root. join ( & exe) ,
4035 plugin_root. join ( "target" ) . join ( "debug" ) . join ( & exe) ,
4136 plugin_root. join ( "target" ) . join ( "release" ) . join ( & exe) ,
4237 ] ;
@@ -50,7 +45,11 @@ pub fn path(plugin_root: &Path) -> Result<PathBuf> {
5045}
5146
5247fn exe_name ( ) -> String {
53- format ! ( "defold-nvim-bridge{EXE_SUFFIX}" )
48+ if cfg ! ( target_os = "windows" ) {
49+ "defold-nvim-bridge.exe" . to_string ( )
50+ } else {
51+ "defold-nvim-bridge" . to_string ( )
52+ }
5453}
5554
5655fn local_path ( ) -> Result < PathBuf > {
@@ -64,96 +63,40 @@ fn local_path() -> Result<PathBuf> {
6463 Ok ( dir. join ( exe_name ( ) ) )
6564}
6665
67- fn version_path ( ) -> Result < PathBuf > {
68- let dir = dirs:: data_dir ( )
69- . context ( "could not get data dir" ) ?
70- . join ( "defold.nvim" )
71- . join ( "meta" ) ;
72-
73- fs:: create_dir_all ( & dir) ?;
74-
75- Ok ( dir. join ( "bridge_version" ) )
76- }
77-
78- fn version ( ) -> Result < String > {
79- let file = version_path ( ) ?;
80-
81- if !file. exists ( ) {
82- bail ! ( "Version not found" ) ;
83- }
84-
85- Ok ( fs:: read_to_string ( file) ?)
86- }
87-
88- fn is_update_available ( ) -> Result < bool > {
89- if !local_path ( ) ?. exists ( ) {
90- return Ok ( true ) ;
91- }
92-
93- if !version_path ( ) ?. exists ( ) {
94- return Ok ( true ) ;
95- }
96-
97- let Ok ( v) = version ( ) else {
98- return Ok ( true ) ;
99- } ;
100-
101- tracing:: debug!( "Bridge Version {v} installed" ) ;
102-
103- let Some ( installed) = Version :: from ( & v) else {
104- tracing:: debug!( "Couldnt parse version" ) ;
105- return Ok ( true ) ;
106- } ;
107-
108- // if min version is higher, force update
109- let min_version = Version :: from ( MIN_VERSION ) . expect ( "cant parse min version" ) ;
110- if installed < min_version {
111- tracing:: debug!( "Bridge Min Version {MIN_VERSION} exceeded (current {v})" ) ;
112- return Ok ( true ) ;
113- }
114-
115- // if the version file is younger than a week dont bother
116- let last_modified = version_path ( ) ?. metadata ( ) ?. modified ( ) ?;
117- if last_modified. elapsed ( ) ? < Duration :: from_hours ( 24 * 7 ) {
118- return Ok ( false ) ;
119- }
120-
121- // re-write the file again so that we only check once a week
122- fs:: write ( version_path ( ) ?, & v) ?;
123-
124- let release = github:: fetch_release ( OWNER , REPOSITORY ) ?;
125-
126- tracing:: debug!( "Bridge Version {} is newest" , release. tag_name) ;
127-
128- let Some ( current) = Version :: from ( & release. tag_name ) else {
129- return Ok ( false ) ;
130- } ;
131-
132- Ok ( current > installed)
133- }
134-
13566fn install ( ) -> Result < PathBuf > {
136- let path = local_path ( ) ?;
137-
138- if path. exists ( ) && !is_update_available ( ) ? {
139- tracing:: debug!( "No update available for {}" , path. display( ) ) ;
140- return Ok ( path) ;
141- }
142-
143- let ( downloaded_file, release) = github:: download_release ( OWNER , REPOSITORY , NAME ) ?;
144-
145- tracing:: debug!( "New Bridge version found {}" , release. tag_name) ;
146-
147- utils:: move_file ( & downloaded_file, & path) ?;
148- fs:: write ( version_path ( ) ?, release. tag_name ) ?;
149-
150- #[ cfg( any( target_os = "linux" , target_os = "macos" ) ) ]
151- {
152- use std:: { fs:: Permissions , os:: unix:: fs:: PermissionsExt } ;
153- fs:: set_permissions ( & path, Permissions :: from_mode ( 0o700 ) ) ?;
154- }
155-
156- github:: clear_downloads ( OWNER , REPOSITORY ) ?;
157-
158- Ok ( path)
67+ let min_version = utils:: version ( ) ;
68+ let min_version = Version :: from ( & min_version) ;
69+ let curr_version = release_downloader:: version ( EXECUTABLE_NAME ) ;
70+ let curr_version = curr_version
71+ . as_ref ( )
72+ . map ( |v| Version :: from ( v) )
73+ . ok ( )
74+ . flatten ( ) ;
75+
76+ release_downloader:: install_with (
77+ OWNER ,
78+ REPOSITORY ,
79+ ASSET_NAME ,
80+ EXECUTABLE_NAME ,
81+ |downloaded_file| {
82+ let path = local_path ( ) ?;
83+
84+ utils:: move_file ( downloaded_file, & path) ?;
85+
86+ #[ cfg( any( target_os = "linux" , target_os = "macos" ) ) ]
87+ {
88+ use std:: { fs:: Permissions , os:: unix:: fs:: PermissionsExt } ;
89+ fs:: set_permissions ( & path, Permissions :: from_mode ( 0o700 ) ) ?;
90+ }
91+
92+ Ok ( ( ) )
93+ } ,
94+ local_path,
95+ match ( min_version, curr_version) {
96+ ( Some ( mv) , Some ( cv) ) => mv > cv,
97+ _ => false ,
98+ } ,
99+ ) ?;
100+
101+ local_path ( )
159102}
0 commit comments