-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
245 lines (210 loc) · 8.66 KB
/
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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
.DEFALT_GOAL = all
LATEST_VERSION := $(shell cat ./VERSIONS/VALD_LATEST_VERSION)
SUPPORT_VERSION := $(shell cat ./VERSIONS/VALD_SUPPORT_VERSION)
GO_VERSION := $(shell cat ./VERSIONS/GO_VERSION)
TARGET_VER = ""
TARGET_TAG := main
SYNC_REPO_PATH := vald
ORIGINAL_DOCS := $(eval ORIGINAL_DOCS:=$(shell find tmp/${SYNC_REPO_PATH}/docs -type f -name "*.md" 2>/dev/null ))$(ORIGINAL_DOCS)
ROOT_DOC_FILES := $(ORIGINAL_DOCS:tmp/${SYNC_REPO_PATH}/docs/%.md=content/docs/%.md)
VERSION_DOC_FILES = $(ORIGINAL_DOCS:tmp/${SYNC_REPO_PATH}/docs/%.md=content/docs/v$(TARGET_TAG)/%.md)
.PHONY: all
all: init \
version/sync \
repo/sync \
update/docs \
checkout/changes \
clean
.PHONY: clean
clean:
@rm tmp -rf
init: \
subup
@go mod tidy
@go mod download
@if [ -z $(shell which mage) ]; then \
echo "\e[1;32minstall mage\e[0m" ; \
mkdir -p tmp ; \
cd tmp ; \
git clone https://github.com/magefile/mage ; \
cd mage ; \
go run bootstrap.go ; \
else \
echo "\e[1;33mmage is already installed\e[0m" ; \
fi
.PHONY: run
run:
hugo server -D --bind 0.0.0.0
.PHONY: subup
subup:
git submodule foreach git pull origin gh-pages
.PHONY: version/latest version/support version/tag version/sync version/go
version/latest:
@echo $(LATEST_VERSION)
version/support:
@echo $(SUPPORT_VERSION)
version/go:
@echo $(GO_VERSION)
version/sync:
@echo "\e[1;32mChecking Vald latest version and Go version...\e[0m"
@mage -d ./magefile SyncVersion
.PHONY: build/stage build/prod
build/stage:
@hugo --environment=staging -D --minify
@cd tmp_pre && cp -r * ../preview/
@cp Makefile preview/Makefile
build/prod:
@hugo --environment=production --minify
@cp tmp_pre/404.html tmp_pre/ipfs-404.html
@cd tmp_pre && cp -r * ../public/
.PHONY: deploy/stage deploy/prod
deploy/stage: subup \
build/stage
@cd preview && git add -A;git commit -m ":arrow_up: v`${LATEST_VERSION}` `date`" && git push origin gh-pages
@rm -rf tmp_pre
deploy/prod: subup \
build/production
@cd public && git add -A;git commit -m ":arrow_up: v`${LATEST_VERSION}` `date`" && git push origin gh-pages
@rm -rf tmp_pre
.PHONY: repo/sync
repo/sync:
@if [ $(TARGET_VER) = "" ]; then \
$(eval TARGET_VER = main) \
echo $(TARGET_VER) ; \
fi
@echo "\e[1;33mGet original document files: tag=$(TARGET_VER)...\e[0m"
@mage -d ./magefile SyncRepo $(TARGET_VER) $(SYNC_REPO_PATH)
@mkdir -p tmp/$(SYNC_REPO_PATH)/docs/contributing
@cp tmp/$(SYNC_REPO_PATH)/CONTRIBUTING.md tmp/$(SYNC_REPO_PATH)/docs/contributing/contributing-guide.md
@mkdir -p tmp/${SYNC_REPO_PATH}/docs/release
@cp tmp/$(SYNC_REPO_PATH)/CHANGELOG.md tmp/$(SYNC_REPO_PATH)/docs/release/changelog.md
.PHONY: update/docs
update/docs:
@if [ $(TARGET_VER) != "" ] && [ $(TARGET_VER) != "main" ]; then \
$(eval TARGET_TAG := $(subst $() ,.,$(wordlist 1,2,$(subst ., ,$(TARGET_VER))))) \
make TARGET_VER=$(TARGET_VER) TARGET_TAG=$(TARGET_TAG) update/docs/tag ; \
else \
make update/docs/root ; \
fi
# update root documents
.PHONY: update/docs/root
update/docs/root: \
contents/prepare/root \
contents/update/root \
contents/publish/root
# update versionning documents
.PHONY: update/docs/tag
update/docs/tag: \
contents/prepare/tag \
contents/update/tag \
contents/publish/tag
# remove target file at once for creating content file by `hugo new` command.
.PHONY: contents/prepare/root contents/prepare/tag
contents/prepare/root:
@echo "\e[1;33mPrepare create root document files...\e[0m"
@if [ -z $(find content/docs -maxdepth 1 -type d | egrep -v "^v{1}+") ]; then \
echo "\e[1;33mRemove current latest docs...\e[0m" ; \
cd content/docs && ls | egrep -v "^v{1}+" | xargs rm -rf ; \
fi
contents/prepare/tag:
@echo "\e[1;33mPrepare create version document files...\e[0m"
@if [ -z $(find content/docs -maxdepth 1 -type d | egrep -v "^v{1}+") ]; then \
echo "\e[1;33mRemove current version docs...\e[0m" ; \
cd content/docs && ls | egrep "^v$(TARGET_TAG)" | xargs rm -rf ; \
fi
.PHONY: $(ROOT_DOC_FILES)
$(ROOT_DOC_FILES): \
$(ORIGINAL_DOCS) \
archetypes/default.md
@echo "\e[1;33mcreate/update root file: $@\e[0m"
$(call create-content-file,$@)
@cat $(patsubst content/docs/%.md,tmp/${SYNC_REPO_PATH}/docs/%.md,$@) >> $@
.PHONY: $(VERSION_DOC_FILES)
$(VERSION_DOC_FILES): \
$(ORIGINAL_DOCS) \
archetypes/default.md
@echo "\e[1;33mcreate/update v$(TARGET_TAG) file: $@\e[0m"
$(call create-content-file,$@)
@cat $(patsubst content/docs/v$(TARGET_TAG)/%.md,tmp/$(SYNC_REPO_PATH)/docs/%.md,$@) >> $@
# contents/update/root or tag updates each content files
# It includes updating document, fixing document link path and fixing image file path for each target content.
.PHONY: contents/update/root contents/update/tag
contents/update/root: \
$(ROOT_DOC_FILES)
@echo "\e[1;33mcreate/update index files...\e[0m"
@$(eval DIR := $(shell find content/docs -maxdepth 3 -type d | egrep -v 'v[0-9]' | egrep "content/docs/"))
$(foreach dir,$(DIR),$(call create-index-file,$(dir)))
@echo "\e[1;33msync image files...\e[0m"
@cp -r tmp/${SYNC_REPO_PATH}/assets/docs/. ./static/images/ && find ./static/images -type f -name "*.drawio" | xargs rm
@echo "\e[1;33mfix document path...\e[0m"
@find content/docs -type f -name "*.md" -not -path "content/docs/v*" | xargs -I{} mage -d ./magefile ConvertLinks {} $(TARGET_TAG)
@echo "\e[1;33mset metadata...\e[0m"
@find content/docs -type f -name "*.md" -not -path "content/docs/v*" | xargs -I{} mage -d ./magefile UpdateMetadata {}
@echo "\e[1;33membed version...\e[0m"
@find content/docs -type f -name "*.md" -not -path "content/docs/v*" | xargs -I{} mage -d ./magefile EmbedVersion {} $(TARGET_TAG) $(TARGET_VER)
contents/update/tag: \
$(VERSION_DOC_FILES)
@echo "\e[1;33mcreate/update version index files...\e[0m"
@$(eval DIR := $(shell find content/docs/v$(TARGET_TAG) -maxdepth 3 -type d | egrep "content/docs/v$(TARGET_TAG)/"))
$(foreach dir,$(DIR),$(call create-index-file,$(dir)))
@echo "\e[1;33mcreate v$(TARGET_TAG) top index file...\e[0m"
@if [ ! -e "content/docs/v$(TARGET_TAG)/_index.md" ]; then \
hugo new --kind version-top "content/docs/v$(TARGET_TAG)/index" >/dev/null ; \
mv content/docs/v$(TARGET_TAG)/index/index.md content/docs/v$(TARGET_TAG)/_index.md ; \
rm -rf content/docs/v$(TARGET_TAG)/index/ ; \
fi
@echo "\e[1;33msync image files...\e[0m"
@cp -r tmp/${SYNC_REPO_PATH}/assets/docs/. static/images/v${TARGET_TAG}/ && find ./static/images/v${TARGET_TAG} -type f -name "*.drawio" | xargs rm
@echo "\e[1;33mfix document path...\e[0m"
@find content/docs/v$(TARGET_TAG) -type f -name "*.md" | xargs -I{} mage -d ./magefile ConvertLinks {} $(TARGET_TAG)
@echo "\e[1;33mset metadata...\e[0m"
@find content/docs/v$(TARGET_TAG) -type f -name "*.md" | xargs -I{} mage -d ./magefile UpdateMetadata {}
@echo "\e[1;33membed version...\e[0m"
@find content/docs/v$(TARGET_TAG) -type f -name "*.md" | xargs -I{} mage -d ./magefile EmbedVersion {} $(TARGET_TAG) $(TARGET_VER)
# contents/publish/root or tag toggle draft value to false for each contents file.
.PHONY: contents/publish/root contents/publish/tag
contents/publish/root:
@find content/docs -type f -name "*.md" -not -path "content/docs/v*" | xargs -I{} mage -d ./magefile Publish {}
contents/publish/tag:
$(eval TARGET_TAG := $(subst $() ,.,$(wordlist 1,2,$(subst ., ,$(TARGET_VER)))))
@find content/docs/v$(TARGET_TAG) -type f -name "*.md" | xargs -I{} mage -d ./magefile Publish {}
# remove file from the list of git stage file if the content file changes only date of metadata.
.PHONY: checkout/changes
checkout/changes:
@$(eval FILE := $(shell git status | grep "modified:" | grep "content/" | grep -v "content/docs/v1" | awk '{print $$2}'))
$(foreach file,$(FILE),$(call check-diff,$(file)))
@$(eval VERSION_FILE := $(shell git status | grep "modified:" | grep "content/docs/v1" | awk '{print $$2}'))
$(foreach file,$(VERSION_FILE),$(call check-diff,$(file)))
define create-content-file
@mkdir -p $(dir $(1))
@rm -rf $(1)
@if [ -z $(findstring _index.md,$(1)) ]; then \
hugo new $(1) >/dev/null ; \
else \
hugo new --kind index $(1) >/dev/null ; \
fi
endef
define create-index-file
@echo "\e[1;33mcreate/update index file: $(1)\e[0m"
@if [ ! -f "$(1)/_index.md" ]; then \
hugo new --kind directory-top $(1) >/dev/null ; \
mv $(1)/index.md $(1)/_index.md ; \
fi
endef
define set-tag
@if [ $(1) != "" ]; then \
$(eval TARGET_TAG = $(subst $() ,.,$(wordlist 1,2,$(subst ., ,$(1))))) \
echo ${TARGET_TAG} ; \
else \
$(eval TARGET_TAG = main ) \
echo ${TARGET_TAG} ; \
fi
endef
define check-diff
$(eval diffNum := $(shell git diff --numstat $(1) | awk '{print $$1+$$2}'))
$(eval detailDiff := $(shell git diff -U0 $(1) | grep "+title" | awk '{print $$1}'))
@if [ $(diffNum) = "2" ] ; then \
echo "remove changes $(1)" ; \
git checkout $(1) >/dev/null ; \
fi
endef