Skip to content

Commit

Permalink
Merge pull request #12 from chaspy/ignore-dir
Browse files Browse the repository at this point in the history
Support ignore dirs
  • Loading branch information
chaspy authored Jan 24, 2024
2 parents 0f4ab5c + 565e546 commit cca84d2
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 86 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ frontend, yarn.lock, TypeScript, 10876
| ------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `GH_REPO` | The repository to query. Defaults to the current repository. |
| `IGNORE_PATH` | List of path for ignoring to output LOC. it should be comma separated like IGNORE_PATH="app1/generated/path.go,app2/generated.path.go" |
| `IGNORE_DIRS` | List of directory for ignoring to output LOC. it should be comma separated like IGNORE_DIRS="app1/generated,app2/generated" |
180 changes: 94 additions & 86 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,113 +18,121 @@ func main() {
}

func run() error {
ignorePaths := getIgnorePaths()

dirs, err := os.ReadDir(".")
if err != nil {
fmt.Println(err)
return fmt.Errorf("failed to read directory")
}

for _, d := range dirs {
if (d.IsDir() && !strings.HasPrefix(d.Name(), ".")) {
dirName := d.Name()
language, packageFile := detectLanguage(dirName)
if language != "" {
loc := countLinesOfCode(dirName, language, ignorePaths)
fmt.Printf("%s, %s, %s, %d\n", dirName, packageFile, language, loc)
} else {
fmt.Println(dirName)
}
}
}
ignorePaths := getIgnorePaths()
ignoreDirs := getIgnoreDirs()

dirs, err := os.ReadDir(".")
if err != nil {
fmt.Println(err)
return fmt.Errorf("failed to read directory")
}

for _, d := range dirs {
if d.IsDir() && !strings.HasPrefix(d.Name(), ".") {
dirName := d.Name()
language, packageFile := detectLanguage(dirName)
if language != "" {
loc := countLinesOfCode(dirName, language, ignorePaths, ignoreDirs)
fmt.Printf("%s, %s, %s, %d\n", dirName, packageFile, language, loc)
} else {
fmt.Println(dirName)
}
}
}
return nil
}

func getIgnorePaths() []string {
ignorePathsEnv := os.Getenv("IGNORE_PATH")
if ignorePathsEnv == "" {
return nil
}
return strings.Split(ignorePathsEnv, ",")
func getIgnoreDirs() []string {
ignoreDirsEnv := os.Getenv("IGNORE_DIRS")
if ignoreDirsEnv == "" {
return nil
}
return strings.Split(ignoreDirsEnv, ",")
}

func getIgnorePaths() []string {
ignorePathsEnv := os.Getenv("IGNORE_PATH")
if ignorePathsEnv == "" {
return nil
}
return strings.Split(ignorePathsEnv, ",")
}

func detectLanguage(dirName string) (string, string) {
files, err := os.ReadDir(dirName)
if err != nil {
return "", ""
}

for _, f := range files {
switch f.Name() {
case "Gemfile":
return "Ruby", "Gemfile"
case "yarn.lock", "pnpm-lock.yaml":
return "TypeScript", f.Name()
case "go.mod":
return "Go", "go.mod"
case "mix.exs":
return "Elixir", "mix.exs"
case "Cargo.toml":
return "Rust", "Cargo.toml"
case "requirements.txt", "setup.py", "poetry.lock", "Pipfile.lock":
return "Python", f.Name()
}
}
return "", ""
for _, f := range files {
switch f.Name() {
case "Gemfile":
return "Ruby", "Gemfile"
case "yarn.lock", "pnpm-lock.yaml":
return "TypeScript", f.Name()
case "go.mod":
return "Go", "go.mod"
case "mix.exs":
return "Elixir", "mix.exs"
case "Cargo.toml":
return "Rust", "Cargo.toml"
case "requirements.txt", "setup.py", "poetry.lock", "Pipfile.lock":
return "Python", f.Name()
}
}
return "", ""
}

// nolint:gocyclo
func countLinesOfCode(dirName, language string, ignorePaths []string) int {
var extension string
switch language {
case "Ruby":
extension = ".rb"
case "TypeScript":
extension = ".ts"
case "Go":
extension = ".go"
case "Elixer":
extension = ".ex"
case "Rust":
extension = ".rs"
case "Python":
extension = ".py"
}

var loc int
err := filepath.Walk(dirName, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, extension) && !shouldIgnore(path, ignorePaths) {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
loc++
}
func countLinesOfCode(dirName, language string, ignorePaths []string, ignoreDirs []string) int {
var extension string
switch language {
case "Ruby":
extension = ".rb"
case "TypeScript":
extension = ".ts"
case "Go":
extension = ".go"
case "Elixir":
extension = ".ex"
case "Rust":
extension = ".rs"
case "Python":
extension = ".py"
}

var loc int
err := filepath.Walk(dirName, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, extension) && !shouldIgnorePath(path, ignorePaths) && !shouldIgnorePath(path, ignoreDirs) {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
loc++
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("failed to scan file: %w", err)
return fmt.Errorf("failed to scan file: %w", err)
}
}
return nil
})
}
return nil
})

if err != nil {
return 0
}

return loc
return loc
}

func shouldIgnore(path string, ignorePaths []string) bool {
for _, ignorePath := range ignorePaths {
if strings.Contains(path, ignorePath) {
return true
}
}
return false
}
func shouldIgnorePath(path string, ignorePath []string) bool {
for _, p := range ignorePath {
if strings.Contains(path, p) {
return true
}
}
return false
}

0 comments on commit cca84d2

Please sign in to comment.