わいわいSwift Package Manager

011714704c4a925e542d426d4cdaa4e3?s=47 giginet
September 12, 2018

わいわいSwift Package Manager

011714704c4a925e542d426d4cdaa4e3?s=128

giginet

September 12, 2018
Tweet

Transcript

  1. Θ͍Θ͍Swift Package Manager Θ͍Θ͍swiftc #5 @giginet

  2. Agenda • SPM System library • Package Description V4.2 •

    How to develop
  3. Motivation • App Store Connect APIΫϥΠΞϯτΛSwiftͰ࡞ͬͯͨ • ৄࡉ͸iOSDC Reject Conference

    Day1Ͱʂʂʂ • JSON Web TokenͷॲཧʹCͷϥΠϒϥϦΛ࢖͍͔ͨͬͨ
  4. System Library • ΄͔ͷύοέʔδϚωʔδϟʔͰೖΕͨStatic LibraryΛSPMͰ ఏڙ͢Δ΍ͭ

  5. OpenSSL • https://github.com/IBM-Swift/OpenSSL . !"" LICENSE.txt !"" Package.swift !"" Package@swift-4.swift

    !"" README.md !"" module.modulemap #"" shim.h
  6. module.modulemap • libssl, libcryptoʹlink module OpenSSL [system] { header "shim.h"

    link "ssl" link "crypto" }
  7. shim.h #ifndef OpenSSLHelper_h #define OpenSSLHelper_h #include <openssl/conf.h> #include <openssl/evp.h> #include

    <openssl/err.h> #include <openssl/bio.h> #include <openssl/ssl.h> #include <openssl/md4.h> #include <openssl/md5.h> #include <openssl/sha.h> #include <openssl/hmac.h> #include <openssl/rand.h> #include <openssl/pkcs12.h> #include <openssl/x509v3.h> // (ry
  8. Package.swift import PackageDescription let package = Package( name: "OpenSSL", providers:

    [ .apt(["openssl libssl-dev"]), .brew(["openssl"]), ], products: [ .library( name: "OpenSSL", targets: ["OpenSSL"] ) ], targets: [ .target(name: "OpenSSL") ] )
  9. How to init modulemap $ swift package init --type modulemap

  10. How to use • .packageͰଞͷύοέʔδͱಉ͡Α͏ʹ૊ΈࠐΊΔ • Swift͔Β؆୯ʹCͷAPIୟ͚Δ import PackageDescription let

    package = Package( name: "Wormhole", products: [ .library( name: "Wormhole", targets: ["Wormhole"]), ], dependencies: [ .package(url: "https://github.com/giginet/libjwt-swift.git", from: "1.0.0"), ], targets: [ .target( name: "Wormhole", dependencies: ["Result", "JWT"]), ] )
  11. libjwt-swift • https://github.com/giginet/libjwt-swift • JWTΛѻ͏CϥΠϒϥϦΛSwift͔Β͔ͭ͑ΔΑ͏ʹౕͨ͠

  12. ! import Foundation import JWT struct JWTEncoder { func encode(issuerID:

    UUID, keyID: String) throws -> String { let object = UnsafeMutablePointer<OpaquePointer?>.allocate(capacity: MemoryLayout<OpaquePointer>.size) jwt_new(object) defer { jwt_free(object.pointee) } let keyPointer = convertToCString(privateKey) defer { keyPointer.deallocate() } jwt_set_alg(object.pointee, JWT_ALG_ES256, keyPointer, Int32(privateKey.utf16.count + 1)) // https://github.com/benmcollins/libjwt/pull/71 jwt_add_header(object.pointee, "kid", keyID) jwt_add_grant(object.pointee, "iss", issuerID.uuidString.lowercased()) let expirationDate = Date().addingTimeInterval(expirationInterval) jwt_add_grant_int(object.pointee, "exp", Int(expirationDate.timeIntervalSince1970)) jwt_add_grant(object.pointee, "aud", "appstoreconnect-v1") guard let encodedCString = jwt_encode_str(object.pointee) else { throw Error.decodeError } return String(cString: encodedCString) } }
  13. Deprecation • Xcode beta 6ͷToolchainͩͱ໰୊ͳ͘ಈ͘ • DEVELOPMENT SNAPSHOT(9/8ݱࡏ)ͩͱwarningग़Δ

  14. None
  15. ࠓ·Ͱͷ࿩͸4.2͔Βdeprecatedͳ ͷͰ๨Ε͍ͯͩ͘͞

  16. System Library(>=4.2) • SPM Package Description v4.2͔ΒSystem Library͕αϙʔτ • System

    LibraryΛύοέʔδ͔Β௚઀ࢀরͰ͖ΔΑ͏ʹ • https://github.com/apple/swift-package-manager/blob/master/ Documentation/PackageDescriptionV4_2.md
  17. How to Use • .systemLibraryͷଘࡏ͸υΩϡϝϯτʹॻ͍ͯ͋Δ • ࢖͍ํͷυΩϡϝϯτ͕ͳ͍ • ॻ͖͔͚ͷυΩϡϝϯτ͕Ϛʔδ͞Εͯͳ͍ঢ়ଶͰൃݟ͞Εͨ •

    https://github.com/apple/swift-package-manager/blob/ 40d319d7bb1a8cb42b187a4cbfa72515c61ebc44/ Documentation/Usage.md
  18. Directories Sources !"" jwt #"" jwt.h !"" module.modulemap

  19. // swift-tools-version:4.2 // The swift-tools-version declares the minimum version of

    Swift required to build this package. import PackageDescription let package = Package( name: "Wormhole", products: [ .library( name: "Wormhole", targets: ["Wormhole"]), ], targets: [ .systemLibrary(name: "JWT", path: "./Sources/jwt", providers: [.brew(["libjwt"]), .apt(["libjwt"])]), .target( name: "Wormhole", dependencies: ["Result", "JWT"]), ] )
  20. Local Package • PD v4.2͔ΒϩʔΧϧύεͰͷdependencyʹରԠ import PackageDescription let package =

    Package( name: "MyPackage", dependencies: [ .package(path: "../example-package-playingcard"), ], targets: [ .target( name: "MyPackage", dependencies: ["PlayingCard"] ), ] )
  21. Achievement • ~ͷల։͕όά͍ͬͯͨͷͰࡢ೔௚ͨ͠ • https://github.com/apple/swift-package-manager/pull/1779 $ swift build --disable-package-manifest-caching error:

    /Users/giginet/work/myexecutable/~/work/MyPackage has no manifest 'myexecutable' /Users/giginet/work/myexecutable: error: product dependency 'MyPackage' not found
  22. Starting SPM development

  23. SPM • SwiftͰ։ൃՄೳ • llbuild͸C++ • ͪΐͬͱPython(2) • Ϗϧυͷཱྀ͕ͳ͍ •

    ϏϧυࡁΈToolchainΛ࢖͑͹ྑ͍ • SPMͱswift-llbuild͚ͩඞཁ
  24. Bootstrap • Documentation/Development.mdͱREADME.mdΛಡΉ • ͦΕͰ΋एׯϋϚΔ

  25. Select Toolchain • /Library/Developer/Toolchains/swift-latest.xctoolchain/ Info.plistΛݟΔ • CFBundleVersionͷ஋Λ؀ڥม਺Ͱ౉͢ $ export TOOLCHAINS=org.swift.4220180908a

  26. Build $ ./Utilities/bootstrap --verbose

  27. Use development SPM #!/usr/bin/env bash /path/to/swift-source/swiftpm/.build/debug/spm "$@"

  28. Xcode $ ./Utilities/bootstrap --generate-xcodeproj generated: ./SwiftPM.xcodeproj $ open SwiftPM.xcodeproj

  29. LLDB • Xcode > Debug > Attach to Process by

    PID or Name...
  30. Next • ManifestͷύʔαʔͰόάݟ͚ͭͨͷͰۙʑ௚͢༧ఆ • ։ൃ؀ڥͰDynamic FrameworkͷLinkʹࣦഊ͢Δ • swift-llbuild΋৮ͬͯΈΔ

  31. Wormhole • ϥΠϒϥϦ࡞ͬͯexecutableͰ࢖͏ࢀߟྫ • https://github.com/giginet/Wormhole • iOS Reject Conference Day1Ͱ࿩͢༧ఆ

    • https://iosdc-reject-conference.connpass.com/event/93314/
  32. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠