Its a easier way to use Process on MacOS for handling STDOUT、STDERR、termination status, and running your script as root
.
Carthage
and CocoaPods
are the optional tools to install Slarder into your project.
Of course, you can also download the source code and drag the source file to your project as you like.
1.Create Cartfile in your project directory
$ touch Cartfile
2.Add this to Cartfile:
github "Khala-wan/Slarder"
3.Run
carthage update
4.Drag Slarder.frameWork to your project directory. Slarder.frameWork stored in:
${ Project }/Carthage/Build/Mac/Slarder.frameWork
5.Embedde in your project:
Target -> General -> Embedded Binaries -> Add Other -> Select "Slarder.frameWork"
6.Where you want to use Slarder
Import Slarder
7.Just try it!
More Carthage usage in Carthage
1.Create Podfile
$ Pod init
2.Add this to Podfile
pod Slarder
3.Where you want to use Slarder
Import Slarder
If you only want to run a command, you can create a process instance as before. If you want to run a script, Slarder provides you with a simple constructor:
let process:Process = try! Process.initWith(scriptName: "commonShell.sh", bundle: nil)
Every Process instance has a property named sd
, by which we can use all the features of Slarder.
Process().sd.someFunc()
Slardar provides two running privileges for your Process: current users and root users
If you need root privileges to lanuch process, set the first parameter sudo
to True
process.sd.launchWith(sudo: false,outputHandler: { (string, input) in
//STDOUT everytime
}, errorputHandler: { (string) in
//STDERR everytime
}) { (statusCode) in
//Process terminated
}
If the errorputHandler
parameter is nil, STDERR is output with STDOUT
If your script has tasks such as delay or network request, you may not be able to get all the output at once, then you need to listen to STDOUT, and connect each output in series.
process.sd.launchAsyncWith(sudo: self.root, outputHandler: { (newStr, input) in
self.outputString.append(newStr)
}, errorputHandler: nil) { (code) in
self.updateConsoleView(newMessage: "statusCode = \(code)")
}
Maybe your script needs to read some data from STDIN, like shell's read userName; echo $ userName
. You can use the outputHandler closure parameter input: Pipe
。
process.sd.launchAsyncWith(sudo: self.root, outputHandler: { (newStr, input) in
let writeHandle = input?.fileHandleForWriting
writeHandle?.write(someData)
}, errorputHandler: nil) { (code) in
self.updateConsoleView(newMessage: "statusCode = \(code)")
}
Slarder is supported by Swift4.0
, but SlarderSimpleExample is not supported. In case you want to use SlarderSimpleExample in Swift4.0
, there is a branch named Swift4.0.
- Test Slarder
- Add more useful features
Contact me if you have any questions. We welcome any contributions to make Slarder better. Look forward to your use ~