在Windows上使用自定义命令, 在 Golang 项目下生成 make 命令使用的 Makefile。
gmk.cmd
在已经加入环境变量PATH的目录 (比如自定义的 C:\bin)中, 创建 gmk.cmd,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | @REM @Author: suifengtec @REM @Date: 2018-12-17 13:06:58 @REM @Last Modified by: suifengtec @REM Modified time: 2018-12-17 13:26:43 @echo off ( echo # Makefile for go project. echo # v20181217 echo .PHONY: clean build tool lint help run fresh init sync list echo targetName=a.exe echo all: build echo init: echo @govendor init echo sync: echo @govendor sync echo list: echo @govendor list echo fresh: echo @fresh echo build: echo @go build -o ${targetName} -v . echo tool: echo go vet ./...; true echo gofmt -w . echo run: echo ./${targetName} echo lint: echo golint ./... echo clean: echo #rm -f ${targetName} echo go clean -i . echo help: echo @echo "make init: 初始化govendor" echo @echo "make sync: govendor sync" echo @echo "make list: govendor list" echo @echo "make: 编译包和依赖为可执行文件" echo @echo "make build: 同上" echo @echo "make run: 运行make生成的可执行文件" echo @echo "make fresh: 生成可执行文件并运行" echo @echo "make tool: 运行特定的go工具" echo @echo "make lint: golint ./..." echo @echo "make clean: 移除缓存文件" )>Makefile |
使用
在 Golang 项目的根目录下执行:
1 | gmk |
可生成 make 使用的 Makefile 文件,内容大概是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # Makefile for go project. # v20181217 .PHONY: clean build tool lint help run fresh init sync list targetName=a.exe all: build init: @govendor init sync: @govendor sync list: @govendor list fresh: @fresh build: @go build -o ${targetName} -v . tool: go vet ./...; true gofmt -w . run: ./${targetName} lint: golint ./... clean: #rm -f ${targetName} go clean -i . help: @echo "make init: 初始化govendor" @echo "make sync: govendor sync" @echo "make list: govendor list" @echo "make: 编译包和依赖为可执行文件" @echo "make build: 同上" @echo "make run: 运行make生成的可执行文件" @echo "make fresh: 生成可执行文件并运行" @echo "make tool: 运行特定的go工具" @echo "make lint: golint ./..." @echo "make clean: 移除缓存文件" |
然后就可以在该项目根目录下使用自定义的make 命令了。
Windows 虽然有点儿那啥,但是还暂时离不开要用它,所以有了这么蹩脚的主意。