aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Makefile
blob: 0076c9ef91a3b42be3f0228206322578d8038df5 (plain) (blame)
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
MAKEFLAGS := --no-builtin-rules --no-builtin-variables --warn-undefined-variables
unexport MAKEFLAGS
.DEFAULT_GOAL := all
.DELETE_ON_ERROR:
.SUFFIXES:
SHELL := bash
# Careful, -eu doesn't work with chruby, I think.
.SHELLFLAGS := -e -o pipefail -c

.PHONY: DO
DO:

escape = $(subst ','\'',$(1))

define noexpand
ifeq ($$(origin $(1)),environment)
    $(1) := $$(value $(1))
endif
ifeq ($$(origin $(1)),environment override)
    $(1) := $$(value $(1))
endif
ifeq ($$(origin $(1)),command line)
    override $(1) := $$(value $(1))
endif
endef

empty :=
space := $(empty) $(empty)
comma := ,

makefile_dir := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))

# Jekyll project is in the parent directory by default.
PROJECT_DIR ?= $(abspath $(dir $(makefile_dir))/..)
RUBY_INSTALL_VERSION ?= 0.8.5
RUBY_VERSION ?= 3.1.2
CHRUBY_VERSION ?= 0.3.9
PREFIX ?= $(HOME)/.local

$(eval $(call noexpand,PROJECT_DIR))
$(eval $(call noexpand,RUBY_INSTALL_VERSION))
$(eval $(call noexpand,RUBY_VERSION))
$(eval $(call noexpand,CHRUBY_VERSION))
$(eval $(call noexpand,PREFIX))

ruby_install_url := https://github.com/postmodern/ruby-install/archive/v$(RUBY_INSTALL_VERSION).tar.gz
ruby_install_archive := $(makefile_dir)/ruby-install-$(RUBY_INSTALL_VERSION).tar.gz
ruby_install_dir := $(makefile_dir)/ruby-install-$(RUBY_INSTALL_VERSION)

chruby_url := https://github.com/postmodern/chruby/archive/v$(CHRUBY_VERSION).tar.gz
chruby_archive := $(makefile_dir)/chruby-$(CHRUBY_VERSION).tar.gz
chruby_dir := $(makefile_dir)/chruby-$(CHRUBY_VERSION)

chruby_sh := $(PREFIX)/share/chruby/chruby.sh
auto_sh := $(PREFIX)/share/chruby/auto.sh

.PHONY: all
all: jekyll/serve

.PHONY: ruby-install
ruby-install:
	wget -O '$(call escape,$(ruby_install_archive))' '$(call escape,$(ruby_install_url))'
	tar -xzvf '$(call escape,$(ruby_install_archive))' -C '$(call escape,$(makefile_dir))'
	cd -- '$(call escape,$(ruby_install_dir))' && make install 'PREFIX=$(call escape,$(PREFIX))'

.PHONY: ruby-install/uninstall
ruby-install/uninstall:
	cd -- '$(call escape,$(ruby_install_dir))' && make uninstall 'PREFIX=$(call escape,$(PREFIX))'

.PHONY: ruby-install/clean
ruby-install/clean:
	rm -rf -- '$(call escape,$(ruby_install_archive))' '$(call escape,$(ruby_install_dir))'

.PHONY: ruby
ruby:
	ruby-install -j2 --cleanup ruby '$(call escape,$(RUBY_VERSION))'

.PHONY: chruby
chruby:
	wget -O '$(call escape,$(chruby_archive))' '$(call escape,$(chruby_url))'
	tar -xzvf '$(call escape,$(chruby_archive))' -C '$(call escape,$(makefile_dir))'
	cd -- '$(call escape,$(chruby_dir))' && $(MAKE) install 'PREFIX=$(call escape,$(PREFIX))'

.PHONY: chruby/uninstall
chruby/uninstall:
	cd -- '$(call escape,$(chruby_dir))' && $(MAKE) uninstall 'PREFIX=$(call escape,$(PREFIX))'

.PHONY: chruby/clean
chruby/clean:
	rm -rf -- '$(call escape,$(chruby_archive))' '$(call escape,$(chruby_dir))'

define chruby_source
if [ -n "$$BASH_VERSION" ] || [ -n "$$ZSH_VERSION" ]; then
    [ -r '$(call escape,$(chruby_sh))' ] && source '$(call escape,$(chruby_sh))'
    [ -r '$(call escape,$(auto_sh))'   ] && source '$(call escape,$(auto_sh))'
fi
endef
export chruby_source

.PHONY: chruby/.bashrc
chruby/.bashrc:
	echo "$$chruby_source" >> ~/.bashrc

.PHONY: chruby/profile.d
chruby/profile.d:
	echo "$$chruby_source" > /etc/profile.d/chruby.sh

.PHONY: chruby/profile.d/clean
chruby/profile.d/clean:
	rm -f -- /etc/profile.d/chruby.sh

chruby := . '$(call escape,$(chruby_sh))' && chruby 'ruby-$(call escape,$(RUBY_VERSION))'
project_chruby := cd -- '$(call escape,$(PROJECT_DIR))' && $(chruby)

bundle := $(project_chruby) && bundle

.PHONY: bundler
bundler:
	$(chruby) && gem install --norc bundler

.PHONY: dependencies
dependencies:
	$(bundle) install --jobs=2 --retry=3

.PHONY: dependencies/update
dependencies/update:
	$(bundle) update --jobs=2

.PHONY: deps
deps: dependencies

.PHONY: deps/update
deps/update: dependencies/update

# List of --config files in alphabetical order.
jekyll_configs := $(shell cd -- '$(call escape,$(PROJECT_DIR))' && find . -mindepth 1 -maxdepth 1 -type f -name '_config*.yml' -print | sort)
jekyll_configs := $(subst $(space),$(comma),$(jekyll_configs))

jekyll_opts := --drafts --config '$(call escape,$(jekyll_configs))'
jekyll := $(bundle) exec jekyll

.PHONY: jekyll/build
jekyll/build:
	$(jekyll) build $(jekyll_opts)

.PHONY: jekyll/serve
jekyll/serve:
	$(jekyll) serve $(jekyll_opts) --host 0.0.0.0

JEKYLL_UID ?= $(shell id -u)
JEKYLL_GID ?= $(shell id -g)
$(eval $(call noexpand,JEKYLL_UID))
$(eval $(call noexpand,JEKYLL_GID))
export JEKYLL_UID JEKYLL_GID

docker_compose := cd -- '$(call escape,$(makefile_dir))' && PROJECT_DIR='$(call escape,$(abspath $(PROJECT_DIR)))' docker-compose

.PHONY: docker/build
docker/build:
	$(docker_compose) build --force-rm --build-arg 'JEKYLL_UID=$(call escape,$(JEKYLL_UID))' --build-arg 'JEKYLL_GID=$(call escape,$(JEKYLL_GID))'

.PHONY: docker/up
docker/up:
	$(docker_compose) up -d project

.PHONY: docker/logs
docker/logs:
	$(docker_compose) logs

.PHONY: docker/down
docker/down:
	$(docker_compose) down -v

.PHONY: docker/shell
docker/shell:
	$(docker_compose) exec project bash --login

.PHONY: docker/clean
docker/clean:
	docker system prune -a -f --volumes