-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.go
133 lines (123 loc) · 3.06 KB
/
module.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
package linkedpackage
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
)
type Module struct {
Lang string
Name string
Path string
Author string
LicenseName string
LicenseContent string
Version string
}
func (m *Module) readLicense(root string) error {
// Find LICENSE*
entries, err := ioutil.ReadDir(filepath.Join(root, m.Path))
if err != nil {
return err
}
for _, entry := range entries {
if !entry.IsDir() && strings.HasPrefix(strings.ToUpper(entry.Name()), "LICENSE") {
licensePath := filepath.Join(root, m.Path, entry.Name())
licenseContent, err := ioutil.ReadFile(licensePath)
if err == nil {
m.LicenseContent = strings.TrimSpace(string(licenseContent))
return nil
}
}
}
for _, entry := range entries {
if !entry.IsDir() && strings.HasPrefix(strings.ToUpper(entry.Name()), "README") {
readmePath := filepath.Join(root, m.Path, entry.Name())
f, err := os.Open(readmePath)
if err != nil {
continue
}
s := bufio.NewScanner(f)
var lines []string
insideLicense := false
var heading int
for s.Scan() {
text := s.Text()
if !insideLicense {
if strings.HasPrefix(text, "#") && strings.Contains(strings.ToUpper(text), "LICENSE") {
left := strings.TrimLeft(text, "#")
heading = len(text) - len(left)
insideLicense = true
}
} else {
if strings.HasPrefix(text, "#") {
left := strings.TrimLeft(text, "#")
if len(text) - len(left) <= heading {
m.LicenseContent = strings.Join(lines, "\n")
return nil
}
}
lines = append(lines, text)
}
}
}
}
return errors.New("license file missing")
}
func UniqueModules(modules []Module) []Module {
used := make(map[string]bool)
result := []Module{}
for _, module := range modules {
key := module.Lang + "----" + module.Path
if used[key] {
continue
}
result = append(result, module)
used[key] = true
}
sort.Slice(result, func(i, j int) bool {
return result[i].Name < result[j].Name
})
return result
}
type GroupedModule struct {
Author string
License string
Modules []Module
}
func GroupingModulesByLicense(modules []Module) []GroupedModule {
result := []GroupedModule{}
indexes := make(map[string]int)
for _, module := range modules {
key := module.Author + "------" + module.LicenseName
index, ok := indexes[key]
if ok {
result[index].Modules = append(result[index].Modules, module)
} else {
indexes[key] = len(result)
result = append(result, GroupedModule{
Author: module.Author,
License: module.LicenseName,
Modules: []Module{
module,
},
})
}
}
return result
}
var projectDataReaders = map[string]func(*Module, string) error{}
func RegisterProjectDataReader(language string, reader func(*Module, string) error) {
projectDataReaders[language] = reader
}
func ReadProjectData(module *Module, root string) error {
reader, ok := projectDataReaders[module.Lang]
if !ok {
return fmt.Errorf("lang %s is not supported", module.Lang)
}
return reader(module, root)
}