Skip to content

Commit

Permalink
Add generate-sequential make target (#259)
Browse files Browse the repository at this point in the history
* Add generate-sequential make target

* fix interface name

* make generate-sequential
  • Loading branch information
petersutter authored Mar 28, 2023
1 parent 5fd2dd9 commit 9386a44
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 39 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ endif

export LD_FLAGS=$(shell ./hack/get-build-ld-flags.sh)

#########################################
# Tools #
#########################################

TOOLS_DIR := hack/tools
include hack/tools.mk

##@ General

# The help target prints out all targets with their descriptions organized
Expand Down Expand Up @@ -55,6 +62,10 @@ go-test: ## Run go tests.
gen-markdown: ## Check that the generated markdown is up-to-date
go run ./internal/gen/markdown.go

.PHONY: generate-sequential
generate-sequential: gen-markdown $(MOCKGEN)
@hack/generate.sh ./pkg/... ./internal/...

##@ Build

.PHONY: build
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ require (
github.com/subosito/gotenv v1.4.2 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sys v0.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
11 changes: 11 additions & 0 deletions hack/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Gardener contributors
#
# SPDX-License-Identifier: Apache-2.0

set -e

echo "> Generate"

go generate $@
15 changes: 15 additions & 0 deletions hack/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build tools
// +build tools

/*
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Gardener contributors
SPDX-License-Identifier: Apache-2.0
*/

// This package imports things required by build scripts, to force `go mod` to see them as dependencies
package tools

import (
_ "github.com/golang/mock/mockgen"
)
40 changes: 40 additions & 0 deletions hack/tools.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Gardener contributors
#
# SPDX-License-Identifier: Apache-2.0

TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
MOCKGEN := $(TOOLS_BIN_DIR)/mockgen

export TOOLS_BIN_DIR := $(TOOLS_BIN_DIR)
export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH)

#########################################
# Common #
#########################################

# Tool targets should declare go.mod as a prerequisite, if the tool's version is managed via go modules. This causes
# make to rebuild the tool in the desired version, when go.mod is changed.
# For tools where the version is not managed via go.mod, we use a file per tool and version as an indicator for make
# whether we need to install the tool or a different version of the tool (make doesn't rerun the rule if the rule is
# changed).

# Use this "function" to add the version file as a prerequisite for the tool target: e.g.
# $(HELM): $(call tool_version_file,$(HELM),$(HELM_VERSION))
tool_version_file = $(TOOLS_BIN_DIR)/.version_$(subst $(TOOLS_BIN_DIR)/,,$(1))_$(2)

# This target cleans up any previous version files for the given tool and creates the given version file.
# This way, we can generically determine, which version was installed without calling each and every binary explicitly.
$(TOOLS_BIN_DIR)/.version_%:
@version_file=$@; rm -f $${version_file%_*}*
@touch $@

.PHONY: clean-tools-bin
clean-tools-bin:
rm -rf $(TOOLS_BIN_DIR)/*

#########################################
# Tools #
#########################################

$(MOCKGEN): go.mod
go build -o $(MOCKGEN) github.com/golang/mock/mockgen
58 changes: 22 additions & 36 deletions pkg/cmd/base/mocks/mock_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/cmd/base/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/gardener/gardenctl-v2/internal/util"
)

//go:generate mockgen -destination=./mocks/mock_options.go -package=mocks github.com/gardener/gardenctl-v2/pkg/cmd/base CommandOptions
//go:generate mockgen -destination=./mocks/mock_options.go -package=mocks github.com/gardener/gardenctl-v2/pkg/cmd/base Runnable

// Runnable is the base interface for command options.
type Runnable interface {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/base/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bar:
var (
ctrl *gomock.Controller
mockFactory *utilmocks.MockFactory
mockOptions *basemocks.MockCommandOptions
mockOptions *basemocks.MockRunnable
runE func(cmd *cobra.Command, args []string) error
cmd *cobra.Command
args []string
Expand All @@ -131,7 +131,7 @@ bar:

BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
mockOptions = basemocks.NewMockCommandOptions(ctrl)
mockOptions = basemocks.NewMockRunnable(ctrl)
mockFactory = utilmocks.NewMockFactory(ctrl)
cmd = &cobra.Command{}
args = []string{"foo", "bar"}
Expand Down

0 comments on commit 9386a44

Please sign in to comment.