Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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