Created
January 1, 2018 14:44
-
-
Save willnode/73e1db980176fb0b985ad1702946ea17 to your computer and use it in GitHub Desktop.
Tool for measuring code metrics in given directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* This is a Go Application for measuring code metrics | |
* File selection can be included via arguments as extension, or: | |
* append '+' for exclusion as filename | |
* append '-' for exclusion as filename | |
* append '!' for exclusion as extension | |
* | |
* Compile => go build -o CodeMetrics.exe CodeMetrics.go | |
* Launch => CodeMetrics js !min.js | |
* License => MIT (c) 2018 Wildan Mubarok | |
* | |
*/ | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"path/filepath" | |
"regexp" | |
"strings" | |
) | |
func main() { | |
path, err := os.Getwd() | |
if err != nil { | |
panic(err) | |
} | |
a := os.Args[1:] | |
b := make([]string, 0) | |
c := []string{"/\\."} | |
for _, v := range a { | |
s := strings.Replace(strings.Replace(v, ".", "\\.", -1), "*", ".*", -1) | |
if v[0] == '-' { | |
c = append(c, s[1:]) | |
} else if v[0] == '!' { | |
c = append(c, "."+s[1:]+"$") | |
} else if v[0] == '+' { | |
b = append(b, "."+s[1:]) | |
} else { | |
b = append(b, "."+s+"$") | |
} | |
} | |
// join extensions from separate arguments | |
execute(path, strings.Join(b, "|"), strings.Join(c, "|")) | |
} | |
func execute(pathS string, incl string, excl string) { | |
var bytes, files, chars, lines, nonwhitelines int64 | |
regexincl, err := regexp.Compile("(" + incl + ")") | |
if err != nil { | |
panic(err) | |
} | |
regexexcl, err := regexp.Compile("(" + excl + ")") | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("Scanning ", pathS) | |
filepath.Walk(pathS, func(path string, f os.FileInfo, _ error) error { | |
if !f.IsDir() { | |
name := f.Name() | |
r := regexincl.MatchString(name) && !regexexcl.MatchString(name) | |
if r && !f.IsDir() { | |
file, err := os.Open(path) | |
if err == nil { | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
txt := scanner.Text() | |
trimmed := strings.Trim(txt, " \t\n\r") | |
bytes += int64(len(txt)) | |
chars += int64(len(trimmed)) | |
lines++ | |
if len(trimmed) > 0 { | |
nonwhitelines++ | |
} | |
} | |
fmt.Println("Scanned ", name) | |
files++ | |
} | |
defer file.Close() | |
} | |
} | |
return nil | |
}) | |
fmt.Println() | |
fmt.Println("Total files: ", files) | |
fmt.Println("Total bytes: ", bytes) | |
fmt.Println("Total non-blank characters: ", chars) | |
fmt.Println("Total lines: ", lines) | |
fmt.Println("Total nonblank lines: ", nonwhitelines) | |
fmt.Println("Done. Press enter to quit.") | |
bufio.NewReader(os.Stdin).ReadByte() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment