2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module Template
|
|
|
|
class BaseTemplate
|
2018-12-13 13:39:08 +05:30
|
|
|
attr_accessor :category
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
def initialize(path, project = nil, category: nil)
|
2016-06-22 15:30:34 +05:30
|
|
|
@path = path
|
2018-11-20 20:47:30 +05:30
|
|
|
@category = category
|
2016-09-13 17:45:13 +05:30
|
|
|
@finder = self.class.finder(project)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
File.basename(@path, self.class.extension)
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
alias_method :key, :name
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def full_name
|
|
|
|
Pathname.new(@path)
|
|
|
|
.relative_path_from(self.class.base_dir)
|
|
|
|
.to_s
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
def content
|
2016-09-13 17:45:13 +05:30
|
|
|
@finder.read(@path)
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# 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
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def to_json(*)
|
2018-12-05 23:21:45 +05:30
|
|
|
{ key: key, name: name, content: content }
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def <=>(other)
|
|
|
|
name <=> other.name
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
class << self
|
2016-09-13 17:45:13 +05:30
|
|
|
def all(project = nil)
|
|
|
|
if categories.any?
|
|
|
|
categories.keys.flat_map { |cat| by_category(cat, project) }
|
|
|
|
else
|
|
|
|
by_category("", project)
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def find(key, project = nil)
|
|
|
|
path = self.finder(project).find(key)
|
|
|
|
path.present? ? new(path, project) : nil
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
# 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-22 15:30:34 +05:30
|
|
|
def categories
|
2016-09-13 17:45:13 +05:30
|
|
|
{}
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def extension
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_dir
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
# 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-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def by_category(category, project = nil)
|
|
|
|
directory = category_directory(category)
|
|
|
|
files = finder(project).list_files_for(directory)
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
files.map { |f| new(f, project, category: category) }.sort
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def category_directory(category)
|
|
|
|
return base_dir unless category.present?
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
File.join(base_dir, categories[category])
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
# 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-06-22 15:30:34 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
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-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|