2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-27 05:00:56 -04:00
|
|
|
module Gitlab
|
|
|
|
module Template
|
|
|
|
class BaseTemplate
|
2018-09-26 06:58:58 -04:00
|
|
|
attr_accessor :category
|
2018-09-05 10:17:29 -04:00
|
|
|
|
|
|
|
def initialize(path, project = nil, category: nil)
|
2016-05-27 05:00:56 -04:00
|
|
|
@path = path
|
2018-09-05 10:17:29 -04:00
|
|
|
@category = category
|
2016-06-24 15:43:46 -04:00
|
|
|
@finder = self.class.finder(project)
|
2016-05-27 05:00:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
File.basename(@path, self.class.extension)
|
|
|
|
end
|
2018-10-02 19:00:38 -04:00
|
|
|
alias_method :key, :name
|
2016-05-27 05:00:56 -04:00
|
|
|
|
2019-04-05 06:18:10 -04:00
|
|
|
def full_name
|
|
|
|
Pathname.new(@path)
|
|
|
|
.relative_path_from(self.class.base_dir)
|
|
|
|
.to_s
|
|
|
|
end
|
|
|
|
|
2016-05-27 05:00:56 -04:00
|
|
|
def content
|
2016-06-24 15:43:46 -04:00
|
|
|
@finder.read(@path)
|
|
|
|
end
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
# Present for compatibility with license templates, which can replace text
|
|
|
|
# like `[fullname]` with a user-specified string. This is a no-op for
|
|
|
|
# other templates
|
|
|
|
def resolve!(_placeholders = {})
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
def to_json
|
2018-10-02 19:00:38 -04:00
|
|
|
{ key: key, name: name, content: content }
|
2016-06-02 12:20:08 -04:00
|
|
|
end
|
|
|
|
|
2017-08-30 02:05:13 -04:00
|
|
|
def <=>(other)
|
|
|
|
name <=> other.name
|
|
|
|
end
|
|
|
|
|
2016-05-27 05:00:56 -04:00
|
|
|
class << self
|
2016-06-24 15:43:46 -04:00
|
|
|
def all(project = nil)
|
|
|
|
if categories.any?
|
|
|
|
categories.keys.flat_map { |cat| by_category(cat, project) }
|
|
|
|
else
|
|
|
|
by_category("", project)
|
|
|
|
end
|
2016-05-27 05:00:56 -04:00
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
def find(key, project = nil)
|
|
|
|
path = self.finder(project).find(key)
|
|
|
|
path.present? ? new(path, project) : nil
|
2016-05-27 05:00:56 -04:00
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
# Set categories as sub directories
|
|
|
|
# Example: { "category_name_1" => "directory_path_1", "category_name_2" => "directory_name_2" }
|
|
|
|
# Default is no category with all files in base dir of each class
|
2016-06-17 02:37:19 -04:00
|
|
|
def categories
|
2016-06-24 15:43:46 -04:00
|
|
|
{}
|
2016-06-17 02:37:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def extension
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_dir
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
# Defines which strategy will be used to get templates files
|
|
|
|
# RepoTemplateFinder - Finds templates on project repository, templates are filtered perproject
|
|
|
|
# GlobalTemplateFinder - Finds templates on gitlab installation source, templates can be used in all projects
|
|
|
|
def finder(project = nil)
|
|
|
|
raise NotImplementedError
|
2016-05-27 05:00:56 -04:00
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
def by_category(category, project = nil)
|
|
|
|
directory = category_directory(category)
|
|
|
|
files = finder(project).list_files_for(directory)
|
|
|
|
|
2018-09-05 10:17:29 -04:00
|
|
|
files.map { |f| new(f, project, category: category) }.sort
|
2016-05-27 05:00:56 -04:00
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
def category_directory(category)
|
|
|
|
return base_dir unless category.present?
|
2016-05-27 05:00:56 -04:00
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
File.join(base_dir, categories[category])
|
2016-05-27 05:00:56 -04:00
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
# If template is organized by category it returns { category_name: [{ name: template_name }, { name: template2_name }] }
|
|
|
|
# If no category is present returns [{ name: template_name }, { name: template2_name}]
|
|
|
|
def dropdown_names(project = nil)
|
|
|
|
return [] if project && !project.repository.exists?
|
2016-05-27 05:00:56 -04:00
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
if categories.any?
|
|
|
|
categories.keys.map do |category|
|
|
|
|
files = self.by_category(category, project)
|
|
|
|
[category, files.map { |t| { name: t.name } }]
|
|
|
|
end.to_h
|
|
|
|
else
|
|
|
|
files = self.all(project)
|
|
|
|
files.map { |t| { name: t.name } }
|
|
|
|
end
|
2016-05-27 05:00:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|