-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.go
130 lines (116 loc) · 2.93 KB
/
addon.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
type Addon struct {
title string
author string
notes string
version string
revision string
dependencies []string
optionalDeps []string
savedVariables []string
x map[string]string
path string
files []string
updateMethod string
}
func GetAddon(dir os.FileInfo) (addon Addon, err error) {
var files []string
files, err = listAddonFiles(dir)
toc := findTocInList(files)
params, tocFileList := parseToc(toc)
addon = Addon{
title: params["Title"],
author: params["Author"],
notes: params["Notes"],
version: params["Version"],
dependencies: parseDependencies(params["Dependencies"]),
optionalDeps: parseDependencies(params["OptionalDeps"]),
savedVariables: parseDependencies(params["SavedVariables"]),
revision: params["Revision"],
files: tocFileList,
path: dir.Name(),
updateMethod: getUpdateMethod(files),
}
return addon, err
}
func getUpdateMethod(files []string) (updateMethod string) {
updateMethod = "curse"
for _, value := range files {
if isGit(value) {
updateMethod = "git"
} else if isSvn(value) {
updateMethod = "svn"
}
}
return updateMethod
}
func isGit(dir string) (result bool) {
matches, _ := regexp.MatchString("^\\.git$", filepath.Base(dir))
return matches
}
func isSvn(dir string) (result bool) {
matches, _ := regexp.MatchString("^\\.svn$", filepath.Base(dir))
return matches
}
func parseDependencies(depList string) (deps []string) {
split := strings.SplitN(depList, ",", 2)
for _, v := range split {
deps = append(deps, strings.TrimSpace(v))
}
return deps
}
func parseToc(path string) (toc map[string]string, files []string) {
toc = make(map[string]string)
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if tocLineIsComment(line) && len(line) > 3 {
split := tocLineParseMetaData(line)
if len(split) >= 2 {
toc[split[0]] = split[1]
}
} else if len(line) > 2 {
files = append(files, line)
}
}
return toc, files
}
func tocLineIsComment(line string) (isComment bool) {
return strings.HasPrefix(line, "##")
}
func tocLineParseMetaData(line string) (split []string) {
trimmed := strings.TrimSpace(strings.TrimPrefix(line, "##"))
split = strings.SplitN(trimmed, ":", 2)
return split
}
func listAddonFiles(dir os.FileInfo) (files []string, err error) {
if !dir.IsDir() {
return files, fmt.Errorf("%v is not a directory", dir.Name())
}
pattern := filepath.Join(getWowPath(), dir.Name(), "*")
files, err = filepath.Glob(pattern)
return files, err
}
func findTocInList(files []string) (file string) {
for _, value := range files {
matches, _ := regexp.MatchString("\\.toc$", value)
if matches {
file = value
}
}
return file
}