Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for filter specified file #1324

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ OPTIONS:
--generalInfo value, -g value Go file path in which 'swagger general API Info' is written (default: "main.go")
--dir value, -d value Directories you want to parse,comma separated and general-info file must be in the first one (default: "./")
--exclude value Exclude directories and files when searching, comma separated
--specified value specified files when searching, comma separated
--propertyStrategy value, -p value Property Naming Strategy like snakecase,camelcase,pascalcase (default: "camelcase")
--output value, -o value Output directory for all the generated files(swagger.json, swagger.yaml and docs.go) (default: "./docs")
--outputTypes value, --ot value Output types of generated files (docs.go, swagger.json, swagger.yaml) like go,json,yaml (default: "go,json,yaml")
Expand Down
10 changes: 10 additions & 0 deletions cmd/swag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
const (
searchDirFlag = "dir"
excludeFlag = "exclude"
specifiedFlag = "specified"
generalInfoFlag = "generalInfo"
propertyStrategyFlag = "propertyStrategy"
outputFlag = "output"
Expand Down Expand Up @@ -57,6 +58,10 @@ var initFlags = []cli.Flag{
Name: excludeFlag,
Usage: "Exclude directories and files when searching, comma separated",
},
&cli.StringFlag{
Name: specifiedFlag,
Usage: "specified files when searching, comma separated",
},
&cli.StringFlag{
Name: propertyStrategyFlag,
Aliases: []string{"p"},
Expand Down Expand Up @@ -151,6 +156,7 @@ func initAction(ctx *cli.Context) error {
return gen.New().Build(&gen.Config{
SearchDir: ctx.String(searchDirFlag),
Excludes: ctx.String(excludeFlag),
Specifieds: ctx.String(specifiedFlag),
MainAPIFile: ctx.String(generalInfoFlag),
PropNamingStrategy: strategy,
OutputDir: ctx.String(outputFlag),
Expand Down Expand Up @@ -208,6 +214,10 @@ func main() {
Name: excludeFlag,
Usage: "Exclude directories and files when searching, comma separated",
},
&cli.StringFlag{
Name: specifiedFlag,
Usage: "specified files when searching, comma separated",
},
&cli.StringFlag{
Name: generalInfoFlag,
Aliases: []string{"g"},
Expand Down
5 changes: 5 additions & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ type Config struct {
// excludes dirs and files in SearchDir,comma separated
Excludes string

// add by tengfei choose files in SearchDir,comma separated
Specifieds string

// OutputDir represents the output directory for all the generated files
OutputDir string

Expand Down Expand Up @@ -162,6 +165,8 @@ func (g *Gen) Build(config *Config) error {
p := swag.New(swag.SetMarkdownFileDirectory(config.MarkdownFilesDir),
swag.SetDebugger(config.Debugger),
swag.SetExcludedDirsAndFiles(config.Excludes),
// add by tengfei
swag.SetSpecifiedDirsAndFiles(config.Specifieds),
swag.SetCodeExamplesDirectory(config.CodeExampleFilesDir),
swag.SetStrict(config.Strict),
swag.SetOverrides(overrides),
Expand Down
37 changes: 34 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ type Parser struct {
// excludes excludes dirs and files in SearchDir
excludes map[string]struct{}

// add by tengfei choose dirs and files in SearchDir
specifieds map[string]struct{}

// debugging output goes here
debug Debugger

Expand Down Expand Up @@ -250,6 +253,19 @@ func SetExcludedDirsAndFiles(excludes string) func(*Parser) {
}
}

// add by tengfei SetSpecifiedDirsAndFiles sets directories and files to be Specified when searching.
func SetSpecifiedDirsAndFiles(specifieds string) func(*Parser) {
return func(p *Parser) {
for _, f := range strings.Split(specifieds, ",") {
f = strings.TrimSpace(f)
if f != "" {
f = filepath.Clean(f)
p.specifieds[f] = struct{}{}
}
}
}
}

// SetStrict sets whether swag should error or warn when it detects cases which are most likely user errors.
func SetStrict(strict bool) func(*Parser) {
return func(p *Parser) {
Expand Down Expand Up @@ -1471,7 +1487,7 @@ func (parser *Parser) getAllGoFileInfo(packageDir, searchDir string) error {
return err
}

return parser.parseFile(filepath.ToSlash(filepath.Dir(filepath.Clean(filepath.Join(packageDir, relPath)))), path, nil)
return parser.parseFile(filepath.ToSlash(filepath.Dir(filepath.Clean(filepath.Join(packageDir, relPath)))), path, f.Name(), nil)
})
}

Expand Down Expand Up @@ -1499,7 +1515,7 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg) error {
}

path := filepath.Join(srcDir, f.Name())
if err := parser.parseFile(pkg.Name, path, nil); err != nil {
if err := parser.parseFile(pkg.Name, path, f.Name(), nil); err != nil {
return err
}
}
Expand All @@ -1513,11 +1529,26 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg) error {
return nil
}

func (parser *Parser) parseFile(packageDir, path string, src interface{}) error {
func (parser *Parser) parseFile(packageDir, path, name string, src interface{}) error {
if strings.HasSuffix(strings.ToLower(path), "_test.go") || filepath.Ext(path) != ".go" {
return nil
}

// add by tengfei 增加是否包含文件名过滤
if parser.specifieds != nil && len(parser.specifieds) > 0 {
var flag = true
for k, _ := range parser.specifieds {
if strings.Contains(name, k) {
flag = false
break
}
}
// 如果没有找到则跳过
if flag {
return nil
}
}

// positions are relative to FileSet
astFile, err := goparser.ParseFile(token.NewFileSet(), path, src, goparser.ParseComments)
if err != nil {
Expand Down