Skip to content

Commit

Permalink
1.5.0 Action extension to create class diagrams directly from Finder (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoEidinger authored Feb 12, 2023
1 parent 113503b commit 44837a7
Show file tree
Hide file tree
Showing 16 changed files with 530 additions and 24 deletions.
10 changes: 10 additions & 0 deletions ActionExtension/ActionExtension.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
58 changes: 58 additions & 0 deletions ActionExtension/ActionRequestHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Foundation
import XPCService

class ActionRequestHandler: NSObject, NSExtensionRequestHandling {
lazy var connection: NSXPCConnection = {
let connection = NSXPCConnection(serviceName: "us.eidinger.SwiftPlantUMLXPCService")
connection.remoteObjectInterface = NSXPCInterface(with: XPCServiceProtocol.self)
connection.resume()
return connection
}()

deinit {
connection.invalidate()
}

func beginRequest(with context: NSExtensionContext) {
// For an Action Extension there will only ever be one extension item.
precondition(context.inputItems.count == 1)
guard let inputItem = context.inputItems[0] as? NSExtensionItem
else { preconditionFailure("Expected an extension item") }

// The extension item's attachments hold the set of files to process.
guard let inputAttachments = inputItem.attachments
else { preconditionFailure("Expected a valid array of attachments") }
precondition(inputAttachments.isEmpty == false, "Expected at least one attachment")

// The output of this extension is the existing swift files
guard let inputAttachments = inputItem.attachments
else { preconditionFailure("Expected a valid array of attachments") }

let handler: (Error) -> Void = { error in
print("remote proxy error: \(error)")
}
let service = connection.remoteObjectProxyWithErrorHandler(handler) as! XPCServiceProtocol
var paths: [String] = []

// Use a dispatch group to synchronise asynchronous calls to loadInPlaceFileRepresentation.
let dispatchGroup = DispatchGroup()

for attachment in inputAttachments {
dispatchGroup.enter()

attachment.loadInPlaceFileRepresentation(forTypeIdentifier: "public.swift-source") { url, _, _ in
if let url = url {
paths.append(url.path)
}

dispatchGroup.leave()
}
}

dispatchGroup.notify(queue: DispatchQueue.main) {
service.generateDiagram(for: paths) {
context.completeRequest(returningItems: [inputItem], completionHandler: nil)
}
}
}
}
40 changes: 40 additions & 0 deletions ActionExtension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIconFile</key>
<string></string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>SUBQUERY (
extensionItems,
$extensionItem,
SUBQUERY (
$extensionItem.attachments,
$attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.swift-source"
).@count == $extensionItem.attachments.@count
).@count == 1</string>
<key>NSExtensionServiceAllowsFinderPreviewItem</key>
<true/>
<key>NSExtensionServiceAllowsTouchBarItem</key>
<false/>
<key>NSExtensionServiceFinderPreviewIconName</key>
<string>Symbol</string>
<key>NSExtensionServiceRoleType</key>
<string>NSExtensionServiceRoleTypeEditor</string>
<key>NSExtensionServiceTouchBarBezelColorName</key>
<string>TouchBarBezel</string>
<key>NSExtensionServiceTouchBarIconName</key>
<string>Symbol</string>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.services</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ActionRequestHandler</string>
</dict>
</dict>
</plist>
6 changes: 6 additions & 0 deletions ActionExtension/Media.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
30 changes: 30 additions & 0 deletions ActionExtension/Media.xcassets/Symbol.symbolset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"info" : {
"author" : "xcode",
"version" : 1
},
"symbols" : [
{
"filename" : "doc.badge.gearshape.svg",
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "light"
}
],
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"idiom" : "universal"
}
]
}
Loading

0 comments on commit 44837a7

Please sign in to comment.