一懒到底

今天在用rebar编程的时候,觉得每次都要rebar compile实在太烦,觉
得应该懒一些,就弄了一个快捷的方式,用了一下,觉得不错,以后自己
的每个项目里面都应该弄一个类似的配置文件,文件内容很简单:

       #!/bin/bash

alias m='./rebar compile'
alias r='rel/test_proj/bin/test_proj console'
alias e='cat < rel/test_proj/log/sasl-error.log'
alias c='git commit -m'
alias p='git push'
       

使用的时候,只用运行

       . ./env.sh
       

然后就可以通过m来编译,c来提交,r来运行等等等等了。

突然想起emacs的配置也更新了好多,但都没有提交,就顺便提交下吧。
检查一下,添加了很多文件,有些还是按照子模块的方式添加,每次都要
看每个如何编译实在太麻烦,既然今天要懒一次,就决定将这个也优化下,
变成一个可以自动化的内容。

首先是按照makefile的方式,因为反正配置是作为一个项目存放的,而
自己又离不开shell的环境,觉得应该哪个都有,以下是内容:

       EMACS ?= emacs
PWD = $(shell pwd)
PRE ?= $(PWD)
ELFILES = $(shell find . -type f -name \*.el ! -path '*/.*/*' -exec echo $(PWD)/{} \;)
ELCFILES = $(ELFILES:.el=.elc)
LIBS = $(shell find . -type d ! -path '*/.*/*')
.PHONY: all compile clean

all: ready compile

clean:
	find . -name \*.elc -exec rm -rf {} \;

ready:
	@echo "">env.el
	@for i in $(LIBS); do echo "(add-to-list 'load-path \"$(PRE)/$$i\")" >> env.el; done
	@$(EMACS) -Q --batch --no-site-file -f batch-byte-compile env.el

compile: $(ELCFILES)
	rm -rf env.el env.elc
$(ELCFILES): %.elc: %.el
	@$(EMACS) -l env.elc --batch --no-site-file -f batch-byte-compile $< || (echo "error" $<)

       

使用的方式也很简单,检出配置只有,make即可,在windows下使用
windows下的emacs.exe的时候,需要添加下PRE=C:/work/emacs_config的
类似定义,原因在于windows下的emacs.exe不能识别cygwin或者msys的
/cygdrive/c或者/c的路径方式。

事情本来到这里就结束了,结果老婆和我换电脑用下,我是编辑远程文
件,用哪个电脑无所谓的,结果安装了git之后,检出了配置,然后在git
的bash shell中运行make,发现竟然没有make,算了,还是写个shell的版
本好通用些:

       #!/bin/bash
emacs=$EMACS
echo "emacs=$emacs"
if [ -z "$PRE" ]; then
PRE=$PWD
fi
echo $PRE
ELFILES=`find . -type f -name \*.el ! -path '*/.*/*' -exec echo $PWD/{} \;`
LIBS=`find . -type d ! -path '*/.*'`
echo "">env.el
for i in $LIBS; do echo "(add-to-list 'load-path \"$PRE/$i\")" >> env.el; done
$emacs -Q --batch --no-site-file -f batch-byte-compile env.el
for i in $ELFILES
do
    $emacs -l env.elc --batch --no-site-file -f batch-byte-compile $i || (echo "error" $i)
done

       

顺便写个clean.sh:

       #!/bin/sh

find . -type f -name \*.elc -exec rm -rf {} \;
       

现在终于对环境的依赖几乎最小了。要获取配置,只需要安装git,然
后检出配置后,在git自带的shell中执行install.sh即可。

需要注意的内容:

1. 上述的编译,即使文件出现错误也会继续后续的编译,不会出现因
为编译错误中断,之所以这样设置是由于一些插件里面有平台的限制或者
判断,脚本只是尽可能的编译每一个脚本。

2. 一般情况下,我喜欢makefile的方式,因为如果文件没有发生更改
的话,makefile是不会再重新编译的,而在shell的方式下,是不理会这些
的。

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据