gocloc

package module
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 14 Imported by: 11

README

gocloc

GoDoc ci Go Report Card Docker Pulls Docker Image Size

A little fast cloc(Count Lines Of Code), written in Go. Inspired by tokei.

Installation

require Go 1.19+

$ go install github.com/hhatto/gocloc/cmd/gocloc@latest

Arch Linux user can also install from AUR: gocloc-git.

Usage

Basic Usage
$ gocloc .
$ gocloc .
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Markdown                         3              8              0             18
Go                               1             29              1            323
-------------------------------------------------------------------------------
TOTAL                            4             37              1            341
-------------------------------------------------------------------------------
Via Docker

with dockerhub

$ docker run --rm -v "${PWD}":/workdir hhatto/gocloc .

with GitHub Packages on GitHub Actions

jobs:
  build:
    name: example of code measurement using gocloc
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@master

      - name: Login GitHub Registry
        run: docker login docker.pkg.github.com -u owner -p ${{ secrets.GITHUB_TOKEN }}

      - name: Run gocloc
        run: docker run --rm -v "${PWD}":/workdir docker.pkg.github.com/hhatto/gocloc/gocloc:latest .
Integration Jenkins CI

use SLOCCount Plugin.

$ cloc --by-file --output-type=sloccount . > sloccount.scc
$ cat sloccount.scc
398 Go      ./main.go
190 Go      ./language.go
132 Markdown        ./README.md
24  Go      ./xml.go
18  Go      ./file.go
15  Go      ./option.go

Support Languages

use --show-lang option

$ gocloc --show-lang

Performance

  • CPU 3.8GHz 8core Intel Core i7 / 32GB 2667MHz DDR4 / MacOSX 13.3.1
  • cloc 1.96
  • tokei 12.1.2 compiled with serialization support: json
  • gocloc a88edc5
  • target repository is golang/go commit:f742ddc
cloc
$ time cloc .
   12003 text files.
   11150 unique files.
    1192 files ignored.

8 errors:
Line count, exceeded timeout:  ./src/cmd/dist/build.go
Line count, exceeded timeout:  ./src/cmd/trace/static/webcomponents.min.js
Line count, exceeded timeout:  ./src/net/http/requestwrite_test.go
Line count, exceeded timeout:  ./src/vendor/golang.org/x/net/idna/tables10.0.0.go
Line count, exceeded timeout:  ./src/vendor/golang.org/x/net/idna/tables11.0.0.go
Line count, exceeded timeout:  ./src/vendor/golang.org/x/net/idna/tables12.0.0.go
Line count, exceeded timeout:  ./src/vendor/golang.org/x/net/idna/tables13.0.0.go
Line count, exceeded timeout:  ./src/vendor/golang.org/x/net/idna/tables9.0.0.go

github.com/AlDanial/cloc v 1.96  T=35.07 s (317.9 files/s, 78679.3 lines/s)
-----------------------------------------------------------------------------------
Language                         files          blank        comment           code
-----------------------------------------------------------------------------------
Go                                9081         205135         337681        1779107
Text                              1194          11530              0         210849
Assembly                           563          15549          21625         122329
HTML                                17           3197             78          24983
C                                  139           1324            982           6895
JSON                                20              0              0           3122
CSV                                  1              0              0           2119
Markdown                            27            674            106           1949
Bourne Shell                        16            253            868           1664
JavaScript                          10            234            221           1517
Perl                                10            173            171           1111
C/C++ Header                        26            145            346            724
Bourne Again Shell                  16            120            263            535
Python                               1            133            104            375
CSS                                  3              4             13            337
DOS Batch                            5             56             66            207
Windows Resource File                4             23              0            146
Logos                                2             16              0            101
Dockerfile                           2             13             15             47
C++                                  2             11             14             24
make                                 5              9             10             21
Objective-C                          1              2              3             11
Fortran 90                           2              1              3              8
awk                                  1              1              6              7
YAML                                 1              0              0              5
MATLAB                               1              1              0              4
-----------------------------------------------------------------------------------
SUM:                             11150         238604         362575        2158197
-----------------------------------------------------------------------------------
cloc .  33.70s user 1.48s system 99% cpu 35.237 total
tokei
$ time tokei --sort code  --exclude "**/*.txt" .
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Go                   9242      2330107      1812147       318036       199924
 GNU Style Assembly    565       159534       127093        16888        15553
 C                     143         9272         6949         1000         1323
 JSON                   21         3122         3122            0            0
 Shell                  16         2785         2267          342          176
 JavaScript             10         1972         1520          218          234
 Perl                    9         1360         1032          170          158
 C Header               27         1222          727          349          146
 BASH                   16          918          521          279          118
 Python                  1          612          421           70          121
 CSS                     3          354          337           13            4
 Autoconf                9          283          274            0            9
 Batch                   5          329          207           66           56
 Alex                    2          117          101            0           16
 Dockerfile              2           75           47           15           13
 C++                     2           49           24           14           11
 Makefile                5           40           20           10           10
 Objective-C             2           21           15            3            3
 FORTRAN Modern          2           12            8            3            1
 Markdown               18         2402            0         1853          549
-------------------------------------------------------------------------------
 HTML                   17        19060        18584           49          427
 |- CSS                  4         2071         1852           10          209
 |- HTML                 1          219          212            0            7
 |- JavaScript           8         6920         6876           16           28
 (Total)                          28270        27524           75          671
===============================================================================
 Total               10117      2533646      1975416       339378       218852
===============================================================================
tokei --sort code --exclude "**/*.txt" .  0.76s user 0.50s system 562% cpu 0.224 total
gocloc
$ time gocloc --exclude-ext=txt .
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Go                            9096         205242         352844        1764503
Assembly                       563          15555          21624         122324
HTML                            17           3197            212          24849
C                              139           1324            983           6894
JSON                            20              0              0           3122
BASH                            27            345           1106           2122
Markdown                        18            549             28           1825
JavaScript                      10            234            218           1520
C Header                        26            145            346            724
Perl                            10            173            584            698
Python                           1            133            104            375
CSS                              3              4             13            337
Batch                            5             56              0            273
Plan9 Shell                      4             23             50             96
Bourne Shell                     5             28             24             78
C++                              2             11             14             24
Makefile                         5             10             10             20
Objective-C                      2              3              3             15
FORTRAN Modern                   2              1              3              8
Awk                              1              1              6              7
-------------------------------------------------------------------------------
TOTAL                         9956         227034         378172        1929814
-------------------------------------------------------------------------------
gocloc --exclude-ext=txt .  0.65s user 0.51s system 119% cpu 0.970 total

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Exts = map[string]string{}/* 238 elements not displayed */

Exts is the definition of the language name, keyed by the extension for each language.

Functions

This section is empty.

Types

type ClocFile

type ClocFile struct {
	Code     int32  `xml:"code,attr" json:"code"`
	Comments int32  `xml:"comment,attr" json:"comment"`
	Blanks   int32  `xml:"blank,attr" json:"blank"`
	Name     string `xml:"name,attr" json:"name"`
	Lang     string `xml:"language,attr" json:"language"`
}

ClocFile is collecting to line count result.

func AnalyzeFile

func AnalyzeFile(filename string, language *Language, opts *ClocOptions) *ClocFile

AnalyzeFile is analyzing file, this function calls AnalyzeReader() inside.

func AnalyzeReader

func AnalyzeReader(filename string, language *Language, file io.Reader, opts *ClocOptions) *ClocFile

AnalyzeReader is analyzing file for io.Reader.

type ClocFiles

type ClocFiles []ClocFile

ClocFiles is gocloc result set.

func (ClocFiles) SortByBlanks added in v0.5.1

func (cf ClocFiles) SortByBlanks()

func (ClocFiles) SortByCode added in v0.5.1

func (cf ClocFiles) SortByCode()

func (ClocFiles) SortByComments added in v0.5.1

func (cf ClocFiles) SortByComments()

func (ClocFiles) SortByName added in v0.5.1

func (cf ClocFiles) SortByName()

type ClocLanguage

type ClocLanguage struct {
	Name       string `xml:"name,attr" json:"name,omitempty"`
	FilesCount int32  `xml:"files_count,attr" json:"files"`
	Code       int32  `xml:"code,attr" json:"code"`
	Comments   int32  `xml:"comment,attr" json:"comment"`
	Blanks     int32  `xml:"blank,attr" json:"blank"`
}

ClocLanguage is provided for xml-cloc and json format.

type ClocOptions

type ClocOptions struct {
	Debug          bool
	SkipDuplicated bool
	ExcludeExts    map[string]struct{}
	IncludeLangs   map[string]struct{}
	ReNotMatch     *regexp.Regexp
	ReMatch        *regexp.Regexp
	ReNotMatchDir  *regexp.Regexp
	ReMatchDir     *regexp.Regexp

	// OnCode is triggered for each line of code.
	OnCode func(line string)
	// OnBlack is triggered for each blank line.
	OnBlank func(line string)
	// OnComment is triggered for each line of comments.
	OnComment func(line string)
}

ClocOptions is gocloc processor options.

func NewClocOptions

func NewClocOptions() *ClocOptions

NewClocOptions create new ClocOptions with default values.

type DefinedLanguages

type DefinedLanguages struct {
	Langs map[string]*Language
}

DefinedLanguages is the type information for mapping language name(key) and NewLanguage.

func NewDefinedLanguages

func NewDefinedLanguages() *DefinedLanguages

NewDefinedLanguages create DefinedLanguages.

func (*DefinedLanguages) GetFormattedString

func (langs *DefinedLanguages) GetFormattedString() string

GetFormattedString return DefinedLanguages as a human-readable string.

type JSONFilesResult

type JSONFilesResult struct {
	Files []ClocFile   `json:"files"`
	Total ClocLanguage `json:"total"`
}

JSONFilesResult defines the result of the analysis(by files) in JSON format.

func NewJSONFilesResultFromCloc

func NewJSONFilesResultFromCloc(total *Language, sortedFiles ClocFiles) JSONFilesResult

NewJSONFilesResultFromCloc returns JSONFilesResult with default data set.

type JSONLanguagesResult

type JSONLanguagesResult struct {
	Languages []ClocLanguage `json:"languages"`
	Total     ClocLanguage   `json:"total"`
}

JSONLanguagesResult defines the result of the analysis in JSON format.

func NewJSONLanguagesResultFromCloc

func NewJSONLanguagesResultFromCloc(total *Language, sortedLanguages Languages) JSONLanguagesResult

NewJSONLanguagesResultFromCloc returns JSONLanguagesResult with default data set.

type Language

type Language struct {
	Name string

	Files    []string
	Code     int32
	Comments int32
	Blanks   int32
	Total    int32
	// contains filtered or unexported fields
}

Language is a type used to definitions and store statistics for one programming language.

func NewLanguage

func NewLanguage(name string, lineComments []string, multiLines [][]string) *Language

NewLanguage create language data store.

type Languages

type Languages []Language

Languages is an array representation of Language.

func (Languages) SortByBlanks added in v0.5.1

func (ls Languages) SortByBlanks()

func (Languages) SortByCode added in v0.5.1

func (ls Languages) SortByCode()

func (Languages) SortByComments added in v0.5.1

func (ls Languages) SortByComments()

func (Languages) SortByFiles added in v0.5.1

func (ls Languages) SortByFiles()

func (Languages) SortByName added in v0.5.1

func (ls Languages) SortByName()

type Processor

type Processor struct {
	// contains filtered or unexported fields
}

Processor is gocloc analyzing processor.

func NewProcessor

func NewProcessor(langs *DefinedLanguages, options *ClocOptions) *Processor

NewProcessor returns Processor.

func (*Processor) Analyze

func (p *Processor) Analyze(paths []string) (*Result, error)

Analyze executes gocloc parsing for the directory of the paths argument and returns the result.

type Result

type Result struct {
	Total         *Language
	Files         map[string]*ClocFile
	Languages     map[string]*Language
	MaxPathLength int
}

Result defined processing result.

type XMLResult

type XMLResult struct {
	XMLName      xml.Name            `xml:"results"`
	XMLFiles     *XMLResultFiles     `xml:"files,omitempty"`
	XMLLanguages *XMLResultLanguages `xml:"languages,omitempty"`
}

XMLResult stores the results in XML format.

func NewXMLResultFromCloc

func NewXMLResultFromCloc(total *Language, sortedLanguages Languages, _ XMLResultType) *XMLResult

NewXMLResultFromCloc returns XMLResult with default data set.

func (*XMLResult) Encode

func (x *XMLResult) Encode()

Encode outputs XMLResult in a human readable format.

type XMLResultFiles

type XMLResultFiles struct {
	Files []ClocFile    `xml:"file"`
	Total XMLTotalFiles `xml:"total"`
}

XMLResultFiles stores per file results in XML format.

type XMLResultLanguages

type XMLResultLanguages struct {
	Languages []ClocLanguage    `xml:"language"`
	Total     XMLTotalLanguages `xml:"total"`
}

XMLResultLanguages stores the results in XML format.

type XMLResultType

type XMLResultType int8

XMLResultType is the result type in XML format.

const (
	// XMLResultWithLangs is the result type for each language in XML format
	XMLResultWithLangs XMLResultType = iota
	// XMLResultWithFiles is the result type for each file in XML format
	XMLResultWithFiles
)

type XMLTotalFiles

type XMLTotalFiles struct {
	Code    int32 `xml:"code,attr"`
	Comment int32 `xml:"comment,attr"`
	Blank   int32 `xml:"blank,attr"`
}

XMLTotalFiles is the total result per file in XML format.

type XMLTotalLanguages

type XMLTotalLanguages struct {
	SumFiles int32 `xml:"sum_files,attr"`
	Code     int32 `xml:"code,attr"`
	Comment  int32 `xml:"comment,attr"`
	Blank    int32 `xml:"blank,attr"`
}

XMLTotalLanguages is the total result in XML format.

Directories

Path Synopsis
cmd
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL