[swift-corelibs-dev] Better integration with UNIX tools
Sneed, Brandon
brsneed at ebay.com
Tue Nov 28 12:28:52 CST 2017
+1 from me.
I ended up making a wrapper objc->swift around system() since it was removed from swift recently, however, it behaves (from a user standpoint) exactly how I’d like. If my command line util jumps out to another program and it needs stdin/out/err, it all just passes through which is what I usually want. I found trying to pipe everything together via the other mechanisms available was just too much and seemed a little error prone. I can’t speak to system()’s security or general implementation, just that it seems to do what I want 100% of the time.
Brandon
From: <swift-corelibs-dev-bounces at swift.org> on behalf of William Dillon via swift-corelibs-dev <swift-corelibs-dev at swift.org>
Reply-To: William Dillon <william at housedillon.com>
Date: Friday, November 17, 2017 at 11:39 AM
To: Tony Parker <anthony.parker at apple.com>
Cc: "swift-corelibs-dev at swift.org" <swift-corelibs-dev at swift.org>
Subject: Re: [swift-corelibs-dev] Better integration with UNIX tools
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<mailto: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://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.swift.org%2Fmailman%2Flistinfo%2Fswift-corelibs-dev&data=02%7C01%7Cbrsneed%40ebay.com%7C6eddf25bfd6742c1971508d52df2e751%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636465443639795145&sdata=pp4hSxpPPGDCB4JtwKo7HB3UIbklF5kD%2B7iZB2kRZdA%3D&reserved=0>
_______________________________________________
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20171128/0cf69941/attachment.html>
More information about the swift-corelibs-dev
mailing list