11package config
22
33import (
4+ "path/filepath"
45 "strings"
56
67 "github.com/jmattheis/goverter/config/parse"
78 "github.com/jmattheis/goverter/pkgload"
89)
910
11+ func resolvePackage (sourceFileName , sourcePackage , targetFile string ) (string , error ) {
12+ relativeFile := targetFile
13+ if filepath .IsAbs (targetFile ) {
14+ var err error
15+ relativeFile , err = filepath .Rel (filepath .Dir (sourceFileName ), targetFile )
16+ if err != nil {
17+ return "" , err
18+ }
19+ }
20+
21+ return filepath .Dir (filepath .Join (sourcePackage , relativeFile )), nil
22+ }
23+
1024func getPackages (raw * Raw ) []string {
1125 lookup := map [string ]struct {}{}
1226 for _ , c := range raw .Converters {
1327 lookup [c .PackagePath ] = struct {}{}
14- registerConverterLines (lookup , c .PackagePath , c .Converter )
15- registerConverterLines (lookup , c .PackagePath , raw .Global )
28+
29+ // the default output:file is in ./generated and is not configured in Raw.
30+ // This preemptively loads this package, in case it already exists.
31+ lookup [filepath .Join (c .PackagePath , "generated" )] = struct {}{}
32+
33+ registerConverterLines (lookup , raw .WorkDir , c .FileName , c .PackagePath , c .Converter )
34+ registerConverterLines (lookup , raw .WorkDir , c .FileName , c .PackagePath , raw .Global )
1635 for _ , m := range c .Methods {
1736 registerMethodLines (lookup , c .PackagePath , m )
1837 }
@@ -26,33 +45,44 @@ func getPackages(raw *Raw) []string {
2645 return pkgs
2746}
2847
29- func registerConverterLines (lookup map [string ]struct {}, cwd string , lines RawLines ) {
48+ func registerConverterLines (lookup map [string ]struct {}, cwd , filename , sourcePackage string , lines RawLines ) {
3049 for _ , line := range lines .Lines {
3150 cmd , rest := parse .Command (line )
32- if cmd == configExtend {
51+ switch cmd {
52+ case configExtend :
3353 for _ , fullMethod := range strings .Fields (rest ) {
34- registerFullMethod (lookup , cwd , fullMethod )
54+ registerFullMethod (lookup , sourcePackage , fullMethod )
55+ }
56+ case configOutputFile :
57+ file , err := parse .File (cwd , rest )
58+ if err != nil {
59+ continue
60+ }
61+ targetPackage , err := resolvePackage (filename , sourcePackage , file )
62+ if err != nil {
63+ continue
3564 }
65+ lookup [targetPackage ] = struct {}{}
3666 }
3767 }
3868}
3969
40- func registerMethodLines (lookup map [string ]struct {}, cwd string , lines RawLines ) {
70+ func registerMethodLines (lookup map [string ]struct {}, sourcePackage string , lines RawLines ) {
4171 for _ , line := range lines .Lines {
4272 cmd , rest := parse .Command (line )
4373 switch cmd {
4474 case configMap :
4575 if _ , _ , custom , err := parseMethodMap (rest ); err == nil && custom != "" {
46- registerFullMethod (lookup , cwd , custom )
76+ registerFullMethod (lookup , sourcePackage , custom )
4777 }
4878 case configDefault :
49- registerFullMethod (lookup , cwd , rest )
79+ registerFullMethod (lookup , sourcePackage , rest )
5080 }
5181 }
5282}
5383
54- func registerFullMethod (lookup map [string ]struct {}, cwd , fullMethod string ) {
55- pkg , _ , err := pkgload .ParseMethodString (cwd , fullMethod )
84+ func registerFullMethod (lookup map [string ]struct {}, sourcePackage , fullMethod string ) {
85+ pkg , _ , err := pkgload .ParseMethodString (sourcePackage , fullMethod )
5686 if err == nil {
5787 lookup [pkg ] = struct {}{}
5888 }
0 commit comments