Skip to content

Commit 64aacaa

Browse files
author
David Siegel
committed
Customize Objective-C output for .h vs .m files
1 parent c3c514e commit 64aacaa

7 files changed

Lines changed: 76 additions & 34 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
"homepage": "https://github.com/quicktype/quicktype-xcode#readme",
2020
"devDependencies": {
2121
"browserify": "^14.5.0",
22-
"quicktype": "8.*.*"
22+
"quicktype": "^8.2.9"
2323
}
2424
}

quicktype-xcode.xcodeproj/xcshareddata/xcschemes/quicktype-xcode.xcscheme

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<BuildableReference
3131
BuildableIdentifier = "primary"
3232
BlueprintIdentifier = "6317CCFD1FECC5DC006CCFC1"
33-
BuildableName = "quicktype.app"
33+
BuildableName = "Paste JSON as Code &#x2022; quicktype.app"
3434
BlueprintName = "quicktype"
3535
ReferencedContainer = "container:quicktype-xcode.xcodeproj">
3636
</BuildableReference>
@@ -69,16 +69,20 @@
6969
debugServiceExtension = "internal"
7070
allowLocationSimulation = "YES"
7171
launchAutomaticallySubstyle = "2">
72-
<BuildableProductRunnable
73-
runnableDebuggingMode = "0">
72+
<RemoteRunnable
73+
runnableDebuggingMode = "0"
74+
BundleIdentifier = "com.apple.dt.Xcode"
75+
RemotePath = "/Applications/Xcode.app">
76+
</RemoteRunnable>
77+
<MacroExpansion>
7478
<BuildableReference
7579
BuildableIdentifier = "primary"
7680
BlueprintIdentifier = "6317CCFD1FECC5DC006CCFC1"
77-
BuildableName = "quicktype.app"
81+
BuildableName = "Paste JSON as Code &#x2022; quicktype.app"
7882
BlueprintName = "quicktype"
7983
ReferencedContainer = "container:quicktype-xcode.xcodeproj">
8084
</BuildableReference>
81-
</BuildableProductRunnable>
85+
</MacroExpansion>
8286
<AdditionalOptions>
8387
</AdditionalOptions>
8488
</LaunchAction>
@@ -94,7 +98,7 @@
9498
<BuildableReference
9599
BuildableIdentifier = "primary"
96100
BlueprintIdentifier = "6317CCFD1FECC5DC006CCFC1"
97-
BuildableName = "quicktype.app"
101+
BuildableName = "Paste JSON as Code &#x2022; quicktype.app"
98102
BlueprintName = "quicktype"
99103
ReferencedContainer = "container:quicktype-xcode.xcodeproj">
100104
</BuildableReference>

quicktype-xcode.xcodeproj/xcshareddata/xcschemes/quicktype.xcscheme

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<BuildableReference
1616
BuildableIdentifier = "primary"
1717
BlueprintIdentifier = "6317CCFD1FECC5DC006CCFC1"
18-
BuildableName = "quicktype.app"
18+
BuildableName = "Paste JSON as Code &#x2022; quicktype.app"
1919
BlueprintName = "quicktype"
2020
ReferencedContainer = "container:quicktype-xcode.xcodeproj">
2121
</BuildableReference>
@@ -34,7 +34,7 @@
3434
<BuildableReference
3535
BuildableIdentifier = "primary"
3636
BlueprintIdentifier = "6317CCFD1FECC5DC006CCFC1"
37-
BuildableName = "quicktype.app"
37+
BuildableName = "Paste JSON as Code &#x2022; quicktype.app"
3838
BlueprintName = "quicktype"
3939
ReferencedContainer = "container:quicktype-xcode.xcodeproj">
4040
</BuildableReference>
@@ -58,7 +58,7 @@
5858
<BuildableReference
5959
BuildableIdentifier = "primary"
6060
BlueprintIdentifier = "6317CCFD1FECC5DC006CCFC1"
61-
BuildableName = "quicktype.app"
61+
BuildableName = "Paste JSON as Code &#x2022; quicktype.app"
6262
BlueprintName = "quicktype"
6363
ReferencedContainer = "container:quicktype-xcode.xcodeproj">
6464
</BuildableReference>
@@ -77,7 +77,7 @@
7777
<BuildableReference
7878
BuildableIdentifier = "primary"
7979
BlueprintIdentifier = "6317CCFD1FECC5DC006CCFC1"
80-
BuildableName = "quicktype.app"
80+
BuildableName = "Paste JSON as Code &#x2022; quicktype.app"
8181
BlueprintName = "quicktype"
8282
ReferencedContainer = "container:quicktype-xcode.xcodeproj">
8383
</BuildableReference>

quicktype-xcode/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<key>CFBundlePackageType</key>
1818
<string>XPC!</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>7.0.8</string>
20+
<string>8.2.9</string>
2121
<key>CFBundleVersion</key>
22-
<string>7.0.8</string>
22+
<string>8.2.9</string>
2323
<key>LSMinimumSystemVersion</key>
2424
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
2525
<key>NSExtension</key>

quicktype-xcode/PasteJSONCommand.swift

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,30 @@ func command(identifier: String) -> Command? {
2222
return Command(rawValue: String(component))
2323
}
2424

25-
struct Options {
26-
let justTypes: Bool
27-
}
25+
let defaultOptionsForCommand: [Command: [String: Any]] = [
26+
.pasteJSONAsTypes: ["just-types": true],
27+
.pasteJSONAsCode: ["just-types": false],
28+
]
2829

29-
let optionsForCommand: [Command: Options] = [
30-
.pasteJSONAsTypes: Options(justTypes: true),
31-
.pasteJSONAsCode: Options(justTypes: false)
30+
let languageOptionsForCommand: [Command: [Language: [String: Any]]] = [
31+
.pasteJSONAsTypes: [
32+
.objc: ["features": "implementation", "just-types": false],
33+
.objcHeader: ["features": "interface"]
34+
],
35+
.pasteJSONAsCode: [
36+
.objc: [
37+
// Objective-C is not ideal yet, so extra comments are useful
38+
"extra-comments": true,
39+
// We also intentionally output implementation *and* interfaces until we can emit
40+
// better instructions for importing your header
41+
"features": "all"
42+
],
43+
.objcHeader: ["features": "interface"],
44+
.swift: ["initializers": true]
45+
]
3246
]
3347

48+
3449
class PasteJSONCommand: NSObject, XCSourceEditorCommand {
3550
func error(_ message: String, details: String = "No details") -> NSError {
3651
return NSError(domain: "quicktype", code: 1, userInfo: [
@@ -136,13 +151,23 @@ class PasteJSONCommand: NSObject, XCSourceEditorCommand {
136151
completionHandler(error(displayMessage, details: message))
137152
}
138153

154+
func getOptions(_ command: Command, _ language: Language) -> [String: Any] {
155+
let defaults = defaultOptionsForCommand[command] ?? [:]
156+
let options = languageOptionsForCommand[command]?[language] ?? [:]
157+
return defaults.merging(options, uniquingKeysWith: { $1 })
158+
}
159+
160+
func getTargetLanguage(_ command: Command, _ invocation: Invocation) -> Language? {
161+
return languageFor(contentUTI: invocation.buffer.contentUTI as CFString)
162+
}
163+
139164
func perform(with invocation: Invocation, completionHandler: @escaping (Error?) -> Void) -> Void {
140165
guard let command = command(identifier: invocation.commandIdentifier) else {
141166
completionHandler(error("Unrecognized command"))
142167
return
143168
}
144169

145-
guard let language = languageFor(contentUTI: invocation.buffer.contentUTI as CFString) else {
170+
guard let language = getTargetLanguage(command, invocation) else {
146171
completionHandler(error("Cannot generate code for \(invocation.buffer.contentUTI)"))
147172
return
148173
}
@@ -152,11 +177,7 @@ class PasteJSONCommand: NSObject, XCSourceEditorCommand {
152177
"language": language.rawValue
153178
])
154179

155-
guard let options = optionsForCommand[command] else {
156-
completionHandler(error("Could not determine command options"))
157-
return
158-
}
159-
180+
let options = getOptions(command, language)
160181
let runtime = Runtime.shared
161182

162183
if !runtime.isInitialized && !runtime.initialize() {
@@ -171,7 +192,7 @@ class PasteJSONCommand: NSObject, XCSourceEditorCommand {
171192

172193
runtime.quicktype(json,
173194
language: language,
174-
justTypes: options.justTypes,
195+
options: options,
175196
fail: { self.handleError(message: $0, invocation, completionHandler) },
176197
success: { self.handleSuccess(lines: $0, invocation, completionHandler) })
177198
}

quicktype-xcode/Runtime.swift

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ import Foundation
22
import JavaScriptCore
33

44
enum Language: String {
5-
case swift, java, cpp, objc
5+
case swift, java, cpp, objc, objcHeader
66
}
77

88
fileprivate let languageUTIs: [CFString: Language] = [
99
kUTTypeSwiftSource: .swift,
1010
kUTTypeObjectiveCSource: .objc,
11-
kUTTypeCHeader: .objc,
11+
kUTTypeCHeader: .objcHeader,
1212
kUTTypeJavaSource: .java,
1313
kUTTypeCPlusPlusSource: .cpp,
1414
kUTTypeObjectiveCPlusPlusSource: .objc,
1515
"com.apple.dt.playground" as CFString: .swift
1616
]
1717

1818
func languageFor(contentUTI: CFString) -> Language? {
19+
print(contentUTI)
1920
for (uti, language) in languageUTIs {
2021
if UTTypeConformsTo(contentUTI as CFString, uti) {
2122
return language
@@ -87,7 +88,25 @@ class Runtime {
8788
context.setObject(rejectBlock, forKeyedSubscript: "reject" as NSString)
8889
}
8990

90-
func quicktype(_ json: String, language: Language, justTypes: Bool, fail: @escaping (String) -> Void, success: @escaping ([String]) -> Void) {
91+
func renderOptionsToJavaScriptObject(_ options: [String: Any]) -> String {
92+
return "{ " + options.map { key, value in
93+
var javaScriptValue = "\(value)"
94+
95+
switch value {
96+
case is String: javaScriptValue = "\"\(value)\""
97+
default: break
98+
}
99+
100+
return "\"\(key)\": \(javaScriptValue)"
101+
}.joined(separator: ", ") + " }"
102+
}
103+
104+
func quicktype(_ json: String, language: Language, options: [String: Any], fail: @escaping (String) -> Void, success: @escaping ([String]) -> Void) {
105+
// .header (C header files) are assumed to be Objective-C headers
106+
if language == .objcHeader {
107+
return quicktype(json, language:.objc, options: options, fail: fail, success: success)
108+
}
109+
91110
resolve { lines in success(lines) }
92111
reject { errorMessage in fail(errorMessage) }
93112

@@ -102,9 +121,7 @@ class Runtime {
102121
samples: [json]
103122
}],
104123
leadingComments: [\(comments)],
105-
rendererOptions: {
106-
"just-types": \(justTypes ? "true" : "false")
107-
}
124+
rendererOptions: \(renderOptionsToJavaScriptObject(options)),
108125
}).then(function(result) {
109126
resolve(result.lines);
110127
}).catch(function(e) {

quicktype/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>7.0.8</string>
20+
<string>8.2.9</string>
2121
<key>CFBundleVersion</key>
22-
<string>7.0.8</string>
22+
<string>8.2.9</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.developer-tools</string>
2525
<key>LSMinimumSystemVersion</key>

0 commit comments

Comments
 (0)