Files
gitlabhq/lib/gitlab/template/base_template.rb
T
Z.J. van de WegandAlfredo Sumaran 620d014aef Implement backend gitlab ci dropdown
This commit builds on the groundwork in
ee008e300b1ec0abcc90e6a30816ec0754cea0dd, which refactored the backend
so the same code could be used for new dropdowns. In this commit its
used for templates for the `.gitlab-ci.yml` files.
2016-06-20 14:48:28 -05:00

68 lines
1.5 KiB
Ruby

module Gitlab
module Template
class BaseTemplate
def initialize(path)
@path = path
end
def name
File.basename(@path, self.class.extension)
end
def content
File.read(@path)
end
def categories
raise NotImplementedError
end
def extension
raise NotImplementedError
end
def base_dir
raise NotImplementedError
end
class << self
def all
self.categories.keys.flat_map { |cat| by_category(cat) }
end
def find(key)
file_name = "#{key}#{self.extension}"
directory = select_directory(file_name)
directory ? new(File.join(category_directory(directory), file_name)) : nil
end
def by_category(category)
templates_for_directory(category_directory(category))
end
def category_directory(category)
File.join(base_dir, categories[category])
end
private
def select_directory(file_name)
categories.keys.find do |category|
File.exist?(File.join(category_directory(category), file_name))
end
end
def templates_for_directory(dir)
dir << '/' unless dir.end_with?('/')
Dir.glob(File.join(dir, "*#{self.extension}")).select { |f| f =~ filter_regex }.map { |f| new(f) }
end
def filter_regex
@filter_reges ||= /#{Regexp.escape(extension)}\z/
end
end
end
end
end