Selaa lähdekoodia

simple project type detection tool.

Lucas Stadler 11 vuotta sitten
vanhempi
commit
44ed3740f1
3 muutettua tiedostoa jossa 132 lisäystä ja 0 poistoa
  1. 1 0
      go/.gitignore
  2. 95 0
      go/detect.go
  3. 36 0
      go/fileutil/fileutil.go

+ 1 - 0
go/.gitignore

1
.go
1
.go
2
2
3
detect
3
qst
4
qst
4
5
5
examples/hello
6
examples/hello

+ 95 - 0
go/detect.go

1
package main
2
3
import (
4
	"fmt"
5
	"os"
6
	"path"
7
8
	"./fileutil"
9
)
10
11
/*
12
	detect - guess the project type from files present
13
*/
14
15
type Detection struct {
16
	Language string
17
	Type     string
18
}
19
20
type detectorFunc func(string) bool
21
22
type Detector struct {
23
	Detection Detection
24
	Detector  detectorFunc
25
}
26
27
var Detectors = []Detector{
28
	{Detection{"clojure", "leiningen"}, clojureLeiningen},
29
	{Detection{"go", "default"}, goDefault},
30
	{Detection{"java", "maven"}, javaMaven},
31
	{Detection{"make", "default"}, makeDefault},
32
	{Detection{"python", "default"}, pythonDefault},
33
	{Detection{"ruby", "rails"}, rubyRails},
34
	{Detection{"ruby", "rake"}, rubyRake},
35
	{Detection{"ruby", "default"}, rubyDefault},
36
}
37
38
func main() {
39
	if len(os.Args) < 2 {
40
		fmt.Printf("Usage: %s <file>\n", os.Args[0])
41
		os.Exit(1)
42
	}
43
44
	file := os.Args[1]
45
46
	for _, detector := range Detectors {
47
		fmt.Println(detector.Detection, detector.Detector(file))
48
	}
49
}
50
51
func matchingFileOrDir(file string, pattern string) bool {
52
	if fileutil.IsFile(file) {
53
		_, f := path.Split(file)
54
		isMatch, _ := path.Match(pattern, f)
55
		return isMatch
56
	} else {
57
		return fileutil.MatchExists(path.Join(path.Dir(file), pattern))
58
	}
59
}
60
61
func hasFile(fileOrDir string, file string) bool {
62
	return fileutil.IsFile(fileutil.Join(fileOrDir, file))
63
}
64
65
func clojureLeiningen(file string) bool {
66
	return hasFile(file, "project.clj")
67
}
68
69
func goDefault(file string) bool {
70
	return matchingFileOrDir(file, "*.go")
71
}
72
73
func javaMaven(file string) bool {
74
	return hasFile(file, "pom.xml")
75
}
76
77
func makeDefault(file string) bool {
78
	return hasFile(file, "Makefile")
79
}
80
81
func pythonDefault(file string) bool {
82
	return matchingFileOrDir(file, "*.py")
83
}
84
85
func rubyRails(file string) bool {
86
	return hasFile(file, "bin/rails")
87
}
88
89
func rubyRake(file string) bool {
90
	return hasFile(file, "Rakefile")
91
}
92
93
func rubyDefault(file string) bool {
94
	return matchingFileOrDir(file, "*.rb")
95
}

+ 36 - 0
go/fileutil/fileutil.go

1
package fileutil
2
3
import (
4
	"os"
5
	"path"
6
	"path/filepath"
7
)
8
9
func IsFile(path string) bool {
10
	info, err := os.Stat(path)
11
	if err != nil {
12
		return false
13
	}
14
	return !info.IsDir()
15
}
16
17
func IsDir(path string) bool {
18
	info, err := os.Stat(path)
19
	if err != nil {
20
		return false
21
	}
22
	return info.IsDir()
23
}
24
25
func MatchExists(glob string) bool {
26
	matches, _ := filepath.Glob(glob)
27
	return len(matches) > 0
28
}
29
30
func Join(fileOrDir string, elem ...string) string {
31
	dir := fileOrDir
32
	if IsFile(fileOrDir) {
33
		dir = path.Dir(fileOrDir)
34
	}
35
	return path.Join(dir, path.Join(elem...))
36
}