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.
 
 
 
 
 
 

408 lines
9.5 KiB

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