使用Rake :: PackageTask创建一个zip文件

时间:2015-06-24 13:39:50

标签: ruby zip rake rake-task rakefile

我正在尝试在我的rake文件中打包一个zip文件,而且我对Rake和ruby缺乏经验,所以我很难确定为什么会失败。

这就是我的尝试:

def create_zip_file(targetDirectory, projectToZip)
    puts "target:" + targetDirectory
    puts "Zipping " + projectToZip
    semver_contents = get_contents_of('SEMVER')
    zipFileName = 'ProjectName.' + semver_contents
    puts zipFileName
    files_to_include = "../"+ projectToZip + "/bin/" + BUILD_CONFIG + "/**"
    puts "Including the following files: " + files_to_include
    Rake::PackageTask.new(zipFileName, semver_contents) do |p|
        p.need_zip = true
        p.package_files.include(files_to_include)
    end
end

我的任务调用如下所示:

exec(:package_zip) {create_zip_file DEPLOY_DIRECTORY, 'ProjectName'} 

我这样称呼它:

bundle exec rake package_zip

现在,我运行它的命令窗口列出了我在脚本中的所有put语句的内容,因此我假设发生故障是在PackageTask任务的内部发生的。

控制台中的错误输出如下所示:

Exec failed, see the build log for more details.
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/albacore-1.0.0/lib/albacore/support/failure.rb:12:in `fail_with_message'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/albacore-1.0.0/lib/albacore/exec.rb:18:in `execute'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/albacore-1.0.0/lib/albacore/support/createtask.rb:24:in `block in exec'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
C:/tools/ruby200/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:201:in `block in invoke_prerequisites'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:199:in `each'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:199:in `invoke_prerequisites'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:178:in `block in invoke_with_call_chain'
C:/tools/ruby200/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
C:/tools/ruby200/lib/ruby/gems/2.0.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
C:/tools/ruby200/bin/rake:23:in `load'
C:/tools/ruby200/bin/rake:23:in `<main>'
Tasks: TOP => package => package_zip

可能性是我错过了一些非常明显的东西或做了一些愚蠢的事情,只是很难抓住它。

2 个答案:

答案 0 :(得分:2)

在与办公室周围的几个人交谈之后,尝试了许多与Pauls回答有关的不同事情,我终于找到了一个解决方案,那就是根本不使用套餐任务。相反,我们使用rubyzip。

以下是我们最终的结果

{{1}}

答案 1 :(得分:1)

exec任务告诉rake在命令行/ shell运行。不要执行任务。

你可能想要:

task :package_zip do
    create_zip_file DEPLOY_DIRECTORY, 'ProjectName'
end

你的所有变量也应该是。虽然只是更容易阅读Ruby,但这并不是什么大不了的事。

你真的想看看字符串插值,这将使你的生活变得轻松

所以例如

puts "target: #{target_directory}"
puts "Zipping #{project_to_zip}"
semver_contents = get_contents_of('SEMVER')
zipFileName = "ProjectName.#{semver_contents}"

(提供的代码示例没有运行它,所以要小心!)

啊,还看着docs for PackageTask

运行Rake::PackageTask.new的代码只会创建其他任务 - 它实际上并不运行它们!

article显示了使用示例

    # Have to keep a reference to the PackageTask object
package_task = Rake::PackageTask.new("gliffy-php-client",GLIFFY_VERSION) do |p|
    p.need_tar = true
    p.need_zip = true
    # Executed BEFORE any other tasks; DOC_FILES don't exist  yet
    p.package_files = SRC_FILES + EXAMPLE_FILES
end

file package_task.package_dir_path => DOC_DIR

file DOC_DIR => SRC_FILES + EXAMPLE_FILES do |t|
    system("phpdoc #{PHP_DOC_ARGS}");
    doc_files = FileList.new(DOC_DIR + "/**/**");
    # Have to add these files to the package_task file list
    package_task.package_files = package_task.package_files + doc_files
end

但是,如果你看一下the source of the task,它最终只是使用filelist和zip命令调用shell。如果您知道在运行任务的任何计算机(或安装位置)的路径上都有zip,那么您可能最好只运行以下内容:

task :make_zip do
  semver_contents = get_contents_of('SEMVER')
  zipFileName = "ProjectName.#{semver_contents}"
  targetDir = "../#{projectToZip}/bin/#{BUILD_CONFIG}/**"
 `zip -r #{zipFileName} #{targetDir}`
end