forked from sourcegraph/srcscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiles.go
121 lines (105 loc) · 2.68 KB
/
profiles.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
package srcscan
import (
"os"
"strings"
)
// Profile represents criteria for a source unit and instructions on how to create it. For example,
// a simple profile might represent the following: "if there is a directory with a package.json
// file, designate the directory as a node.js package and include all *.js files in its
// subdirectories."
type Profile struct {
// Name describes the source unit matched by this Profile.
Name string
Dir DirMatcher
File FileMatcher
TopLevelOnly bool
Unit func(abspath, relpath string, config Config, info os.FileInfo) Unit
}
func (p Profile) DirMatches(path string, filenames []string) bool {
if p.Dir.DirMatches(path, filenames) {
return true
}
return false
}
type FileMatcher interface {
FileMatches(path string) bool
}
type DirMatcher interface {
DirMatches(path string, filenames []string) bool
}
// FileInDir matches directories containing a file with the specified name.
type FileInDir struct{ Filename string }
func (c FileInDir) DirMatches(path string, filenames []string) bool {
for _, f := range filenames {
if f == c.Filename {
return true
}
}
return false
}
// FileSuffixInDir matches directories containing a file with the specified filename suffix.
type FileSuffixInDir struct{ Suffix string }
func (c FileSuffixInDir) DirMatches(path string, filenames []string) bool {
for _, f := range filenames {
if strings.HasSuffix(f, c.Suffix) {
return true
}
}
return false
}
type FileHasSuffix struct{ Suffix string }
func (c FileHasSuffix) FileMatches(path string) bool {
return strings.HasSuffix(path, c.Suffix)
}
var AllProfiles = []Profile{
Profile{
Name: "NPM package",
Dir: FileInDir{"package.json"},
Unit: readNPMPackage,
},
Profile{
Name: "Bower component",
Dir: FileInDir{"bower.json"},
Unit: readBowerComponent,
},
Profile{
Name: "Python package and module",
TopLevelOnly: true,
Dir: FileInDir{"__init__.py"},
File: FileHasSuffix{".py"},
Unit: func(abspath, relpath string, config Config, info os.FileInfo) Unit {
if info.IsDir() {
return &PythonPackage{relpath}
} else {
return &PythonModule{relpath}
}
},
},
Profile{
Name: "Go package",
Dir: FileSuffixInDir{".go"},
Unit: readGoPackage,
},
Profile{
Name: "Java Maven project",
Dir: FileInDir{"pom.xml"},
Unit: readJavaMavenProject,
},
Profile{
Name: "Ruby Gem",
Dir: FileSuffixInDir{".gemspec"},
Unit: readRubyGem,
},
Profile{
Name: "Ruby app",
Dir: FileInDir{"config.ru"},
Unit: readRubyApp,
},
Profile{
Name: "Ruby file",
File: FileHasSuffix{".rb"},
Unit: func(abspath, relpath string, config Config, info os.FileInfo) Unit {
return &RubyFile{relpath}
},
},
}