Skip to content

Commit 1a4d34b

Browse files
authored
handle toolbox tasks in makefile inclide (#131)
* prefix for toolbox * refactor * include file * tests
1 parent 626c5c5 commit 1a4d34b

19 files changed

+341
-365
lines changed

.toolbox.mk

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## toolbox - start
2+
## Current working directory
3+
TB_LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || pwd)
4+
## Location to install dependencies to
5+
TB_LOCALBIN ?= $(TB_LOCALDIR)/bin
6+
$(TB_LOCALBIN):
7+
mkdir -p $(TB_LOCALBIN)
8+
9+
## Tool Binaries
10+
TB_DEEPCOPY_GEN ?= $(TB_LOCALBIN)/deepcopy-gen
11+
TB_GINKGO ?= $(TB_LOCALBIN)/ginkgo
12+
TB_GOLANGCI_LINT ?= $(TB_LOCALBIN)/golangci-lint
13+
TB_GORELEASER ?= $(TB_LOCALBIN)/goreleaser
14+
TB_OAPI_CODEGEN ?= $(TB_LOCALBIN)/oapi-codegen
15+
TB_SEMVER ?= $(TB_LOCALBIN)/semver
16+
17+
## Tool Versions
18+
# renovate: packageName=k8s.io/code-generator/cmd/deepcopy-gen
19+
TB_DEEPCOPY_GEN_VERSION ?= v0.31.1
20+
# renovate: packageName=github.com/golangci/golangci-lint/cmd/golangci-lint
21+
TB_GOLANGCI_LINT_VERSION ?= v1.61.0
22+
# renovate: packageName=github.com/goreleaser/goreleaser/v2
23+
TB_GORELEASER_VERSION ?= v2.3.2
24+
# renovate: packageName=github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen
25+
TB_OAPI_CODEGEN_VERSION ?= v2.4.1
26+
# renovate: packageName=github.com/bakito/semver
27+
TB_SEMVER_VERSION ?= v1.1.3
28+
29+
## Tool Installer
30+
.PHONY: tb.deepcopy-gen
31+
tb.deepcopy-gen: $(TB_DEEPCOPY_GEN) ## Download deepcopy-gen locally if necessary.
32+
$(TB_DEEPCOPY_GEN): $(TB_LOCALBIN)
33+
test -s $(TB_LOCALBIN)/deepcopy-gen || GOBIN=$(TB_LOCALBIN) go install k8s.io/code-generator/cmd/deepcopy-gen@$(TB_DEEPCOPY_GEN_VERSION)
34+
.PHONY: tb.ginkgo
35+
tb.ginkgo: $(TB_GINKGO) ## Download ginkgo locally if necessary.
36+
$(TB_GINKGO): $(TB_LOCALBIN)
37+
test -s $(TB_LOCALBIN)/ginkgo || GOBIN=$(TB_LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo
38+
.PHONY: tb.golangci-lint
39+
tb.golangci-lint: $(TB_GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
40+
$(TB_GOLANGCI_LINT): $(TB_LOCALBIN)
41+
test -s $(TB_LOCALBIN)/golangci-lint || GOBIN=$(TB_LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(TB_GOLANGCI_LINT_VERSION)
42+
.PHONY: tb.goreleaser
43+
tb.goreleaser: $(TB_GORELEASER) ## Download goreleaser locally if necessary.
44+
$(TB_GORELEASER): $(TB_LOCALBIN)
45+
test -s $(TB_LOCALBIN)/goreleaser || GOBIN=$(TB_LOCALBIN) go install github.com/goreleaser/goreleaser/v2@$(TB_GORELEASER_VERSION)
46+
.PHONY: tb.oapi-codegen
47+
tb.oapi-codegen: $(TB_OAPI_CODEGEN) ## Download oapi-codegen locally if necessary.
48+
$(TB_OAPI_CODEGEN): $(TB_LOCALBIN)
49+
test -s $(TB_LOCALBIN)/oapi-codegen || GOBIN=$(TB_LOCALBIN) go install github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@$(TB_OAPI_CODEGEN_VERSION)
50+
.PHONY: tb.semver
51+
tb.semver: $(TB_SEMVER) ## Download semver locally if necessary.
52+
$(TB_SEMVER): $(TB_LOCALBIN)
53+
test -s $(TB_LOCALBIN)/semver || GOBIN=$(TB_LOCALBIN) go install github.com/bakito/semver@$(TB_SEMVER_VERSION)
54+
55+
## Update Tools
56+
.PHONY: tb.update
57+
tb.update:
58+
@rm -f \
59+
$(TB_LOCALBIN)/deepcopy-gen \
60+
$(TB_LOCALBIN)/ginkgo \
61+
$(TB_LOCALBIN)/golangci-lint \
62+
$(TB_LOCALBIN)/goreleaser \
63+
$(TB_LOCALBIN)/oapi-codegen \
64+
$(TB_LOCALBIN)/semver
65+
toolbox makefile --renovate -f $(TB_LOCALDIR)/Makefile \
66+
k8s.io/code-generator/cmd/deepcopy-gen@github.com/kubernetes/code-generator \
67+
github.com/golangci/golangci-lint/cmd/golangci-lint \
68+
github.com/goreleaser/goreleaser/v2 \
69+
github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen \
70+
github.com/bakito/semver
71+
## toolbox - end

Makefile

+12-72
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,22 @@
1+
# Include toolbox tasks
2+
include ./.toolbox.mk
3+
14
# Run go golanci-lint
2-
lint: golangci-lint
3-
$(GOLANGCI_LINT) run --fix
5+
lint: tb.golangci-lint
6+
$(TB_GOLANGCI_LINT) run --fix
47

58
# Run go mod tidy
69
tidy:
710
go mod tidy
811

912
# Run tests
10-
test: ginkgo tidy lint
11-
$(GINKGO) -r --cover --coverprofile=coverage.out
13+
test: tb.ginkgo tidy lint
14+
$(TB_GINKGO) -r --cover --coverprofile=coverage.out
1215

13-
release: goreleaser semver
14-
@version=$$($(SEMVER)); \
16+
release: tb.goreleaser tb.semver
17+
@version=$$($(TB_SEMVER)); \
1518
git tag -s $$version -m"Release $$version"
16-
$(GORELEASER) --clean
17-
18-
test-release: goreleaser
19-
$(GORELEASER) --skip=publish --snapshot --clean
20-
21-
## toolbox - start
22-
## Current working directory
23-
LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || pwd)
24-
## Location to install dependencies to
25-
LOCALBIN ?= $(LOCALDIR)/bin
26-
$(LOCALBIN):
27-
mkdir -p $(LOCALBIN)
28-
29-
## Tool Binaries
30-
DEEPCOPY_GEN ?= $(LOCALBIN)/deepcopy-gen
31-
GINKGO ?= $(LOCALBIN)/ginkgo
32-
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
33-
GORELEASER ?= $(LOCALBIN)/goreleaser
34-
SEMVER ?= $(LOCALBIN)/semver
35-
36-
## Tool Versions
37-
# renovate: packageName=k8s.io/code-generator/cmd/deepcopy-gen
38-
DEEPCOPY_GEN_VERSION ?= v0.31.1
39-
# renovate: packageName=github.com/golangci/golangci-lint/cmd/golangci-lint
40-
GOLANGCI_LINT_VERSION ?= v1.61.0
41-
# renovate: packageName=github.com/goreleaser/goreleaser/v2
42-
GORELEASER_VERSION ?= v2.3.2
43-
# renovate: packageName=github.com/bakito/semver
44-
SEMVER_VERSION ?= v1.1.3
45-
46-
## Tool Installer
47-
.PHONY: deepcopy-gen
48-
deepcopy-gen: $(DEEPCOPY_GEN) ## Download deepcopy-gen locally if necessary.
49-
$(DEEPCOPY_GEN): $(LOCALBIN)
50-
test -s $(LOCALBIN)/deepcopy-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/deepcopy-gen@$(DEEPCOPY_GEN_VERSION)
51-
.PHONY: ginkgo
52-
ginkgo: $(GINKGO) ## Download ginkgo locally if necessary.
53-
$(GINKGO): $(LOCALBIN)
54-
test -s $(LOCALBIN)/ginkgo || GOBIN=$(LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo
55-
.PHONY: golangci-lint
56-
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
57-
$(GOLANGCI_LINT): $(LOCALBIN)
58-
test -s $(LOCALBIN)/golangci-lint || GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
59-
.PHONY: goreleaser
60-
goreleaser: $(GORELEASER) ## Download goreleaser locally if necessary.
61-
$(GORELEASER): $(LOCALBIN)
62-
test -s $(LOCALBIN)/goreleaser || GOBIN=$(LOCALBIN) go install github.com/goreleaser/goreleaser/v2@$(GORELEASER_VERSION)
63-
.PHONY: semver
64-
semver: $(SEMVER) ## Download semver locally if necessary.
65-
$(SEMVER): $(LOCALBIN)
66-
test -s $(LOCALBIN)/semver || GOBIN=$(LOCALBIN) go install github.com/bakito/semver@$(SEMVER_VERSION)
19+
$(TB_GORELEASER) --clean
6720

68-
## Update Tools
69-
.PHONY: update-toolbox-tools
70-
update-toolbox-tools:
71-
@rm -f \
72-
$(LOCALBIN)/deepcopy-gen \
73-
$(LOCALBIN)/ginkgo \
74-
$(LOCALBIN)/golangci-lint \
75-
$(LOCALBIN)/goreleaser \
76-
$(LOCALBIN)/semver
77-
toolbox makefile --renovate -f $(LOCALDIR)/Makefile \
78-
k8s.io/code-generator/cmd/deepcopy-gen@github.com/kubernetes/code-generator \
79-
github.com/golangci/golangci-lint/cmd/golangci-lint \
80-
github.com/goreleaser/goreleaser/v2 \
81-
github.com/bakito/semver
82-
## toolbox - end
21+
test-release: tb.goreleaser
22+
$(TB_GORELEASER) --skip=publish --snapshot --clean

cmd/makefile.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package cmd
22

33
import (
4-
"errors"
5-
"os"
6-
74
"github.com/bakito/toolbox/pkg/makefile"
85
"github.com/go-resty/resty/v2"
96
"github.com/spf13/cobra"
@@ -21,29 +18,21 @@ var (
2118
makefileCmd = &cobra.Command{
2219
Use: "makefile [tools]",
2320
Short: "Adds tools to a Makefile",
24-
Args: func(_ *cobra.Command, args []string) error {
25-
if _, err := os.Stat(toolsGo); err != nil {
26-
if len(args) == 0 {
27-
return errors.New("at least one tool must be provided")
28-
}
29-
}
30-
return nil
31-
},
3221
RunE: func(cmd *cobra.Command, args []string) error {
3322
client := resty.New()
3423
mf, err := cmd.Flags().GetString(flagFile)
3524
if err != nil {
3625
return err
3726
}
38-
return makefile.Generate(client, cmd.OutOrStderr(), mf, renovate, toolsGo, args...)
27+
return makefile.Generate(client, mf, renovate, toolsGo, args...)
3928
},
4029
}
4130
)
4231

4332
func init() {
4433
rootCmd.AddCommand(makefileCmd)
4534

46-
makefileCmd.Flags().StringP(flagFile, "f", "", "The Makefile path to generate tools in")
35+
makefileCmd.Flags().StringP(flagFile, "f", "Makefile", "The Makefile path to generate tools in")
4736
makefileCmd.Flags().StringVar(&toolsGo, flagToolsGo, "tools.go", "The tools.go file to check for tools dependencies")
4837
makefileCmd.Flags().BoolVar(&renovate, "renovate", false, "If enables, renovate config is added to the Makefile (renovate.json file, if existing)")
4938
}

pkg/makefile/Makefile.tpl

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
## Current working directory
2-
LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || pwd)
2+
TB_LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || pwd)
33
## Location to install dependencies to
4-
LOCALBIN ?= $(LOCALDIR)/bin
5-
$(LOCALBIN):
6-
mkdir -p $(LOCALBIN)
4+
TB_LOCALBIN ?= $(TB_LOCALDIR)/bin
5+
$(TB_LOCALBIN):
6+
mkdir -p $(TB_LOCALBIN)
77

88
## Tool Binaries
99
{{- range .Tools }}
10-
{{.UpperName}} ?= $(LOCALBIN)/{{.Name}}
10+
TB_{{.UpperName}} ?= $(TB_LOCALBIN)/{{.Name}}
1111
{{- end }}
1212
{{- if .WithVersions }}
1313

@@ -17,25 +17,25 @@ $(LOCALBIN):
1717
{{- if $.Renovate }}
1818
# renovate: packageName={{.ToolName}}
1919
{{- end }}
20-
{{.UpperName}}_VERSION ?= {{.Version}}
20+
TB_{{.UpperName}}_VERSION ?= {{.Version}}
2121
{{- end }}
2222
{{- end }}
2323
{{- end }}
2424

2525
## Tool Installer
2626
{{- range .Tools }}
27-
.PHONY: {{.Name}}
28-
{{.Name}}: $({{.UpperName}}) ## Download {{.Name}} locally if necessary.
29-
$({{.UpperName}}): $(LOCALBIN)
30-
test -s $(LOCALBIN)/{{.Name}} || GOBIN=$(LOCALBIN) go install {{.ToolName}}{{- if .Version }}@$({{.UpperName}}_VERSION){{- end }}
27+
.PHONY: tb.{{.Name}}
28+
tb.{{.Name}}: $(TB_{{.UpperName}}) ## Download {{.Name}} locally if necessary.
29+
$(TB_{{.UpperName}}): $(TB_LOCALBIN)
30+
test -s $(TB_LOCALBIN)/{{.Name}} || GOBIN=$(TB_LOCALBIN) go install {{.ToolName}}{{- if .Version }}@$(TB_{{.UpperName}}_VERSION){{- end }}
3131
{{- end }}
3232

3333
## Update Tools
34-
.PHONY: update-toolbox-tools
35-
update-toolbox-tools:
34+
.PHONY: tb.update
35+
tb.update:
3636
@rm -f{{- range .Tools }} \
37-
$(LOCALBIN)/{{.Name}}
37+
$(TB_LOCALBIN)/{{.Name}}
3838
{{- end }}
39-
toolbox makefile {{ if $.Renovate }}--renovate {{ end }}-f $(LOCALDIR)/Makefile{{- range .Tools }}{{- if not .FromToolsGo }} \
39+
toolbox makefile {{ if $.Renovate }}--renovate {{ end }}-f $(TB_LOCALDIR)/Makefile{{- range .Tools }}{{- if not .FromToolsGo }} \
4040
{{.Tool}}{{- end }}
4141
{{- end }}

pkg/makefile/consts.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
)
77

88
const (
9-
markerStart = "## toolbox - start"
10-
markerEnd = "## toolbox - end"
9+
markerStart = "## toolbox - start"
10+
markerEnd = "## toolbox - end"
11+
includeFileName = ".toolbox.mk"
1112
)
1213

1314
var (

pkg/makefile/make.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package makefile
33
import (
44
"bytes"
55
"fmt"
6-
"io"
76
"os"
7+
"path/filepath"
88
"regexp"
99
"slices"
1010
"sort"
@@ -20,12 +20,12 @@ var (
2020
getRelease = github.LatestRelease
2121
)
2222

23-
func Generate(client *resty.Client, writer io.Writer, makefile string, renovate bool, toolsFile string, tools ...string) error {
23+
func Generate(client *resty.Client, makefile string, renovate bool, toolsFile string, tools ...string) error {
2424
argTools, toolData := mergeWithToolsGo(toolsFile, unique(tools))
25-
return generate(client, writer, makefile, renovate, argTools, toolData)
25+
return generate(client, makefile, renovate, argTools, toolData)
2626
}
2727

28-
func generate(client *resty.Client, writer io.Writer, makefile string, renovate bool, argTools []string, toolData []toolData) error {
28+
func generate(client *resty.Client, makefile string, renovate bool, argTools []string, toolData []toolData) error {
2929
for _, t := range argTools {
3030
td, err := dataForArg(client, t)
3131
if err != nil {
@@ -55,11 +55,13 @@ func generate(client *resty.Client, writer io.Writer, makefile string, renovate
5555
return err
5656
}
5757

58-
if makefile == "" {
59-
_, err := writer.Write(out.Bytes())
58+
makefile, err := filepath.Abs(makefile)
59+
if err != nil {
6060
return err
6161
}
6262

63+
includeFile := filepath.Join(filepath.Dir(makefile), includeFileName)
64+
6365
data, err := os.ReadFile(makefile)
6466
if err != nil {
6567
return err
@@ -75,15 +77,24 @@ func generate(client *resty.Client, writer io.Writer, makefile string, renovate
7577
end = parts[1]
7678
}
7779
}
78-
file := start
79-
file += out.String()
80-
file += end
80+
81+
var file string
82+
if !strings.Contains(string(data), fmt.Sprintf("include ./%s", includeFileName)) {
83+
file = fmt.Sprintf("# Include toolbox tasks\ninclude ./%s\n\n", includeFileName)
84+
}
85+
86+
file += start
87+
file += strings.TrimSpace(end)
8188

8289
if renovate {
8390
if err := updateRenovateConf(); err != nil {
8491
return err
8592
}
8693
}
94+
if err := os.WriteFile(includeFile, out.Bytes(), 0o600); err != nil {
95+
return err
96+
}
97+
8798
return os.WriteFile(makefile, []byte(file), 0o600)
8899
}
89100

0 commit comments

Comments
 (0)