Class: Appifier::Components::Template

Inherits:
Object
  • Object
show all
Extended by:
Carioca::Injector
Defined in:
lib/appifier/components/template.rb

Constant Summary collapse

IGNORE_LIST =
[".git"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template: nil, path: nil) ⇒ Template

Returns a new instance of Template.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/appifier/components/template.rb', line 14

def initialize(template: nil, path: nil)
  raise "Missing template or path keyword" if template.nil? && path.nil?
  @path = File.expand_path("#{Appifier::DEFAULT_TEMPLATES_PATH}/#{template}") if template
  @path = path if path
  @appifile_name = "#{@path}/Appifile"
  @skeleton_path = "#{@path}/skeleton"
  @readme_path = "#{@path}/README.md"
  begin
    @appifile = Appifier::Components::Appifile::new(path: @appifile_name)
  rescue RuntimeError
    raise "Template format error"
  end
end

Instance Attribute Details

#appifileObject

Returns the value of attribute appifile.



10
11
12
# File 'lib/appifier/components/template.rb', line 10

def appifile
  @appifile
end

#readme_pathObject (readonly)

Returns the value of attribute readme_path.



11
12
13
# File 'lib/appifier/components/template.rb', line 11

def readme_path
  @readme_path
end

#skeleton_pathObject (readonly)

Returns the value of attribute skeleton_path.



11
12
13
# File 'lib/appifier/components/template.rb', line 11

def skeleton_path
  @skeleton_path
end

Class Method Details

.lint(template) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/appifier/components/template.rb', line 112

def self.lint(template)
  begin
    res = Appifier::Components::Template.validate!(template: template)
    output.info "List of warnings for template #{template}"
    res[:warn].each do |warn|
      output.warn warn
    end
  rescue RuntimeError
    raise "Template not found"
  end
end

.listObject



54
55
56
57
58
59
60
# File 'lib/appifier/components/template.rb', line 54

def self.list
  output.info "List of available templates for user : #{current_user} :"
  template_path = File.expand_path(Appifier::DEFAULT_TEMPLATES_PATH)
  Dir.glob("#{template_path}/*").map { |item| item.delete_prefix("#{template_path}/") }.each do |template|
    output.item "#{template}"
  end
end

.rm(template) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/appifier/components/template.rb', line 62

def self.rm(template)
  output.info "Removing template #{template} for user : #{current_user}"
  template_path = File.expand_path(Appifier::DEFAULT_TEMPLATES_PATH)
  begin
    if File::exist? "#{template_path}/#{template}"
      FileUtils.rm_rf "#{template_path}/#{template}"
      output.ok "Template #{template} deleted of bundle for user #{current_user}"
    else
      raise "Template #{template} not found in bundle for user #{current_user}"
    end
  rescue Errno::ENOENT
    raise "Template #{template} not found in bundle for user #{current_user}"
  end
end

.show(template) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/appifier/components/template.rb', line 77

def self.show(template)
  data = Appifier::Components::Template::new(template: template)
  data_template = data.appifile.content[:template]
  output.info "Template #{template} informations"
  output.item "Name : #{data_template[:name]}"
  output.item "Version : #{data_template[:version]}"
  if data_template[:authors]
    output.item "Authors : "
    data_template[:authors].each do |author| 
      output.arrow author
    end
  end
  output.item "Description : #{data_template[:description]}" if data_template[:description]
  output.item "Contact : #{data_template[:contact]}" if data_template[:contact]
  output.info "Dataset informations : "
  data_template[:dataset].each do |rule, definition|
    output.item "Rule : #{rule}"
    output.arrow "Description : #{definition[:description]}"
    output.arrow "Default : #{definition[:default]}" if definition[:default]
    output.arrow "Format : #{definition[:format]}"
  end

end

.treeview(template) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/appifier/components/template.rb', line 101

def self.treeview(template)
  begin
    data = Appifier::Components::Template::new(template: template)
    output.info "Getting tree view for template #{template} : "
    tree = TTY::Tree.new(data.skeleton_path)
    puts tree.render
  rescue RuntimeError
    raise "Template not found"
  end
end

.validate!(template: nil, path: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/appifier/components/template.rb', line 32

def self.validate!(template: nil, path: nil)
  raise "Missing template or path keyword" if template.nil? && path.nil?
  path = File.expand_path("#{Appifier::DEFAULT_TEMPLATES_PATH}/#{template}") if template
  path = path if path
  appifile_name = "#{path}/Appifile"
  
  result = Appifier::Components::Appifile.validate!(path: appifile_name)
  IGNORE_LIST.each do |item|
    item_path = "#{path}/#{item}"
    if File.exist? item_path
      FileUtils::rm_rf item_path 
      result[:cleaned].push item
    end
  end unless result.include? :error
  result[:error].push "Skeleton path missing" unless File.exist?("#{path}/skeleton")
  result[:warn].push "README.md missing" unless File.exist?("#{path}/README.md")
  result[:status] = :ok
  result[:status] = :partial unless result[:warn].empty?
  result[:status] = :ko unless result[:error].empty?
  return result
end

Instance Method Details

#readme?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/appifier/components/template.rb', line 28

def readme?
  File.exist? @readme_path
end