Skip to content

Commit

Permalink
Merge pull request #1679 from cubxxw/feature/makefile-help
Browse files Browse the repository at this point in the history
feature(makefile): Add help and necessary comments to the makefile to…
  • Loading branch information
jkutner authored Mar 28, 2023
2 parents cc42e0a + de7bf41 commit d781d82
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 56 deletions.
131 changes: 82 additions & 49 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ TEST_TIMEOUT?=1200s
UNIT_TIMEOUT?=$(TEST_TIMEOUT)
NO_DOCKER?=

# Clean build flags
clean_build := $(strip ${PACK_BUILD})
clean_sha := $(strip ${PACK_GITSHA1})

# append build number and git sha to version, if not-empty
# Append build number and git sha to version, if not-empty
ifneq ($(and $(clean_build),$(clean_sha)),)
PACK_VERSION:=${PACK_VERSION}+git-${clean_sha}.build-${clean_build}
else ifneq ($(clean_build),)
Expand All @@ -50,53 +51,43 @@ export CGO_ENABLED=0

BINDIR:=/usr/bin/

.DEFAULT_GOAL := build

# this target must be listed first in order for it to be a default target,
# so that ubuntu_ppa's may be constructed using default build tools.
## build: Build the program
build: out
@echo "> Building..."
@echo "=====> Building..."
$(GOCMD) build -ldflags "-s -w -X 'github.com/buildpacks/pack.Version=${PACK_VERSION}' -extldflags ${LDFLAGS}" -trimpath -o ./out/$(PACK_BIN) -a ./cmd/pack

## all: Run clean, verify, test, and build operations
all: clean verify test build

# used by apt-get install when installing ubuntu ppa.
# move pack binary onto a path location.
install:
mkdir -p ${DESTDIR}${BINDIR}
cp ./out/$(PACK_BIN) ${DESTDIR}${BINDIR}/

mod-tidy:
$(GOCMD) mod tidy -compat=1.19
cd tools && $(GOCMD) mod tidy -compat=1.19

tidy: mod-tidy format

package: out
tar czf .$/out$/$(ARCHIVE_NAME).tgz -C .$/out$/ $(PACK_BIN)

install-mockgen:
@echo "> Installing mockgen..."
cd tools && $(GOCMD) install github.com/golang/mock/mockgen

install-goimports:
@echo "> Installing goimports..."
cd tools && $(GOCMD) install golang.org/x/tools/cmd/goimports
## clean: Clean the workspace
clean:
@echo "=====> Cleaning workspace..."
@$(RMRF) .$/out benchmarks.test || (exit 0)

## format: Format the code
format: install-goimports
@echo "> Formating code..."
@echo "=====> Formatting code..."
@goimports -l -w -local ${PACKAGE_BASE} ${SRC}
@go run tools/pedantic_imports/main.go ${PACKAGE_BASE} ${SRC}

install-golangci-lint:
@echo "> Installing golangci-lint..."
cd tools && $(GOCMD) install github.com/golangci/golangci-lint/cmd/golangci-lint
## generate: Generate mocks
generate: install-mockgen
@echo "=====> Generating mocks..."
$(GOCMD) generate ./...

## lint: Check the code
lint: install-golangci-lint
@echo "> Linting code..."
@echo "=====> Linting code..."
@golangci-lint run -c golangci.yaml

## test: Run unit and acceptance tests
test: unit acceptance

# append coverage arguments
## unit: Append coverage arguments
ifeq ($(TEST_COVERAGE), 1)
unit: GOTESTFLAGS:=$(GOTESTFLAGS) -coverprofile=./out/tests/coverage-unit.txt -covermode=atomic
endif
Expand All @@ -107,29 +98,18 @@ unit: out
@echo "> Running unit/integration tests..."
$(GOCMD) test $(GOTESTFLAGS) -timeout=$(UNIT_TIMEOUT) ./...

## acceptance: Run acceptance tests
acceptance: out
@echo "> Running acceptance tests..."
@echo "=====> Running acceptance tests..."
$(GOCMD) test $(GOTESTFLAGS) -timeout=$(ACCEPTANCE_TIMEOUT) -tags=acceptance ./acceptance

## acceptance-all: Run all acceptance tests
acceptance-all: export ACCEPTANCE_SUITE_CONFIG:=$(shell $(CAT) .$/acceptance$/testconfig$/all.json)
acceptance-all:
@echo "> Running acceptance tests..."
@echo "=====> Running acceptance tests..."
$(GOCMD) test $(GOTESTFLAGS) -timeout=$(ACCEPTANCE_TIMEOUT) -tags=acceptance ./acceptance

clean:
@echo "> Cleaning workspace..."
@$(RMRF) .$/out benchmarks.test || (exit 0)

verify: verify-format lint

generate: install-mockgen
@echo "> Generating mocks..."
$(GOCMD) generate ./...

verify-format: install-goimports
@echo "> Verifying format..."
$(if $(shell goimports -l -local ${PACKAGE_BASE} ${SRC}), @echo ERROR: Format verification failed! && goimports ${GOIMPORTS_DIFF_OPTION} -local ${PACKAGE_BASE} ${SRC} && exit 1)

## prepare-for-pr: Run clean, verify, and test operations and check for uncommitted changes
prepare-for-pr: tidy verify test
@git diff-index --quiet HEAD -- ||\
(echo "-----------------" &&\
Expand All @@ -141,18 +121,71 @@ prepare-for-pr: tidy verify test
echo "-----------------\n" &&\
exit 0)

## verify: Run format and lint checks
verify: verify-format lint

## verify-format: Verify the format
verify-format: install-goimports
@echo "=====> Verifying format..."
$(if $(shell goimports -l -local ${PACKAGE_BASE} ${SRC}), @echo ERROR: Format verification failed! && goimports ${GOIMPORTS_DIFF_OPTION} -local ${PACKAGE_BASE} ${SRC} && exit 1)

## benchmark: Run benchmark tests
benchmark: out
@echo "> Running Benchmarks"
@echo "=====> Running benchmarks"
$(GOCMD) test -run=^$ -bench=. -benchtime=1s -benchmem -memprofile=./out/bench_mem.out -cpuprofile=./out/bench_cpu.out -tags=benchmarks ./benchmarks/ -v
# NOTE: You can analyze the results, using go tool pprof. For instance, you can start a server to see a graph of the cpu usage by running
# go tool pprof -http=":8082" out/bench_cpu.out. Alternatively, you can run go tool pprof, and in the ensuing cli, run
# commands like top10 or web to dig down into the cpu and memory usage
# For more, see https://blog.golang.org/pprof

## package: Package the program
package: out
tar czf .$/out$/$(ARCHIVE_NAME).tgz -C .$/out$/ $(PACK_BIN)

## install: Install the program to the system
install:
mkdir -p ${DESTDIR}${BINDIR}
cp ./out/$(PACK_BIN) ${DESTDIR}${BINDIR}/

## install-mockgen: Used only by apt-get install when installing ubuntu ppa
install-mockgen:
@echo "=====> Installing mockgen..."
cd tools && $(GOCMD) install github.com/golang/mock/mockgen

## install-goimports: Install goimports dependency
install-goimports:
@echo "=====> Installing goimports..."
cd tools && $(GOCMD) install golang.org/x/tools/cmd/goimports

## install-golangci-lint: Install golangci-lint dependency
install-golangci-lint:
@echo "=====> Installing golangci-lint..."
cd tools && $(GOCMD) install github.com/golangci/golangci-lint/cmd/golangci-lint

## mod-tidy: Tidy Go modules
mod-tidy:
$(GOCMD) mod tidy -compat=1.19
cd tools && $(GOCMD) mod tidy -compat=1.19

## tidy: Tidy modules and format the code
tidy: mod-tidy format

# NOTE: Windows doesn't support `-p`
## out: Make a directory for output
out:
@mkdir out || (exit 0)
mkdir out$/tests || (exit 0)


.PHONY: clean build format imports lint test unit acceptance prepare-for-pr verify verify-format benchmark
## help: Display help information
help: Makefile
@echo ""
@echo "Usage:"
@echo ""
@echo " make [target]"
@echo ""
@echo "Targets:"
@echo ""
@awk -F ':|##' '/^[^\.%\t][^\t]*:.*##/{printf " \033[36m%-20s\033[0m %s\n", $$1, $$NF}' $(MAKEFILE_LIST) | sort
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'

.PHONY: clean build format imports lint test unit acceptance prepare-for-pr verify verify-format benchmark
10 changes: 3 additions & 7 deletions pkg/client/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *Client) parseTagReference(imageName string) (name.Reference, error) {
return nil, errors.New("image is a required parameter")
}
if _, err := name.ParseReference(imageName, name.WeakValidation); err != nil {
return nil, err
return nil, fmt.Errorf("'%s' is not a valid tag reference: %s", imageName, err)
}
ref, err := name.NewTag(imageName, name.WeakValidation)
if err != nil {
Expand Down Expand Up @@ -108,17 +108,13 @@ func contains(slc []string, v string) bool {
}

func getBestRunMirror(registry string, runImage string, mirrors []string, preferredMirrors []string) string {
var runImageList []string
runImageList = append(runImageList, preferredMirrors...)
runImageList = append(runImageList, runImage)
runImageList = append(runImageList, mirrors...)

runImageList := append(append(append([]string{}, preferredMirrors...), runImage), mirrors...)
for _, img := range runImageList {
ref, err := name.ParseReference(img, name.WeakValidation)
if err != nil {
continue
}
if ref.Context().RegistryStr() == registry {
if reg := ref.Context().RegistryStr(); reg == registry {
return img
}
}
Expand Down

0 comments on commit d781d82

Please sign in to comment.