2016-07-13 17:05:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-15 11:18:11 +00:00
|
|
|
"bytes"
|
2016-07-13 17:05:23 +00:00
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-04-18 11:02:46 +00:00
|
|
|
"log"
|
2016-07-13 17:05:23 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-10 10:00:47 +00:00
|
|
|
"strings"
|
2016-07-13 17:05:23 +00:00
|
|
|
|
2017-06-08 07:27:27 +00:00
|
|
|
"gopkg.in/src-d/enry.v1"
|
2016-07-13 17:05:23 +00:00
|
|
|
)
|
|
|
|
|
2017-07-07 09:20:54 +00:00
|
|
|
var (
|
|
|
|
Version = "undefined"
|
|
|
|
GitHash = "undefined"
|
|
|
|
)
|
|
|
|
|
2016-07-13 17:05:23 +00:00
|
|
|
func main() {
|
2017-04-18 11:02:46 +00:00
|
|
|
flag.Usage = usage
|
2017-06-15 11:18:11 +00:00
|
|
|
breakdownFlag := flag.Bool("breakdown", false, "")
|
|
|
|
jsonFlag := flag.Bool("json", false, "")
|
2016-07-13 17:05:23 +00:00
|
|
|
flag.Parse()
|
2017-06-15 11:18:11 +00:00
|
|
|
|
2016-07-13 22:38:06 +00:00
|
|
|
root, err := filepath.Abs(flag.Arg(0))
|
2017-04-18 11:02:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2016-07-13 22:38:06 +00:00
|
|
|
}
|
2016-07-13 17:05:23 +00:00
|
|
|
|
2017-04-18 11:02:46 +00:00
|
|
|
errors := false
|
2017-06-02 10:41:39 +00:00
|
|
|
out := make(map[string][]string, 0)
|
2016-07-13 22:38:06 +00:00
|
|
|
err = filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2017-04-18 11:02:46 +00:00
|
|
|
errors = true
|
|
|
|
log.Println(err)
|
|
|
|
return filepath.SkipDir
|
2016-07-13 22:38:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:41:39 +00:00
|
|
|
relativePath, err := filepath.Rel(root, path)
|
|
|
|
if err != nil {
|
|
|
|
errors = true
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if relativePath == "." {
|
2017-07-11 10:27:48 +00:00
|
|
|
fmt.Print(printFileAnalysis(root))
|
|
|
|
return nil
|
2017-06-02 10:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.IsDir() {
|
|
|
|
relativePath = relativePath + "/"
|
|
|
|
}
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
if enry.IsVendor(relativePath) || enry.IsDotFile(relativePath) ||
|
|
|
|
enry.IsDocumentation(relativePath) || enry.IsConfiguration(relativePath) {
|
2016-07-13 17:05:23 +00:00
|
|
|
if f.IsDir() {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-06 09:59:15 +00:00
|
|
|
language, ok := enry.GetLanguageByExtension(path)
|
|
|
|
if !ok {
|
|
|
|
if language, ok = enry.GetLanguageByFilename(path); !ok {
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
errors = true
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
language = enry.GetLanguage(filepath.Base(path), content)
|
|
|
|
if language == enry.OtherLanguage {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-07-13 17:05:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:41:39 +00:00
|
|
|
out[language] = append(out[language], relativePath)
|
2016-07-13 17:05:23 +00:00
|
|
|
return nil
|
|
|
|
})
|
2017-07-11 10:27:48 +00:00
|
|
|
|
2017-04-18 11:02:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-07-13 22:38:06 +00:00
|
|
|
|
2017-06-15 11:18:11 +00:00
|
|
|
var buff bytes.Buffer
|
|
|
|
switch {
|
|
|
|
case *jsonFlag && !*breakdownFlag:
|
|
|
|
printJson(out, &buff)
|
|
|
|
case *jsonFlag && *breakdownFlag:
|
|
|
|
printBreakDown(out, &buff)
|
|
|
|
case *breakdownFlag:
|
|
|
|
printPercents(out, &buff)
|
|
|
|
buff.WriteByte('\n')
|
|
|
|
printBreakDown(out, &buff)
|
|
|
|
default:
|
|
|
|
printPercents(out, &buff)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Print(buff.String())
|
2016-07-13 17:05:23 +00:00
|
|
|
}
|
2016-07-13 22:38:06 +00:00
|
|
|
|
|
|
|
func usage() {
|
|
|
|
fmt.Fprintf(
|
2017-07-07 09:20:54 +00:00
|
|
|
os.Stderr,
|
|
|
|
` %[1]s %[2]s commit: %[3]s
|
|
|
|
enry, A simple (and faster) implementation of github/linguist
|
|
|
|
usage: %[1]s <path>
|
|
|
|
%[1]s [-json] [-breakdown] <path>
|
|
|
|
%[1]s [-json] [-breakdown]
|
|
|
|
`,
|
|
|
|
os.Args[0], Version, GitHash,
|
2016-07-13 22:38:06 +00:00
|
|
|
)
|
2017-06-15 11:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func printBreakDown(out map[string][]string, buff *bytes.Buffer) {
|
|
|
|
for name, language := range out {
|
|
|
|
writeStringLn(name, buff)
|
|
|
|
for _, file := range language {
|
|
|
|
writeStringLn(file, buff)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeStringLn("", buff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printJson(out map[string][]string, buff *bytes.Buffer) {
|
|
|
|
data, _ := json.Marshal(out)
|
|
|
|
buff.Write(data)
|
2017-07-05 10:12:23 +00:00
|
|
|
buff.WriteByte('\n')
|
2017-06-15 11:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func printPercents(out map[string][]string, buff *bytes.Buffer) {
|
|
|
|
fileCount := make(map[string]int, len(out))
|
|
|
|
total := 0
|
|
|
|
for name, language := range out {
|
|
|
|
fileCount[name] = len(language)
|
|
|
|
total += len(language)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, count := range fileCount {
|
|
|
|
percent := float32(count) / float32(total) * 100
|
|
|
|
buff.WriteString(fmt.Sprintf("%.2f%% %s\n", percent, name))
|
|
|
|
}
|
|
|
|
}
|
2016-07-13 22:38:06 +00:00
|
|
|
|
2017-07-11 09:13:49 +00:00
|
|
|
func printFileAnalysis(file string) string {
|
2017-07-10 10:00:47 +00:00
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2017-07-11 10:27:48 +00:00
|
|
|
|
|
|
|
totalLines, nonBlank := getLines(file, string(content))
|
2017-07-10 10:00:47 +00:00
|
|
|
fileType := getFileType(file, content)
|
|
|
|
language := enry.GetLanguage(file, content)
|
2017-07-11 10:27:48 +00:00
|
|
|
mimeType := enry.GetMimeType(file, language)
|
2017-07-10 10:00:47 +00:00
|
|
|
|
2017-07-11 09:13:49 +00:00
|
|
|
return fmt.Sprintf(
|
|
|
|
`%s: %d lines (%d sloc)
|
2017-07-11 10:27:48 +00:00
|
|
|
type: %s
|
|
|
|
mime_type: %s
|
|
|
|
language: %s
|
|
|
|
`,
|
|
|
|
filepath.Base(file), totalLines, nonBlank, fileType, mimeType, language,
|
|
|
|
)
|
2017-07-10 10:00:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 09:13:49 +00:00
|
|
|
func getLines(file string, content string) (int, int) {
|
|
|
|
totalLines := strings.Count(content, "\n")
|
2017-07-11 10:27:48 +00:00
|
|
|
nonBlank := totalLines - strings.Count(content, "\n\n")
|
|
|
|
return totalLines, nonBlank
|
2017-07-10 10:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getFileType(file string, content []byte) string {
|
|
|
|
switch {
|
2017-07-11 09:13:49 +00:00
|
|
|
case enry.IsImage(file):
|
2017-07-10 10:00:47 +00:00
|
|
|
return "Image"
|
|
|
|
case enry.IsBinary(content):
|
|
|
|
return "Binary"
|
|
|
|
default:
|
|
|
|
return "Text"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:18:11 +00:00
|
|
|
func writeStringLn(s string, buff *bytes.Buffer) {
|
|
|
|
buff.WriteString(s)
|
|
|
|
buff.WriteByte('\n')
|
2016-07-13 22:38:06 +00:00
|
|
|
}
|