@@ -70,20 +70,36 @@ pub mod tools;
70
70
struct FileBuilder {
71
71
path : PathBuf ,
72
72
body : String ,
73
+ executable : bool ,
73
74
}
74
75
75
76
impl FileBuilder {
76
- pub fn new ( path : PathBuf , body : & str ) -> FileBuilder {
77
+ pub fn new ( path : PathBuf , body : & str , executable : bool ) -> FileBuilder {
77
78
FileBuilder {
78
79
path,
79
80
body : body. to_string ( ) ,
81
+ executable : executable,
80
82
}
81
83
}
82
84
83
- fn mk ( & self ) {
85
+ fn mk ( & mut self ) {
86
+ if self . executable {
87
+ self . path . set_extension ( env:: consts:: EXE_EXTENSION ) ;
88
+ }
89
+
84
90
self . dirname ( ) . mkdir_p ( ) ;
85
91
fs:: write ( & self . path , & self . body )
86
92
. unwrap_or_else ( |e| panic ! ( "could not create file {}: {}" , self . path. display( ) , e) ) ;
93
+
94
+ #[ cfg( unix) ]
95
+ if self . executable {
96
+ use std:: os:: unix:: fs:: PermissionsExt ;
97
+
98
+ let mut perms = fs:: metadata ( & self . path ) . unwrap ( ) . permissions ( ) ;
99
+ let mode = perms. mode ( ) ;
100
+ perms. set_mode ( mode | 0o111 ) ;
101
+ fs:: set_permissions ( & self . path , perms) . unwrap ( ) ;
102
+ }
87
103
}
88
104
89
105
fn dirname ( & self ) -> & Path {
@@ -122,11 +138,16 @@ impl SymlinkBuilder {
122
138
}
123
139
124
140
#[ cfg( windows) ]
125
- fn mk ( & self ) {
141
+ fn mk ( & mut self ) {
126
142
self . dirname ( ) . mkdir_p ( ) ;
127
143
if self . src_is_dir {
128
144
t ! ( os:: windows:: fs:: symlink_dir( & self . dst, & self . src) ) ;
129
145
} else {
146
+ if let Some ( ext) = self . dst . extension ( ) {
147
+ if ext == env:: consts:: EXE_EXTENSION {
148
+ self . src . set_extension ( ext) ;
149
+ }
150
+ }
130
151
t ! ( os:: windows:: fs:: symlink_file( & self . dst, & self . src) ) ;
131
152
}
132
153
}
@@ -177,13 +198,22 @@ impl ProjectBuilder {
177
198
178
199
/// Adds a file to the project.
179
200
pub fn file < B : AsRef < Path > > ( mut self , path : B , body : & str ) -> Self {
180
- self . _file ( path. as_ref ( ) , body) ;
201
+ self . _file ( path. as_ref ( ) , body, false ) ;
181
202
self
182
203
}
183
204
184
- fn _file ( & mut self , path : & Path , body : & str ) {
185
- self . files
186
- . push ( FileBuilder :: new ( self . root . root ( ) . join ( path) , body) ) ;
205
+ /// Adds an executable file to the project.
206
+ pub fn executable < B : AsRef < Path > > ( mut self , path : B , body : & str ) -> Self {
207
+ self . _file ( path. as_ref ( ) , body, true ) ;
208
+ self
209
+ }
210
+
211
+ fn _file ( & mut self , path : & Path , body : & str , executable : bool ) {
212
+ self . files . push ( FileBuilder :: new (
213
+ self . root . root ( ) . join ( path) ,
214
+ body,
215
+ executable,
216
+ ) ) ;
187
217
}
188
218
189
219
/// Adds a symlink to a file to the project.
@@ -219,13 +249,17 @@ impl ProjectBuilder {
219
249
220
250
let manifest_path = self . root . root ( ) . join ( "Cargo.toml" ) ;
221
251
if !self . no_manifest && self . files . iter ( ) . all ( |fb| fb. path != manifest_path) {
222
- self . _file ( Path :: new ( "Cargo.toml" ) , & basic_manifest ( "foo" , "0.0.1" ) )
252
+ self . _file (
253
+ Path :: new ( "Cargo.toml" ) ,
254
+ & basic_manifest ( "foo" , "0.0.1" ) ,
255
+ false ,
256
+ )
223
257
}
224
258
225
259
let past = time:: SystemTime :: now ( ) - Duration :: new ( 1 , 0 ) ;
226
260
let ftime = filetime:: FileTime :: from_system_time ( past) ;
227
261
228
- for file in self . files . iter ( ) {
262
+ for file in self . files . iter_mut ( ) {
229
263
file. mk ( ) ;
230
264
if is_coarse_mtime ( ) {
231
265
// Place the entire project 1 second in the past to ensure
@@ -237,7 +271,7 @@ impl ProjectBuilder {
237
271
}
238
272
}
239
273
240
- for symlink in self . symlinks . iter ( ) {
274
+ for symlink in self . symlinks . iter_mut ( ) {
241
275
symlink. mk ( ) ;
242
276
}
243
277
@@ -316,7 +350,7 @@ impl Project {
316
350
317
351
/// Changes the contents of an existing file.
318
352
pub fn change_file ( & self , path : & str , body : & str ) {
319
- FileBuilder :: new ( self . root ( ) . join ( path) , body) . mk ( )
353
+ FileBuilder :: new ( self . root ( ) . join ( path) , body, false ) . mk ( )
320
354
}
321
355
322
356
/// Creates a `ProcessBuilder` to run a program in the project
0 commit comments