git.iko.soy/SourceDocsSwift
on master help sign in
3 files changed+121−23
since splitsnapshots
unifiedsplit
README.md+12−2 SourceDocsSwift/TeXer.swift+50−13 SourceDocsSwift/main.swift+59−8
README.md+12−2
@@ -11,10 +11,14 @@ brew install ilyakooo0/tap/sourceDocsSwift
1111## Использование
1212
1313```bash
14−sourceDocsSwift -i path/to/XcodePrjectDirectory/ -o path/to/output/docs.tex
14+sourceDocsSwift -i path/to/XcodePrjectDirectory/ -o path/to/output/doc.tex -s latex -l 1
1515```
1616
17−По умолчанию ищет проект в текущей папке и пишет результат в эту же папку в файл `docas.tex`.
17+```bash
18+sourceDocsSwift -s tech -l 2
19+```
20+
21+По умолчанию ищет проект в текущей папке и пишет результат в эту же папку в файл `doc.tex`.
1822
1923В LaTeX документ можно вставить просто положив рядом с щсновным `.tex` документом и вставив в нужное место:
2024
@@ -27,3 +31,9 @@ sourceDocsSwift -i path/to/XcodePrjectDirectory/ -o path/to/output/docs.tex
2731```LaTeX
2832\usepackage{longtable}
2933```
34+### Параметры
35+
36+- `[-s | --style] [latex | tech]` -- стиль команды самого внешнего уровня
37+- `[-l | --level] [1 | 2 | 3]` -- отступ в уровнях
38+- `[-i | --input] path/to/projectDirectory` -- путь к папке с проектом
39+- `[-o | --output] path/to/output.tex` -- путь к фалу, в который писать результат
SourceDocsSwift/TeXer.swift+50−13
@@ -7,26 +7,26 @@
77//
88
99struct TeXer {
10− static func convert(objects: [Object]) -> String {
10+ static func convert(objects: [Object], with style: TexType = .laTeX(2)) -> String {
1111 return """
12− \\subsection{Описание классов и структур}
12+ \\\(style.section(for: 1)){Описание классов и структур}
1313
14− \\begin{longtable}{|p{0.55\\textwidth} | p{0.4\\textwidth}|}
14+ \\begin{longtable}{| >{\\raggedright\\arraybackslash}p{0.55\\textwidth} | p{0.4\\textwidth}|}
1515 \\hline
1616 \\textbf{Класс или стркура} & \\textbf{Описание} \\\\ \\hline
17− \( objects.map {"\\texttt{\($0.declaration)} & \($0.documentation ?? "{\\color{red} TODO}") \\\\ \\hline"} .joined(separator: "\n") )
17+ \( objects.map {"\\texttt{\($0.declaration)} & {\\color{red} TODO} \\\\ \\hline"} .joined(separator: "\n") )
1818 \\end{longtable}
19−
2019
21− \\subsection{Описание полей классов и структур}
20+
21+ \\\(style.section(for: 1)){Описание полей классов и структур}
2222
2323 \(
2424 objects.compactMap {
2525 if $0.fields.count > 0 {
2626 return """
27− \\subsubsection{\\texttt{\($0.declaration)}}
27+ \\\(style.section(for: 2))*{\\texttt{\($0.declaration)}}
2828
29− \\begin{longtable}{|p{0.55\\textwidth} | p{0.4\\textwidth}|}
29+ \\begin{longtable}{| >{\\raggedright\\arraybackslash}p{0.55\\textwidth} | p{0.4\\textwidth}|}
3030 \\hline
3131 \\textbf{Поле} & \\textbf{Описание} \\\\ \\hline
3232 \( $0.fields.map {"\\texttt{\($0.declaration.prefix {$0 != "="})} & {\\color{red} TODO} \\\\ \\hline"} .joined(separator: "\n") )
@@ -38,19 +38,19 @@ struct TeXer {
3838 }
3939 } .joined(separator: "\n")
4040 )
41−
42− \\subsection{Описание методов классов и структур}
41+
42+ \\\(style.section(for: 1)){Описание методов классов и структур}
4343
4444 \(
4545 objects.compactMap {
4646 if $0.methods.count > 0 {
4747 return """
48− \\subsubsection{\\texttt{\($0.declaration)}}
48+ \\\(style.section(for: 2))*{\\texttt{\($0.declaration)}}
4949
50− \\begin{longtable}{|p{0.55\\textwidth} | p{0.4\\textwidth}|}
50+ \\begin{longtable}{| >{\\raggedright\\arraybackslash}p{0.55\\textwidth} | p{0.4\\textwidth}|}
5151 \\hline
5252 \\textbf{Метод} & \\textbf{Описание} \\\\ \\hline
53− \( $0.methods.map {"\\texttt{\($0.declaration)} & \($0.documentation ?? "{\\color{red} TODO}") \\\\ \\hline"} .joined(separator: "\n") )
53+ \( $0.methods.map {"\\texttt{\($0.declaration)} & {\\color{red} TODO} \\\\ \\hline"} .joined(separator: "\n") )
5454 \\end{longtable}
5555
5656 """
@@ -61,4 +61,41 @@ struct TeXer {
6161 )
6262 """
6363 }
64+
65+ enum TexType {
66+ case techDoc(Int)
67+ case laTeX(Int)
68+
69+ func section(`for` level: Int) -> String {
70+ switch self {
71+ case .laTeX(let i):
72+ return String(repeating: "sub", count: i+level-2).appending("section")
73+ case .techDoc(let i):
74+ switch i+level-2 {
75+ case 0:
76+ return "addition"
77+ default:
78+ return TexType.laTeX(i).section(for: level)
79+ }
80+ }
81+ }
82+ var levelOffset: Int {
83+ get {
84+ switch self {
85+ case .laTeX(let i):
86+ return i
87+ case .techDoc(let i):
88+ return i
89+ }
90+ }
91+ set {
92+ switch self {
93+ case .laTeX(_):
94+ self = .laTeX(newValue)
95+ case .techDoc(_):
96+ self = .techDoc(newValue)
97+ }
98+ }
99+ }
100+ }
64101}
SourceDocsSwift/main.swift+59−8
@@ -9,30 +9,75 @@
99import Foundation
1010
1111let args = Array(CommandLine.arguments.dropFirst())
12−let temp = FileManager.default.temporaryDirectory.appendingPathComponent("SourceDocsSwift/build\(Date().timeIntervalSince1970)")
12+//let temp = FileManager.default.temporaryDirectory.appendingPathComponent("SourceDocsSwift/build\(Date().timeIntervalSince1970)", isDirectory: true)
1313
1414var output = "docs.tex"
1515var directory: String?
16+var style: TeXer.TexType = .laTeX(2)
1617
1718var i = 0
1819while i < args.count {
1920 switch args[i] {
20− case "-o":
21+ case "-o", "--output":
2122 if i + 1 < args.count {
2223 i += 1
2324 output = args[i]
2425 }
25− case "-i":
26+ case "-i", "--input":
2627 if i + 1 < args.count {
2728 i += 1
2829 directory = args[i]
2930 }
31+ case "-s", "--style":
32+ if i + 1 < args.count {
33+ i += 1
34+ switch args[i].lowercased() {
35+ case "latex", "xetex", "tex":
36+ style = .laTeX(style.levelOffset)
37+ case "tech", "techdoc", "techdocs":
38+ style = .techDoc(style.levelOffset)
39+ default:
40+ print("""
41+ That is not a valid style.
42+
43+ Valid styles are:
44+ latex
45+ techdoc
46+ """)
47+ exit(1)
48+ }
49+ }
50+ case "-l", "--level":
51+ if i + 1 < args.count {
52+ i += 1
53+ guard let l = Int(args[i]) else {
54+ print("""
55+ That is not a valid int.
56+ """)
57+ exit(1)
58+ }
59+ if l < 1 {
60+ print("""
61+ Level must be greater or equal to 1.
62+ """)
63+ exit(1)
64+ }
65+ style.levelOffset = l
66+ }
3067 default:
3168 break
3269 }
3370 i += 1
3471}
3572
73+var outputIsDirectory: ObjCBool = false
74+
75+FileManager.default.fileExists(atPath: output, isDirectory: &outputIsDirectory)
76+
77+if outputIsDirectory.boolValue {
78+ output.append("/docs.tex")
79+}
80+
3681let decoder = JSONDecoder()
3782
3883guard let xcbTagetData = Shell.launch(executable: "/usr/bin/xcodebuild", arguments: ["-list", "-json"], launchDirectory: directory) else {
@@ -54,8 +99,8 @@ let targets = xcbResponse.targets
5499var files: [String: [Object]] = [:]
55100
56101for target in targets {
57− try? FileManager.default.removeItem(at: temp)
58− guard let skResponseData = Shell.launch(executable: "/usr/local/bin/sourcekitten", arguments: ["doc", "--", "-target", target, "CONFIGURATION_BUILD_DIR=\(temp)"], launchDirectory: directory) else {
102+// try? FileManager.default.removeItem(at: temp)
103+ guard let skResponseData = Shell.launch(executable: "/usr/local/bin/sourcekitten", arguments: ["doc", "--", "-target", target], launchDirectory: directory) else {
59104 print("""
60105 Make sure sourcekitten is installed
61106
@@ -75,9 +120,15 @@ for target in targets {
75120
76121
77122
78−let tex = TeXer.convert(objects: files.flatMap {$0.value})
79−
80−try? tex.write(toFile: output, atomically: true, encoding: .utf8)
123+let tex = TeXer.convert(objects: files.flatMap {$0.value}, with: style)
124+do {
125+ try tex.write(toFile: output, atomically: true, encoding: .utf8)
126+} catch {
127+ print("""
128+ There was an error writing the outuput file.
129+ """)
130+ exit(1)
131+}
81132
82133print("""
83134+-------+