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.
 
 
 
 
 
 

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