You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

411 lines
9.5 KiB

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