Skip to content

Commit

Permalink
buildable
Browse files Browse the repository at this point in the history
  • Loading branch information
Hideo Hattori committed Jan 16, 2018
1 parent 37108f0 commit 969da1d
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
^gocloc$
/bin
*.sw?
GTAGS
GPATH
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
.PHONY: test build

build:
go build -v ./
go build cmd/gocloc/main.go
mkdir -p bin
go build -o ./bin/gocloc cmd/gocloc/main.go

update-package:
go get -u github.com/hhatto/gocloc

test:
go test -v
112 changes: 59 additions & 53 deletions cmd/gocloc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
flags "github.com/jessevdk/go-flags"
)

const OutputTypeDefault string = "default"
const OutputTypeClocXml string = "cloc-xml"
const OutputTypeSloccount string = "sloccount"

const FILE_HEADER string = "File"
const LANG_HEADER string = "Language"
const COMMON_HEADER string = "files blank comment code"
Expand All @@ -22,6 +26,7 @@ var rowLen = 79

func main() {
var opts gocloc.Options
clocOpts := gocloc.NewClocOptions()
// parse command line options
parser := flags.NewParser(&opts, flags.Default)
parser.Name = "gocloc"
Expand All @@ -33,7 +38,7 @@ func main() {
}

if opts.ShowLang {
PrintDefinedLanguages()
gocloc.PrintDefinedLanguages()
return
}

Expand All @@ -43,40 +48,41 @@ func main() {
}

// setup option for exclude extensions
ExcludeExts = make(map[string]struct{})
for _, ext := range strings.Split(opts.ExcludeExt, ",") {
e, ok := Exts[ext]
e, ok := gocloc.Exts[ext]
if ok {
ExcludeExts[e] = struct{}{}
clocOpts.ExcludeExts[e] = struct{}{}
} else {
ExcludeExts[ext] = struct{}{}
clocOpts.ExcludeExts[ext] = struct{}{}
}
}

// setup option for not match directory
if opts.NotMatchDir != "" {
reNotMatchDir = regexp.MustCompile(opts.NotMatchDir)
clocOpts.ReNotMatchDir = regexp.MustCompile(opts.NotMatchDir)
}
if opts.MatchDir != "" {
reMatchDir = regexp.MustCompile(opts.MatchDir)
clocOpts.ReMatchDir = regexp.MustCompile(opts.MatchDir)
}

// value for language result
languages := GetDefinedLanguages()
languages := gocloc.GetDefinedLanguages()

// setup option for include languages
IncludeLangs = make(map[string]struct{})
for _, lang := range strings.Split(opts.IncludeLang, ",") {
if _, ok := languages[lang]; ok {
IncludeLangs[lang] = struct{}{}
clocOpts.IncludeLangs[lang] = struct{}{}
}
}

total := NewLanguage("TOTAL", []string{}, "", "")
num, maxPathLen := getAllFiles(paths, languages)
clocOpts.Debug = opts.Debug
clocOpts.SkipDuplicated = opts.SkipDuplicated

total := gocloc.NewLanguage("TOTAL", []string{}, "", "")
num, maxPathLen := gocloc.GetAllFiles(paths, languages, clocOpts)
headerLen := 28
header := LANG_HEADER
clocFiles := make(map[string]*ClocFile, num)
clocFiles := make(map[string]*gocloc.ClocFile, num)

// write header
if opts.Byfile {
Expand All @@ -91,53 +97,53 @@ func main() {
}

for _, language := range languages {
if language.printed {
if language.Printed {
continue
}

for _, file := range language.files {
cf := analyzeFile(file, language)
cf.Lang = language.name
for _, file := range language.Files {
cf := gocloc.AnalyzeFile(file, language, &clocOpts)
cf.Lang = language.Name

language.code += cf.Code
language.comments += cf.Comments
language.blanks += cf.Blanks
language.Code += cf.Code
language.Comments += cf.Comments
language.Blanks += cf.Blanks
clocFiles[file] = cf
}

files := int32(len(language.files))
if len(language.files) <= 0 {
files := int32(len(language.Files))
if len(language.Files) <= 0 {
continue
}

language.printed = true
language.Printed = true

total.total += files
total.blanks += language.blanks
total.comments += language.comments
total.code += language.code
total.Total += files
total.Blanks += language.Blanks
total.Comments += language.Comments
total.Code += language.Code
}

// write result
if opts.Byfile {
var sortedFiles ClocFiles
var sortedFiles gocloc.ClocFiles
for _, file := range clocFiles {
sortedFiles = append(sortedFiles, *file)
}
sort.Sort(sortedFiles)

switch opts.OutputType {
case OutputTypeClocXml:
t := XMLTotal{
Code: total.code,
Comment: total.comments,
Blank: total.blanks,
t := gocloc.XMLTotalFiles{
Code: total.Code,
Comment: total.Comments,
Blank: total.Blanks,
}
f := XMLResultFiles{
f := &gocloc.XMLResultFiles{
Files: sortedFiles,
Total: t,
}
xmlResult := XMLResult{
xmlResult := gocloc.XMLResult{
XMLFiles: f,
}
xmlResult.Encode()
Expand All @@ -161,45 +167,45 @@ func main() {
}
}
} else {
var sortedLanguages Languages
var sortedLanguages gocloc.Languages
for _, language := range languages {
if len(language.files) != 0 && language.printed {
if len(language.Files) != 0 && language.Printed {
sortedLanguages = append(sortedLanguages, *language)
}
}
sort.Sort(sortedLanguages)

switch opts.OutputType {
case OutputTypeClocXml:
var langs []ClocLanguage
var langs []gocloc.ClocLanguage
for _, language := range sortedLanguages {
c := ClocLanguage{
Name: language.name,
FilesCount: int32(len(language.files)),
Code: language.code,
Comments: language.comments,
Blanks: language.blanks,
c := gocloc.ClocLanguage{
Name: language.Name,
FilesCount: int32(len(language.Files)),
Code: language.Code,
Comments: language.Comments,
Blanks: language.Blanks,
}
langs = append(langs, c)
}
t := XMLTotalLanguages{
Code: total.code,
Comment: total.comments,
Blank: total.blanks,
SumFiles: total.total,
t := gocloc.XMLTotalLanguages{
Code: total.Code,
Comment: total.Comments,
Blank: total.Blanks,
SumFiles: total.Total,
}
f := &XMLResultLanguages{
f := &gocloc.XMLResultLanguages{
Languages: langs,
Total: t,
}
xmlResult := XMLResult{
xmlResult := gocloc.XMLResult{
XMLLanguages: f,
}
xmlResult.Encode()
default:
for _, language := range sortedLanguages {
fmt.Printf("%-27v %6v %14v %14v %14v\n",
language.name, len(language.files), language.blanks, language.comments, language.code)
language.Name, len(language.Files), language.Blanks, language.Comments, language.Code)
}
}
}
Expand All @@ -209,10 +215,10 @@ func main() {
fmt.Printf("%.[2]*[1]s\n", ROW, rowLen)
if opts.Byfile {
fmt.Printf("%-[1]*[2]v %6[3]v %14[4]v %14[5]v %14[6]v\n",
maxPathLen, "TOTAL", total.total, total.blanks, total.comments, total.code)
maxPathLen, "TOTAL", total.Total, total.Blanks, total.Comments, total.Code)
} else {
fmt.Printf("%-27v %6v %14v %14v %14v\n",
"TOTAL", total.total, total.blanks, total.comments, total.code)
"TOTAL", total.Total, total.Blanks, total.Comments, total.Code)
}
fmt.Printf("%.[2]*[1]s\n", ROW, rowLen)
}
Expand Down
2 changes: 1 addition & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (cf ClocFiles) Less(i, j int) bool {
return cf[i].Code > cf[j].Code
}

func analyzeFile(filename string, language *Language, opts Options) *ClocFile {
func AnalyzeFile(filename string, language *Language, opts *ClocOptions) *ClocFile {
if opts.Debug {
fmt.Printf("filename=%v\n", filename)
}
Expand Down
15 changes: 10 additions & 5 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class A:
`))

language := NewLanguage("Python", []string{"#"}, "\"\"\"", "\"\"\"")
clocFile := analyzeFile(tmpfile.Name(), language)
clocOpts := NewClocOptions()
clocFile := AnalyzeFile(tmpfile.Name(), language, clocOpts)
tmpfile.Close()

if clocFile.Blanks != 1 {
Expand Down Expand Up @@ -57,7 +58,8 @@ class A:
`))

language := NewLanguage("Python", []string{"#"}, "\"\"\"", "\"\"\"")
clocFile := analyzeFile(tmpfile.Name(), language)
clocOpts := NewClocOptions()
clocFile := AnalyzeFile(tmpfile.Name(), language, clocOpts)
tmpfile.Close()

if clocFile.Blanks != 1 {
Expand Down Expand Up @@ -90,7 +92,8 @@ func main() {
`))

language := NewLanguage("Go", []string{"//"}, "/*", "*/")
clocFile := analyzeFile(tmpfile.Name(), language)
clocOpts := NewClocOptions()
clocFile := AnalyzeFile(tmpfile.Name(), language, clocOpts)
tmpfile.Close()

if clocFile.Blanks != 1 {
Expand Down Expand Up @@ -124,7 +127,8 @@ func main() {
`))

language := NewLanguage("Go", []string{"//"}, "/*", "*/")
clocFile := analyzeFile(tmpfile.Name(), language)
clocOpts := NewClocOptions()
clocFile := AnalyzeFile(tmpfile.Name(), language, clocOpts)
tmpfile.Close()

if clocFile.Blanks != 1 {
Expand Down Expand Up @@ -156,7 +160,8 @@ func main() {
`))

language := NewLanguage("Go", []string{"//"}, "/*", "*/")
clocFile := analyzeFile(tmpfile.Name(), language)
clocOpts := NewClocOptions()
clocFile := AnalyzeFile(tmpfile.Name(), language, clocOpts)
tmpfile.Close()

if clocFile.Blanks != 1 {
Expand Down
28 changes: 14 additions & 14 deletions language.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ type ClocLanguage struct {
}

type Language struct {
name string
Name string
lineComments []string
multiLine string
multiLineEnd string
files []string
code int32
comments int32
blanks int32
total int32
printed bool
Files []string
Code int32
Comments int32
Blanks int32
Total int32
Printed bool
}
type Languages []Language

Expand All @@ -45,10 +45,10 @@ func (ls Languages) Swap(i, j int) {
ls[i], ls[j] = ls[j], ls[i]
}
func (ls Languages) Less(i, j int) bool {
if ls[i].code == ls[j].code {
return ls[i].name < ls[j].name
if ls[i].Code == ls[j].Code {
return ls[i].Name < ls[j].Name
}
return ls[i].code > ls[j].code
return ls[i].Code > ls[j].Code
}

var reShebangEnv = regexp.MustCompile("^#! *(\\S+/env) ([a-zA-Z]+)")
Expand Down Expand Up @@ -293,7 +293,7 @@ func getFileTypeByShebang(path string) (shebangLang string, ok bool) {
return
}

func getFileType(path string, opts Options) (ext string, ok bool) {
func getFileType(path string, opts ClocOptions) (ext string, ok bool) {
ext = filepath.Ext(path)
base := filepath.Base(path)

Expand Down Expand Up @@ -345,11 +345,11 @@ func getFileType(path string, opts Options) (ext string, ok bool) {

func NewLanguage(name string, lineComments []string, multiLine, multiLineEnd string) *Language {
return &Language{
name: name,
Name: name,
lineComments: lineComments,
multiLine: multiLine,
multiLineEnd: multiLineEnd,
files: []string{},
Files: []string{},
}
}

Expand All @@ -376,7 +376,7 @@ func lang2exts(lang string) (exts string) {
func PrintDefinedLanguages() {
printLangs := []string{}
for _, lang := range GetDefinedLanguages() {
printLangs = append(printLangs, lang.name)
printLangs = append(printLangs, lang.Name)
}
sort.Strings(printLangs)
for _, lang := range printLangs {
Expand Down
Loading

0 comments on commit 969da1d

Please sign in to comment.