2016-07-13 17:05:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-02-23 15:06:05 +00:00
|
|
|
"bufio"
|
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-09-30 13:11:48 +00:00
|
|
|
"sort"
|
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"
|
2017-07-18 07:58:44 +00:00
|
|
|
"gopkg.in/src-d/enry.v1/data"
|
2016-07-13 17:05:23 +00:00
|
|
|
)
|
|
|
|
|
2017-07-07 09:20:54 +00:00
|
|
|
var (
|
2017-07-11 14:32:01 +00:00
|
|
|
version = "undefined"
|
|
|
|
build = "undefined"
|
|
|
|
commit = "undefined"
|
2017-07-07 09:20:54 +00:00
|
|
|
)
|
|
|
|
|
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, "")
|
2017-09-30 13:48:00 +00:00
|
|
|
showVersion := flag.Bool("version", false, "Show the enry version information")
|
2018-02-23 15:06:05 +00:00
|
|
|
onlyProg := flag.Bool("prog", false, "Only show programming file types in output")
|
|
|
|
countMode := flag.String("mode", "file", "the method used to count file size. Available options are: file, line and byte")
|
|
|
|
|
2016-07-13 17:05:23 +00:00
|
|
|
flag.Parse()
|
2017-06-15 11:18:11 +00:00
|
|
|
|
2017-09-30 13:48:00 +00:00
|
|
|
if *showVersion {
|
|
|
|
fmt.Println(version)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-07-21 11:38:19 +00:00
|
|
|
fileInfo, err := os.Stat(root)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileInfo.Mode().IsRegular() {
|
2018-02-23 15:06:05 +00:00
|
|
|
err = printFileAnalysis(root)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2017-07-21 11:38:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
log.Println(err)
|
|
|
|
return filepath.SkipDir
|
2016-07-13 22:38:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 09:54:48 +00:00
|
|
|
if !f.Mode().IsDir() && !f.Mode().IsRegular() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:41:39 +00:00
|
|
|
relativePath, err := filepath.Rel(root, path)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if relativePath == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-23 15:06:05 +00:00
|
|
|
// If we are displaying only prog. and language is not prog. skip it.
|
|
|
|
if *onlyProg && enry.GetLanguageType(language) != enry.Programming {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-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:
|
2018-02-23 15:06:05 +00:00
|
|
|
printPercents(out, &buff, *countMode)
|
2017-06-15 11:18:11 +00:00
|
|
|
buff.WriteByte('\n')
|
|
|
|
printBreakDown(out, &buff)
|
|
|
|
default:
|
2018-02-23 15:06:05 +00:00
|
|
|
printPercents(out, &buff, *countMode)
|
2017-06-15 11:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2017-07-18 07:58:44 +00:00
|
|
|
` %[1]s %[2]s build: %[3]s commit: %[4]s, based on linguist commit: %[5]s
|
2017-07-11 14:32:01 +00:00
|
|
|
%[1]s, A simple (and faster) implementation of github/linguist
|
2018-02-23 15:06:05 +00:00
|
|
|
usage: %[1]s [-mode=(file|line|byte)] [-prog] <path>
|
|
|
|
%[1]s [-mode=(file|line|byte)] [-prog] [-json] [-breakdown] <path>
|
|
|
|
%[1]s [-mode=(file|line|byte)] [-prog] [-json] [-breakdown]
|
2017-09-30 13:48:00 +00:00
|
|
|
%[1]s [-version]
|
2017-07-07 09:20:54 +00:00
|
|
|
`,
|
2017-07-18 07:58:44 +00:00
|
|
|
os.Args[0], version, build, commit, data.LinguistCommit[:7],
|
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
|
|
|
}
|
|
|
|
|
2018-02-23 15:06:05 +00:00
|
|
|
// filelistError represents a failed operation that took place across multiple files.
|
|
|
|
type filelistError []string
|
|
|
|
|
|
|
|
func (e filelistError) Error() string {
|
|
|
|
return fmt.Sprintf("Could not process the following files:\n%s", strings.Join(e, "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func printPercents(fSummary map[string][]string, buff *bytes.Buffer, mode string) {
|
|
|
|
// Select the way we quantify 'amount' of code.
|
|
|
|
var reducer func([]string) (float64, filelistError)
|
|
|
|
switch mode {
|
|
|
|
case "file":
|
|
|
|
reducer = fileCountValues
|
|
|
|
case "line":
|
|
|
|
reducer = lineCountValues
|
|
|
|
case "byte":
|
|
|
|
reducer = byteCountValues
|
|
|
|
default:
|
|
|
|
reducer = fileCountValues
|
2017-06-15 11:18:11 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 15:06:05 +00:00
|
|
|
// Reduce the list of files to a quantity of file type.
|
|
|
|
var total float64
|
|
|
|
fileValues := make(map[string]float64)
|
|
|
|
keys := []string{}
|
|
|
|
var unreadableFiles filelistError
|
|
|
|
for fType, files := range fSummary {
|
|
|
|
val, err := reducer(files)
|
|
|
|
if err != nil {
|
|
|
|
unreadableFiles = append(unreadableFiles, err...)
|
|
|
|
}
|
|
|
|
fileValues[fType] = val
|
|
|
|
keys = append(keys, fType)
|
|
|
|
total += val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slice the keys by their quantity (file count, line count, byte size, etc.).
|
|
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
|
|
return fileValues[keys[i]] > fileValues[keys[j]]
|
|
|
|
})
|
|
|
|
|
|
|
|
// Calculate and write percentages of each file type.
|
|
|
|
for _, fType := range keys {
|
|
|
|
val := fileValues[fType]
|
|
|
|
percent := val / total * 100.0
|
|
|
|
buff.WriteString(fmt.Sprintf("%.2f%%\t%s\n", percent, fType))
|
|
|
|
if unreadableFiles != nil {
|
|
|
|
buff.WriteString(fmt.Sprintf("\n%s", unreadableFiles.Error()))
|
|
|
|
}
|
2017-06-15 11:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 22:38:06 +00:00
|
|
|
|
2018-02-23 15:06:05 +00:00
|
|
|
func fileCountValues(files []string) (float64, filelistError) {
|
|
|
|
return float64(len(files)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lineCountValues(files []string) (float64, filelistError) {
|
|
|
|
var filesErr filelistError
|
|
|
|
var t float64
|
|
|
|
for _, fName := range files {
|
|
|
|
content, err := ioutil.ReadFile(fName)
|
|
|
|
if err != nil {
|
|
|
|
filesErr = append(filesErr, fName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l, _ := getLines(content)
|
|
|
|
t += float64(l)
|
|
|
|
}
|
|
|
|
return t, filesErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func byteCountValues(files []string) (float64, filelistError) {
|
|
|
|
var filesErr filelistError
|
|
|
|
var t float64
|
|
|
|
for _, fName := range files {
|
|
|
|
f, err := os.Open(fName)
|
|
|
|
if err != nil {
|
|
|
|
filesErr = append(filesErr, fName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fi, err := f.Stat()
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
filesErr = append(filesErr, fName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t += float64(fi.Size())
|
|
|
|
}
|
|
|
|
return t, filesErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func printFileAnalysis(fName string) error {
|
|
|
|
content, err := ioutil.ReadFile(fName)
|
2017-07-10 10:00:47 +00:00
|
|
|
if err != nil {
|
2018-02-23 15:06:05 +00:00
|
|
|
return err
|
2017-07-10 10:00:47 +00:00
|
|
|
}
|
2017-07-11 10:27:48 +00:00
|
|
|
|
2018-02-23 15:06:05 +00:00
|
|
|
totalLines, nonBlank := getLines(content)
|
|
|
|
fileType := getFileType(fName, content)
|
|
|
|
language := enry.GetLanguage(fName, content)
|
|
|
|
mimeType := enry.GetMimeType(fName, language)
|
2017-07-10 10:00:47 +00:00
|
|
|
|
2017-07-21 11:38:19 +00:00
|
|
|
fmt.Printf(
|
2017-07-11 09:13:49 +00:00
|
|
|
`%s: %d lines (%d sloc)
|
2017-07-11 10:27:48 +00:00
|
|
|
type: %s
|
|
|
|
mime_type: %s
|
|
|
|
language: %s
|
|
|
|
`,
|
2018-02-23 15:06:05 +00:00
|
|
|
filepath.Base(fName), totalLines, nonBlank, fileType, mimeType, language,
|
2017-07-11 10:27:48 +00:00
|
|
|
)
|
2018-02-23 15:06:05 +00:00
|
|
|
return nil
|
2017-07-10 10:00:47 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 15:06:05 +00:00
|
|
|
func getLines(b []byte) (total int, nonBlank int) {
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(b))
|
|
|
|
lineCt := 0
|
|
|
|
blankCt := 0
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
lineCt++
|
|
|
|
line := bytes.TrimSpace(scanner.Bytes())
|
|
|
|
if len(line) == 0 {
|
|
|
|
blankCt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Scanner doesn't catch the case of last byte newline.
|
|
|
|
if len(b) > 0 && b[len(b)-1] == '\n' {
|
|
|
|
lineCt++
|
|
|
|
blankCt++
|
|
|
|
}
|
|
|
|
|
|
|
|
return lineCt, lineCt - blankCt
|
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
|
|
|
}
|