Blog

Blog

PHODAL

一个python程序员的ruby三日游(三)——构建工具

在构建上,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初步

安装Rake

gem install rake

不过通常这个是已经有了,在那之前建议用RVM进行Ruby的版本管理。

RVM简介

>Ruby Version Manager,Ruby版本管理器,包括Ruby的版本管理和Gem库管理(gemset)。目前支持Ruby的大多数版本,有 1.8.7,1.9.1,1.9.2和Ruby Enterprise Editon,通过RVM可以很方便的在多个Ruby版本中快速切换。RVM同时 也支持JRuby。

RVM安装

curl -L https://get.rvm.io | bash -s stable

剩下部分可以参考网上写的指南大抵就是:

列出已知的ruby版本

rvm list known
#安装一个ruby版本
rvm install 1.9.3
#这里安装了最新的1.9.3, rvm list known列表里面的都可以拿来安装。
#使用一个ruby版本
rvm use 1.9.3
#如果想设置为默认版本,可以这样
rvm use 1.9.3 --default

简单的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

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签