Skip to content

Commit eb97334

Browse files
authored
Merge pull request #6 from Yelp/maxr/dont_use_tmp
Use NSTemporaryDirectory() instead of /tmp/Salsa
2 parents e76bb29 + 905e126 commit eb97334

File tree

6 files changed

+50
-38
lines changed

6 files changed

+50
-38
lines changed

Example/generate_sketch

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
echo "Building project and exporting view snapshots"
4-
xcodebuild test -workspace SalsaExample.xcworkspace/ -scheme SalsaExample -destination 'platform=iOS Simulator,name=iPhone 7,OS=11.3'
4+
xcodebuild test -workspace SalsaExample.xcworkspace/ -scheme SalsaExample -destination 'platform=iOS Simulator,name=iPhone 8'
55
if [ $? -ne 0 ]; then
66
echo "Failed to export view snapshots"
77
exit 1

SalsaCompiler/SalsaCompiler/SketchJson/Page+SketchJson.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111
extension Page: SketchJsonConvertable {
1212
func toSketchJson() -> [String: Any] {
1313
guard let id = IdentifierStore.identifier(forPageNamed: name) else {
14-
fatalError("Couldn't get id for page: \(name)")
14+
exit(withMessage: "Couldn't get id for page: \(name)")
1515
}
1616
return Layer.makeBaseSketchJson().merging(with: [
1717
"do_objectID": id,

SalsaCompiler/SalsaCompiler/SketchJson/SymbolInstance+SketchJson.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111
extension SymbolInstance {
1212
override func toSketchJson() -> [String : Any] {
1313
guard let symbolID = IdentifierStore.identifier(forSymbolNamed: name) else {
14-
fatalError("Could not find symbol named: \(name)")
14+
exit(withMessage: "Could not find symbol named: \(name)")
1515
}
1616
return super.toSketchJson().merging(with: [
1717
"_class": "symbolInstance",

SalsaCompiler/SalsaCompiler/SketchJson/SymbolMaster+SketchJson.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111
extension SymbolMaster {
1212
override func toSketchJson() -> [String : Any] {
1313
guard let symbolID = IdentifierStore.identifier(forSymbolNamed: name) else {
14-
fatalError("Could not find symbol named: \(name)")
14+
exit(withMessage: "Could not find symbol named: \(name)")
1515
}
1616
return super.toSketchJson().merging(with: [
1717
"_class": "symbolMaster",

SalsaCompiler/SalsaCompiler/SketchJson/Text+SketchJson.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import Cocoa
1010

1111
extension Text {
1212
override func toSketchJson() -> [String: Any] {
13-
guard let firstSegment = segments.first else { fatalError("Can't initialize text with no text segments") }
13+
guard let firstSegment = segments.first else {
14+
exit(withMessage: "Can't initialize text with no text segments")
15+
}
1416
var constraints: [ResizingConstraint] = {
1517
switch firstSegment.style.alignment {
1618
case .left: return [.left]
@@ -73,7 +75,7 @@ extension Text {
7375
let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
7476
paragraphStyle.alignment = segment.style.alignment.nsTextAlignment()
7577
guard let font = NSFont(name: segment.style.fontDescriptor, size: segment.style.fontSize) else {
76-
fatalError("Could not find font: \(segment.style.fontDescriptor)")
78+
exit(withMessage: "Could not find font: \(segment.style.fontDescriptor)")
7779
}
7880

7981
// Sketch creates attributed strings with a custom attribute for font
@@ -256,7 +258,7 @@ extension Text.Style.Alignment {
256258
extension Text.SharedStyle: SketchJsonConvertable {
257259
func toSketchJson() -> [String: Any] {
258260
guard let id = IdentifierStore.identifier(forTextStyle: textStyle) else {
259-
fatalError("Failed to get identifier for shared style named: \(name)")
261+
exit(withMessage: "Failed to get identifier for shared style named: \(name)")
260262
}
261263
return [
262264
"_class": "sharedStyle",

SalsaCompiler/SalsaCompiler/main.swift

+41-31
Original file line numberDiff line numberDiff line change
@@ -50,113 +50,123 @@ let exportFile: URL = {
5050

5151

5252
// Find a name for the working directory
53-
let workingDirectory: String = {
53+
let workingDirectory: URL = {
54+
let tempDir = URL(fileURLWithPath: NSTemporaryDirectory())
5455
while true {
55-
let directory = "/tmp/Salsa/\(UUID().uuidString)"
56-
if !FileManager.default.fileExists(atPath: directory) {
56+
let directory = tempDir.appendingPathComponent(UUID().uuidString, isDirectory: true)
57+
if !FileManager.default.fileExists(atPath: directory.path) {
5758
return directory
5859
}
5960
}
6061
}()
6162

63+
func cleanupWorkingDirectory() {
64+
try? FileManager.default.removeItem(at: workingDirectory)
65+
}
66+
67+
func exit(withMessage message: String) -> Never {
68+
cleanupWorkingDirectory()
69+
fatalError(message)
70+
}
71+
6272
// Create the working directory
6373
do {
64-
try FileManager.default.createDirectory(atPath: workingDirectory, withIntermediateDirectories: true, attributes: nil)
74+
try FileManager.default.createDirectory(at: workingDirectory, withIntermediateDirectories: true, attributes: nil)
6575
} catch {
66-
fatalError("Failed to create directory: \(workingDirectory)")
76+
exit(withMessage: "Failed to create directory: \(workingDirectory.path)")
6777
}
6878

6979
// Clone images into the working directory
7080
do {
71-
try FileManager.default.copyItem(atPath: imagesDirectory.path, toPath: "\(workingDirectory)/images")
81+
try FileManager.default.copyItem(at: imagesDirectory, to: workingDirectory.appendingPathComponent("images", isDirectory: true))
7282
} catch {
73-
fatalError("Failed to clone images directory: \(imagesDirectory)")
83+
exit(withMessage: "Failed to clone images directory: \(imagesDirectory.path)")
7484
}
7585

7686
// Read in the input file
7787
let json = parseJsonFile(at: filePath.path)
7888
guard let document = Document(json: json) else {
79-
fatalError("Failed to parse \(filePath.lastPathComponent)")
89+
exit(withMessage: "Failed to parse \(filePath.lastPathComponent)")
8090
}
8191

8292
// Create /pages/ directory
83-
let pagesPath = "\(workingDirectory)/pages/"
93+
let pagesDirectory = workingDirectory.appendingPathComponent("pages", isDirectory: true)
8494
do {
85-
try FileManager.default.createDirectory(atPath: pagesPath, withIntermediateDirectories: true, attributes: nil)
95+
try FileManager.default.createDirectory(at: pagesDirectory, withIntermediateDirectories: true, attributes: nil)
8696
} catch {
87-
fatalError("Failed to create directory: \(pagesPath)")
97+
exit(withMessage: "Failed to create directory: \(pagesDirectory.path)")
8898
}
8999

90100
// Configure identifiers for shared objects
91101
IdentifierStore.configure(with: document)
92102

93103
// Convert pages to json and save them to disk
94104
document.pages.enumerated().forEach { index, page in
95-
guard let pageID = IdentifierStore.identifier(forPageNamed: page.name) else { fatalError("Failed to create page \(index + 1)") }
105+
guard let pageID = IdentifierStore.identifier(forPageNamed: page.name) else { exit(withMessage: "Failed to create page \(index + 1)") }
96106
do {
97-
let url = NSURL.fileURL(withPath: "\(workingDirectory)/pages/\(pageID).json")
98-
guard let pageJsonString = page.simplified().toSketchJson().toJsonString() else { fatalError("Failed to parse page \(index + 1)") }
107+
let url = pagesDirectory.appendingPathComponent("\(pageID).json")
108+
guard let pageJsonString = page.simplified().toSketchJson().toJsonString() else { exit(withMessage: "Failed to parse page \(index + 1)") }
99109
try pageJsonString.write(to: url, atomically: false, encoding: String.Encoding.utf8)
100110
} catch {
101-
fatalError("Failed to save page \(index + 1)")
111+
exit(withMessage: "Failed to save page \(index + 1)")
102112
}
103113
}
104114

105115
// Create document.json
106-
guard let documentJson = document.toSketchJson(pageIds: document.pages.reversed().flatMap { IdentifierStore.identifier(forPageNamed: $0.name) }).toJsonString() else {
107-
fatalError("Failed to convert document to json")
116+
guard let documentJson = document.toSketchJson(pageIds: document.pages.reversed().compactMap { IdentifierStore.identifier(forPageNamed: $0.name) }).toJsonString() else {
117+
exit(withMessage: "Failed to convert document to json")
108118
}
109119

110120
// Save document.json to disk
111121
do {
112-
let url = NSURL.fileURL(withPath: "\(workingDirectory)/document.json")
122+
let url = workingDirectory.appendingPathComponent("document.json")
113123
try documentJson.write(to: url, atomically: false, encoding: String.Encoding.utf8)
114124
} catch {
115-
fatalError("Failed to save document.json to disk")
125+
exit(withMessage: "Failed to save document.json to disk")
116126
}
117127

118128
// Create meta.json
119129
guard let metaData = makeMetaData().toJsonString() else {
120-
fatalError("Failed to generate meta.json")
130+
exit(withMessage: "Failed to generate meta.json")
121131
}
122132

123133
// Save meta.json to disk
124134
do {
125-
let url = NSURL.fileURL(withPath: "\(workingDirectory)/meta.json")
135+
let url = workingDirectory.appendingPathComponent("meta.json")
126136
try metaData.write(to: url, atomically: false, encoding: String.Encoding.utf8)
127137
} catch {
128-
fatalError("Failed to save meta.json")
138+
exit(withMessage: "Failed to save meta.json")
129139
}
130140

131141
// Sketch requires that this file exists but it doesn't need to contain anything
132142
// Its used to recover your position and state in the document on subsequent launches
133143
do {
134-
let url = NSURL.fileURL(withPath: "\(workingDirectory)/user.json")
144+
let url = workingDirectory.appendingPathComponent("user.json")
135145
try [String: Any]().toJsonString()!.write(to: url, atomically: false, encoding: String.Encoding.utf8)
136146
} catch {
137-
fatalError("Failed to save user.json")
147+
exit(withMessage: "Failed to save user.json")
138148
}
139149

140150

141-
let exportDirectory = exportFile.deletingLastPathComponent().path
151+
let exportDirectory = exportFile.deletingLastPathComponent()
142152
let exportFileName = exportFile.lastPathComponent
143153

144154
// Create the export directory if it doesn't exist
145-
if !FileManager.default.fileExists(atPath: exportDirectory) {
155+
if !FileManager.default.fileExists(atPath: exportDirectory.path) {
146156
do {
147-
try FileManager.default.createDirectory(atPath: exportDirectory, withIntermediateDirectories: true, attributes: nil)
157+
try FileManager.default.createDirectory(at: exportDirectory, withIntermediateDirectories: true, attributes: nil)
148158
} catch {
149-
fatalError("Failed to create directory: \(exportDirectory)")
159+
exit(withMessage: "Failed to create directory: \(exportDirectory.path)")
150160
}
151161
}
152162

153163
// Swift doesn't come with zipping utilities. Instead of adding one as a dependency we just use bash to zip our exported files into a sketch file
154-
guard bash(command: "zip", arguments: ["-r", "\(exportDirectory)/\(exportFileName)", "./"], directory: workingDirectory).returnCode == 0 else {
155-
fatalError("Failed to create zipped .sketch file. Do you have zip installed?")
164+
guard bash(command: "zip", arguments: ["-r", "\(exportDirectory.path)/\(exportFileName)", "./"], directory: workingDirectory.path).returnCode == 0 else {
165+
exit(withMessage: "Failed to create zipped .sketch file. Do you have zip installed?")
156166
}
157167

158168
// Clean up our working directory
159-
try? FileManager.default.removeItem(atPath: workingDirectory)
169+
cleanupWorkingDirectory()
160170

161171
print("(☞゚ヮ゚)☞ Generated \(exportFile.path) ☜(゚ヮ゚☜)")
162172

0 commit comments

Comments
 (0)