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!]

C4861b1dfdf3bbb21faec4a1acdf183d?s=128

Ellen Shapiro
PRO

December 15, 2020
Tweet

Transcript

  1. GETTING RESOURCEFUL WITH SWIFT PACKAGE MANAGER SWIFTERS BUDAPEST | THE

    INTERNETS | DECEMBER 2020 ELLEN SHAPIRO | @DESIGNATEDNERD | APOLLOGRAPHQL.COM
  2. None
  3. None
  4. None
  5. BUT I CAN'T USE SPM WITH XIBS, COREDATA MODELS, ASSET

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

  7. None
  8. A BRIEF ! DETOUR

  9. HANK

  10. GEORGE MICHAEL

  11. HANK

  12. KITTSTAGRAM APP GITHUB.COM/DESIGNATEDNERD/KITTSTAGRAM

  13. None
  14. HANKBYTES LIBRARY GITHUB.COM/DESIGNATEDNERD/HANKBYTES

  15. // swift-tools-version:5.3

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

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

    ] ), .testTarget( name: "HankBytesTests", dependencies: ["HankBytes"]), ]
  18. .process

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

  20. .copy

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

  22. RESOURCES ARE SCOPED PER-TARGET

  23. None
  24. Bundle.module

  25. Bundle.module THIS IS THE BUNDLE FOR THE CURRENT MODULE CREATED

    BY THE SPM TARGET
  26. WITHIN YOUR LIBRARY Bundle.module.url(forResource: self.fileName, withExtension: self.fileExtension) UIImage(named: self.fileName, in:

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

    .module, compatibleWith: nil)
  28. 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) } }
  29. 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) } }
  30. None
  31. LOCALIZATION

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

  33. None
  34. let package = Package( name: "HankBytes", defaultLocalization: "en", platforms: [

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

    .iOS(.v14) ], products: [ .library( name: "HankBytes", targets: ["HankBytes"]), ], dependencies: [], targets: [...
  36. BASE.LPROJ

  37. BASE.LPROJ ✅ Storyboards and Nibs

  38. BASE.LPROJ ✅ Storyboards and Nibs " .strings files

  39. USE ISO CODES English: en.lproj

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

  41. USE ISO CODES English: en.lproj ! English: en_GB.lproj " English:

    en_US.lproj
  42. None
  43. None
  44. "Avoiding unintentionally copying files not intended to be localized variants

    into the product"
  45. DON'T COPY RESOURCES FOR UNSUPPORTED LANGUAGES

  46. WHAT CAN'T YOU INCLUDE USING RESOURCES?

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

  48. BUILD SCRIPTS IN BASH OR OTHER LANGUAGES

  49. None
  50. !

  51. OBLIGATORY SUMMARY SLIDE

  52. OBLIGATORY SUMMARY SLIDE > SPM lets you include resources used

    in your code in a target like images, core data models, xibs, etc.
  53. 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
  54. 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
  55. 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!
  56. 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
  57. THANK YOU!

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

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