Skip to content

Added URL API to Foundation.Process #1462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CoreFoundation/Locale.subproj/CFLocale.c
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ _CFLocaleCalendarDirection _CFLocaleGetCalendarDirection(void) {
#endif
}

#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
static CFArrayRef _CFLocaleCopyPreferredLanguagesFromPrefs(CFArrayRef languagesArray) {
CFMutableArrayRef newArray = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
if (languagesArray && (CFArrayGetTypeID() == CFGetTypeID(languagesArray))) {
Expand All @@ -1100,7 +1100,7 @@ static CFArrayRef _CFLocaleCopyPreferredLanguagesFromPrefs(CFArrayRef languagesA
#endif

CFArrayRef CFLocaleCopyPreferredLanguages(void) {
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
CFArrayRef languagesArray = (CFArrayRef)CFPreferencesCopyAppValue(CFSTR("AppleLanguages"), kCFPreferencesCurrentApplication);
CFArrayRef result = _CFLocaleCopyPreferredLanguagesFromPrefs(languagesArray);
if (languagesArray) CFRelease(languagesArray);
Expand Down
42 changes: 21 additions & 21 deletions Foundation/NSLocale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ open class NSLocale: NSObject, NSCopying, NSSecureCoding {
}

extension NSLocale {
open class var current: Locale {
open static var current: Locale {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why static? This doesn't match the Darwin signature:

class var current: Locale { get }

return CFLocaleCopyCurrent()._swiftObject
}

open class var system: Locale {
open static var system: Locale {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above for all static members. Please revert?

return CFLocaleGetSystem()._swiftObject
}
}
Expand All @@ -98,55 +98,55 @@ extension NSLocale {
return object(forKey: .identifier) as! String
}

open class var availableLocaleIdentifiers: [String] {
open static var availableLocaleIdentifiers: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyAvailableLocaleIdentifiers()._nsObject {
identifiers.append(obj as! String)
}
return identifiers
}

open class var isoLanguageCodes: [String] {
open static var isoLanguageCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOLanguageCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
identifiers.append(obj as! String)
}
return identifiers
}

open class var isoCountryCodes: [String] {
open static var isoCountryCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOCountryCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
identifiers.append(obj as! String)
}
return identifiers
}

open class var isoCurrencyCodes: [String] {
open static var isoCurrencyCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOCurrencyCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
identifiers.append(obj as! String)
}
return identifiers
}

open class var commonISOCurrencyCodes: [String] {
open static var commonISOCurrencyCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyCommonISOCurrencyCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
identifiers.append(obj as! String)
}
return identifiers
}

open class var preferredLanguages: [String] {
open static var preferredLanguages: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyPreferredLanguages()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
identifiers.append(obj as! String)
}
return identifiers
}

open class func components(fromLocaleIdentifier string: String) -> [String : String] {
open static func components(fromLocaleIdentifier string: String) -> [String : String] {
var comps = Dictionary<String, String>()
let values = CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, string._cfObject)._nsObject
values.enumerateKeysAndObjects(options: []) { (k, v, stop) in
Expand All @@ -157,27 +157,27 @@ extension NSLocale {
return comps
}

open class func localeIdentifier(fromComponents dict: [String : String]) -> String {
open static func localeIdentifier(fromComponents dict: [String : String]) -> String {
return CFLocaleCreateLocaleIdentifierFromComponents(kCFAllocatorSystemDefault, dict._cfObject)._swiftObject
}

open class func canonicalLocaleIdentifier(from string: String) -> String {
open static func canonicalLocaleIdentifier(from string: String) -> String {
return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorSystemDefault, string._cfObject)._swiftObject
}

open class func canonicalLanguageIdentifier(from string: String) -> String {
open static func canonicalLanguageIdentifier(from string: String) -> String {
return CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorSystemDefault, string._cfObject)._swiftObject
}

open class func localeIdentifier(fromWindowsLocaleCode lcid: UInt32) -> String? {
open static func localeIdentifier(fromWindowsLocaleCode lcid: UInt32) -> String? {
return CFLocaleCreateLocaleIdentifierFromWindowsLocaleCode(kCFAllocatorSystemDefault, lcid)._swiftObject
}

open class func windowsLocaleCode(fromLocaleIdentifier localeIdentifier: String) -> UInt32 {
open static func windowsLocaleCode(fromLocaleIdentifier localeIdentifier: String) -> UInt32 {
return CFLocaleGetWindowsLocaleCodeFromLocaleIdentifier(localeIdentifier._cfObject)
}

open class func characterDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection {
open static func characterDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection {
let dir = CFLocaleGetLanguageCharacterDirection(isoLangCode._cfObject)
#if os(OSX) || os(iOS)
return NSLocale.LanguageDirection(rawValue: UInt(dir.rawValue))!
Expand All @@ -186,7 +186,7 @@ extension NSLocale {
#endif
}

open class func lineDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection {
open static func lineDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection {
let dir = CFLocaleGetLanguageLineDirection(isoLangCode._cfObject)
#if os(OSX) || os(iOS)
return NSLocale.LanguageDirection(rawValue: UInt(dir.rawValue))!
Expand Down
28 changes: 27 additions & 1 deletion Foundation/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,40 @@ open class Process: NSObject {

}

// these methods can only be set before a launch

// These methods can only be set before a launch.

open var launchPath: String?
open var arguments: [String]?
open var environment: [String : String]? // if not set, use current

open var currentDirectoryPath: String = FileManager.default.currentDirectoryPath

open var executableURL: URL? {
get {
guard let launchPath = self.launchPath else {
return nil
}

return URL(fileURLWithPath: launchPath)
}
set {
self.launchPath = newValue?.path
}
}

open var currentDirectoryURL: URL {
get {
return URL(fileURLWithPath: self.currentDirectoryPath)
}
set {
self.currentDirectoryPath = newValue.path
}
}


// standard I/O channels; could be either a FileHandle or a Pipe

open var standardInput: Any? {
willSet {
precondition(newValue is Pipe || newValue is FileHandle,
Expand Down
13 changes: 9 additions & 4 deletions TestFoundation/TestNSLocale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class TestNSLocale : XCTestCase {
static var allTests: [(String, (TestNSLocale) -> () throws -> Void)] {
return [
("test_constants", test_constants),
("test_Identifier", test_Identifier),
("test_identifier", test_identifier),
("test_copy", test_copy),
("test_availableIdentifiers", test_availableIdentifiers),
("test_staticProperties", test_staticProperties),
("test_localeProperties", test_localeProperties),
]
}

func test_Identifier() {
func test_identifier() {
// Current locale identifier should not be empty
// Or things like NumberFormatter spellOut style won't work
XCTAssertFalse(Locale.current.identifier.isEmpty)
Expand Down Expand Up @@ -110,8 +110,13 @@ class TestNSLocale : XCTestCase {
XCTAssertTrue(locale == localeCopy)
}

func test_availableIdentifiers() {
func test_staticProperties() {
XCTAssertNoThrow(Locale.availableIdentifiers)
XCTAssertNoThrow(Locale.preferredLanguages)
XCTAssertNoThrow(Locale.commonISOCurrencyCodes)
XCTAssertNoThrow(Locale.isoCurrencyCodes)
XCTAssertNoThrow(Locale.isoRegionCodes)
XCTAssertNoThrow(Locale.isoLanguageCodes)
}

func test_localeProperties(){
Expand Down
10 changes: 7 additions & 3 deletions TestFoundation/TestProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ class TestProcess : XCTestCase {

let process = Process()

process.launchPath = "/bin/bash"
let executablePath = "/bin/bash"
process.executableURL = URL(fileURLWithPath: executablePath)

XCTAssertEqual(executablePath, process.launchPath)

process.arguments = ["-c", "exit 0"]

process.launch()
process.waitUntilExit()
XCTAssertEqual(process.terminationStatus, 0)
XCTAssertEqual(process.terminationReason, .exit)
}

func test_exit1() {

let process = Process()
Expand Down Expand Up @@ -277,7 +281,7 @@ class TestProcess : XCTestCase {
func test_current_working_directory() {
do {
let previousWorkingDirectory = FileManager.default.currentDirectoryPath

// Darwin Foundation requires the full path to the executable (.launchPath)
let (output, _) = try runTask(["/bin/bash", "-c", "pwd"], currentDirectoryPath: "/bin")
XCTAssertEqual(output.trimmingCharacters(in: .newlines), "/bin")
Expand Down