Skip to content

Commit

Permalink
feat: fork gocloc and integrate token counting
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaohui Wang authored and wangyaohui committed Oct 4, 2023
1 parent 2cf4d60 commit 636a8ce
Show file tree
Hide file tree
Showing 25 changed files with 103 additions and 133 deletions.
62 changes: 0 additions & 62 deletions .github/workflows/docker-image-push.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ jobs:
- name: Lint Dockerfile
run: docker run --rm -i hadolint/hadolint < Dockerfile
- name: Build Docker Container
run: docker build -t gocloc .
run: docker build -t ctoc .
- name: Test Container
run: docker run -v ${PWD}:/workdir gocloc .
run: docker run -v ${PWD}:/workdir ctoc .
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: go mod download

- name: Build
run: go build -o ./bin/gocloc cmd/gocloc/main.go
run: go build -o ./bin/ctoc cmd/ctoc/main.go

- name: Check imports
shell: bash
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ GRTAGS

Gopkg.lock
vendor/
bin/gocloc
dist/
coverage.out

.idea/
6 changes: 3 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
project_name: gocloc
project_name: ctoc
env:
- GO111MODULE=on
before:
hooks:
- go mod tidy
builds:
- main: 'cmd/gocloc/main.go'
binary: gocloc
- main: 'cmd/ctoc/main.go'
binary: ctoc
ldflags:
- -s -w
- -X main.Version={{.Version}}
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ COPY . .

RUN GO111MODULE=on CGO_ENABLED=0 go build \
-ldflags='-w -s -extldflags "-static"' \
-o ./bin/gocloc cmd/gocloc/main.go \
&& upx-ucl --best --ultra-brute ./bin/gocloc
-o ./bin/ctoc cmd/ctoc/main.go \
&& upx-ucl --best --ultra-brute ./bin/ctoc

FROM scratch
COPY --from=builder /build/bin/gocloc /bin/
COPY --from=builder /build/bin/ctoc /bin/
WORKDIR /workdir
ENTRYPOINT ["/bin/gocloc"]
ENTRYPOINT ["/bin/ctoc"]
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Copyright (C) 2016- gocloc authors
Copyright (C) 2023- ctoc authors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

build:
mkdir -p bin
GO111MODULE=on go build -o ./bin/gocloc cmd/gocloc/main.go
GO111MODULE=on go build -o ./bin/ctoc cmd/ctoc/main.go

update-package:
GO111MODULE=on go get -u github.com/hhatto/gocloc
GO111MODULE=on go get -u github.com/yaohui-wyh/ctoc

cleanup-package:
GO111MODULE=on go mod tidy
Expand Down
2 changes: 1 addition & 1 deletion bspool.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gocloc
package ctoc

import (
"bytes"
Expand Down
56 changes: 28 additions & 28 deletions cmd/gocloc/main.go → cmd/ctoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"regexp"
"strings"

"github.com/hhatto/gocloc"
"github.com/jessevdk/go-flags"
"github.com/yaohui-wyh/ctoc"
)

// Version is version string for gocloc command
Expand All @@ -31,12 +31,12 @@ const OutputTypeJSON string = "json"

const fileHeader string = "File"
const languageHeader string = "Language"
const commonHeader string = "files blank comment code"
const commonHeader string = "files blank comment code tokens"
const defaultOutputSeparator string = "-------------------------------------------------------------------------" +
"-------------------------------------------------------------------------" +
"-------------------------------------------------------------------------"

var rowLen = 79
var rowLen = 96

// CmdOptions is gocloc command options.
// It is necessary to use notation that follows go-flags.
Expand All @@ -58,10 +58,10 @@ type CmdOptions struct {

type outputBuilder struct {
opts *CmdOptions
result *gocloc.Result
result *ctoc.Result
}

func newOutputBuilder(result *gocloc.Result, opts *CmdOptions) *outputBuilder {
func newOutputBuilder(result *ctoc.Result, opts *CmdOptions) *outputBuilder {
return &outputBuilder{
opts,
result,
Expand Down Expand Up @@ -92,22 +92,22 @@ func (o *outputBuilder) WriteFooter() {
if o.opts.OutputType == OutputTypeDefault {
fmt.Printf("%.[2]*[1]s\n", defaultOutputSeparator, rowLen)
if o.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)
fmt.Printf("%-[1]*[2]v %6[3]v %14[4]v %14[5]v %14[6]v %14[7]v\n",
maxPathLen, "TOTAL", total.Total, total.Blanks, total.Comments, total.Code, total.Tokens)
} else {
fmt.Printf("%-27v %6v %14v %14v %14v\n",
"TOTAL", total.Total, total.Blanks, total.Comments, total.Code)
fmt.Printf("%-27v %6v %14v %14v %14v %14v\n",
"TOTAL", total.Total, total.Blanks, total.Comments, total.Code, total.Tokens)
}
fmt.Printf("%.[2]*[1]s\n", defaultOutputSeparator, rowLen)
}
}

func writeResultWithByFile(opts *CmdOptions, result *gocloc.Result) {
func writeResultWithByFile(opts *CmdOptions, result *ctoc.Result) {
clocFiles := result.Files
total := result.Total
maxPathLen := result.MaxPathLength

var sortedFiles gocloc.ClocFiles
var sortedFiles ctoc.ClocFiles
for _, file := range clocFiles {
sortedFiles = append(sortedFiles, *file)
}
Expand All @@ -124,16 +124,16 @@ func writeResultWithByFile(opts *CmdOptions, result *gocloc.Result) {

switch opts.OutputType {
case OutputTypeClocXML:
t := gocloc.XMLTotalFiles{
t := ctoc.XMLTotalFiles{
Code: total.Code,
Comment: total.Comments,
Blank: total.Blanks,
}
f := &gocloc.XMLResultFiles{
f := &ctoc.XMLResultFiles{
Files: sortedFiles,
Total: t,
}
xmlResult := gocloc.XMLResult{
xmlResult := ctoc.XMLResult{
XMLFiles: f,
}
xmlResult.Encode()
Expand All @@ -150,7 +150,7 @@ func writeResultWithByFile(opts *CmdOptions, result *gocloc.Result) {
file.Code, file.Lang, p, file.Name)
}
case OutputTypeJSON:
jsonResult := gocloc.NewJSONFilesResultFromCloc(total, sortedFiles)
jsonResult := ctoc.NewJSONFilesResultFromCloc(total, sortedFiles)
buf, err := json.Marshal(jsonResult)
if err != nil {
fmt.Println(err)
Expand All @@ -160,8 +160,8 @@ func writeResultWithByFile(opts *CmdOptions, result *gocloc.Result) {
default:
for _, file := range sortedFiles {
clocFile := file
fmt.Printf("%-[1]*[2]s %21[3]v %14[4]v %14[5]v\n",
maxPathLen, file.Name, clocFile.Blanks, clocFile.Comments, clocFile.Code)
fmt.Printf("%-[1]*[2]s %21[3]v %14[4]v %14[5]v %14[6]v\n",
maxPathLen, file.Name, clocFile.Blanks, clocFile.Comments, clocFile.Code, clocFile.Tokens)
}
}
}
Expand All @@ -176,7 +176,7 @@ func (o *outputBuilder) WriteResult() {
if o.opts.ByFile {
writeResultWithByFile(o.opts, o.result)
} else {
var sortedLanguages gocloc.Languages
var sortedLanguages ctoc.Languages
for _, language := range clocLangs {
if len(language.Files) != 0 {
sortedLanguages = append(sortedLanguages, *language)
Expand All @@ -197,10 +197,10 @@ func (o *outputBuilder) WriteResult() {

switch o.opts.OutputType {
case OutputTypeClocXML:
xmlResult := gocloc.NewXMLResultFromCloc(total, sortedLanguages, gocloc.XMLResultWithLangs)
xmlResult := ctoc.NewXMLResultFromCloc(total, sortedLanguages, ctoc.XMLResultWithLangs)
xmlResult.Encode()
case OutputTypeJSON:
jsonResult := gocloc.NewJSONLanguagesResultFromCloc(total, sortedLanguages)
jsonResult := ctoc.NewJSONLanguagesResultFromCloc(total, sortedLanguages)
buf, err := json.Marshal(jsonResult)
if err != nil {
fmt.Println(err)
Expand All @@ -209,8 +209,8 @@ func (o *outputBuilder) WriteResult() {
os.Stdout.Write(buf)
default:
for _, language := range sortedLanguages {
fmt.Printf("%-27v %6v %14v %14v %14v\n",
language.Name, len(language.Files), language.Blanks, language.Comments, language.Code)
fmt.Printf("%-27v %6v %14v %14v %14v %14v\n",
language.Name, len(language.Files), language.Blanks, language.Comments, language.Code, language.Tokens)
}
}
}
Expand All @@ -221,10 +221,10 @@ func (o *outputBuilder) WriteResult() {

func main() {
var opts CmdOptions
clocOpts := gocloc.NewClocOptions()
clocOpts := ctoc.NewClocOptions()
// parse command line options
parser := flags.NewParser(&opts, flags.Default)
parser.Name = "gocloc"
parser.Name = "ctoc"
parser.Usage = "[OPTIONS] PATH[...]"

paths, err := flags.Parse(&opts)
Expand All @@ -233,7 +233,7 @@ func main() {
}

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

if opts.ShowVersion {
fmt.Printf("%s (%s)\n", Version, GitCommit)
Expand All @@ -258,7 +258,7 @@ func main() {

// setup option for exclude extensions
for _, ext := range strings.Split(opts.ExcludeExt, ",") {
e, ok := gocloc.Exts[ext]
e, ok := ctoc.Exts[ext]
if ok {
clocOpts.ExcludeExts[e] = struct{}{}
} else {
Expand Down Expand Up @@ -290,10 +290,10 @@ func main() {
clocOpts.Debug = opts.Debug
clocOpts.SkipDuplicated = opts.SkipDuplicated

processor := gocloc.NewProcessor(languages, clocOpts)
processor := ctoc.NewProcessor(languages, clocOpts)
result, err := processor.Analyze(paths)
if err != nil {
fmt.Printf("fail gocloc analyze. error: %v\n", err)
fmt.Printf("fail ctoc analyze. error: %v\n", err)
return
}

Expand Down
10 changes: 5 additions & 5 deletions examples/files/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package main
import (
"fmt"

"github.com/hhatto/gocloc"
"github.com/yaohui-wyh/ctoc"
)

func main() {
languages := gocloc.NewDefinedLanguages()
options := gocloc.NewClocOptions()
languages := ctoc.NewDefinedLanguages()
options := ctoc.NewClocOptions()
paths := []string{
".",
}

processor := gocloc.NewProcessor(languages, options)
processor := ctoc.NewProcessor(languages, options)
result, err := processor.Analyze(paths)
if err != nil {
fmt.Printf("gocloc fail. error: %v\n", err)
fmt.Printf("ctoc fail. error: %v\n", err)
return
}

Expand Down
10 changes: 5 additions & 5 deletions examples/languages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package main
import (
"fmt"

"github.com/hhatto/gocloc"
"github.com/yaohui-wyh/ctoc"
)

func main() {
languages := gocloc.NewDefinedLanguages()
options := gocloc.NewClocOptions()
languages := ctoc.NewDefinedLanguages()
options := ctoc.NewClocOptions()
paths := []string{
".",
}

processor := gocloc.NewProcessor(languages, options)
processor := ctoc.NewProcessor(languages, options)
result, err := processor.Analyze(paths)
if err != nil {
fmt.Printf("gocloc fail. error: %v\n", err)
fmt.Printf("ctoc fail. error: %v\n", err)
return
}

Expand Down
Loading

0 comments on commit 636a8ce

Please sign in to comment.