[swift-corelibs-dev] Better integration with UNIX tools
William Dillon
william at housedillon.com
Fri Nov 17 13:38:48 CST 2017
I, to, have wished for such an API.
I think that would be a very welcome addition.
> On Nov 17, 2017, at 11:34 AM, Tony Parker via swift-corelibs-dev <swift-corelibs-dev at swift.org> wrote:
>
> Hi Abhi,
>
> It does seem like there is a possibility of some better convenience API here.
>
> Any ideas on what form it would take? A class method on Process that returns the output, maybe?
>
> - Tony
>
>> On Nov 16, 2017, at 3:34 PM, Abhi Beckert via swift-corelibs-dev <swift-corelibs-dev at swift.org <mailto:swift-corelibs-dev at swift.org>> wrote:
>>
>> Swift is a great shell scripting language except for it's lack of any API to execute UNIX commands. Compare these two shell scripts:
>>
>>> #!/usr/bin/php
>>> <?
>>>
>>> $files = `find ~/Desktop -name *.png`;
>>>
>>> foreach (explode("\n", $files) as $file) {
>>> // do something with $file
>>> }
>>
>> -
>>
>>> #!/usr/bin/swift
>>>
>>> import Foundation
>>>
>>> let process = Process()
>>> process.launchPath = "/usr/bin/find"
>>> process.arguments = [
>>> NSString(string:"~/Desktop").expandingTildeInPath,
>>> "-name",
>>> "*.png"
>>> ]
>>>
>>> let output = Pipe()
>>> process.standardOutput = output
>>>
>>> process.launch()
>>>
>>> let files: String
>>> if let filesUtf8 = NSString(data: output.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8.rawValue) {
>>> files = filesUtf8 as String
>>> } else {
>>> files = NSString(data: output.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.isoLatin1.rawValue) as NSString! as String
>>> }
>>>
>>> files.enumerateLines { file, _ in
>>> // do something with file
>>> }
>>
>> It's a contrived example, I could have used NSFileManager, but I run into this all the time integrating with more complex tools such as rsync.
>>
>> Adding my own high level wrapper around the Process command isn't an option since there is no good way to import code from another file when executing swift asa shell script. All your code needs to be in one file.
>>
>> - Abhi
>> _______________________________________________
>> swift-corelibs-dev mailing list
>> swift-corelibs-dev at swift.org <mailto:swift-corelibs-dev at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev <https://lists.swift.org/mailman/listinfo/swift-corelibs-dev>
> _______________________________________________
> swift-corelibs-dev mailing list
> swift-corelibs-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20171117/e6c97c7b/attachment.html>
More information about the swift-corelibs-dev
mailing list