-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.swift
90 lines (63 loc) · 3.66 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// main.swift
// FontBuilder
//
// Created by Brendan Lee on 8/10/17.
// Copyright © 2017 Brendan Lee. All rights reserved.
//
import Foundation
// Configuration
let fontDataSource = URL(string: "https://raw.githubusercontent.com/mcritz/iosfonts/master/data/iosfonts.json")!
let outputDirectory = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.desktopDirectory, .userDomainMask, true)[0]).appendingPathComponent("Fonts")
guard let rawJSONString = try? String(contentsOf: fontDataSource) else {
fatalError("Unable to download font info JSON")
}
guard let jsonData = rawJSONString.data(using: .utf8), let jsonDict = try? JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as? [String : Any] else {
fatalError("Unable to parse font info JSON")
}
guard let fonts = jsonDict?["fonts"] as? [[String : Any]] else {
fatalError("Unable to locate font info in JSON")
}
print("Number of fonts: \(fonts.count)")
// Delete existing files
_ = try? FileManager.default.removeItem(at: outputDirectory)
// Create folder
_ = try? FileManager.default.createDirectory(at: outputDirectory, withIntermediateDirectories: true, attributes: nil)
var collectedExtensions = ""
var collectedPodSpecs = ""
for currentFont in fonts
{
guard let familyName = currentFont["family_name"] as? String else {
print("Warning: Unable to determine a family name. Skipping: \(String(describing: currentFont))")
continue
}
// San francisco has varying styles, per device, along with weird overlaps. We remove it here, as it's already available as a 'real' API from UIFont, so not worth supporting.
guard familyName.lowercased() != "san francisco" else {
continue
}
guard let extensionContent = generateFontFamilyExtension(fontFamily: currentFont) else {
print("Warning: Unable to create extension for font: \(familyName)")
continue
}
collectedExtensions += extensionContent
collectedExtensions += "\n"
let extensionWriteDirectory = outputDirectory.appendingPathComponent(familyName)
_ = try? FileManager.default.createDirectory(at: extensionWriteDirectory, withIntermediateDirectories: true, attributes: nil)
let extensionWriteFile = extensionWriteDirectory.appendingPathComponent("\(familyName)-FontStyles.swift")
_ = try? ("import UIKit\n\n" + extensionContent).write(to: extensionWriteFile, atomically: true, encoding: .utf8)
collectedPodSpecs += " s.subspec '\(normalizeFontName(fontName: familyName))' do |spec|\n"
collectedPodSpecs += "\tspec.source_files = 'Fonts/\(familyName)/*.{swift}'\n"
collectedPodSpecs += " end\n\n"
}
let extensionWriteDirectory = outputDirectory.appendingPathComponent("All Fonts")
_ = try? FileManager.default.createDirectory(at: extensionWriteDirectory, withIntermediateDirectories: true, attributes: nil)
let extensionWriteFile = extensionWriteDirectory.appendingPathComponent("FontStyles.swift")
_ = try? ("import UIKit\n\n" + collectedExtensions).write(to: extensionWriteFile, atomically: true, encoding: .utf8)
collectedPodSpecs += " s.subspec '\("AllFonts")' do |spec|\n"
collectedPodSpecs += "\tspec.source_files = 'Fonts/\("All Fonts")/*.{swift}'\n"
collectedPodSpecs += " end\n\n"
guard let podspecTemplate = try? String(contentsOfFile: "PodSpecTemplate.txt") else {
fatalError("Unable to locate PodSpecTemplate.txt")
}
let replacedPodSpec = podspecTemplate.replacingOccurrences(of: "##INSERT_FONT_SUBSPECS", with: collectedPodSpecs)
_ = try? replacedPodSpec.write(to: outputDirectory.deletingLastPathComponent().appendingPathComponent("FontConstants.podspec"), atomically: true, encoding: .utf8)