介绍

以下makefile脚本是我用来自动更新博客文章的,因为设计流程、变量、判断和shell执行,比较具有代表性,贴出来以供参考。

脚本

 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
project = lflxp.github.com
# 判断lflxp.github.com文件夹是否存在,要精确匹配
count = $(shell ls ..|grep -w $(project)|wc -l|xargs echo)

# 整体流程: hugo -> git clone/git pull -> copy -> update
.PHONY: clean refresh lflxp.github.com update copy install

install: update

update: copy
	@echo "==> 更新 githubpages 项目 <=="
	rm -rf public; \
	git add .; \
	git commit -m "`date`"; \
	git push origin $(shell git branch|grep '*'|awk '{print $$2}') 

copy: refresh
	@echo "==> 复制public数据到lflxp.github.com并更新 <=="
	cp -r public/* ../lflxp.github.com/ 
	cd ../lflxp.github.com; \
	git add .; \
	git commit -m "`date`"; \
	git push origin $(shell git branch|grep '*'|awk '{print $$2}')

refresh: hugo
	@echo "==> 更新 lflxp.github.com 项目 <=="
	cd ../lflxp.github.com; \
	git pull origin master; \
	rm -rf *

# 判断lflxp.github.com文件夹是否存在 以此来触发不同的操作
ifeq ($(count),1)
hugo: content CNAME Makefile 
	@echo "==> 仅更新数据完毕 <=="
	@echo "==> 生成静态文件public和CNAME配置 <=="
	hugo
	cp CNAME public/CNAME
else
hugo: content CNAME Makefile lflxp.github.com 
	@echo "==> 全量克隆并更新数据完毕 <=="
	@echo "==> 生成静态文件public和CNAME配置 <=="
	hugo
	cp CNAME public/CNAME
endif

lflxp.github.com:
	@echo "==> 克隆 lflxp.github.com项目 <=="
	cd .. && git clone https://github.com/lflxp/lflxp.github.com && pwd && ls

clean:
	@echo 删除public文件夹
	rm -rf public
	rm -f *.tar.gz

.PHONY: check tar.gz pull hugoinstall
hugoinstall:
	go get github.com/gohugoio/hugo
	cd $$GOPATH/src/github.com/gohugoio/hugo; \
	pwd; \
	go install
	
check:
	@echo $(count)
	@echo $(project)
	git status
	git log --pretty=format:\"%an-%h-%s\" -1|xargs echo

pull:
	git pull origin $(shell git branch|grep '*'|awk '{print $$2}') 

tar.gz:
	tar -zcvf githubpages.tar.gz .