Friday, 29 November 2019

Relocate Golang Boilerplate Packages using Makefile

Supposing you've found a golang boilerplate and you want to relocate the project under your name. Here's a makefile to help you do so. > Note: A makefile is a file containing a set of directives used by a make build automation tool to generate a target/goal. First, let's declare some variables for further use. ```makefile GITHUB := "github.com" PROJECTNAME := $(shell basename "$(PWD)") PACKAGENAME := $(GITHUB)/$(shell basename "$(shell dirname "$(PWD)")")/$(PROJECTNAME) GOBASE := $(shell pwd) ``` A code snippet for running coverage tests is shown below. It is triggered by running ``make test`` ```makefile .PHONY .SILENT: test ## test: Run coverage tests test: # Example: make test @echo " *** Running Coverage Tests ***" $(GOBASE)/test.sh @echo " *** Completed *** " ``` The author has built a shell script to handle the coverage tests. Here we just need to call ``test.sh`` A simple test is required to see if a target path is provided or not. A sample usage would be ``make relocate TARGET=github.com/wingkwong/myproject`` ```makefile @test ${TARGET} || ( echo ">> TARGET is not set. Use: make relocate TARGET="; exit 1 ) ``` Then, we need to have our variables escaped as we will to sed to perform the replacement ```makefile $(eval ESCAPED_PACKAGENAME := $(shell echo "${PACKAGENAME}" | sed -e 's/[\/&]/\\&/g')) $(eval ESCAPED_PROJECTNAME := $(shell echo "${PROJECTNAME}" | sed -e 's/[\/&]/\\&/g')) $(eval ESCAPED_TARGET_PACKAGENAME := $(shell echo "${TARGET}" | sed -e 's/[\/&]/\\&/g')) $(eval ESCAPED_TARGET_PROJECTNAME := $(shell basename "$(shell dirname ${TARGET})" | sed -e 's/[\/&]/\\&/g')) $(eval ESCAPED_PARENT_DIRECTORY:= $(shell cd ../ && pwd | sed -e 's/[\/&]/\\&/g')) $(eval ESCAPED_PARENT_DIRECTORYNAME:= $(shell basename $(ESCAPED_PARENT_DIRECTORY))) ``` We need to replace the package name ``github.com/author/project`` with the desired one. ```makefile @grep -rlI '${PACKAGENAME}' --include=*.go ./ | xargs -I@ sed -i '' 's/${ESCAPED_PACKAGENAME}/${ESCAPED_TARGET_PACKAGENAME}/g' @ ``` We also need to replace the project name ``project`` with the desired one. ```makefile @grep -rlI '${PROJECTNAME}' --include=*.go ./ | xargs -I@ sed -i '' 's/${ESCAPED_PROJECTNAME}/${ESCAPED_TARGET_PROJECTNAME}/g' @ ``` Sample usage: make relocate TARGET=github.com/wingkwong/myproject ``` GITHUB := "github.com" PROJECTNAME := $(shell basename "$(PWD)") PACKAGENAME := $(GITHUB)/$(shell basename "$(shell dirname "$(PWD)")")/$(PROJECTNAME) GOBASE := $(shell pwd) .PHONY .SILENT: relocate ## relocate: Relocate packages relocate: # Example: make relocate TARGET=github.com/wingkwong/myproject @test ${TARGET} || ( echo ">> TARGET is not set. Use: make relocate TARGET="; exit 1 ) @echo " *** Relocating packages to $(TARGET) *** " $(eval ESCAPED_PACKAGENAME := $(shell echo "${PACKAGENAME}" | sed -e 's/[\/&]/\\&/g')) $(eval ESCAPED_PROJECTNAME := $(shell echo "${PROJECTNAME}" | sed -e 's/[\/&]/\\&/g')) $(eval ESCAPED_TARGET_PACKAGENAME := $(shell echo "${TARGET}" | sed -e 's/[\/&]/\\&/g')) $(eval ESCAPED_TARGET_PROJECTNAME := $(shell basename "$(shell dirname ${TARGET})" | sed -e 's/[\/&]/\\&/g')) $(eval ESCAPED_PARENT_DIRECTORY:= $(shell cd ../ && pwd | sed -e 's/[\/&]/\\&/g')) $(eval ESCAPED_PARENT_DIRECTORYNAME:= $(shell basename $(ESCAPED_PARENT_DIRECTORY))) # Replacing ${ESCAPED_PACKAGENAME} to ${ESCAPED_TARGET_PACKAGENAME} @echo " *** Replacing ${ESCAPED_PACKAGENAME} to ${ESCAPED_TARGET_PACKAGENAME} *** " @grep -rlI '${PACKAGENAME}' --include=*.go ./ | xargs -I@ sed -i '' 's/${ESCAPED_PACKAGENAME}/${ESCAPED_TARGET_PACKAGENAME}/g' @ # Replacing ${PROJECTNAME} to ${ESCAPED_TARGET_PROJECTNAME} @echo " *** Replacing ${PROJECTNAME} to ${ESCAPED_TARGET_PROJECTNAME} *** " @grep -rlI '${PROJECTNAME}' --include=*.go ./ | xargs -I@ sed -i '' 's/${ESCAPED_PROJECTNAME}/${ESCAPED_TARGET_PROJECTNAME}/g' @ @echo " *** Completed *** " .PHONY: help all: help help: Makefile @echo @echo " Choose a command run in "$(PROJECTNAME)":" @echo @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' @echo ```

No comments:

Post a Comment

A Fun Problem - Math

# Problem Statement JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today t...