Sunday, 29 December 2019
Relocate Golang Boilerplate Packages using Taskfile
Supposing you've found a Go boilerplate and you want to relocate the project under your name. Here's a TaskFile to help you do so.
Task is a task runner / build tool that aims to be simpler and easier to use than, for example, [GNU Make](https://www.gnu.org/software/make/).
TaskFile is a yml file. A simple is shown below
```
version: '2'
tasks:
hello:
cmds:
- echo 'Hello World from Task!'
silent: true
```
Compared with Makefile, it indeed looks simpler.
For running the coverage tests, we just need add our commands under ``cmds`` block
```
test:
cmds:
- echo " *** Running Coverage Tests ***"
- $(pwd)/test.sh
- echo " *** Completed *** "
silent: true
```
What about ``relocate``? That would be a bit different as I need to think about how to inject the variables into it. Thanks to TaskFile development team, I could do something like this.
```
relocate:
cmds:
- echo " *** Relocating packages to {{.TARGET_PATH}} ***"
- task: replace-string
vars: {
SOURCE_STR: "{{.PACKAGE_NAME}}",
TARGET_STG: "{{.TARGET_PATH}}"
}
- task: replace-string
vars: {
SOURCE_STR: "{{.PROJECT_NAME}}",
TARGET_STG: "{{.TARGET_PROJECTNAME}}"
}
- echo " *** Completed *** "
silent: true
```
In Taskfile, a variable is represented like this: ``{{.VAR}}``. We just need to declare all the variables there.
```
vars:
GITHUB: "github.com"
PROJECT_NAME_DIR:
sh: echo $(basename "$(dirname "$(pwd)")")
PROJECT_NAME:
sh: echo $(basename "$(pwd)" | sed -e 's/[\/&]/\\&/g')
PACKAGE_NAME:
sh: echo "{{.GITHUB}}/{{.PROJECT_NAME_DIR}}/{{.PROJECT_NAME}}" | sed -e 's/[\/&]/\\&/g'
TARGET_PATH:
sh: echo "{{.TARGET}}" | sed -e 's/[\/&]/\\&/g'
TARGET_PROJECTNAME:
sh: basename "dirname {{.TARGET_PATH}}" | sed -e 's/[\/&]/\\&/g'
```
Remember ``relocate`` is only triggered if there is a TARGET path? A preconditions block can help in this case. The variable ``TARGET`` is a parameters when calling ``task relocate``.
```
preconditions:
- sh: "[ {{.TARGET}} != '' ]"
```
Wait a minute, then what is replace-string? replace-string is another task used to grep the file and perform the replacement.
```
replace-string:
cmds:
- grep -rlI '{{.SOURCE_STR}}' --include=*.{go,json} ./ | xargs -I@ sed -i '' 's/{{.SOURCE_STR}}/{{.TARGET_STG}}/g' @
silent: true
```
By adding ``silent: true``, we can make the console cleaner. Just like we add ``.SILENT`` in Makefile.
Sample usage: task relocate TARGET=github.com/wingkwong/project
Complete Code:
```
version: '2'
tasks:
relocate:
cmds:
- echo " *** Relocating packages to {{.TARGET_PATH}} ***"
- task: replace-string
vars: {
SOURCE_STR: "{{.PACKAGE_NAME}}",
TARGET_STG: "{{.TARGET_PATH}}"
}
- task: replace-string
vars: {
SOURCE_STR: "{{.PROJECT_NAME}}",
TARGET_STG: "{{.TARGET_PROJECTNAME}}"
}
- echo " *** Completed *** "
silent: true
vars:
GITHUB: "github.com"
PROJECT_NAME_DIR:
sh: echo $(basename "$(dirname "$(pwd)")")
PROJECT_NAME:
sh: echo $(basename "$(pwd)" | sed -e 's/[\/&]/\\&/g')
PACKAGE_NAME:
sh: echo "{{.GITHUB}}/{{.PROJECT_NAME_DIR}}/{{.PROJECT_NAME}}" | sed -e 's/[\/&]/\\&/g'
TARGET_PATH:
sh: echo "{{.TARGET}}" | sed -e 's/[\/&]/\\&/g'
TARGET_PROJECTNAME:
sh: basename "dirname {{.TARGET_PATH}}" | sed -e 's/[\/&]/\\&/g'
preconditions:
- sh: "[ {{.TARGET}} != '' ]"
replace-string:
cmds:
- grep -rlI '{{.SOURCE_STR}}' --include=*.{go,json} ./ | xargs -I@ sed -i '' 's/{{.SOURCE_STR}}/{{.TARGET_STG}}/g' @
silent: true
```
Subscribe to:
Post Comments (Atom)
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...
-
SHA stands for Secure Hashing Algorithm and 2 is just a version number. SHA-2 revises the construction and the big-length of the signature f...
-
Contest Link: [https://www.e-olymp.com/en/contests/19775](https://www.e-olymp.com/en/contests/19775) Full Solution: [https://github.com/...
No comments:
Post a Comment