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.
 
 
 
 
 
 

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