Skip to content

Instantly share code, notes, and snippets.

@masartz
Created February 20, 2022 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masartz/95bedea33f526ca142a89fe44bf9644e to your computer and use it in GitHub Desktop.
Save masartz/95bedea33f526ca142a89fe44bf9644e to your computer and use it in GitHub Desktop.
Laravel のソースツリーをターゲットとして、Model classのメソッド数とその一覧を出力するためのスクリプト
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
)
var (
targetDir = os.Getenv("COUNT_TARGET_DIR")
)
func main() {
result := parseModel(targetDir)
type total struct {
count int
cm int
sm int
}
t := total{}
for i, row := range result {
t.count++
t.cm += len(row.classMethods)
t.sm += len(row.staticMethods)
fmt.Printf(
"%d - Model: %s\n Class Methods: (%d) %v\n Static Methods: (%d) %v\n",
i+1, row.Name, len(row.classMethods), row.classMethods, len(row.staticMethods), row.staticMethods,
)
}
fmt.Printf(
"-------------------\nTotal\n Model: %d\n Class Methods: %d\n Static Methods: %d\n",
t.count, t.cm, t.sm,
)
os.Exit(0)
}
type MethodsOnModel struct {
Name string
classMethods []string
staticMethods []string
}
func parseModel(dir string) (modelList []MethodsOnModel) {
files, _ := filepath.Glob(dir + "/*.php")
for _, file := range files {
var model = MethodsOnModel{}
model.Name = parseModelName(dir, file)
model.classMethods, model.staticMethods = readModelFile(file)
modelList = append(modelList, model)
}
return modelList
}
func parseModelName(dirPath, fullPath string) string {
path := regexp.MustCompile(dirPath + "/")
suffix := regexp.MustCompile(".php")
modelFilename := path.ReplaceAllString(fullPath, "")
modelName := suffix.ReplaceAllString(modelFilename, "")
return modelName
}
func readModelFile(fullpath string) (cm, sm []string) {
fp, err := os.Open(fullpath)
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
cmRE := regexp.MustCompile(`public\sfunction\s+([A-za-z_]+)\(`)
group := cmRE.FindStringSubmatch(scanner.Text())
if group != nil {
cm = append(cm, group[len(group)-1])
}
smRE := regexp.MustCompile(`public\sstatic\sfunction\s+([A-za-z_]+)\(`)
group = smRE.FindStringSubmatch(scanner.Text())
if group != nil {
sm = append(sm, group[len(group)-1])
}
}
return cm, sm
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment