Skip to content

Commit

Permalink
fix(gen): fix remove extra file
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Dec 24, 2024
1 parent 98c279b commit 111d9db
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
21 changes: 13 additions & 8 deletions internal/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ func Run(c config.Config) error {

func RemoveExtraFiles(wd, style string) {
if pathx.FileExists(filepath.Join("desc", "api")) {
if desc.GetApiFrameMainGoFilename(wd, style) != "main.go" {
if err := os.Remove(filepath.Join(wd, desc.GetApiFrameMainGoFilename(wd, style))); err != nil {
logx.Debugf("remove api frame main go file error: %s", err.Error())
}
}
if desc.GetApiFrameEtcFilename(wd, style) != "etc.yaml" {
if err := os.Remove(filepath.Join(wd, "etc", desc.GetApiFrameEtcFilename(wd, style))); err != nil {
logx.Debugf("remove api etc file error: %s", err.Error())
apiFilenames, err := desc.FindApiFiles(filepath.Join("desc", "api"))
if err == nil {
for _, v := range apiFilenames {
if desc.GetApiFrameMainGoFilename(wd, v, style) != "main.go" {
if err := os.Remove(filepath.Join(wd, desc.GetApiFrameMainGoFilename(wd, v, style))); err != nil {
logx.Debugf("remove api frame main go file error: %s", err.Error())
}
}
if desc.GetApiFrameEtcFilename(wd, v, style) != "etc.yaml" {
if err := os.Remove(filepath.Join(wd, "etc", desc.GetApiFrameEtcFilename(wd, v, style))); err != nil {
logx.Debugf("remove api etc file error: %s", err.Error())
}
}
}
}
}
Expand Down
34 changes: 23 additions & 11 deletions pkg/desc/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,32 @@ func protoHasService(fp string) (bool, error) {
return len(parse.Service) > 0, nil
}

func GetApiServiceName(apiDirName string) string {
fs, err := getApiFileRelPath(apiDirName)
if err != nil {
return ""
}
for _, file := range fs {
apiSpec, err := parser.Parse(filepath.Join(apiDirName, file), "")
func GetApiServiceName(apiDirName string, files ...string) string {
if len(files) == 0 {
fs, err := getApiFileRelPath(apiDirName)
if err != nil {
return ""
}
for _, file := range fs {
apiSpec, err := parser.Parse(filepath.Join(apiDirName, file), "")
if err != nil {
return ""
}
if apiSpec.Service.Name != "" {
return apiSpec.Service.Name
}
}
} else {
file := files[0]
apiSpec, err := parser.Parse(file, "")
if err != nil {
return ""
}
if apiSpec.Service.Name != "" {
return apiSpec.Service.Name
}
}

return ""
}

Expand Down Expand Up @@ -182,8 +194,8 @@ func FindRouteApiFiles(dir string) ([]string, error) {
}

// GetApiFrameMainGoFilename goctl/api/gogen/genmain.go
func GetApiFrameMainGoFilename(wd, style string) string {
serviceName := GetApiServiceName(filepath.Join(wd, "desc", "api"))
func GetApiFrameMainGoFilename(wd, file, style string) string {
serviceName := GetApiServiceName(filepath.Join(wd, "desc", "api"), file)
serviceName = strings.ToLower(serviceName)
filename, err := format.FileNamingFormat(style, serviceName)
if err != nil {
Expand All @@ -197,8 +209,8 @@ func GetApiFrameMainGoFilename(wd, style string) string {
}

// GetApiFrameEtcFilename goctl/api/gogen/genetc.go
func GetApiFrameEtcFilename(wd, style string) string {
serviceName := GetApiServiceName(filepath.Join(wd, "desc", "api"))
func GetApiFrameEtcFilename(wd, file, style string) string {
serviceName := GetApiServiceName(filepath.Join(wd, "desc", "api"), file)
filename, err := format.FileNamingFormat(style, serviceName)
if err != nil {
return ""
Expand Down

0 comments on commit 111d9db

Please sign in to comment.