如何在常规任务中构建文件和目录 Rake 任务?
我想在db/
目录中生成文件my.db
。我不熟悉如何在常规任务
中构建文件
和目录
任务。帮助!
task :create, [:name, :type] do |t, args|
args.with_defaults(:name => "mydb", :type => "mysql")
directory "db"
file "db/my.db" => "db" do
sh "echo 'Hello db' > db/my.db"
end
puts "Create a '#{args.type}' database called '#{args.name}'"
end
I want to generate the file my.db
in the db/
directory. I'm unfamiliar with how to structure the file
and directory
tasks within a regular task
. Help!
task :create, [:name, :type] do |t, args|
args.with_defaults(:name => "mydb", :type => "mysql")
directory "db"
file "db/my.db" => "db" do
sh "echo 'Hello db' > db/my.db"
end
puts "Create a '#{args.type}' database called '#{args.name}'"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下代码将创建数据库和文件,除非已经存在...
如果您希望在单个 rake 任务中使用命令,则可以使用此代码
如果您希望使用文件任务由 rake 提供,您需要执行此操作...
在这个示例中,您实际上告诉 rake 创建名为“db”和“db/my.db”的任务,这些任务具有创建目录或文件的副作用。
希望这会有所帮助,对于最初的困惑表示歉意:)
The following code will create the db and file unless the already exist...
You can use this if you wish to have the commands in a single rake task
If you wish to use the file task provided by rake you would need to do this...
In this example your actually telling rake to create tasks called "db" and "db/my.db" which have the side effect of creating the directory or file.
Hope this helps, sorry for the initial confusion :)