From 9a46413c3ea701060becf228536f5ac06b761148 Mon Sep 17 00:00:00 2001 From: afmahmudax Date: Sat, 12 Nov 2022 11:11:29 +0700 Subject: [PATCH 1/4] add options --- astParser/parser.go | 38 ++++++++++++++++++++---- astParser/parser_option.go | 25 ++++++++++++++++ astParser/parser_option_test.go | 51 +++++++++++++++++++++++++++++++++ astParser/parser_test.go | 31 ++++++++++++++++++++ cmd/generate.go | 15 +++++++--- 5 files changed, 151 insertions(+), 9 deletions(-) create mode 100644 astParser/parser_option.go create mode 100644 astParser/parser_option_test.go diff --git a/astParser/parser.go b/astParser/parser.go index 03f885d..1f160ff 100644 --- a/astParser/parser.go +++ b/astParser/parser.go @@ -2,7 +2,6 @@ package astParser import ( "fmt" - "github.com/bykof/go-plantuml/domain" "go/ast" "go/parser" "go/token" @@ -10,10 +9,18 @@ import ( "log" "path/filepath" "reflect" + "regexp" "strings" + + "github.com/bykof/go-plantuml/domain" ) -func ParseDirectory(directoryPath string, recursive bool) domain.Packages { +func ParseDirectory(directoryPath string, opts ...ParserOptionFunc) domain.Packages { + options := &parserOptions{} + for _, opt := range opts { + opt(options) + } + var packages domain.Packages files, err := ioutil.ReadDir(directoryPath) if err != nil { @@ -31,14 +38,15 @@ func ParseDirectory(directoryPath string, recursive bool) domain.Packages { for _, file := range files { fullPath := filepath.Join(directoryPath, file.Name()) if !file.IsDir() { - if filepath.Ext(file.Name()) != ".go" || strings.Contains(file.Name(), "_test") { + if isExcluded(file.Name(), options.excludedFilesRegex) { continue } + parsedPackage := ParseFile(fullPath) currentPackage = currentPackage.Add(parsedPackage) } else { - if recursive { - packages = append(packages, ParseDirectory(fullPath, recursive)...) + if options.recursive { + packages = append(packages, ParseDirectory(fullPath, opts...)...) } } } @@ -50,6 +58,26 @@ func ParseDirectory(directoryPath string, recursive bool) domain.Packages { return packages } +func isExcluded(fileName string, regex *regexp.Regexp) bool { + if filepath.Ext(fileName) != ".go" { + return true + } + + if strings.HasSuffix(fileName, "_test.go") { + return true + } + + if regex == nil { + return false + } + + if regex.Match([]byte(fileName)) { + return true + } + + return false +} + func ParseFile(filePath string) domain.Package { var domainPackage domain.Package diff --git a/astParser/parser_option.go b/astParser/parser_option.go new file mode 100644 index 0000000..3c9588a --- /dev/null +++ b/astParser/parser_option.go @@ -0,0 +1,25 @@ +package astParser + +import ( + "regexp" +) + +type parserOptions struct { + recursive bool + excludedFilesRegex *regexp.Regexp +} + +type ParserOptionFunc func(*parserOptions) + +func WithRecursive() func(*parserOptions) { + return func(opt *parserOptions) { + opt.recursive = true + } +} + +func WithFileExclusion(exclusionRegex string) func(*parserOptions) { + return func(opt *parserOptions) { + var re = regexp.MustCompile(exclusionRegex) + opt.excludedFilesRegex = re + } +} diff --git a/astParser/parser_option_test.go b/astParser/parser_option_test.go new file mode 100644 index 0000000..27fbd71 --- /dev/null +++ b/astParser/parser_option_test.go @@ -0,0 +1,51 @@ +package astParser + +import ( + "reflect" + "regexp" + "testing" +) + +func TestWithRecursive(t *testing.T) { + tests := []struct { + name string + want *parserOptions + }{ + {"positive", &parserOptions{recursive: true}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + op := &parserOptions{} + opt := WithRecursive() + opt(op) + if !reflect.DeepEqual(op, tt.want) { + t.Errorf("WithRecursive() = %v, want %v", op, tt.want) + } + }) + } +} + +func TestWithFileExclusion(t *testing.T) { + type args struct { + exclusionRegex string + } + tests := []struct { + name string + args args + want *parserOptions + }{ + {"positive", args{"^somefile.go$"}, &parserOptions{excludedFilesRegex: regexp.MustCompile("^somefile.go$")}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + op := &parserOptions{} + opt := WithFileExclusion(tt.args.exclusionRegex) + opt(op) + if !reflect.DeepEqual(op, tt.want) { + t.Errorf("WithRecursive() = %v, want %v", op, tt.want) + } + }) + } +} diff --git a/astParser/parser_test.go b/astParser/parser_test.go index 198a7f9..aa9b21c 100644 --- a/astParser/parser_test.go +++ b/astParser/parser_test.go @@ -1 +1,32 @@ package astParser + +import ( + "regexp" + "testing" +) + +func Test_isExcluded(t *testing.T) { + type args struct { + fileName string + regex *regexp.Regexp + } + tests := []struct { + name string + args args + want bool + }{ + {"positive_non_go_file_returns_true", args{"parser.js", nil}, true}, + {"positive_go_test_file_returns_true", args{"parser_test.go", nil}, true}, + {"positive_match_regex_returns_true", args{"parser_mock.go", regexp.MustCompile(`^.+_mock.go$`)}, true}, + {"positive_match_regex_due_to_dot_returns_true", args{"parser_mock|tmp.go", regexp.MustCompile(`^.+_mock.tmp.go$`)}, true}, + {"negative_go_file_no_regex_returns_false", args{"parser.go", nil}, false}, + {"negative_go_file_did_not_match_regex_returns_false", args{"parser_mick.go", regexp.MustCompile(`^.+_mock.go$`)}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := isExcluded(tt.args.fileName, tt.args.regex); got != tt.want { + t.Errorf("isExcluded() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/cmd/generate.go b/cmd/generate.go index ec4d4d7..8f81e71 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -1,12 +1,14 @@ package cmd import ( + "io/ioutil" + "log" + + "github.com/spf13/cobra" + "github.com/bykof/go-plantuml/astParser" "github.com/bykof/go-plantuml/domain" "github.com/bykof/go-plantuml/formatter" - "github.com/spf13/cobra" - "io/ioutil" - "log" ) var ( @@ -24,8 +26,13 @@ var ( packages = append(packages, astParser.ParseFile(file)) } + options := []astParser.ParserOptionFunc{} + if recursive { + options = append(options, astParser.WithRecursive()) + } + for _, directory := range directories { - packages = append(packages, astParser.ParseDirectory(directory, recursive)...) + packages = append(packages, astParser.ParseDirectory(directory, options...)...) } formattedPlantUML := formatter.FormatPlantUML(packages) From 8a456a4d29f3813cc52b4cb65b3d2beb9cf9aed1 Mon Sep 17 00:00:00 2001 From: afmahmudax Date: Sat, 12 Nov 2022 11:15:04 +0700 Subject: [PATCH 2/4] add -x flag --- cmd/generate.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/generate.go b/cmd/generate.go index 8f81e71..b0a706c 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -15,6 +15,7 @@ var ( outPath string directories []string files []string + exclusion string recursive bool generateCmd = &cobra.Command{ Use: "generate", @@ -73,5 +74,12 @@ func init() { false, "traverse the given directories recursively", ) + generateCmd.Flags().StringVarP( + &exclusion, + "exclusion", + "x", + "", + "exclude file matching given regex expression, not used if using -f flag", + ) rootCmd.AddCommand(generateCmd) } From 7c7ee177d996a59809e684c7b535e72c480fd864 Mon Sep 17 00:00:00 2001 From: afmahmudax Date: Sat, 12 Nov 2022 11:21:04 +0700 Subject: [PATCH 3/4] adjust flag and readme --- README.md | 13 +++++++------ cmd/generate.go | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8ae9116..af83327 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# go-plantuml +# go-plantuml [![Build Status](https://github.com/bykof/go-plantuml/actions/workflows/test.yml/badge.svg)](https://github.com/bykof/go-plantuml/actions/workflows/test.yml/badge.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/bykof/go-plantuml)](https://goreportcard.com/report/github.com/bykof/go-plantuml) @@ -41,6 +41,7 @@ Flags: -h, --help help for generate -o, --out string the graphfile (default "graph.puml") -r, --recursive traverse the given directories recursively + -x, --exclude exclude file matching given regex expression, not used if using -f flag ``` ## Example @@ -82,10 +83,10 @@ type ( func (address Address) FullAddress(withPostalCode bool) string { return fmt.Sprintf( - "%s %s %d", - PackageVariable, - AnotherPackageVariable, - StartingStreetNumber, + "%s %s %d", + PackageVariable, + AnotherPackageVariable, + StartingStreetNumber, ) } @@ -132,4 +133,4 @@ Pull requests are welcome. For major changes, please open an issue first to disc Please make sure to update tests as appropriate. ## License -[MIT](https://choosealicense.com/licenses/mit/) \ No newline at end of file +[MIT](https://choosealicense.com/licenses/mit/) diff --git a/cmd/generate.go b/cmd/generate.go index b0a706c..a221342 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -76,7 +76,7 @@ func init() { ) generateCmd.Flags().StringVarP( &exclusion, - "exclusion", + "exclude", "x", "", "exclude file matching given regex expression, not used if using -f flag", From ce64a16d9623acbd81f8f383248543561bcfefa7 Mon Sep 17 00:00:00 2001 From: afmahmudax Date: Sat, 12 Nov 2022 12:59:41 +0700 Subject: [PATCH 4/4] add options --- cmd/generate.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/generate.go b/cmd/generate.go index a221342..e11a8f4 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -32,6 +32,10 @@ var ( options = append(options, astParser.WithRecursive()) } + if exclusion!=""{ + options = append(options, astParser.WithFileExclusion(exclusion)) + } + for _, directory := range directories { packages = append(packages, astParser.ParseDirectory(directory, options...)...) }