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

Create a zip file using Rake::PackageTask

我正在尝试在我的 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

现在,我 运行 来自的命令 window 列出了我在脚本中的所有 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

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

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,它最终只是使用文件列表和 zip 命令调用 shell。如果您知道 zip 在任何机器上的路径上 运行ning 您的任务(或它的安装位置),您最好简单地 运行ning 像这样的东西:

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

在与办公室里的几个人交谈后,尝试了很多与 Pauls 答案相关的不同事情,我终于找到了一个解决方案,那就是根本不使用 package 任务。相反,我们使用了 ruby​​zip。

这是我们最终得到的

def create_zip_file(targetDirectory, projectToZip)
    semver_contents = get_contents_of('SEMVER')
    zipFileName = "#{targetDirectory}/#{projectToZip}.#{semver_contents}.zip"
    filesToZip =  FileList["../#{projectToZip}/bin/#{BUILD_CONFIG}/**"]   
    Zip::File.open(zipFileName, Zip::File::CREATE) do |zipFile|
        filesToZip.each do |filename|
            zipFile.add(filename, filename)
        end
    end     
end