Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support to iOS 13.0 and Above #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"revision": "2fd91357e90efe82bfa6845d1e7d5bc2f5025d35",
"version": "1.8.1"
}
},
{
"package": "SwiftShell",
"repositoryURL": "https://github.com/kareman/SwiftShell",
"state": {
"branch": null,
"revision": "99680b2efc7c7dbcace1da0b3979d266f02e213c",
"version": "5.1.0"
}
}
]
},
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import PackageDescription
let package = Package(
name: "SwiftShortcuts",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
],
products: [
Expand All @@ -14,14 +13,15 @@ let package = Package(
],
dependencies: [
.package(name: "SnapshotTesting", url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.8.1"),
.package(url: "https://github.com/kareman/SwiftShell", from: "5.1.0"),
],
targets: [
.target(
name: "CSymbols",
dependencies: []),
.target(
name: "SwiftShortcuts",
dependencies: ["CSymbols"]),
dependencies: ["CSymbols", "SwiftShell"]),
.testTarget(
name: "SwiftShortcutsTests",
dependencies: ["SwiftShortcuts", "SnapshotTesting"],
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ To create a file that you can import into the Shortcuts app, call the `build()`
// continued from above

let shortcut = HelloWorldShortcut()
let data = try shortcut.build()
try data.write(to: URL(fileURLWithPath: "Hello World.shortcut"))
try shortcut.buildAndSave(atPath: URL(fileURLWithPath: "HelloWorld.shortcut"))
```

Now you can share (for example, via AirDrop) the _Hello World.shortcut_ file to your device and it will open in the Shortcuts app. Unfortunately iOS 13 does not support opening serialized `.shortcut` files.
Now you can share (for example, via AirDrop) the _Hello World.shortcut_ file to your device and it will open in the Shortcuts app.

## Examples

Expand Down
52 changes: 52 additions & 0 deletions Sources/SwiftShortcuts/Core/Shortcut+Build.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import Foundation
import SwiftShell

enum BuildError: Error {
case pathNotValid
}

extension Shortcut {
var payload: ShortcutPayload {
Expand All @@ -10,9 +15,56 @@ extension Shortcut {
/// Serializes the body of the Shortcut to a file format suitable for opening in the Shortcuts app.
/// - Throws: An error of type `EncodingError`, if one is thrown during the encoding process.
/// - Returns: The binary data representing this Shortcut.
@available(*, deprecated, message: "Please use buildAndSave(toPath:sign:) instead")
public func build() throws -> Data {
let encoder = PropertyListEncoder()
encoder.outputFormat = .binary
return try encoder.encode(payload)
}

/// Build the Shortcut, sign it with Apple and save it to a file.
/// - Parameters:
/// - path: path to save the file
/// - sign: After iOS 13.0, you can't install unsigned shortcut file. You must sign the file in order to install it into `Shortcut` app. You should be aware that this `shortcuts sign` command can only run on mac with AppleID login.
/// - Throws: An error of type `EncodingError`, if one is thrown during the encoding process. Or any error during the `shortcuts sign` and save file process.
public func buildAndSave(toPath path: URL, sign: Bool = true) throws {
let data = try build()
try data.write(to: path)
try ShortcutSign(path: path, signMode: .anyone).run()
}
}

struct ShortcutSign {
enum SignMode: String {
case peopleWhoKnowMe = "people-who-know-me"
case anyone
}

enum Error: Swift.Error {
case signFailed
}

let path: URL
let signMode: SignMode

private let fm = FileManager.default

func run() throws {
let outputPath = URL(fileURLWithPath: path.relativeString.replacingOccurrences(of: ".shortcut", with: "-signed.shortcut"))
if fm.fileExists(atPath: outputPath.relativeString) {
try fm.removeItem(at: outputPath)
}
let output = SwiftShell.run(
"shortcuts", "sign",
"--mode", signMode.rawValue,
"--input", path.relativeString,
"--output", outputPath.relativeString
)
print(output.stdout)
if !fm.fileExists(atPath: outputPath.relativeString) {
throw output.error ?? Error.signFailed
}
try fm.removeItem(at: path)
try fm.moveItem(at: outputPath, to: path)
}
}