BigW Consortium Gitlab

update_templates.rake 1.42 KB
Newer Older
1 2 3
namespace :gitlab do
  desc "GitLab | Update templates"
  task :update_templates do
4
    TEMPLATE_DATA.each { |template| update(template) }
5 6
  end

7 8 9 10 11 12
  def update(template)
    sub_dir = template.repo_url.match(/([a-z-]+)\.git\z/)[1]
    dir = File.join(vendor_directory, sub_dir)

    unless clone_repository(template.repo_url, dir)
      puts "Cloning the #{sub_dir} templates failed".red
13 14 15
      return
    end

16
    remove_unneeded_files(dir, template.cleanup_regex)
17 18 19
    puts "Done".green
  end

20 21
  def clone_repository(url, directory)
    FileUtils.rm_rf(directory) if Dir.exist?(directory)
22

23
    system("git clone #{url} --depth=1 --branch=master #{directory}")
24 25 26 27
  end

  # Retain only certain files:
  # - The LICENSE, because we have to
28 29
  # - The sub dirs so we can organise the file by category
  # - The templates themself
30
  # - Dir.entries returns also the entries '.' and '..'
31
  def remove_unneeded_files(directory, regex)
32 33 34 35 36 37 38
    Dir.foreach(directory) do |file|
      FileUtils.rm_rf(File.join(directory, file)) unless file =~ regex
    end
  end

  private

39
  Template = Struct.new(:repo_url, :cleanup_regex)
Z.J. van de Weg committed
40 41 42 43 44 45 46 47 48 49
  TEMPLATE_DATA = [
    Template.new(
      "https://github.com/github/gitignore.git",
      /(\.{1,2}|LICENSE|Global|\.gitignore)\z/
    ),
    Template.new(
      "https://gitlab.com/gitlab-org/gitlab-ci-yml.git",
      /(\.{1,2}|LICENSE|Pages|\.gitlab-ci.yml)\z/
    )
  ]
50 51 52 53 54

  def vendor_directory
    Rails.root.join('vendor')
  end
end