@@ -15,15 +15,18 @@ import (
1515)
1616
1717var (
18- pattern = regexp .MustCompile (`^github\.com\ /([\w-]+\ /[\w-]+).*$` )
19- getRelease = github .LatestRelease
18+ githubPattern = regexp .MustCompile (`^github\.com/([\w-]+/[\w-]+).*$` )
19+ getRelease = github .LatestRelease
2020)
2121
22- func Generate (client * resty.Client , writer io.Writer , makefile string , tools ... string ) error {
23- var toolData []toolData
22+ func Generate (client * resty.Client , writer io.Writer , makefile string , toolsFile string , tools ... string ) error {
23+ argTools , toolData := mergeWithToolsGo (toolsFile , tools )
24+ return generate (client , writer , makefile , argTools , toolData )
25+ }
2426
25- for _ , t := range tools {
26- td , err := data (client , t )
27+ func generate (client * resty.Client , writer io.Writer , makefile string , argTools []string , toolData []toolData ) error {
28+ for _ , t := range argTools {
29+ td , err := dataForArg (client , t )
2730 if err != nil {
2831 return err
2932 }
@@ -34,10 +37,18 @@ func Generate(client *resty.Client, writer io.Writer, makefile string, tools ...
3437 return toolData [i ].Name < toolData [j ].Name
3538 })
3639
40+ withVersions := false
41+ for _ , td := range toolData {
42+ if ! withVersions && td .Version != "" {
43+ withVersions = true
44+ }
45+ }
46+
3747 out := & bytes.Buffer {}
3848 t := template .Must (template .New ("Makefile" ).Parse (makefileTemplate ))
3949 if err := t .Execute (out , map [string ]interface {}{
40- "Tools" : toolData ,
50+ "Tools" : toolData ,
51+ "WithVersions" : withVersions ,
4152 }); err != nil {
4253 return err
4354 }
@@ -69,37 +80,73 @@ func Generate(client *resty.Client, writer io.Writer, makefile string, tools ...
6980 return os .WriteFile (makefile , []byte (file ), 0o600 )
7081}
7182
72- func data (client * resty.Client , tool string ) (toolData , error ) {
83+ func dataForArg (client * resty.Client , tool string ) (toolData , error ) {
7384 toolRepo := strings .Split (tool , "@" )
74-
7585 toolName := toolRepo [0 ]
86+
87+ td := dataForTool (false , toolName , tool )
88+
7689 repo := toolRepo [len (toolRepo )- 1 ]
77- match := pattern .FindStringSubmatch (repo )
78- t := toolData {}
90+ match := githubPattern .FindStringSubmatch (repo )
7991
8092 if len (match ) != 2 {
81- return t , fmt .Errorf ("invalid tool %q" , tool )
93+ return td , fmt .Errorf ("invalid tool %q" , tool )
8294 }
83-
8495 ghr , err := getRelease (client , match [1 ], true )
8596 if err != nil {
86- return t , err
97+ return td , err
8798 }
99+ td .Version = ghr .TagName
88100
89- parts := strings .Split (toolName , "/" )
101+ return td , nil
102+ }
90103
91- t .Version = ghr .TagName
92- t .ToolName = toolName
93- t .Tool = tool
94- t .Name = parts [len (parts )- 1 ]
95- t .UpperName = strings .ReplaceAll (strings .ToUpper (t .Name ), "-" , "_" )
96- return t , nil
104+ func dataForTool (fromToolsGo bool , toolName string , fullTool ... string ) (td toolData ) {
105+ parts := strings .Split (toolName , "/" )
106+ td .ToolName = toolName
107+ if len (fullTool ) == 1 {
108+ td .Tool = fullTool [0 ]
109+ } else {
110+ td .Tool = toolName
111+ }
112+ td .Name = parts [len (parts )- 1 ]
113+ td .UpperName = strings .ReplaceAll (strings .ToUpper (td .Name ), "-" , "_" )
114+ td .FromToolsGo = fromToolsGo
115+ return
97116}
98117
99118type toolData struct {
100- Name string `json:"Name"`
101- UpperName string `json:"UpperName"`
102- Version string `json:"Version"`
103- Tool string `json:"Tool"`
104- ToolName string `json:"ToolName"`
119+ Name string `json:"Name"`
120+ UpperName string `json:"UpperName"`
121+ Version string `json:"Version"`
122+ Tool string `json:"Tool"`
123+ ToolName string `json:"ToolName"`
124+ FromToolsGo bool `json:"FromToolsGo"`
125+ }
126+
127+ func mergeWithToolsGo (fileName string , inTools []string ) ([]string , []toolData ) {
128+ content , err := os .ReadFile (fileName )
129+ if err != nil {
130+ return inTools , nil
131+ }
132+
133+ t := make (map [string ]bool )
134+ for _ , tool := range inTools {
135+ t [tool ] = true
136+ }
137+
138+ r := regexp .MustCompile (`"(.*)"` )
139+ var goTools []toolData
140+ for _ , m := range r .FindAllStringSubmatch (string (content ), - 1 ) {
141+ tool := m [1 ]
142+ goTools = append (goTools , dataForTool (true , tool ))
143+ delete (t , tool )
144+ }
145+
146+ var argTools []string
147+ for t := range t {
148+ argTools = append (argTools , t )
149+ }
150+
151+ return argTools , goTools
105152}
0 commit comments