一个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 社区中广泛使用的成熟系统。(转载保留 Phodal's Blog Phodal's  zenthink
Rake 是用 Ruby 编写的,并使用 Ruby 作为它的语法,因此学习曲线很短。Rake 使用 Ruby 的元编程功能来扩展语言,使之更利落地适应自动化任务。Rake 附带的 rdoc 中列出了一些优点(请注意,前两个是诸如 make 的其他任务自动化工具所共有的):

  1. 用户可以用先决条件指定任务。
  2. Rake 支持规则模式来合并隐式任务。
  3. 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<stdio.h>

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值