gocloc

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2022 License: MIT Imports: 15 Imported by: 27 modules, 21 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

# Go version >= 1.16
$ go install github.com/hhatto/gocloc/cmd/gocloc@latest

# Go version < 1.16
$ go get -u github.com/hhatto/gocloc/cmd/gocloc

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

cloc
$ time cloc .
    5171 text files.
    5052 unique files.
     420 files ignored.

https://github.com/AlDanial/cloc v 1.66  T=23.31 s (204.0 files/s, 48203.3 lines/s)
-----------------------------------------------------------------------------------
Language                         files          blank        comment           code
-----------------------------------------------------------------------------------
Go                                4197         101140         125939         780280
Assembly                           331           6128          14654          40531
HTML                                41           4927            198          33316
C                                   90           1076            940           7390
Perl                                12            185            177           1135
Bourne Again Shell                  25            209            266            933
XML                                  4             85              9            623
Bourne Shell                         8             71            302            467
Python                               1            121             88            295
DOS Batch                            5             55              1            238
JavaScript                           4             48            122            231
C/C++ Header                        15             50            147            211
CSS                                  3             51              9            176
yacc                                 1             27             20            155
Protocol Buffers                     1              1              0            144
Windows Resource File                4             25              0            116
JSON                                 2              0              0             36
make                                 7             12             10             35
Fortran 90                           2              1              3              8
C++                                  1              3              5              7
awk                                  1              1              6              7
-----------------------------------------------------------------------------------
SUM:                              4755         114216         142896         866334
-----------------------------------------------------------------------------------
cloc .  13.57s user 7.89s system 105% cpu 20.413 total
tokei
$ time tokei --sort code .
-------------------------------------------------------------------------------
 Language            Files        Total       Blanks     Comments         Code
-------------------------------------------------------------------------------
 Go                   4272      1027537       103241       150411       773970
 Plain Text             28            0            0            0       137715
 Assembly              334        61318         6130            0        55188
 HTML                   41        38441         4927          204        33316
 C                      92         9436         1081          946         7409
 BASH                   27         2134          260          570         1304
 XML                     4          717           85            9          623
 Perl                   10         1255          151         1096          343
 Python                  1          504          121           56          327
 Batch                   5          294           55            0          239
 JavaScript              4          401           48          122          231
 C Header               15          408           50          147          211
 CSS                     3          236           51            9          176
 Protocol Buffers        1          145            1            0          144
 Markdown                3            0            0            0          115
 JSON                    2            0            0            0           36
 Makefile                7           57           13           10           34
 FORTRAN Modern          2           12            1            3            8
 C++                     1           15            3            5            7
-------------------------------------------------------------------------------
 Total                4852      1142910       116218       153588      1011396
-------------------------------------------------------------------------------
tokei --sort code .  1.27s user 0.06s system 99% cpu 1.328 total
gocloc
$ time gocloc --exclude-ext=txt .
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Go                            4197         101140         133703         773058
Assembly                       331           6128          14694          40510
HTML                            41           4927            198          33316
C                               90           1076            991           7389
BASH                            28            257            539           1330
Perl                            12            185            177           1135
XML                              4             85              9            623
Python                           1            121             88            295
Batch                            5             55              1            238
JavaScript                       4             48            122            231
C Header                        15             50            147            211
CSS                              3             51              9            176
Yacc                             1             27             20            155
Protocol Buffers                 1              1              0            144
Markdown                         3             29              0             86
Plan9 Shell                      4             25             42             74
Bourne Shell                     5             23             29             70
JSON                             2              0              0             36
make                             7             13             10             34
FORTRAN Modern                   2              1              3              8
Awk                              1              1              6              7
C++                              1              3              5              7
-------------------------------------------------------------------------------
TOTAL                         4758         114246         150793         859133
-------------------------------------------------------------------------------
gocloc --exclude-ext=txt .  0.76s user 0.15s system 116% cpu 0.775 total

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Exts = map[string]string{}/* 220 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) Len

func (cf ClocFiles) Len() int

func (ClocFiles) Less

func (cf ClocFiles) Less(i, j int) bool

func (ClocFiles) Swap

func (cf ClocFiles) Swap(i, j int)

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 provide 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) Len

func (ls Languages) Len() int

func (Languages) Less

func (ls Languages) Less(i, j int) bool

func (Languages) Swap

func (ls Languages) Swap(i, j int)

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, option 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
gocloc command
examples
files command
languages command

Jump to

Keyboard shortcuts

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