Skip to content

Commit

Permalink
Merge pull request #22 from khorevaa/master
Browse files Browse the repository at this point in the history
fix: opt module
  • Loading branch information
srikrsna authored Sep 16, 2021
2 parents 2851931 + c972fe8 commit 329b36e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ import (
)

func main() {
pgs.Init(pgs.DebugEnv("GOTAG_DEBUG")).RegisterModule(module.New()).RegisterPostProcessor(pgsgo.GoFmt()).Render()
pgs.Init(pgs.DebugEnv("GOTAG_DEBUG")).
RegisterModule(module.New()).
RegisterPostProcessor(pgsgo.GoFmt()).
Render()
}
16 changes: 15 additions & 1 deletion module/tagger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package module

import (
"fmt"
"go/parser"
"go/printer"
"go/token"
Expand Down Expand Up @@ -45,6 +46,8 @@ func (m mod) Execute(targets map[string]pgs.File, packages map[string]pgs.Packag
autoTags = strings.Split(autoTag, "+")
}

module := m.Parameters().Str("module")

extractor := newTagExtractor(m, m.Context, autoTags)

for _, f := range targets {
Expand All @@ -60,6 +63,17 @@ func (m mod) Execute(targets map[string]pgs.File, packages map[string]pgs.Packag
filename = filepath.Join(outdir, gfname)
}

if module != "" {

filename = strings.ReplaceAll(filename, string(filepath.Separator), "/")
trim := module + "/"
if !strings.HasPrefix(filename, trim) {
m.Debug(fmt.Sprintf("%v: generated file does not match prefix %q", filename, module))
m.Exit(1)
}
filename = strings.TrimPrefix(filename, trim)
}

fs := token.NewFileSet()
fn, err := parser.ParseFile(fs, filename, nil, parser.ParseComments)
m.CheckErr(err)
Expand All @@ -69,7 +83,7 @@ func (m mod) Execute(targets map[string]pgs.File, packages map[string]pgs.Packag
var buf strings.Builder
m.CheckErr(printer.Fprint(&buf, fs, fn))

m.OverwriteGeneratorFile(gfname, buf.String())
m.OverwriteGeneratorFile(filename, buf.String())
}

return m.Artifacts()
Expand Down

0 comments on commit 329b36e

Please sign in to comment.