Skip to content

Use FoundationEssentials if it's available #674

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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: 4 additions & 0 deletions Examples/count-lines/CountLines.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//===----------------------------------------------------------------------===//

import ArgumentParser
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

@main
@available(macOS 12, *)
Expand Down
5 changes: 4 additions & 1 deletion Plugins/GenerateManual/GenerateManualPluginError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
import PackagePlugin

enum GenerateManualPluginError: Error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ As promised, here's the complete `count` command, for your experimentation:

```swift
import ArgumentParser
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

@main
struct Count: ParsableCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ To use `async`/`await` code in your commands' `run()` method implementations, fo
The following example declares a `CountLines` command that uses Foundation's asynchronous `FileHandle.AsyncBytes` to read the lines from a file:

```swift
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

@main
struct CountLines: AsyncParsableCommand {
Expand Down
9 changes: 7 additions & 2 deletions Sources/ArgumentParser/Usage/DumpHelpGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=5.11)
#if compiler(>=6.0)
#if canImport(FoundationEssentials)
internal import ArgumentParserToolInfo
internal import class FoundationEssentials.JSONEncoder
#else
internal import ArgumentParserToolInfo
internal import class Foundation.JSONEncoder
#elseif swift(>=5.10)
#endif
#elseif compiler(>=5.10)
import ArgumentParserToolInfo
import class Foundation.JSONEncoder
#else
Expand Down
17 changes: 7 additions & 10 deletions Sources/ArgumentParser/Usage/MessageInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=5.11)
#if compiler(>=6.0)
#if canImport(FoundationEssentials)
internal import protocol FoundationEssentials.LocalizedError
#else
internal import protocol Foundation.LocalizedError
internal import class Foundation.NSError
#elseif swift(>=5.10)
#endif
#elseif compiler(>=5.10)
import protocol Foundation.LocalizedError
import class Foundation.NSError
#else
@_implementationOnly import protocol Foundation.LocalizedError
@_implementationOnly import class Foundation.NSError
#endif

enum MessageInfo {
Expand Down Expand Up @@ -119,11 +120,7 @@ enum MessageInfo {
case let error as LocalizedError where error.errorDescription != nil:
self = .other(message: error.errorDescription!, exitCode: .failure)
default:
if Swift.type(of: error) is NSError.Type {
Copy link
Author

Choose a reason for hiding this comment

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

NSError does not exist in Foundation Essentials so I've removed this check and defaulted to String(describing:). What are folks thoughts on this? Seems excessive to bring in all of Foundation just for this

Choose a reason for hiding this comment

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

If we really want to preserve the behavior we can use reflection (ArgumentParser already depends on it heavily), but that's really overkill.

Copy link
Member

Choose a reason for hiding this comment

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

@0xTim - what's the difference in behavior? Can you try throwing an NSError so we can make sure it isn't a quality regression?

self = .other(message: error.localizedDescription, exitCode: .failure)
} else {
self = .other(message: String(describing: error), exitCode: .failure)
}
self = .other(message: String(describing: error), exitCode: .failure)
}
} else if let parserError = parserError {
let usage: String = {
Expand Down
8 changes: 6 additions & 2 deletions Sources/ArgumentParser/Usage/UsageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=5.11)
#if compiler(>=6.0)
#if canImport(FoundationEssentials)
internal import protocol FoundationEssentials.LocalizedError
#else
internal import protocol Foundation.LocalizedError
#elseif swift(>=5.10)
#endif
#elseif compiler(>=5.10)
import protocol Foundation.LocalizedError
#else
@_implementationOnly import protocol Foundation.LocalizedError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ extension DumpHelpGenerationTests {
var opt: Color?

@Option(help: "An optional color with a default value.")
var optWithDefault: Color? = .yellow
var optWithDefault: Color = .yellow

@Option(help: .init(discussion: "A preamble for the list of values in the discussion section."))
var extra: Color
Expand Down
2 changes: 1 addition & 1 deletion Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import ArgumentParserTestHelpers
final class HelpGenerationTests: XCTestCase {
}

extension Foundation.URL: ArgumentParser.ExpressibleByArgument {
extension URL: ArgumentParser.ExpressibleByArgument {
public init?(argument: String) {
guard let url = URL(string: argument) else {
return nil
Expand Down
4 changes: 4 additions & 0 deletions Tools/changelog-authors/ChangelogAuthors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
#if os(macOS)

import ArgumentParser
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

// MARK: Command

Expand Down
4 changes: 4 additions & 0 deletions Tools/generate-manual/DSL/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

import ArgumentParser
import ArgumentParserToolInfo
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

struct Document: MDocComponent {
var multiPage: Bool
Expand Down
4 changes: 4 additions & 0 deletions Tools/generate-manual/DSL/Preamble.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

import ArgumentParser
import ArgumentParserToolInfo
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

struct Preamble: MDocComponent {
var date: Date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
//===----------------------------------------------------------------------===//

import ArgumentParser
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

extension Foundation.Date: ArgumentParser.ExpressibleByArgument {
extension Date: ArgumentParser.ExpressibleByArgument {
// parsed as `yyyy-mm-dd`
public init?(argument: String) {
// ensure the input argument is composed of exactly 3 components separated
Expand Down