9
9
@testable import ShapeScript
10
10
import XCTest
11
11
12
- private let projectDirectory = URL ( fileURLWithPath: #file)
12
+ private let projectDirectory : URL = URL ( fileURLWithPath: #file)
13
13
. deletingLastPathComponent ( ) . deletingLastPathComponent ( )
14
14
15
15
private let changelogURL = projectDirectory
16
16
. appendingPathComponent ( " CHANGELOG.md " )
17
17
18
- private let podspecURL = projectDirectory
18
+ private let podspecURL : URL = projectDirectory
19
19
. appendingPathComponent ( " ShapeScript.podspec.json " )
20
20
21
- private let projectURL = projectDirectory
21
+ private let projectURL : URL = projectDirectory
22
22
. appendingPathComponent ( " ShapeScript.xcodeproj " )
23
23
. appendingPathComponent ( " project.pbxproj " )
24
24
25
- private let helpDirectory = projectDirectory
25
+ private let helpDirectory : URL = projectDirectory
26
26
. appendingPathComponent ( " docs " )
27
27
28
- private let helpSourceDirectory = helpDirectory
28
+ private let helpSourceDirectory : URL = helpDirectory
29
29
. appendingPathComponent ( " src " )
30
30
31
- private let helpIndexURL = helpSourceDirectory
31
+ private let helpIndexURL : URL = helpSourceDirectory
32
32
. appendingPathComponent ( " index.md " )
33
33
34
- private let imagesDirectory = helpDirectory
34
+ private let imagesDirectory : URL = helpDirectory
35
35
. appendingPathComponent ( " images " )
36
36
37
- private let examplesDirectory = projectDirectory
37
+ private let examplesDirectory : URL = projectDirectory
38
38
. appendingPathComponent ( " Examples " )
39
39
40
- private let exampleURLs = try ! FileManager . default
40
+ private let exampleURLs : [ URL ] = try ! FileManager . default
41
41
. contentsOfDirectory ( atPath: examplesDirectory. path)
42
42
. map { URL ( fileURLWithPath: $0, relativeTo: examplesDirectory) }
43
43
. filter { $0. pathExtension == " shape " }
@@ -56,12 +56,12 @@ private func findHeadings(in string: String) -> [String] {
56
56
. map { String ( $0. dropFirst ( 3 ) ) }
57
57
}
58
58
59
- private let headerLinks = [
59
+ private let headerLinks : [ ( String , String ) ] = [
60
60
( " Getting Started " , " getting-started.md " ) ,
61
61
( " Camera Control " , " camera-control.md " ) ,
62
62
]
63
63
64
- private let geometryLinks = [
64
+ private let geometryLinks : [ ( String , String ) ] = [
65
65
( " Primitives " , " primitives.md " ) ,
66
66
( " Options " , " options.md " ) ,
67
67
( " Materials " , " materials.md " ) ,
@@ -77,7 +77,7 @@ private let geometryLinks = [
77
77
( " Cameras " , " cameras.md " ) ,
78
78
]
79
79
80
- private let syntaxLinks = [
80
+ private let syntaxLinks : [ ( String , String ) ] = [
81
81
( " Comments " , " comments.md " ) ,
82
82
( " Literals " , " literals.md " ) ,
83
83
( " Symbols " , " symbols.md " ) ,
@@ -91,21 +91,33 @@ private let syntaxLinks = [
91
91
( " Import " , " import.md " ) ,
92
92
]
93
93
94
- private let footerLinks = [
94
+ private let footerLinks : [ ( String , String ) ] = [
95
95
( " Examples " , " examples.md " ) ,
96
96
( " Glossary " , " glossary.md " ) ,
97
97
]
98
98
99
+ private let versions : [ String ] = {
100
+ let fm = FileManager . default
101
+ var versions = Set ( [ shapeScriptVersion] )
102
+ let files = try ! fm. contentsOfDirectory ( atPath: helpDirectory. path)
103
+ for file in files where file. hasPrefix ( " 1. " ) {
104
+ versions. insert ( file)
105
+ }
106
+ return versions. sorted ( by: {
107
+ $0. localizedStandardCompare ( $1) == . orderedAscending
108
+ } )
109
+ } ( )
110
+
99
111
private extension URL {
100
112
func hasSuffix( _ suffix: String ) -> Bool {
101
- deletingPathExtension ( ) . lastPathComponent. hasSuffix ( " - " + suffix)
113
+ deletingPathExtension ( ) . lastPathComponent. hasSuffix ( suffix)
102
114
}
103
115
104
- func withSuffix ( _ suffix: String ) -> URL {
116
+ func appendingSuffix ( _ suffix: String ) -> URL {
105
117
let name = deletingPathExtension ( ) . lastPathComponent
106
118
return deletingPathExtension ( )
107
119
. deletingLastPathComponent ( )
108
- . appendingPathComponent ( name + " - " + suffix)
120
+ . appendingPathComponent ( name + suffix)
109
121
. appendingPathExtension ( pathExtension)
110
122
}
111
123
@@ -116,7 +128,7 @@ private extension URL {
116
128
let name = deletingPathExtension ( ) . lastPathComponent
117
129
return deletingPathExtension ( )
118
130
. deletingLastPathComponent ( )
119
- . appendingPathComponent ( String ( name. dropLast ( suffix. count + 1 ) ) )
131
+ . appendingPathComponent ( String ( name. dropLast ( suffix. count) ) )
120
132
. appendingPathExtension ( pathExtension)
121
133
}
122
134
}
@@ -207,7 +219,7 @@ class MetadataTests: XCTestCase {
207
219
208
220
for (index, url) in [
209
221
( indexMac, helpIndexURL) ,
210
- ( indexIOS, helpIndexURL. withSuffix ( " ios " ) ) ,
222
+ ( indexIOS, helpIndexURL. appendingSuffix ( " - ios" ) ) ,
211
223
] {
212
224
let existing = try String ( contentsOf: url)
213
225
XCTAssertEqual ( existing, index)
@@ -342,7 +354,7 @@ class MetadataTests: XCTestCase {
342
354
let enumerator = try XCTUnwrap ( fm. enumerator ( atPath: helpSourceDirectory. path) )
343
355
for case let file as String in enumerator {
344
356
let fileURL = helpSourceDirectory. appendingPathComponent ( file)
345
- guard fileURL. pathExtension == " md " , !fileURL. hasSuffix ( " ios " ) else {
357
+ guard fileURL. pathExtension == " md " , !fileURL. hasSuffix ( " - ios" ) else {
346
358
enumerator. skipDescendants ( )
347
359
continue
348
360
}
@@ -365,12 +377,9 @@ class MetadataTests: XCTestCase {
365
377
enumerator. skipDescendants ( )
366
378
continue
367
379
}
368
- if fileURL. hasSuffix ( " ios " ) {
369
- file = String ( fileURL
370
- . deletingPathExtension ( )
371
- . lastPathComponent
372
- . dropLast ( 4 ) ) + " .md "
373
- } else if fm. fileExists ( atPath: fileURL. withSuffix ( " ios " ) . path) {
380
+ if fileURL. hasSuffix ( " -ios " ) {
381
+ file = fileURL. deletingSuffix ( " -ios " ) . lastPathComponent
382
+ } else if fm. fileExists ( atPath: fileURL. appendingSuffix ( " -ios " ) . path) {
374
383
continue
375
384
}
376
385
let text = try XCTUnwrap ( String ( contentsOf: fileURL) )
@@ -387,18 +396,25 @@ class MetadataTests: XCTestCase {
387
396
else {
388
397
continue
389
398
}
390
- if ! url. hasSuffix ( " ios " ) {
391
- let isMac = url. hasSuffix ( " mac " )
399
+ if url. pathExtension == " png " {
400
+ let isMac = url. hasSuffix ( " - mac" )
392
401
if isMac {
393
- url = url. deletingSuffix ( " mac " )
402
+ url = url. deletingSuffix ( " -mac " )
403
+ }
404
+ if !url. hasSuffix ( " -ios " ) {
405
+ let iosURL = url. appendingSuffix ( " -ios " )
406
+ let absoluteURL = URL ( fileURLWithPath: iosURL. path, relativeTo: fileURL)
407
+ if fm. fileExists ( atPath: absoluteURL. path) {
408
+ url = iosURL
409
+ }
394
410
}
395
- let iosURL = url . withSuffix ( " ios " )
396
- let absoluteURL = URL ( fileURLWithPath : iosURL. path , relativeTo : fileURL )
397
- if url . pathExtension == " png " , fm . fileExists ( atPath : absoluteURL . path) {
398
- url = iosURL
399
- } else if isMac {
400
- let macFile = url . withSuffix ( " mac " ) . lastPathComponent
401
- XCTFail ( " File ' \( macFile ) ' has no iOS equivalent " )
411
+ for version in versions . reversed ( ) {
412
+ let iosURL = url . appendingSuffix ( " - \( version ) " )
413
+ let absoluteURL = URL ( fileURLWithPath : iosURL . path, relativeTo : fileURL )
414
+ if fm . fileExists ( atPath : absoluteURL . path ) {
415
+ url = iosURL
416
+ break
417
+ }
402
418
}
403
419
nsText. replaceCharacters ( in: range, with: url. absoluteString)
404
420
}
0 commit comments