Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

441 řádky
10 KiB

  1. # Makefile --- 10sr make dotfiles
  2. # 2014, 10sr. Unlicensed <http://unlicense.org>
  3. # TODO: Add help message
  4. # Variable definitions
  5. dotfiles_url_base := https://raw.githubusercontent.com/10sr/dotfiles/master
  6. use_git ?= t
  7. git_auth ?= t
  8. dotfiles_git_path := 10sr/dotfiles.git
  9. ifneq (,$(git_auth))
  10. dotfiles_git := git@github.com:$(dotfiles_git_path)
  11. else
  12. $(warning 'git_auth' is empty. Use public read-only git repository.)
  13. dotfiles_git := http://github.com/$(dotfiles_git_path)
  14. endif
  15. current_origin_url := $(shell git config remote.origin.url)
  16. ifeq (,$(dotfiles_dir))
  17. ifneq (,$(findstring $(dotfiles_git_path),$(current_origin_url)))
  18. $(warning Currently in dotfiles repository)
  19. dotfiles_dir := $(PWD)
  20. else
  21. # Current directory is not 10sr/dotfiles.git reposioty
  22. ifeq (,$(DOTFILES_DIR))
  23. $(warning Neigher DOTFILES_DIR nor dotfiles_dir is defined)
  24. $(warning Use default value)
  25. dotfiles_dir := $(HOME)/10sr_dotfiles
  26. else
  27. # dotfiles_dir is empty but DOTFILES_DIR has a value
  28. $(warning dotfiles_dir is set from DOTFILES_DIR)
  29. dotfiles_dir := $(DOTFILES_DIR)
  30. endif
  31. endif
  32. endif
  33. $(warning dotfiles_dir: $(dotfiles_dir))
  34. dotfiles_home_dir := $(dotfiles_dir)/.home
  35. ifeq ($(home),)
  36. ifeq ($(global_home),)
  37. $(warning home not set and global_home is empty)
  38. # TODO: What this should be?
  39. home := $(dotfiles_home_dir)
  40. else
  41. home := $(HOME)
  42. endif
  43. endif
  44. $(warning home: $(home))
  45. localdir := $(home)/.local
  46. vardir := $(home)/.var
  47. bindir := $(localdir)/bin
  48. directories := $(dotfiles_dir) $(home) $(localdir) $(vardir) $(bindir) \
  49. $(home)/.emacs.d
  50. current := $(shell date)
  51. uname := $(shell uname)
  52. shrc_loadables := sh bash zsh
  53. emacs ?= $(shell which emacs 2>/dev/null)
  54. git ?= $(shell which git 2>/dev/null)
  55. curl ?= $(shell which curl 2>/dev/null)
  56. grep ?= GREP_OPTIONS= $(shell which grep 2>/dev/null)
  57. files := Makefile emacs.el profile shrc tmux.conf vimrc _keysnail.js
  58. # Targets
  59. all: default
  60. tests := test-el
  61. test: test-syntax $(tests)
  62. test_syntaxes := test-syntax-el test-syntax-sh
  63. test-syntax: $(test_syntaxes)
  64. setups := setup-darwin setup-directories setup-emacs setup-gitconf \
  65. setup-repository setup-util setup-rc
  66. # `make setup` to setup these all sounds to be too match
  67. setup-all: $(setups)
  68. runs := run-emacs run-bash run-zsh
  69. # `make check` is just an alias for `make test`
  70. check: test
  71. # Similarly, check-syntax is test-syntax
  72. check-syntax: test-syntax
  73. .PHONY: all default clean \
  74. test check $(tests) \
  75. test-syntax check-syntax $(test_syntaxes)\
  76. setup-all $(setups)
  77. $(directories):
  78. test -d "$@" || mkdir -vp "$@"
  79. # System detection
  80. # ================
  81. # Is this usefull? Just checking uname is not enough?
  82. ismsys :=
  83. iscygwin :=
  84. iswindows :=
  85. isdarwin :=
  86. isfreebsd :=
  87. isbsd :=
  88. islinux :=
  89. ifneq (,$(findstring MINGW,$(uname)))
  90. ismsys := t
  91. endif
  92. ifneq (,$(findstring CYGWIN,$(uname)))
  93. iscygwin := t
  94. endif
  95. ifneq (,$(ismsys)$(iscygwin))
  96. iswindows := t
  97. endif
  98. ifneq (,$(findstring Darwin,$(uname)))
  99. isdarwin := t
  100. endif
  101. ifneq (,$(findstring FreeBSD,$(uname)))
  102. isfreebsd := t
  103. endif
  104. ifneq (,$(isdarwin)$(isfreebsd))
  105. isbsd := t
  106. endif
  107. ifneq (,$(findstring Linux,$(uname)))
  108. islinux := t
  109. endif
  110. # Clean
  111. # =====
  112. # Usage: Define a target as a dependency for clean and .PHONY
  113. clean: clean-home
  114. .PHONY: clean-home
  115. clean-home:
  116. $(RM) -r $(dotfiles_home_dir)
  117. # preparing files
  118. # ===============
  119. files_fullpath := $(files:%=$(dotfiles_dir)/%)
  120. fetch_files := $(files:%=fetch-%)
  121. .PHONY: $(fetch_files)
  122. $(fetch_files): fetch-%: $(dotfiles_dir)
  123. curl --url $(dotfiles_url_base)/$* --output $@
  124. ifeq (,$(use_git))
  125. $(files_fullpath): $(dotfiles_dir)/%: fetch-%
  126. $(warning 'use_git' is empty. Use curl to fetch files)
  127. else
  128. $(warning 'use_git' is not empty. Use git to retrieve files)
  129. $(files_fullpath): setup-repository
  130. test -f "$@"
  131. endif
  132. # setups
  133. # ======
  134. # setup git repository
  135. # --------------------
  136. setup-repository: $(dotfiles_dir)/.git
  137. $(dotfiles_dir)/.git:
  138. ifeq (,$(git))
  139. false "Git not installed"
  140. endif
  141. $(git) clone $(dotfiles_git) $(dotfiles_dir)
  142. # utils
  143. # -----
  144. setup_utils := colortable16.sh 256colors2.pl pacapt ack-2.12
  145. setup-util: $(setup_utils)
  146. .PHONY: $(setup_utils)
  147. setup_utils_path := $(setup_utils:%=$(bindir)/%)
  148. $(setup_utils): %: $(bindir)/%
  149. $(setup_utils_path): $(bindir)
  150. $(curl) -L --url "$(util_url)" --output "$@"
  151. chmod +x "$@"
  152. colortable16.sh: \
  153. util_url := https://gist.github.com/10sr/6852317/raw/colortable16.sh
  154. 256colors2.pl: util_url := https://gist.github.com/10sr/6852331/raw/256colors2.pl
  155. pacapt: util_url := https://github.com/icy/pacapt/raw/ng/pacapt
  156. ack-2.12: util_url := http://beyondgrep.com/ack-2.12-single-file
  157. # darwin setup
  158. # ------------
  159. setup_darwins := setup-darwin-defaults setup-darwin-daemon
  160. setup-darwin: $(setup_darwins)
  161. .PHONY: $(setup_darwins)
  162. setup-darwin-defaults:
  163. # http://appdrill.net/60641/mac-boot-mute.html
  164. #sudo nvram SystemAudioVolume=%80
  165. # add quit entry in menu
  166. defaults write com.apple.finder QuitMenuItem -bool YES
  167. # show full path on titlebar
  168. defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
  169. # do not show desktop icons
  170. defaults write com.apple.finder CreateDesktop -boolean false
  171. killall Finder
  172. # disable dashboard
  173. #defaults write com.apple.dashboard mcx-disabled -bool YES
  174. setup-darwin-daemon:
  175. test "`launchctl getenv LC_ALL`" = C || sudo launchctl setenv LC_ALL C
  176. if ! (launchctl list | grep com.apple.locate) >/dev/null ;\
  177. then \
  178. sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist ;\
  179. fi
  180. # emacs setup
  181. # -----------
  182. setup-emacs: $(dotfiles_dir)/emacs.el
  183. $(emacs) -q --debug-init --batch --load $< -f my-auto-install-package
  184. # git config setup
  185. # ----------------
  186. ifneq (,$(git))
  187. git_conf := $(git) config --global
  188. endif
  189. xz := $(shell which xz 2>/dev/null)
  190. setup-gitconf:
  191. ifeq (,$(git))
  192. $(warnning "Git program not found")
  193. else
  194. $(git_conf) user.name '10sr'
  195. $(git_conf) user.email '8slashes+git@gmail.com'
  196. $(git_conf) core.autocrlf false
  197. $(git_conf) core.excludesfile '~/.gitignore'
  198. $(git_conf) color.ui auto
  199. $(git_conf) status.relativePaths false
  200. $(git_conf) status.showUntrackedFiles normal
  201. $(git_conf) log.date iso
  202. $(git_conf) push.default current
  203. ifneq (,$(xz))
  204. $(git_conf) tar.txz.command "xz -c"
  205. endif
  206. $(git_conf) alias.graph "log --graph --date-order -C -M --pretty=tformat:\"%C(green)%h%C(reset) %C(white)%ad%C(reset) %C(red)%an%C(reset)%C(yellow)%d%C(reset) %C(white bold)%s%C(reset)\" --date=short -n 499"
  207. $(git_conf) alias.st "status -s -b"
  208. $(git_conf) alias.b "branch"
  209. $(git_conf) alias.sb "show-branch"
  210. $(git_conf) alias.ci "commit --verbose"
  211. $(git_conf) alias.co "checkout"
  212. $(git_conf) alias.cim "commit --verbose -m"
  213. $(git_conf) alias.di "diff --color"
  214. $(git_conf) alias.me "merge --no-ff --stat --verbose"
  215. $(git_conf) alias.ffme "merge --ff-only --stat --verbose"
  216. $(git_conf) alias.gr "grep -n"
  217. $(git_conf) alias.ls "ls-files"
  218. # $(git_conf) alias.ls "ls-files -v --full-name"
  219. # $(git_conf) alias.ls "status -u -s ."
  220. $(git_conf) alias.sl "!sl"
  221. # $(git_conf) alias.my-ls "ls-files | xargs ls"
  222. # $(git_conf) alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  223. $(git_conf) alias.addi "add -i"
  224. # Add patch to index
  225. $(git_conf) alias.ap "apply --cached"
  226. $(git_conf) alias.clean-p "diff --quiet"
  227. $(git_conf) alias.echo-ref "for-each-ref --format='%(refname:short)'"
  228. $(git_conf) alias.todo "grep -nH -E -i 'todo:|note:|fixme:'"
  229. $(git_conf) alias.stashsnap '! gitdir="`git rev-parse --git-dir`" && : >>"$$gitdir"/logs/refs/snapshot && cmt=`git stash create` && test -n "$$cmt" && git update-ref refs/snapshot $$cmt && echo Snapshot created: $$cmt'
  230. $(git_conf) alias.snap '! gitdir="`git rev-parse --git-dir`" && export GIT_INDEX_FILE="$$gitdir"/snapshot.index && cp "$$gitdir"/index "$$GIT_INDEX_FILE" && git add -A && : >>"$$gitdir"/logs/refs/snapshot && git update-ref refs/snapshot $$(git commit-tree $$(git write-tree) -m Snapshot -p HEAD) && git show --stat snapshot'
  231. #$(git_conf) alias.wc "!git ls-files -z | xargs -0 wc"
  232. # $(git_conf) push.default "simple"
  233. ifneq (,$(iswindows))
  234. $(git_conf) core.fileMode false
  235. endif
  236. endif
  237. # setup rc files
  238. # --------------
  239. # Generate load codes from the files themselves.
  240. # Load codes are defined by following SETUP_LOAD: indicator.
  241. # String DOTFILES_DIR in the load codes will be replaced into the value of
  242. # $(dotfiles_dir).
  243. # The load codes are appended to $(topfile).
  244. setup_rcs := setup-rc-vimrc setup-rc-tmux.conf setup-rc-emacs.el
  245. setup-rc: $(setup_rcs)
  246. .PHONY: $(setup_rcs)
  247. setup-rc-vimrc: $(home)/.vimrc
  248. setup-rc-tmux.conf: $(home)/.tmux.conf
  249. setup-rc-emacs.el: $(home)/.emacs.d/init.el
  250. $(home)/.emacs.d/init.el: $(dotfiles_dir)/emacs.el $(home) $(home)/.emacs.d
  251. $(home)/.vimrc: $(dotfiles_dir)/vimrc $(home)
  252. $(home)/.tmux.conf: $(dotfiles_dir)/tmux.conf $(home)
  253. command_extract_setup_load := $(grep) -e 'SETUP_LOAD: ' | \
  254. sed -e 's/^.*SETUP_LOAD: //' -e 's|DOTFILES_DIR|$(dotfiles_dir)|'
  255. setup_rc_marker := ADDED BY 10sr_dotfiles/Makefile
  256. $(home)/.emacs.d/init.el $(home)/.vimrc $(home)/.tmux.conf:
  257. set -x; if ! $(grep) "$(setup_rc_marker)" "$@"; \
  258. then \
  259. (echo '$(line_comment)' $(setup_rc_marker); cat "$<" | $(command_extract_setup_load)) \
  260. | tee -a "$@"; \
  261. fi
  262. $(home)/.emacs.d/init.el: line_comment := ;;
  263. $(home)/.vimrc: line_comment := \"
  264. $(home)/.tmux.conf: line_comment := \#
  265. # run
  266. # ===
  267. run-emacs: $(home)/.emacs.d/init.el
  268. $(emacs) -q \
  269. --eval "(setq user-emacs-directory \"$(home)/.emacs.d/\")" --load "$<"
  270. # test
  271. # ====
  272. test_els := test-el-emacs.el
  273. test-el: $(test_els)
  274. .PHONY: $(test_els)
  275. $(test_els): test-el-%: $(dotfiles_dir)/% $(home)
  276. $(emacs) -Q -batch -f batch-byte-compile $<
  277. EMACS_EL_DRY_RUN=t $(emacs) -q --debug-init --batch \
  278. --eval "(setq debug-on-error t)" \
  279. --eval "(setq user-emacs-directory \"$(home)/.emacs.d/\")" \
  280. --load $<c --kill
  281. # test syntax
  282. # ===========
  283. test_syntax_shs := test-syntax-shrc test-syntax-profile \
  284. test-syntax-xinitrc test-syntax-xprograms
  285. test-syntax-sh: $(test_syntax_shs)
  286. .PHONY: $(test_syntax_shs)
  287. $(test_syntax_shs): test-syntax-%: $(dotfiles_dir)/%
  288. sh -ec 'for sh in $(shrc_loadables); do $$sh -n $<; done'
  289. test_syntax_els := test-syntax-emacs.el
  290. test-syntax-el: $(test_syntax_els)
  291. .PHONY: $(test_syntax_els)
  292. sexp_elisp_syntax_check := \
  293. (with-temp-buffer \
  294. (emacs-lisp-mode) \
  295. (insert-file-contents file) \
  296. (condition-case err \
  297. (check-parens) \
  298. (user-error \
  299. (error (format "%s:%d:%d:Unmatched brancet or quote" \
  300. file \
  301. (line-number-at-pos) \
  302. (- (point) (point-at-bol)))))))
  303. $(test_syntax_els): test-syntax-%: $(dotfiles_dir)/%
  304. $(emacs) -Q --debug-init --batch \
  305. --eval '(let ((file "$<")) $(sexp_elisp_syntax_check))' --kill