github.com/ianlewis/go-gitignore@v0.1.1-0.20231110021210-4a0f15cbd56f/Makefile (about)

     1  # Copyright 2023 Google LLC
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #      http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  SHELL := /bin/bash
    16  OUTPUT_FORMAT ?= $(shell if [ "${GITHUB_ACTIONS}" == "true" ]; then echo "github"; else echo ""; fi)
    17  
    18  .PHONY: help
    19  help: ## Shows all targets and help from the Makefile (this message).
    20  	@echo "go-gitignore Makefile"
    21  	@echo "Usage: make [COMMAND]"
    22  	@echo ""
    23  	@grep --no-filename -E '^([/a-z.A-Z0-9_%-]+:.*?|)##' $(MAKEFILE_LIST) | \
    24  		awk 'BEGIN {FS = "(:.*?|)## ?"}; { \
    25  			if (length($$1) > 0) { \
    26  				printf "  \033[36m%-20s\033[0m %s\n", $$1, $$2; \
    27  			} else { \
    28  				if (length($$2) > 0) { \
    29  					printf "%s\n", $$2; \
    30  				} \
    31  			} \
    32  		}'
    33  
    34  node_modules/.installed: package.json package-lock.json
    35  	npm ci
    36  	touch node_modules/.installed
    37  
    38  ## Testing
    39  #####################################################################
    40  
    41  .PHONY: unit-test
    42  unit-test: ## Runs all unit tests.
    43  	@set -e;\
    44  		go mod vendor; \
    45  		extraargs=""; \
    46  		if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
    47  			extraargs="-v"; \
    48  		fi; \
    49  		go test -mod=vendor $$extraeargs ./...
    50  
    51  .PHONY: coverage
    52  coverage: ## Generate coverage report.
    53  	@set -e;\
    54  		go mod vendor; \
    55  		go test -mod=vendor -race -coverprofile=coverage.out -covermode=atomic ./...
    56  
    57  ## Tools
    58  #####################################################################
    59  
    60  .PHONY: autogen
    61  autogen: ## Runs autogen on code files.
    62  	@set -euo pipefail; \
    63  		md_files=$$( \
    64  			find . -type f \
    65  				\( \
    66  					-name '*.go' -o \
    67  					-name '*.yaml' -o \
    68  					-name '*.yml' \
    69  				\) \
    70  				-not -iwholename '*/.git/*' \
    71  				-not -iwholename '*/vendor/*' \
    72  				-not -iwholename '*/node_modules/*' \
    73  		); \
    74  		for filename in $${md_files}; do \
    75  			if ! ( head "$${filename}" | grep -iL "Copyright" > /dev/null ); then \
    76  				autogen -i --no-code --no-tlc -c "Google LLC" -l apache "$${filename}"; \
    77  			fi; \
    78  		done; \
    79  		if ! ( head Makefile | grep -iL "Copyright" > /dev/null ); then \
    80  			autogen -i --no-code --no-tlc -c "Google LLC" -l apache Makefile; \
    81  		fi;
    82  
    83  
    84  ## Linters
    85  #####################################################################
    86  
    87  .PHONY: lint
    88  lint: markdownlint golangci-lint yamllint actionlint ## Run all linters.
    89  
    90  .PHONY: actionlint
    91  actionlint: ## Runs the actionlint linter.
    92  	@# NOTE: We need to ignore config files used in tests.
    93  	@set -e;\
    94  		files=$$( \
    95  			find .github/workflows/ -type f \
    96  				\( \
    97  					-name '*.yaml' -o \
    98  					-name '*.yml' \
    99  				\) \
   100  		); \
   101  		if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
   102  			actionlint -format '{{range $$err := .}}::error file={{$$err.Filepath}},line={{$$err.Line}},col={{$$err.Column}}::{{$$err.Message}}%0A```%0A{{replace $$err.Snippet "\\n" "%0A"}}%0A```\n{{end}}' -ignore 'SC2016:' $${files}; \
   103  		else \
   104  			actionlint $${files}; \
   105  		fi
   106  
   107  .PHONY: markdownlint
   108  markdownlint: node_modules/.installed ## Runs the markdownlint linter.
   109  	@set -e;\
   110  		if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
   111  			exit_code=0; \
   112  			while IFS="" read -r p && [ -n "$$p" ]; do \
   113  				file=$$(echo "$$p" | jq -c -r '.fileName // empty'); \
   114  				line=$$(echo "$$p" | jq -c -r '.lineNumber // empty'); \
   115  				endline=$${line}; \
   116  				message=$$(echo "$$p" | jq -c -r '.ruleNames[0] + "/" + .ruleNames[1] + " " + .ruleDescription + " [Detail: \"" + .errorDetail + "\", Context: \"" + .errorContext + "\"]"'); \
   117  				exit_code=1; \
   118  				echo "::error file=$${file},line=$${line},endLine=$${endline}::$${message}"; \
   119  			done <<< "$$(./node_modules/.bin/markdownlint --dot --json . 2>&1 | jq -c '.[]')"; \
   120  			exit "$${exit_code}"; \
   121  		else \
   122  			npm run lint; \
   123  		fi
   124  
   125  .PHONY: golangci-lint
   126  golangci-lint: ## Runs the golangci-lint linter.
   127  	@set -e;\
   128  		extraargs=""; \
   129  		if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
   130  			extraargs="--out-format github-actions"; \
   131  		fi; \
   132  		golangci-lint run -c .golangci.yml ./... $$extraargs
   133  
   134  .PHONY: yamllint
   135  yamllint: ## Runs the yamllint linter.
   136  	@set -e;\
   137  		extraargs=""; \
   138  		if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
   139  			extraargs="-f github"; \
   140  		fi; \
   141  		yamllint --strict -c .yamllint.yaml . $$extraargs
   142  
   143  ## Maintenance
   144  #####################################################################
   145  
   146  .PHONY: clean
   147  clean: ## Delete temporary files.
   148  	rm -rf vendor node_modules