Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update_templates.rake « gitlab « tasks « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 36ffad8aae9035b5b8ce20c4b9b8e2d38f41e08e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
namespace :gitlab do
  desc "GitLab | Update templates"
  task :update_templates do
    update("gitignore")
    update("gitlab-ci-yml")
  end

  def update(directory)
    unless clone_repository(directory)
      puts "Cloning the #{directory} templates failed".red
      return
    end

    remove_unneeded_files(directory)
    puts "Done".green
  end

  def clone_repository(directory)
    dir = File.join(vendor_directory, directory)
    FileUtils.rm_rf(dir) if Dir.exist?(dir)
    FileUtils.cd vendor_directory

    system("git clone --depth=1 --branch=master #{TEMPLATE_DATA[directory]}")
  end

  # Retain only certain files:
  # - The LICENSE, because we have to
  # - The sub dir global
  # - The gitignores themself
  # - Dir.entires returns also the entries '.' and '..'
  def remove_unneeded_files(directory)
    regex = CLEANUP_REGEX[directory]
    Dir.foreach(directory) do |file|
      FileUtils.rm_rf(File.join(directory, file)) unless file =~ regex
    end
  end

  private

  TEMPLATE_DATA = {
      "gitignore" => "https://github.com/github/gitignore.git",
      "gitlab-ci-yml" => "https://gitlab.com/gitlab-org/gitlab-ci-yml.git"
  }.freeze

  CLEANUP_REGEX = {
    "gitignore" => /(\.{1,2}|LICENSE|Global|\.gitignore)\z/,
    "gitlab-ci-yml" => /(\.{1,2}|LICENSE|Pages|\.gitignore)\z/
  }.freeze

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

  def gitignore_directory
    File.join(vendor_directory, 'gitignore')
  end

  def gitlab_ci_directory
    File.join(vendor_directory, 'gitlab-ci')
  end
end