-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
356 lines (277 loc) · 8.31 KB
/
main.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"os"
"path"
"github.com/inconshreveable/log15"
"path/filepath"
"errors"
"strings"
"io"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"fmt"
)
const OVERVIEW_SUMMARY = "overview-summary.html"
var log = log15.New()
var toIndex []string
func main() {
arguments := os.Args
argLength := len(arguments)
if (argLength == 2 && arguments[1] == "--help") {
printUsage()
return
} else if (argLength != 3) {
log.Error("Invalid argument(s) provided")
printUsage()
os.Exit(1)
}
docsetName := path.Clean(arguments[1])
var javadocPath = path.Clean(arguments[2])
log.Info("Running with arguments", "docsetName", docsetName, "javadocPath", javadocPath)
docsetDirectoryPath := docsetName + ".docset"
if exists, _ := pathExists(docsetDirectoryPath); exists {
log.Info("Removing existing docset directory", "Docset directory path", docsetDirectoryPath)
if err := os.RemoveAll(docsetDirectoryPath); err != nil {
log.Error(
"Unable to remove existing docset directory",
"Docset directory path", docsetDirectoryPath,
"error", err,
)
os.Exit(1)
}
}
contentsDirectoryPath := docsetDirectoryPath + "/Contents"
resourcesDirectoryPath := contentsDirectoryPath + "/Resources"
documentsDirectoryPath := resourcesDirectoryPath + "/Documents"
log.Info("Creating docset folder structure...")
if err := os.MkdirAll(documentsDirectoryPath, os.ModePerm); err != nil {
log.Error("Unable to create docset folder structure", "Docset directory", docsetDirectoryPath)
os.Exit(1)
}
var docsetIndexFile string
overviewSummaryPath := javadocPath + OVERVIEW_SUMMARY
var summaryFound = false
if exists, _ := pathExists(overviewSummaryPath); !exists {
walkCount := 0
filepath.Walk(javadocPath, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
log.Error("Failed to walk path", "path", filePath, "err", err)
os.Exit(1)
}
walkCount++
if walkCount < 10000 {
if info.Name() == OVERVIEW_SUMMARY {
javadocPath = path.Dir(filePath)
summaryFound = true
}
return nil
} else {
return errors.New("Hit file enumeration limit")
}
})
} else {
summaryFound = true
}
if summaryFound {
docsetIndexFile = OVERVIEW_SUMMARY
}
hasMultipleIndices := false
indexFilesPath := javadocPath + "index-files"
if exists, _ := pathExists(indexFilesPath); exists {
if docsetIndexFile == "" {
docsetIndexFile = "index-files/index-1.html"
}
hasMultipleIndices = true
}
log.Info("Done!")
copyFiles(documentsDirectoryPath, javadocPath)
documentsDirectoryIndex := documentsDirectoryPath + "/index-all.html"
if exists, _ := pathExists(documentsDirectoryIndex); !hasMultipleIndices && exists {
toIndex = append(toIndex, documentsDirectoryIndex)
if docsetIndexFile == "" {
docsetIndexFile = "index-all.html"
}
} else {
indexFilesPath := documentsDirectoryPath + "/index-files"
filepath.Walk(indexFilesPath, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
log.Error("Failed to walk path", "filePath", filePath, "err", err)
os.Exit(1)
}
filename := info.Name()
if strings.HasPrefix(filename, "index-") && strings.HasSuffix(filename, ".html") {
toIndex = append(toIndex, filePath)
}
return err
})
}
if len(toIndex) == 0 {
log.Error("API folder specified does not contain any index files (either an 'index-all.html' file or an 'index-files' folder and is not valid")
printUsage()
return
}
writeInfoPlist(docsetName, docsetIndexFile, contentsDirectoryPath)
initDB(resourcesDirectoryPath, index(toIndex))
}
func printUsage() {
log.Info("Usage: javadocset <docset name> <javadoc API folder>")
log.Info("<docset name> - anything you want")
log.Info("<javadoc API folder> - the path of the javadoc API folder you want to index")
}
func copyFiles(documentsDirectoryPath, javadocPath string) {
log.Info("Copying files...", "source", javadocPath, "destination", documentsDirectoryPath)
src := path.Clean(javadocPath)
dst := path.Clean(documentsDirectoryPath)
srcBase := path.Base(src)
filepath.Walk(src, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
log.Error("Error walking path", "filePath", filePath)
os.Exit(1)
}
if info.IsDir() {
if path.Base(filePath) != srcBase {
// We only want to copy the directories within the source directory
// to the destination directory
directoryName := strings.Split(filePath, srcBase)[1]
err := os.MkdirAll(dst + directoryName, os.ModePerm)
if err != nil {
log.Error("Unable to create directory", "directory", directoryName)
os.Exit(1)
}
}
} else {
// Copy file
fileName := filepath.Base(filePath)
directoryName := strings.Split(filepath.Dir(filePath), srcBase)[1]
dstPath := filepath.Clean(dst + directoryName + "/" + fileName)
err = copyFileContents(filePath, dstPath)
if err != nil {
log.Error("Unable to copy file", "src", filePath, "dst", dstPath)
os.Exit(1)
}
}
return err
})
log.Info("Done!")
}
func writeInfoPlist(docsetName, docsetIndexFile, contentsDirectoryPath string) {
plistContentTemplate := "<?xml version=\"1.0\" encoding=\"UTF-8\"?><plist version=\"1.0\"><dict><key>CFBundleIdentifier</key> <string>%v</string><key>CFBundleName</key> <string>%v</string> <key>DocSetPlatformFamily</key> <string>%v</string> <key>dashIndexFilePath</key><string>%v</string><key>DashDocSetFamily</key><string>java</string><key>isDashDocset</key><true/></dict></plist>"
docsetIdentifier := firstPhraseLowerCased(docsetName)
plistContent := fmt.Sprintf(
plistContentTemplate,
docsetIdentifier,
docsetName,
docsetIdentifier,
docsetIndexFile,
)
infoPlistPath := contentsDirectoryPath + "/Info.plist"
err := writeStringToFile(plistContent, infoPlistPath)
if err != nil {
log.Error("Unable to write to plist file", "plistPath", infoPlistPath)
}
}
func initDB(resourcesDirectoryPath string, dbFunc func(*sql.DB)) {
dbPath := filepath.Clean(resourcesDirectoryPath + "/docSet.dsidx")
// We don't care, we just want to remove the index
os.Remove(dbPath)
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Error("Unable to create sqlite database", "destination", dbPath, "error", err)
os.Exit(1)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT)")
if err != nil {
log.Error("Unable to create table", "error", err)
os.Exit(1)
}
if dbFunc != nil {
dbFunc(db)
}
}
func index(indicesToIndex []string) func(db *sql.DB) {
return func(db *sql.DB) {
tx, err := db.Begin()
if err != nil {
log.Error("Unable to begin transactions for database", "error", err)
os.Exit(1)
}
stmt, err := tx.Prepare("INSERT INTO searchIndex(name, type, path) VALUES (?, ?, ?)")
if err != nil {
log.Error("Unable to create statement to insert into database", "error", err)
os.Exit(1)
}
defer stmt.Close()
added := make(map[string]bool)
for _, toIndex := range indicesToIndex {
parseIndex(toIndex, func(entry IndexEntry) {
name, elementType, path := entry.name, entry.elementType.value(), entry.path
uniqueKey := name + elementType + path
if !added[uniqueKey] {
_, err := stmt.Exec(name, elementType, path)
if err != nil {
log.Error(
"Unable to insert entry",
"name", name,
"elementType", elementType,
"path", path,
)
os.Exit(1)
}
added[uniqueKey] = true
}
})
}
tx.Commit()
}
}
/**
Utility functions
*/
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func writeStringToFile(content, dst string) error {
file, err := os.Create(dst)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write([]byte(content))
return err
}
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
log.Error("Error copying", "error", err)
return
}
err = out.Sync()
return
}
func firstPhraseLowerCased(s string) string {
return strings.ToLower(func() string {
return strings.Split(s, " ")[0]
}())
}