Skip to content

Commit

Permalink
handle toolbox tasks in makefile inclide (#131)
Browse files Browse the repository at this point in the history
* prefix for toolbox

* refactor

* include file

* tests
  • Loading branch information
bakito authored Sep 28, 2024
1 parent 626c5c5 commit 1a4d34b
Show file tree
Hide file tree
Showing 19 changed files with 341 additions and 365 deletions.
71 changes: 71 additions & 0 deletions .toolbox.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
## toolbox - start
## Current working directory
TB_LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || pwd)
## Location to install dependencies to
TB_LOCALBIN ?= $(TB_LOCALDIR)/bin
$(TB_LOCALBIN):
mkdir -p $(TB_LOCALBIN)

## Tool Binaries
TB_DEEPCOPY_GEN ?= $(TB_LOCALBIN)/deepcopy-gen
TB_GINKGO ?= $(TB_LOCALBIN)/ginkgo
TB_GOLANGCI_LINT ?= $(TB_LOCALBIN)/golangci-lint
TB_GORELEASER ?= $(TB_LOCALBIN)/goreleaser
TB_OAPI_CODEGEN ?= $(TB_LOCALBIN)/oapi-codegen
TB_SEMVER ?= $(TB_LOCALBIN)/semver

## Tool Versions
# renovate: packageName=k8s.io/code-generator/cmd/deepcopy-gen
TB_DEEPCOPY_GEN_VERSION ?= v0.31.1
# renovate: packageName=github.com/golangci/golangci-lint/cmd/golangci-lint
TB_GOLANGCI_LINT_VERSION ?= v1.61.0
# renovate: packageName=github.com/goreleaser/goreleaser/v2
TB_GORELEASER_VERSION ?= v2.3.2
# renovate: packageName=github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen
TB_OAPI_CODEGEN_VERSION ?= v2.4.1
# renovate: packageName=github.com/bakito/semver
TB_SEMVER_VERSION ?= v1.1.3

## Tool Installer
.PHONY: tb.deepcopy-gen
tb.deepcopy-gen: $(TB_DEEPCOPY_GEN) ## Download deepcopy-gen locally if necessary.
$(TB_DEEPCOPY_GEN): $(TB_LOCALBIN)
test -s $(TB_LOCALBIN)/deepcopy-gen || GOBIN=$(TB_LOCALBIN) go install k8s.io/code-generator/cmd/deepcopy-gen@$(TB_DEEPCOPY_GEN_VERSION)
.PHONY: tb.ginkgo
tb.ginkgo: $(TB_GINKGO) ## Download ginkgo locally if necessary.
$(TB_GINKGO): $(TB_LOCALBIN)
test -s $(TB_LOCALBIN)/ginkgo || GOBIN=$(TB_LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo
.PHONY: tb.golangci-lint
tb.golangci-lint: $(TB_GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(TB_GOLANGCI_LINT): $(TB_LOCALBIN)
test -s $(TB_LOCALBIN)/golangci-lint || GOBIN=$(TB_LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(TB_GOLANGCI_LINT_VERSION)
.PHONY: tb.goreleaser
tb.goreleaser: $(TB_GORELEASER) ## Download goreleaser locally if necessary.
$(TB_GORELEASER): $(TB_LOCALBIN)
test -s $(TB_LOCALBIN)/goreleaser || GOBIN=$(TB_LOCALBIN) go install github.com/goreleaser/goreleaser/v2@$(TB_GORELEASER_VERSION)
.PHONY: tb.oapi-codegen
tb.oapi-codegen: $(TB_OAPI_CODEGEN) ## Download oapi-codegen locally if necessary.
$(TB_OAPI_CODEGEN): $(TB_LOCALBIN)
test -s $(TB_LOCALBIN)/oapi-codegen || GOBIN=$(TB_LOCALBIN) go install github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@$(TB_OAPI_CODEGEN_VERSION)
.PHONY: tb.semver
tb.semver: $(TB_SEMVER) ## Download semver locally if necessary.
$(TB_SEMVER): $(TB_LOCALBIN)
test -s $(TB_LOCALBIN)/semver || GOBIN=$(TB_LOCALBIN) go install github.com/bakito/semver@$(TB_SEMVER_VERSION)

## Update Tools
.PHONY: tb.update
tb.update:
@rm -f \
$(TB_LOCALBIN)/deepcopy-gen \
$(TB_LOCALBIN)/ginkgo \
$(TB_LOCALBIN)/golangci-lint \
$(TB_LOCALBIN)/goreleaser \
$(TB_LOCALBIN)/oapi-codegen \
$(TB_LOCALBIN)/semver
toolbox makefile --renovate -f $(TB_LOCALDIR)/Makefile \
k8s.io/code-generator/cmd/deepcopy-gen@github.com/kubernetes/code-generator \
github.com/golangci/golangci-lint/cmd/golangci-lint \
github.com/goreleaser/goreleaser/v2 \
github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen \
github.com/bakito/semver
## toolbox - end
84 changes: 12 additions & 72 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,82 +1,22 @@
# Include toolbox tasks
include ./.toolbox.mk

# Run go golanci-lint
lint: golangci-lint
$(GOLANGCI_LINT) run --fix
lint: tb.golangci-lint
$(TB_GOLANGCI_LINT) run --fix

# Run go mod tidy
tidy:
go mod tidy

# Run tests
test: ginkgo tidy lint
$(GINKGO) -r --cover --coverprofile=coverage.out
test: tb.ginkgo tidy lint
$(TB_GINKGO) -r --cover --coverprofile=coverage.out

release: goreleaser semver
@version=$$($(SEMVER)); \
release: tb.goreleaser tb.semver
@version=$$($(TB_SEMVER)); \
git tag -s $$version -m"Release $$version"
$(GORELEASER) --clean

test-release: goreleaser
$(GORELEASER) --skip=publish --snapshot --clean

## toolbox - start
## Current working directory
LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || pwd)
## Location to install dependencies to
LOCALBIN ?= $(LOCALDIR)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
DEEPCOPY_GEN ?= $(LOCALBIN)/deepcopy-gen
GINKGO ?= $(LOCALBIN)/ginkgo
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
GORELEASER ?= $(LOCALBIN)/goreleaser
SEMVER ?= $(LOCALBIN)/semver

## Tool Versions
# renovate: packageName=k8s.io/code-generator/cmd/deepcopy-gen
DEEPCOPY_GEN_VERSION ?= v0.31.1
# renovate: packageName=github.com/golangci/golangci-lint/cmd/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.61.0
# renovate: packageName=github.com/goreleaser/goreleaser/v2
GORELEASER_VERSION ?= v2.3.2
# renovate: packageName=github.com/bakito/semver
SEMVER_VERSION ?= v1.1.3

## Tool Installer
.PHONY: deepcopy-gen
deepcopy-gen: $(DEEPCOPY_GEN) ## Download deepcopy-gen locally if necessary.
$(DEEPCOPY_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/deepcopy-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/deepcopy-gen@$(DEEPCOPY_GEN_VERSION)
.PHONY: ginkgo
ginkgo: $(GINKGO) ## Download ginkgo locally if necessary.
$(GINKGO): $(LOCALBIN)
test -s $(LOCALBIN)/ginkgo || GOBIN=$(LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo
.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
test -s $(LOCALBIN)/golangci-lint || GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
.PHONY: goreleaser
goreleaser: $(GORELEASER) ## Download goreleaser locally if necessary.
$(GORELEASER): $(LOCALBIN)
test -s $(LOCALBIN)/goreleaser || GOBIN=$(LOCALBIN) go install github.com/goreleaser/goreleaser/v2@$(GORELEASER_VERSION)
.PHONY: semver
semver: $(SEMVER) ## Download semver locally if necessary.
$(SEMVER): $(LOCALBIN)
test -s $(LOCALBIN)/semver || GOBIN=$(LOCALBIN) go install github.com/bakito/semver@$(SEMVER_VERSION)
$(TB_GORELEASER) --clean

## Update Tools
.PHONY: update-toolbox-tools
update-toolbox-tools:
@rm -f \
$(LOCALBIN)/deepcopy-gen \
$(LOCALBIN)/ginkgo \
$(LOCALBIN)/golangci-lint \
$(LOCALBIN)/goreleaser \
$(LOCALBIN)/semver
toolbox makefile --renovate -f $(LOCALDIR)/Makefile \
k8s.io/code-generator/cmd/deepcopy-gen@github.com/kubernetes/code-generator \
github.com/golangci/golangci-lint/cmd/golangci-lint \
github.com/goreleaser/goreleaser/v2 \
github.com/bakito/semver
## toolbox - end
test-release: tb.goreleaser
$(TB_GORELEASER) --skip=publish --snapshot --clean
15 changes: 2 additions & 13 deletions cmd/makefile.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cmd

import (
"errors"
"os"

"github.com/bakito/toolbox/pkg/makefile"
"github.com/go-resty/resty/v2"
"github.com/spf13/cobra"
Expand All @@ -21,29 +18,21 @@ var (
makefileCmd = &cobra.Command{
Use: "makefile [tools]",
Short: "Adds tools to a Makefile",
Args: func(_ *cobra.Command, args []string) error {
if _, err := os.Stat(toolsGo); err != nil {
if len(args) == 0 {
return errors.New("at least one tool must be provided")
}
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
client := resty.New()
mf, err := cmd.Flags().GetString(flagFile)
if err != nil {
return err
}
return makefile.Generate(client, cmd.OutOrStderr(), mf, renovate, toolsGo, args...)
return makefile.Generate(client, mf, renovate, toolsGo, args...)
},
}
)

func init() {
rootCmd.AddCommand(makefileCmd)

makefileCmd.Flags().StringP(flagFile, "f", "", "The Makefile path to generate tools in")
makefileCmd.Flags().StringP(flagFile, "f", "Makefile", "The Makefile path to generate tools in")
makefileCmd.Flags().StringVar(&toolsGo, flagToolsGo, "tools.go", "The tools.go file to check for tools dependencies")
makefileCmd.Flags().BoolVar(&renovate, "renovate", false, "If enables, renovate config is added to the Makefile (renovate.json file, if existing)")
}
28 changes: 14 additions & 14 deletions pkg/makefile/Makefile.tpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## Current working directory
LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || pwd)
TB_LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || pwd)
## Location to install dependencies to
LOCALBIN ?= $(LOCALDIR)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
TB_LOCALBIN ?= $(TB_LOCALDIR)/bin
$(TB_LOCALBIN):
mkdir -p $(TB_LOCALBIN)

## Tool Binaries
{{- range .Tools }}
{{.UpperName}} ?= $(LOCALBIN)/{{.Name}}
TB_{{.UpperName}} ?= $(TB_LOCALBIN)/{{.Name}}
{{- end }}
{{- if .WithVersions }}

Expand All @@ -17,25 +17,25 @@ $(LOCALBIN):
{{- if $.Renovate }}
# renovate: packageName={{.ToolName}}
{{- end }}
{{.UpperName}}_VERSION ?= {{.Version}}
TB_{{.UpperName}}_VERSION ?= {{.Version}}
{{- end }}
{{- end }}
{{- end }}

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

## Update Tools
.PHONY: update-toolbox-tools
update-toolbox-tools:
.PHONY: tb.update
tb.update:
@rm -f{{- range .Tools }} \
$(LOCALBIN)/{{.Name}}
$(TB_LOCALBIN)/{{.Name}}
{{- end }}
toolbox makefile {{ if $.Renovate }}--renovate {{ end }}-f $(LOCALDIR)/Makefile{{- range .Tools }}{{- if not .FromToolsGo }} \
toolbox makefile {{ if $.Renovate }}--renovate {{ end }}-f $(TB_LOCALDIR)/Makefile{{- range .Tools }}{{- if not .FromToolsGo }} \
{{.Tool}}{{- end }}
{{- end }}
5 changes: 3 additions & 2 deletions pkg/makefile/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

const (
markerStart = "## toolbox - start"
markerEnd = "## toolbox - end"
markerStart = "## toolbox - start"
markerEnd = "## toolbox - end"
includeFileName = ".toolbox.mk"
)

var (
Expand Down
29 changes: 20 additions & 9 deletions pkg/makefile/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package makefile
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"slices"
"sort"
Expand All @@ -20,12 +20,12 @@ var (
getRelease = github.LatestRelease
)

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

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

if makefile == "" {
_, err := writer.Write(out.Bytes())
makefile, err := filepath.Abs(makefile)
if err != nil {
return err
}

includeFile := filepath.Join(filepath.Dir(makefile), includeFileName)

data, err := os.ReadFile(makefile)
if err != nil {
return err
Expand All @@ -75,15 +77,24 @@ func generate(client *resty.Client, writer io.Writer, makefile string, renovate
end = parts[1]
}
}
file := start
file += out.String()
file += end

var file string
if !strings.Contains(string(data), fmt.Sprintf("include ./%s", includeFileName)) {
file = fmt.Sprintf("# Include toolbox tasks\ninclude ./%s\n\n", includeFileName)
}

file += start
file += strings.TrimSpace(end)

if renovate {
if err := updateRenovateConf(); err != nil {
return err
}
}
if err := os.WriteFile(includeFile, out.Bytes(), 0o600); err != nil {
return err
}

return os.WriteFile(makefile, []byte(file), 0o600)
}

Expand Down
Loading

0 comments on commit 1a4d34b

Please sign in to comment.