@@ -204,43 +204,6 @@ macro_rules! run_cmd {
204
204
} } ;
205
205
}
206
206
207
- /// Envrionment settings
208
- pub struct Env {
209
- current_dir : String ,
210
- variables : HashMap < String , String > ,
211
- }
212
-
213
- impl Env {
214
- pub fn new ( ) -> Self {
215
- Self {
216
- current_dir : "." . to_string ( ) ,
217
- variables : HashMap :: new ( ) ,
218
- }
219
- }
220
-
221
- pub fn cd ( & mut self , dir : & str ) -> & Self {
222
- self . current_dir = dir. to_string ( ) ;
223
- self
224
- }
225
-
226
- pub fn pwd ( & self ) -> & str {
227
- & self . current_dir
228
- }
229
-
230
- pub fn set ( & mut self , key : String , val : String ) -> & Self {
231
- self . variables . insert ( key, val) ;
232
- self
233
- }
234
-
235
- pub fn get ( & mut self , key : & str ) -> Option < & String > {
236
- self . variables . get ( key)
237
- }
238
-
239
- pub fn exec ( & self , cmd : & str ) -> Process {
240
- Process :: new_with_env ( self , cmd)
241
- }
242
- }
243
-
244
207
#[ doc( hidden) ]
245
208
pub trait ProcessResult {
246
209
fn get_result ( process : & mut Process ) -> Self ;
@@ -257,30 +220,22 @@ pub trait ProcessResult {
257
220
/// .wait::<CmdResult>()?
258
221
/// ```
259
222
///
260
- pub struct Process < ' a > {
261
- env : Option < & ' a Env > ,
223
+ pub struct Process {
262
224
cur_dir : Option < String > ,
263
225
full_cmd : Vec < Vec < String > > ,
264
226
}
265
227
266
- impl < ' a > Process < ' a > {
228
+ impl Process {
267
229
pub fn new < S : Borrow < str > > ( pipe_cmd : S ) -> Self {
268
230
let args = parse_args ( pipe_cmd. borrow ( ) ) ;
269
231
let argv = parse_argv ( args) ;
270
232
271
233
Self {
272
- env : None ,
273
234
cur_dir : None ,
274
235
full_cmd : vec ! [ argv] ,
275
236
}
276
237
}
277
238
278
- fn new_with_env ( env : & ' a Env , cmd : & str ) -> Self {
279
- let mut p = Process :: new ( cmd) ;
280
- p. env = Some ( env) ;
281
- p
282
- }
283
-
284
239
pub fn current_dir < S : Borrow < str > > ( & mut self , dir : S ) -> & mut Self {
285
240
self . cur_dir = Some ( dir. borrow ( ) . to_string ( ) ) ;
286
241
self
@@ -335,18 +290,14 @@ fn format_full_cmd(full_cmd: &Vec<Vec<String>>) -> String {
335
290
fn run_full_cmd ( process : & mut Process , pipe_last : bool ) -> Result < ( Child , String ) > {
336
291
let mut full_cmd_str = format_full_cmd ( & process. full_cmd ) ;
337
292
let first_cmd = & process. full_cmd [ 0 ] ;
338
- let cur_dir = if let Some ( dir) = & process. cur_dir {
293
+ let mut cmd = Command :: new ( & first_cmd[ 0 ] ) ;
294
+ if let Some ( dir) = & process. cur_dir {
339
295
full_cmd_str += & format ! ( " (cd: {})" , dir) ;
340
- dir
341
- } else if let Some ( env) = process. env {
342
- full_cmd_str += & format ! ( " (cd: {})" , env. current_dir) ;
343
- & env. current_dir
344
- } else {
345
- "."
346
- } ;
296
+ cmd. current_dir ( dir) ;
297
+ }
347
298
info ! ( "Running \" {}\" ..." , full_cmd_str) ;
348
- let mut last_proc = Command :: new ( & first_cmd [ 0 ] )
349
- . current_dir ( cur_dir )
299
+
300
+ let mut last_proc = cmd
350
301
. args ( & first_cmd[ 1 ..] )
351
302
. stdout ( if pipe_last || process. full_cmd . len ( ) > 1 {
352
303
Stdio :: piped ( )
@@ -370,11 +321,39 @@ fn run_full_cmd(process: &mut Process, pipe_last: bool) -> Result<(Child, String
370
321
Ok ( ( last_proc, full_cmd_str) )
371
322
}
372
323
373
- fn run_pipe_cmd ( full_command : & str ) -> CmdResult {
324
+ fn run_pipe_cmd ( full_command : & str , cd_opt : & mut Option < String > ) -> CmdResult {
374
325
let pipe_args = parse_pipes ( full_command. trim ( ) ) ;
375
326
let pipe_argv = parse_argv ( pipe_args) ;
376
327
328
+ let mut pipe_iter = pipe_argv[ 0 ] . split_whitespace ( ) ;
329
+ let cmd = pipe_iter. next ( ) . unwrap ( ) ;
330
+ if cmd == "cd" || cmd == "lcd" {
331
+ let dir = pipe_iter. next ( ) . unwrap ( ) . trim_matches ( '"' ) ;
332
+ if pipe_iter. next ( ) != None {
333
+ let err = Error :: new ( ErrorKind :: Other ,
334
+ format ! ( "{} format wrong: {}" , cmd, full_command) ) ;
335
+ return Err ( err) ;
336
+ } else {
337
+ if cmd == "cd" {
338
+ info ! ( "Set env current_dir: \" {}\" " , dir) ;
339
+ return std:: env:: set_current_dir ( dir) ;
340
+ } else {
341
+ info ! ( "Set local current_dir: \" {}\" " , dir) ;
342
+ * cd_opt = Some ( dir. into ( ) ) ;
343
+ return Ok ( ( ) ) ;
344
+ }
345
+ }
346
+ } else if cmd == "pwd" {
347
+ let pwd = std:: env:: current_dir ( ) ?;
348
+ info ! ( "Running \" pwd\" ..." ) ;
349
+ println ! ( "{}" , pwd. display( ) ) ;
350
+ return Ok ( ( ) ) ;
351
+ }
352
+
377
353
let mut last_proc = Process :: new ( pipe_argv[ 0 ] . clone ( ) ) ;
354
+ if let Some ( dir) = cd_opt {
355
+ last_proc. current_dir ( dir. clone ( ) ) ;
356
+ }
378
357
for pipe_cmd in pipe_argv. iter ( ) . skip ( 1 ) {
379
358
last_proc. pipe ( pipe_cmd. clone ( ) ) ;
380
359
}
@@ -386,6 +365,13 @@ fn run_pipe_fun(full_command: &str) -> FunResult {
386
365
let pipe_args = parse_pipes ( full_command. trim ( ) ) ;
387
366
let pipe_argv = parse_argv ( pipe_args) ;
388
367
368
+ let mut pipe_iter = pipe_argv[ 0 ] . split_whitespace ( ) ;
369
+ let cmd = pipe_iter. next ( ) . unwrap ( ) ;
370
+ if cmd == "pwd" {
371
+ let pwd = std:: env:: current_dir ( ) ?;
372
+ return Ok ( format ! ( "{}" , pwd. display( ) ) ) ;
373
+ }
374
+
389
375
let mut last_proc = Process :: new ( pipe_argv[ 0 ] . clone ( ) ) ;
390
376
for pipe_cmd in pipe_argv. iter ( ) . skip ( 1 ) {
391
377
last_proc. pipe ( pipe_cmd. clone ( ) ) ;
@@ -403,8 +389,9 @@ pub fn run_fun(cmds: &str) -> FunResult {
403
389
pub fn run_cmd ( cmds : & str ) -> CmdResult {
404
390
let cmd_args = parse_cmds ( cmds) ;
405
391
let cmd_argv = parse_argv ( cmd_args) ;
392
+ let mut cd_opt: Option < String > = None ;
406
393
for cmd in cmd_argv {
407
- if let Err ( e) = run_pipe_cmd ( & cmd) {
394
+ if let Err ( e) = run_pipe_cmd ( & cmd, & mut cd_opt ) {
408
395
return Err ( e) ;
409
396
}
410
397
}
0 commit comments