Thinking Outside The Box with Swift Package Manager - Hacking With Swift Live, Bath, UK, July 2019

Thinking Outside The Box with Swift Package Manager - Hacking With Swift Live, Bath, UK, July 2019

A brief history of being able to use other people's code with your code, and then some examples of neat stuff you can do using SPM for things you'd normally have to do with bash.

Sample code: https://github.com/designatednerd/PanoramaRama

C4861b1dfdf3bbb21faec4a1acdf183d?s=128

Ellen Shapiro

July 08, 2019
Tweet

Transcript

  1. 1.

    THINKING OUTSIDE THE BOX WITH ! SWIFT PACKAGE MANAGER HACKING

    WITH SWIFT LIVE | BATH, UK | JULY 2019 BY ELLEN SHAPIRO | @DESIGNATEDNERD | APOLLOGRAPHQL.COM
  2. 2.
  3. 8.
  4. 12.
  5. 13.
  6. 16.
  7. 18.
  8. 21.
  9. 27.
  10. 36.

    main.swift let args = CommandLine.arguments.dropFirst() print("Arguments: \(args)") if args.contains("--prebuild") {

    print("Run pre-build tasks") } if args.contains("--postbuild") { print("Run post-build tasks") }
  11. 37.

    main.swift let args = CommandLine.arguments.dropFirst() print("Arguments: \(args)") if args.contains("--prebuild") {

    print("Run pre-build tasks") } if args.contains("--postbuild") { print("Run post-build tasks") }
  12. 38.

    main.swift let args = CommandLine.arguments.dropFirst() print("Arguments: \(args)") if args.contains("--prebuild") {

    print("Run pre-build tasks") } if args.contains("--postbuild") { print("Run post-build tasks") }
  13. 39.

    main.swift let args = CommandLine.arguments.dropFirst() print("Arguments: \(args)") if args.contains("--prebuild") {

    print("Run pre-build tasks") } if args.contains("--postbuild") { print("Run post-build tasks") }
  14. 42.
  15. 43.
  16. 47.
  17. 48.
  18. 49.
  19. 53.
  20. 54.
  21. 56.

    EnvironmentUpdater.swift import Files import Plister struct EnvironmentUpdater { static func

    updateEnvironment(file: File) throws { try Plister.setValue("I was updated!", for: "string_secret", in: file) } static func resetEnvironment(file: File) throws { try file.resetToGitHEAD() } }
  22. 57.

    EnvironmentUpdater.swift import Files import Plister struct EnvironmentUpdater { static func

    updateEnvironment(file: File) throws { try Plister.setValue("I was updated!", for: "string_secret", in: file) } static func resetEnvironment(file: File) throws { try file.resetToGitHEAD() } }
  23. 58.

    EnvironmentUpdater.swift import Files import Plister struct EnvironmentUpdater { static func

    updateEnvironment(file: File) throws { try Plister.setValue("I was updated!", for: "string_secret", in: file) } static func resetEnvironment(file: File) throws { try file.resetToGitHEAD() } }
  24. 59.

    EnvironmentUpdater.swift import Files import Plister struct EnvironmentUpdater { static func

    updateEnvironment(file: File) throws { try Plister.setValue("I was updated!", for: "string_secret", in: file) } static func resetEnvironment(file: File) throws { try file.resetToGitHEAD() } }
  25. 60.

    EnvironmentUpdater.swift import Files import Plister struct EnvironmentUpdater { static func

    updateEnvironment(file: File) throws { try Plister.setValue("I was updated!", for: "string_secret", in: file) } static func resetEnvironment(file: File) throws { try file.resetToGitHEAD() } }
  26. 61.

    main.swift let args = CommandLine.arguments.dropFirst() let plistFile = try SourceFolders.appRoot.file(named:

    "Environment.plist") if args.contains("--prebuild") { print("Run pre-build tasks") try EnvironmentUpdater.updateEnvironment(file: plistFile) } if args.contains("--postbuild") { print("Run post-build tasks") try EnvironmentUpdater.resetEnvironment(file: plistFile) }
  27. 62.

    main.swift let args = CommandLine.arguments.dropFirst() let plistFile = try SourceFolders.appRoot.file(named:

    "Environment.plist") if args.contains("--prebuild") { print("Run pre-build tasks") try EnvironmentUpdater.updateEnvironment(file: plistFile) } if args.contains("--postbuild") { print("Run post-build tasks") try EnvironmentUpdater.resetEnvironment(file: plistFile) }
  28. 63.

    main.swift let args = CommandLine.arguments.dropFirst() let plistFile = try SourceFolders.appRoot.file(named:

    "Environment.plist") if args.contains("--prebuild") { print("Run pre-build tasks") try EnvironmentUpdater.updateEnvironment(file: plistFile) } if args.contains("--postbuild") { print("Run post-build tasks") try EnvironmentUpdater.resetEnvironment(file: plistFile) }
  29. 64.
  30. 65.

    main.swift let args = CommandLine.arguments.dropFirst() let plistFile = try SourceFolders.appRoot.file(named:

    "Environment.plist") if args.contains("--prebuild") { print("Run pre-build tasks") try EnvironmentUpdater.updateEnvironment(file: plistFile) } if args.contains("--postbuild") { print("Run post-build tasks") try EnvironmentUpdater.resetEnvironment(file: plistFile) }
  31. 66.
  32. 67.

    EnvironmentUpdater.swift import Files import Plister struct EnvironmentUpdater { static func

    updateEnvironment(file: File) throws { try Plister.setValue("I was updated!", for: "string_secret", in: file) } static func resetEnvironment(file: File) throws { try file.resetToGitHEAD() } }
  33. 68.
  34. 69.
  35. 70.
  36. 71.

    EnvironmentUpdater.swift static func updateEnvironment(file: File) throws { if let secretsFolder

    = SourceFolders.secrets, let secrets = try? secretsFolder.file(named: "production.json") { let json = try JSONLoader.loadJSONDictionary(from: secrets) try json.forEach { key, value in try Plister.setValue(value, for: key, in: file) } } else { let env = ProcessInfo.processInfo.environment guard let stringSecret = env["string_secret"] else { throw EnvironmentUpdater.EnvError.couldntLoadFromEnv } try Plister.setValue(stringSecret, for: "string_secret", in: file) } }
  37. 72.

    EnvironmentUpdater.swift static func updateEnvironment(file: File) throws { if let secretsFolder

    = SourceFolders.secrets, let secrets = try? secretsFolder.file(named: "production.json") { let json = try JSONLoader.loadJSONDictionary(from: secrets) try json.forEach { key, value in try Plister.setValue(value, for: key, in: file) } } else { let env = ProcessInfo.processInfo.environment guard let stringSecret = env["string_secret"] else { throw EnvironmentUpdater.EnvError.couldntLoadFromEnv } try Plister.setValue(stringSecret, for: "string_secret", in: file) } }
  38. 73.

    EnvironmentUpdater.swift static func updateEnvironment(file: File) throws { if let secretsFolder

    = SourceFolders.secrets, let secrets = try? secretsFolder.file(named: "production.json") { let json = try JSONLoader.loadJSONDictionary(from: secrets) try json.forEach { key, value in try Plister.setValue(value, for: key, in: file) } } else { let env = ProcessInfo.processInfo.environment guard let stringSecret = env["string_secret"] else { throw EnvironmentUpdater.EnvError.couldntLoadFromEnv } try Plister.setValue(stringSecret, for: "string_secret", in: file) } }
  39. 74.
  40. 76.

    EnvironmentUpdater.swift static func updateEnvironment(file: File) throws { if let secretsFolder

    = SourceFolders.secrets, let secrets = try? secretsFolder.file(named: "production.json") { let json = try JSONLoader.loadJSONDictionary(from: secrets) try json.forEach { key, value in try Plister.setValue(value, for: key, in: file) } } else { let env = ProcessInfo.processInfo.environment guard let stringSecret = env["string_secret"] else { throw EnvironmentUpdater.EnvError.couldntLoadFromEnv } try Plister.setValue(stringSecret, for: "string_secret", in: file) } }
  41. 77.

    EnvironmentUpdater.swift static func updateEnvironment(file: File) throws { if let secretsFolder

    = SourceFolders.secrets, let secrets = try? secretsFolder.file(named: "production.json") { let json = try JSONLoader.loadJSONDictionary(from: secrets) try json.forEach { key, value in try Plister.setValue(value, for: key, in: file) } } else { let env = ProcessInfo.processInfo.environment guard let stringSecret = env["string_secret"] else { throw EnvironmentUpdater.EnvError.couldntLoadFromEnv } try Plister.setValue(stringSecret, for: "string_secret", in: file) } }
  42. 78.
  43. 88.

    YourCode.swift NSLocalizedString("The key that will be displayed by default", "Some

    comment") Base.lproj/Localizable.strings /* Some Comment */ "The key that will be displayed by default" = "The key that will be displayed by default";
  44. 89.

    YourCode.swift NSLocalizedString("The key that will be displayed by default", "Some

    comment") Base.lproj/Localizable.strings /* Some Comment */ "The key that will be displayed by default" = "The key that will be displayed by default"; nl.lproj/Localizable.strings /* Some Comment */ "The key that will be displayed by default" = "De sleutel die standaard wordt weergegeven";
  45. 90.

    YourCode.swift NSLocalizedString("default_key", "Some comment") Base.lproj/Localizable.strings /* Some Comment */ "default_key"

    = "The key that will be displayed by default"; nl.lproj/Localizable.strings /* Some Comment */ "default_key" = "De sleutel die standaard wordt weergegeven";
  46. 92.

    YourCode.swift LocalizedString(key: .default_key) Base.lproj/Localizable.strings /* Some Comment */ "default_key" =

    "The key that will be displayed by default"; nl.lproj/Localizable.strings /* Some Comment */ "default_key" = "De sleutel die standaard wordt weergegeven";
  47. 94.
  48. 100.
  49. 102.
  50. 103.
  51. 105.
  52. 106.

    OBLIGATORY SUMMARY SLIDE > Swift Package Manager can help you

    make your iOS Projects better today > Move build scripts to swift and keep type safety
  53. 107.

    OBLIGATORY SUMMARY SLIDE > Swift Package Manager can help you

    make your iOS Projects better today > Move build scripts to swift and keep type safety > Actually test your build scripts!
  54. 108.

    OBLIGATORY SUMMARY SLIDE > Swift Package Manager can help you

    make your iOS Projects better today > Move build scripts to swift and keep type safety > Actually test your build scripts! > In Xcode 11, you should be able to use it for your main app and scripts in the same project.
  55. 109.

    !

  56. 110.

    LINKS! > It's time to use Swift Package Manager https://artsy.github.io/blog/2019/01/05/its-time-to-

    use-spm/ > Marathon https://github.com/JohnSundell/Marathon > swift-sh https://github.com/mxcl/swift-sh