$30 off During Our Annual Pro Sale. View Details »

Thinking Outside The Box with Swift Package Man...

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

Ellen Shapiro

July 08, 2019
Tweet

More Decks by Ellen Shapiro

Other Decks in Technology

Transcript

  1. THINKING OUTSIDE THE BOX WITH ! SWIFT PACKAGE MANAGER HACKING

    WITH SWIFT LIVE | BATH, UK | JULY 2019 BY ELLEN SHAPIRO | @DESIGNATEDNERD | APOLLOGRAPHQL.COM
  2. 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") }
  3. 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") }
  4. 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") }
  5. 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") }
  6. 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() } }
  7. 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() } }
  8. 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() } }
  9. 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() } }
  10. 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() } }
  11. 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) }
  12. 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) }
  13. 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) }
  14. 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) }
  15. 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() } }
  16. 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) } }
  17. 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) } }
  18. 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) } }
  19. 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) } }
  20. 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) } }
  21. 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";
  22. 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";
  23. 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";
  24. 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";
  25. OBLIGATORY SUMMARY SLIDE > Swift Package Manager can help you

    make your iOS Projects better today > Move build scripts to swift and keep type safety
  26. 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!
  27. 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.
  28. !

  29. 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