Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Resourceful With Swift Package Manager - Swifters Budapest, Online, December 2020

Getting Resourceful With Swift Package Manager - Swifters Budapest, Online, December 2020

A lightning talk from Swifters Budapest about SPM 5.3's new ability to handle resources.

Kittstagram app: https://github.com/designatednerd/Kittstagram
HankBytes Library: https://github.com/designatednerd/HankBytes
Video: [Coming soon!]

Ellen Shapiro

December 15, 2020
Tweet

More Decks by Ellen Shapiro

Other Decks in Technology

Transcript

  1. GETTING RESOURCEFUL
    WITH SWIFT PACKAGE MANAGER
    SWIFTERS BUDAPEST | THE INTERNETS | DECEMBER 2020
    ELLEN SHAPIRO | @DESIGNATEDNERD | APOLLOGRAPHQL.COM

    View full-size slide

  2. BUT I CAN'T USE SPM WITH
    XIBS, COREDATA MODELS, ASSET CATALOGS, ETC.

    View full-size slide

  3. SE-271
    PACKAGE MANAGER RESOURCES
    HTTPS://GITHUB.COM/APPLE/SWIFT-EVOLUTION/BLOB/MAIN/PROPOSALS/0271-PACKAGE-MANAGER-RESOURCES.MD

    View full-size slide

  4. A BRIEF
    !
    DETOUR

    View full-size slide

  5. GEORGE MICHAEL

    View full-size slide

  6. KITTSTAGRAM APP
    GITHUB.COM/DESIGNATEDNERD/KITTSTAGRAM

    View full-size slide

  7. HANKBYTES LIBRARY
    GITHUB.COM/DESIGNATEDNERD/HANKBYTES

    View full-size slide

  8. // swift-tools-version:5.3

    View full-size slide

  9. targets: [
    .target(
    name: "HankBytes",
    dependencies: [],
    resources: [
    .process("Resources")
    ]
    ),
    .testTarget(
    name: "HankBytesTests",
    dependencies: ["HankBytes"]),
    ]

    View full-size slide

  10. targets: [
    .target(
    name: "HankBytes",
    dependencies: [],
    resources: [
    .process("Resources")
    ]
    ),
    .testTarget(
    name: "HankBytesTests",
    dependencies: ["HankBytes"]),
    ]

    View full-size slide

  11. .process
    PROCESS THIS BASED ON PER-PLATFORM
    RULES AND OPTIMIZATIONS

    View full-size slide

  12. .copy
    COPY THIS EXACTLY AS IT IS
    (INCLUDING DIRECTORY STRUCTURE)

    View full-size slide

  13. RESOURCES ARE SCOPED
    PER-TARGET

    View full-size slide

  14. Bundle.module

    View full-size slide

  15. Bundle.module
    THIS IS THE BUNDLE FOR THE CURRENT
    MODULE CREATED BY THE SPM TARGET

    View full-size slide

  16. WITHIN YOUR LIBRARY
    Bundle.module.url(forResource: self.fileName,
    withExtension: self.fileExtension)
    UIImage(named: self.fileName,
    in: .module,
    compatibleWith: nil)

    View full-size slide

  17. WITHIN YOUR LIBRARY
    Bundle.module.url(forResource: self.fileName,
    withExtension: self.fileExtension)
    UIImage(named: self.fileName,
    in: .module,
    compatibleWith: nil)

    View full-size slide

  18. PUBLIC API YOUR APP CAN CONSUME
    public var image: UIImage? {
    if let image = UIImage(named: self.fileName,
    in: .module,
    compatibleWith: nil) {
    return image
    } else {
    guard let path = self.photoURL?.path else {
    return nil
    }
    return UIImage(contentsOfFile: path)
    }
    }

    View full-size slide

  19. PUBLIC API YOUR APP CAN CONSUME
    public var image: UIImage? {
    if let image = UIImage(named: self.fileName,
    in: .module,
    compatibleWith: nil) {
    return image
    } else {
    guard let path = self.photoURL?.path else {
    return nil
    }
    return UIImage(contentsOfFile: path)
    }
    }

    View full-size slide

  20. LOCALIZATION

    View full-size slide

  21. SE-278
    PACKAGE MANAGER LOCALIZED RESOURCES
    HTTPS://GITHUB.COM/APPLE/SWIFT-EVOLUTION/BLOB/MAIN/PROPOSALS/0278-PACKAGE-MANAGER-LOCALIZED-RESOURCES.MD

    View full-size slide

  22. let package = Package(
    name: "HankBytes",
    defaultLocalization: "en",
    platforms: [
    .iOS(.v14)
    ],
    products: [
    .library(
    name: "HankBytes",
    targets: ["HankBytes"]),
    ],
    dependencies: [],
    targets: [...

    View full-size slide

  23. let package = Package(
    name: "HankBytes",
    defaultLocalization: "en",
    platforms: [
    .iOS(.v14)
    ],
    products: [
    .library(
    name: "HankBytes",
    targets: ["HankBytes"]),
    ],
    dependencies: [],
    targets: [...

    View full-size slide

  24. BASE.LPROJ

    Storyboards and Nibs

    View full-size slide

  25. BASE.LPROJ

    Storyboards and Nibs
    "
    .strings files

    View full-size slide

  26. USE ISO CODES
    English: en.lproj

    View full-size slide

  27. USE ISO CODES
    English: en.lproj
    !
    English: en_GB.lproj

    View full-size slide

  28. USE ISO CODES
    English: en.lproj
    !
    English: en_GB.lproj
    "
    English: en_US.lproj

    View full-size slide

  29. "Avoiding unintentionally copying
    files not intended to be localized
    variants into the product"

    View full-size slide

  30. DON'T COPY RESOURCES FOR
    UNSUPPORTED LANGUAGES

    View full-size slide

  31. WHAT CAN'T YOU INCLUDE
    USING RESOURCES?

    View full-size slide

  32. THINGS YOU DON'T WANT
    INCLUDED IN YOUR LIBRARY

    View full-size slide

  33. BUILD SCRIPTS
    IN BASH OR OTHER LANGUAGES

    View full-size slide

  34. OBLIGATORY SUMMARY SLIDE

    View full-size slide

  35. OBLIGATORY SUMMARY SLIDE
    > SPM lets you include resources used in your code in a
    target like images, core data models, xibs, etc.

    View full-size slide

  36. OBLIGATORY SUMMARY SLIDE
    > SPM lets you include resources used in your code in a
    target like images, core data models, xibs, etc.
    > Resources are scoped to a particular target

    View full-size slide

  37. OBLIGATORY SUMMARY SLIDE
    > SPM lets you include resources used in your code in a
    target like images, core data models, xibs, etc.
    > Resources are scoped to a particular target
    > Bundle.module auto-generated when targets
    declare resources

    View full-size slide

  38. OBLIGATORY SUMMARY SLIDE
    > SPM lets you include resources used in your code in a
    target like images, core data models, xibs, etc.
    > Resources are scoped to a particular target
    > Bundle.module auto-generated when targets
    declare resources
    > Resources can be localized!

    View full-size slide

  39. OBLIGATORY SUMMARY SLIDE
    > SPM lets you include resources used in your code in a
    target like images, core data models, xibs, etc.
    > Resources are scoped to a particular target
    > Bundle.module auto-generated when targets
    declare resources
    > Resources can be localized!
    > It does not let you include build helpers

    View full-size slide

  40. APPLE LINKS
    > https://developer.apple.com/documentation/
    swift_packages/
    bundling_resources_with_a_swift_package
    > https://developer.apple.com/documentation/
    swift_packages/
    localizing_package_resources

    View full-size slide

  41. NOT-APPLE LINKS
    > https://useyourloaf.com/blog/add-
    resources-to-swift-packages/
    > https://medium.com/better-programming/how-
    to-add-resources-in-swift-package-manager-
    c437d44ec593

    View full-size slide