kareman / SwiftShell

A Swift framework for shell scripting.
https://kareman.github.io/SwiftShell
MIT License
1.03k stars 87 forks source link

Is there a way to change the directory on a per thread basis instead of for the entire application? #70

Closed patchthecode closed 6 years ago

patchthecode commented 6 years ago

Commands can't change the context they run in (or anything else internally in your application) so e.g. main.run("cd", "somedirectory") will achieve nothing. Use main.currentdirectory = "somedirectory" instead, this changes the current working directory for the entire application.

I'm not sure if this might be a problem, but i'm asking before i code it. I might have multiple threads running, and they may change the directory before running other commands. It would be bad if changing the directory changes it for the entire application because there will be conflicts on different threads.

Is my concern valid? Thanks.

kareman commented 6 years ago

Yes, that is definitely a valid concern. You work around it by using custom contexts instead of ‘main’, maybe one per thread:

var context = CustomContext(main) 
context.currentdirectory = ...

context.run(...)

You can also change other properties of the contexts like environment variables and their input and outputs. Also be aware that when you change the current directory of ‘main’ it also affects some Foundation classes like URL.

patchthecode commented 6 years ago

Awesome, i didnt know you could do that. thanks again. closing this.