# 構建篇:Build
構建是一個很大的話題,特別是對于傳統軟件來說,對于Web應用也是相當重要的。
在構建上,Ruby比Python會強大些。 Ruby用的是Rake,Python興許是scons,如果是用于python的話可以用shovel,這個Python就沒有和一個好的標準,
Rakefile算是Ruby的一個標準。
### Rake簡介
> Make 是一個 UNIX? 的本機實用程序,是為管理軟件編譯過程而設計的。它十分通用,足以用于許多其他環境中,即使它已用于將文檔編譯成書,維護 Web 站點以及裁減發行版。但是,make 也有自身的約束。它具有自己的語法,這取決于制表符的(tabbed)和非制表符的(nontabbed)空白空間。許多其他工具已經進行了擴展,可以彌 補 make 的一些不足,如 Aegis 和 Ant,但這兩者也都具有自己的問題。
> Make 以及類似的工具都有改進的余地,但是它們都不可能讓 Ruby 黑客十分開心。您從這里要去哪里?幸好,可以使用一些 Ruby 選項。Rant 是一個由 Stefan Lang 編寫的工具(請參閱 參考資料)。Rant 仍處于開發周期的初級階段,因此它可能還沒有成熟到足以適用于每個人。Jim Weirich 編寫的 Rake 是一個在 Ruby 社區中廣泛使用的成熟系統。
> Rake 是用 Ruby 編寫的,并使用 Ruby 作為它的語法,因此學習曲線很短。Rake 使用 Ruby 的元編程功能來擴展語言,使之更利落地適應自動化任務。Rake 附帶的 rdoc 中列出了一些優點(請注意,前兩個是諸如 make 的其他任務自動化工具所共有的):
- 用戶可以用先決條件指定任務。
- Rake 支持規則模式來合并隱式任務。
- Rake 是輕量級的。它可以用其他項目發布為單個文件。依靠 Rake 的項目不需要在目標系統上安裝 Rake。
### 簡單的Rakefile
~~~
task :default do
puts "Simple Rakefile Example"
end
~~~
運行結果
~~~
Simple Rakefile Example
[Finished in 0.2s]
~~~
### Shovel
官方是這么介紹的
> Shovel is like Rake for python. Turn python functions into tasks simply, and access and invoke them from the command line. ’Nuff said. New Shovel also now has support for invoking the same tasks in the browser you’d normally run from the command line, without any modification to your shovel scripts.
那么就
~~~
git clone https://github.com/seomoz/shovel.git
cd shovel
python setup.py install
~~~
與用官方的示例,有一個foo.py
~~~
from shovel import task
@task
def howdy(times=1):
'''Just prints "Howdy" as many times as requests.
Examples:
shovel foo.howdy 10
http://localhost:3000/foo.howdy?15'''
print('\n'.join(['Howdy'] * int(times)))
~~~
shovel一下 shovel foo.howdy 10
### 構建C語言的Hello,World: Makefile
C代碼
~~~
#include
int main(){
printf("Hello,world\n");
return 0;
}
~~~
一個簡單的makefile示例
~~~
hello:c
gcc hello.c -o hello
clean:
rm hello
~~~
執行:
~~~
make
~~~
就會生成hello的可執行文件,再執行
~~~
make clean
~~~
清理。
### Rakefile
~~~
task :default => :make
file 'hello.o' => 'hello.c' do
`gcc -c hello.c`
end
task :make => 'hello.o' do
`gcc hello.o -o hello`
end
task :clean do
`rm -f *.o hello`
end
~~~
再Rake一下,似乎Ruby中的 Rake用來作構建工具很強大,當然還有其他語言的也可以,旨在可以替代Makefile
### Scons
新建一個SConstruct
~~~
Program('hello.c')
~~~
Program(‘hello.c’)
~~~
scons
~~~
,過程如下
~~~
phodal@linux-dlkp:~/helloworld> scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o hello.o -c hello.c
gcc -o hello hello.o
scons: done building targets.
~~~
總結
~~~
Rakefile
~~~