-
Notifications
You must be signed in to change notification settings - Fork 2
/
app_make.ps1
142 lines (129 loc) · 4.74 KB
/
app_make.ps1
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Windows PowerShell Script
# Do not use this script directly, it is called automatically by make.ps1
param ($stage)
# $GOPATH = go env GOPATH
# $KRATOS_VERSION_TMP = go mod graph | grep go-kratos/kratos/v2 | head -n 1
# $KRATOS_VERSION = $KRATOS_VERSION_TMP.Substring($KRATOS_VERSION_TMP.LastIndexOf('@') + 1)
# $KRATOS = $GOPATH + '\pkg\mod\github.com\go-kratos\kratos\v2@' + $KRATOS_VERSION
$VERSION = git describe --tags --always
$APP_RELATIVE_PATH = (gi $PWD).Parent.Parent.BaseName + '/' + (basename $PWD)
$APP_BASENAME = basename $APP_RELATIVE_PATH
$INTERNAL_PROTO_FILES = (gci internal -r -fi *.proto).FullName
$API_PROTO_FILES = (gci ..\..\api\$APP_BASENAME -r -fi *.proto).FullName
$APP_NAME = echo $APP_RELATIVE_PATH | sed -E 's|\/|-|g'
$CONTAINER_IMAGE = 'yrcs/{0}:0.1.0' -f (echo $APP_NAME)
switch ($stage) {
# init env
'init' {
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2
go install github.com/envoyproxy/protoc-gen-validate@latest
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
go install github.com/swaggo/swag/cmd/swag
Break
}
# generate internal conf.pb.go
'config' {
protoc --proto_path=$PWD\internal `
--proto_path=..\..\third_party `
--go_out=paths=source_relative:.\internal `
$INTERNAL_PROTO_FILES
Break
}
# generate pagination.pb.go
'page' {
protoc --proto_path=$PSScriptRoot\third_party `
--go_out=paths=source_relative:$PSScriptRoot\third_party `
--validate_out=paths=source_relative,lang=go:$PSScriptRoot\third_party `
$PSScriptRoot\third_party\pagination\pagination.proto
Break
}
# generate api *.pb.go
'api' {
protoc --proto_path=$PSScriptRoot\api\$APP_BASENAME `
--proto_path=$PSScriptRoot\third_party `
--go_out=paths=source_relative:$PSScriptRoot\api\$APP_BASENAME `
--go-errors_out=paths=source_relative:$PSScriptRoot\api\$APP_BASENAME `
--validate_out=paths=source_relative,lang=go:$PSScriptRoot\api\$APP_BASENAME `
--go-grpc_out=paths=source_relative:$PSScriptRoot\api\$APP_BASENAME `
--go-http_out=paths=source_relative:$PSScriptRoot\api\$APP_BASENAME `
$API_PROTO_FILES
swag fmt -d internal/server,$PSScriptRoot/api/$APP_BASENAME/v1 -g http.go
swag init -d internal/server,$PSScriptRoot/api/$APP_BASENAME/v1 --pd -g http.go -o $PSScriptRoot/api/$APP_BASENAME/v1/docs
Break
}
# wire
'wire' {
go mod tidy
go install github.com/google/wire/cmd/wire@latest
cd cmd\$APP_BASENAME
wire
cd ..\..
Break
}
# test
'test' {
go test -v ./... -cover
Break
}
# build
'build' {
if (!(Test-Path -Path bin)) {
mkdir bin
}
go build -ldflags "-X main.Name=$APP_NAME -X main.Version=$VERSION" -o ./bin/ ./...
Write-Host "Project app/$APP_BASENAME has been successfully built."
Write-Host "Use the following commands to start the project:"
Write-Host "$ cd app\$APP_BASENAME"
Write-Host "$ .\bin\$APP_BASENAME -conf ./configs"
Break
}
# generate all
'all' {
.\make.ps1 config
.\make.ps1 api
.\make.ps1 wire
.\make.ps1 test
.\make.ps1 build
Break
}
# build container image (for WSLv2 only)
'container' {
cd ../..
podman build -f deploy/build/Containerfile --build-arg APP_BASENAME=$APP_BASENAME --rm -t $CONTAINER_IMAGE .
Break
}
# show help
'help' {
Write-Host ""
Write-Host "Usage:`r`n .\make.ps1 [target]"
Write-Host ""
Write-Host "Targets:"
Write-Host "init`t`t`t`t" -ForegroundColor DarkGreen -NoNewline
Write-Host "init env"
Write-Host "config`t`t`t`t" -ForegroundColor DarkGreen -NoNewline
Write-Host "generate internal conf.pb.go"
Write-Host "page`t`t`t`t" -ForegroundColor DarkGreen -NoNewline
Write-Host "generate pagination.pb.go"
Write-Host "api`t`t`t`t" -ForegroundColor DarkGreen -NoNewline
Write-Host "generate api *.pb.go and docs"
Write-Host "wire`t`t`t`t" -ForegroundColor DarkGreen -NoNewline
Write-Host "generate wire_gen.go"
Write-Host "test`t`t`t`t" -ForegroundColor DarkGreen -NoNewline
Write-Host "go test"
Write-Host "build`t`t`t`t" -ForegroundColor DarkGreen -NoNewline
Write-Host "go build"
Write-Host "all`t`t`t`t" -ForegroundColor DarkGreen -NoNewline
Write-Host "generate all pb.go, wire, test and build"
Write-Host "container`t`t`t" -ForegroundColor Yellow -NoNewline
Write-Host "build container image (for WSLv2 only)"
Write-Host "help`t`t`t`t" -ForegroundColor DarkGreen -NoNewline
Write-Host "show help"
Break
}
Default {
.\make.ps1 help;
}
}