11
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
- extern crate dirs;
15
- extern crate fs_extra;
16
14
17
15
use std:: error:: Error ;
18
16
use std:: fmt;
19
- #[ allow( unused_imports) ]
20
- use std:: fs:: { File , OpenOptions } ;
21
- use std:: io:: prelude:: * ;
22
17
use std:: path:: { Path , PathBuf } ;
23
18
use std:: { env, fs} ;
24
19
20
+ use fs_extra:: dir:: CopyOptions ;
25
21
use glob:: glob;
26
- use java_locator;
27
- use sha2:: { Digest , Sha256 } ;
28
22
29
23
// This is the version of the jar that should be used
30
- const VERSION : & ' static str = "0.20.0-SNAPSHOT" ;
31
- const JAVA_FX_VERSION : & ' static str = "21.0.2" ;
24
+ const VERSION : & str = "0.20.0-SNAPSHOT" ;
25
+ const JAVA_FX_VERSION : & str = "21.0.2" ;
32
26
33
27
fn main ( ) -> Result < ( ) , J4rsBuildError > {
34
28
let out_dir = env:: var ( "OUT_DIR" ) ?;
35
- let source_jar_location = format ! ( "../java/target/j4rs-{}-jar-with-dependencies.jar" , VERSION ) ;
36
- if File :: open ( & source_jar_location) . is_ok ( ) {
37
- println ! ( "cargo:rerun-if-changed={}" , source_jar_location) ;
29
+ let source_jar_location = PathBuf :: from ( format ! (
30
+ "../java/target/j4rs-{VERSION}-jar-with-dependencies.jar"
31
+ ) ) ;
32
+ if Path :: new ( & source_jar_location) . exists ( ) {
33
+ println ! (
34
+ "cargo:rerun-if-changed={}" ,
35
+ source_jar_location. to_string_lossy( )
36
+ ) ;
38
37
}
39
38
40
39
let target_os_res = env:: var ( "CARGO_CFG_TARGET_OS" ) ;
@@ -55,76 +54,53 @@ fn main() -> Result<(), J4rsBuildError> {
55
54
56
55
fn generate_src ( out_dir : & str ) -> Result < ( ) , J4rsBuildError > {
57
56
let dest_path = Path :: new ( & out_dir) . join ( "j4rs_init.rs" ) ;
58
- let mut f = File :: create ( & dest_path) ?;
59
-
60
57
let contents = format ! (
61
58
"
62
59
pub(crate) fn j4rs_version() -> &'static str {{
63
- \" {}\"
60
+ \" {VERSION }\"
64
61
}}
65
62
66
63
pub(crate) fn java_fx_version() -> &'static str {{
67
- \" {}\"
64
+ \" {JAVA_FX_VERSION }\"
68
65
}}
69
- " ,
70
- VERSION ,
71
- JAVA_FX_VERSION
66
+ "
72
67
) ;
73
-
74
- f. write_all ( contents. as_bytes ( ) ) ?;
68
+ std:: fs:: write ( dest_path, contents) ?;
75
69
Ok ( ( ) )
76
70
}
77
71
78
72
// Copies the jars from the `java` directory to the source directory of rust.
79
- fn copy_jars_from_java ( jar_source_path : & str ) -> Result < ( ) , J4rsBuildError > {
80
- if let Ok ( mut source_jar_file ) = File :: open ( & jar_source_path) {
73
+ fn copy_jars_from_java ( jar_source_path : & Path ) -> Result < ( ) , J4rsBuildError > {
74
+ if jar_source_path. exists ( ) {
81
75
// Find the destination file
82
76
let home = env:: var ( "CARGO_MANIFEST_DIR" ) ?;
83
77
let jassets_path_buf = Path :: new ( & home) . join ( "jassets" ) ;
84
78
let jassets_path = jassets_path_buf. to_str ( ) . unwrap ( ) . to_owned ( ) ;
85
79
86
- let destination_jar_file_res = {
87
- let djpb = Path :: new ( & jassets_path)
88
- . join ( format ! ( "j4rs-{}-jar-with-dependencies.jar" , VERSION ) ) ;
89
- File :: open ( djpb)
90
- } ;
80
+ let destination_jar_file =
81
+ Path :: new ( & jassets_path) . join ( format ! ( "j4rs-{VERSION}-jar-with-dependencies.jar" ) ) ;
91
82
92
83
// Copy only if the files are not the same
93
- let do_copy = if destination_jar_file_res. is_ok ( ) {
94
- let mut destination_jar_file = destination_jar_file_res. unwrap ( ) ;
95
- !are_same_files ( & mut source_jar_file, & mut destination_jar_file) . unwrap_or ( true )
84
+ let do_copy = if destination_jar_file. exists ( ) {
85
+ !are_same_files ( & jar_source_path, & destination_jar_file) . unwrap_or ( true )
96
86
} else {
97
87
true
98
88
} ;
99
89
100
90
if do_copy {
101
- fs_extra:: remove_items ( vec ! [ jassets_path. clone ( ) ] . as_ref ( ) ) ?;
91
+ fs_extra:: remove_items ( & [ & jassets_path] ) ?;
102
92
103
- let _ = fs:: create_dir_all ( jassets_path_buf. clone ( ) )
104
- . map_err ( |error| panic ! ( "Cannot create dir '{:?}': {:?}" , jassets_path_buf , error ) ) ;
93
+ let _ = fs:: create_dir_all ( & jassets_path_buf)
94
+ . map_err ( |error| panic ! ( "Cannot create dir '{jassets_path_buf :?}': {error :?}" ) ) ;
105
95
106
- let ref options = fs_extra:: dir:: CopyOptions :: new ( ) ;
107
- let _ = fs_extra:: copy_items ( vec ! [ jar_source_path] . as_ref ( ) , jassets_path, options) ?;
96
+ fs_extra:: copy_items ( & [ jar_source_path] , jassets_path, & CopyOptions :: new ( ) ) ?;
108
97
}
109
98
}
110
99
Ok ( ( ) )
111
100
}
112
101
113
- fn are_same_files ( f1 : & mut File , f2 : & mut File ) -> Result < bool , J4rsBuildError > {
114
- let mut buffer1: Vec < u8 > = Vec :: new ( ) ;
115
- let mut hasher1 = Sha256 :: new ( ) ;
116
- let mut buffer2: Vec < u8 > = Vec :: new ( ) ;
117
- let mut hasher2 = Sha256 :: new ( ) ;
118
-
119
- f1. read_to_end ( & mut buffer1) ?;
120
- hasher1. update ( & buffer1) ;
121
- let hash1 = hasher1. finalize ( ) ;
122
-
123
- f2. read_to_end ( & mut buffer2) ?;
124
- hasher2. update ( & buffer2) ;
125
- let hash2 = hasher2. finalize ( ) ;
126
-
127
- Ok ( hash1 == hash2)
102
+ fn are_same_files ( path1 : & Path , path2 : & Path ) -> Result < bool , J4rsBuildError > {
103
+ Ok ( std:: fs:: read ( path1) ? == std:: fs:: read ( path2) ?)
128
104
}
129
105
130
106
// Copies the jars to and returns the PathBuf of the exec directory.
@@ -134,43 +110,31 @@ fn copy_jars_to_exec_directory(out_dir: &str) -> Result<PathBuf, J4rsBuildError>
134
110
exec_dir_path_buf. pop ( ) ;
135
111
exec_dir_path_buf. pop ( ) ;
136
112
137
- let jassets_output = exec_dir_path_buf. clone ( ) ;
138
- let jassets_output_dir = jassets_output. to_str ( ) . unwrap ( ) ;
113
+ let jassets_output_dir = exec_dir_path_buf. to_str ( ) . unwrap ( ) ;
139
114
140
115
let home = env:: var ( "CARGO_MANIFEST_DIR" ) ?;
141
116
let jassets_path_buf = Path :: new ( & home) . join ( "jassets" ) ;
142
117
let jassets_path = jassets_path_buf. to_str ( ) . unwrap ( ) . to_owned ( ) ;
143
118
144
- let jassets_jar_file_res = {
145
- let japb =
146
- Path :: new ( & jassets_path) . join ( format ! ( "j4rs-{}-jar-with-dependencies.jar" , VERSION ) ) ;
147
- File :: open ( japb)
148
- } ;
149
- let jassets_output_file_res = {
150
- let jaopb = Path :: new ( & jassets_output_dir)
151
- . join ( "jassets" )
152
- . join ( format ! ( "j4rs-{}-jar-with-dependencies.jar" , VERSION ) ) ;
153
- File :: open ( jaopb)
154
- } ;
119
+ let jassets_jar_file =
120
+ Path :: new ( & jassets_path) . join ( format ! ( "j4rs-{VERSION}-jar-with-dependencies.jar" ) ) ;
121
+ let jassets_output_file = Path :: new ( & jassets_output_dir)
122
+ . join ( "jassets" )
123
+ . join ( format ! ( "j4rs-{VERSION}-jar-with-dependencies.jar" ) ) ;
155
124
156
125
// Delete the target jassets and copy only if the files are not the same
157
- let do_copy = if jassets_jar_file_res. is_ok ( ) && jassets_output_file_res. is_ok ( ) {
158
- let mut jassets_jar_file = jassets_jar_file_res. unwrap ( ) ;
159
- let mut jassets_output_jar_file = jassets_output_file_res. unwrap ( ) ;
160
-
161
- !are_same_files ( & mut jassets_jar_file, & mut jassets_output_jar_file) . unwrap_or ( true )
126
+ let do_copy = if jassets_jar_file. exists ( ) && jassets_output_file. exists ( ) {
127
+ !are_same_files ( & jassets_jar_file, & jassets_output_file) . unwrap_or ( true )
162
128
} else {
163
129
true
164
130
} ;
165
131
166
132
if do_copy {
167
- fs_extra:: remove_items ( vec ! [ format!( "{}/jassets" , jassets_output_dir) ] . as_ref ( ) ) ?;
168
-
169
- let ref options = fs_extra:: dir:: CopyOptions :: new ( ) ;
170
- let _ = fs_extra:: copy_items ( vec ! [ jassets_path] . as_ref ( ) , jassets_output_dir, options) ?;
133
+ fs_extra:: remove_items ( & [ format ! ( "{jassets_output_dir}/jassets" ) ] ) ?;
134
+ fs_extra:: copy_items ( & [ jassets_path] , jassets_output_dir, & CopyOptions :: new ( ) ) ?;
171
135
}
172
136
173
- let jassets_output_files = glob ( format ! ( "{}/jassets/**/*" , jassets_output_dir ) . as_str ( ) ) ?;
137
+ let jassets_output_files = glob ( & format ! ( "{jassets_output_dir }/jassets/**/*" ) ) ?;
174
138
for glob_res in jassets_output_files {
175
139
println ! ( "cargo:rerun-if-changed={}" , glob_res?. to_str( ) . unwrap( ) ) ;
176
140
}
@@ -210,14 +174,6 @@ impl From<std::io::Error> for J4rsBuildError {
210
174
}
211
175
}
212
176
213
- impl From < java_locator:: errors:: JavaLocatorError > for J4rsBuildError {
214
- fn from ( err : java_locator:: errors:: JavaLocatorError ) -> J4rsBuildError {
215
- J4rsBuildError {
216
- description : format ! ( "{:?}" , err) ,
217
- }
218
- }
219
- }
220
-
221
177
impl From < fs_extra:: error:: Error > for J4rsBuildError {
222
178
fn from ( err : fs_extra:: error:: Error ) -> J4rsBuildError {
223
179
J4rsBuildError {
0 commit comments