Class: Siba::Generator

Inherits:
Object
  • Object
show all
Extended by:
FilePlug, KernelPlug
Includes:
FilePlug, KernelPlug
Defined in:
lib/siba/generator.rb

Constant Summary collapse

PLUGIN_YAML_NAME =
"options.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KernelPlug

siba_kernel, siba_kernel, siba_kernel=

Methods included from FilePlug

siba_file, siba_file, siba_file=

Constructor Details

#initialize(name) ⇒ Generator

Returns a new instance of Generator.



11
12
13
# File 'lib/siba/generator.rb', line 11

def initialize(name)
  @name = String.new name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/siba/generator.rb', line 10

def name
  @name
end

Class Method Details

.get_plugin_user_choice(types) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/siba/generator.rb', line 98

def get_plugin_user_choice(types)
  msg = "\nEnter plugin number from 1 to #{types.size}, or 0 to exit.\n> "
  siba_kernel.printf msg
  while true
    user_choice = siba_kernel.gets.chomp.strip
    number = Integer(user_choice) rescue -1
    if number >= 0 && number <= types.size
      return if number == 0
      return types[number-1]
    else
      siba_kernel.printf msg
    end
  end
end

.get_plugin_yaml_path(category, type) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/siba/generator.rb', line 87

def get_plugin_yaml_path(category, type)
  siba_file.run_this do
    dir = InstalledPlugins.plugin_path(category, type)
    options_path = File.join dir, PLUGIN_YAML_NAME
    unless siba_file.file_file? options_path
      raise Siba::Error, "Failed to load options for #{InstalledPlugins.plugin_category_and_type(category, type)} plugin from file: #{options_path}"
    end
    options_path
  end
end

.load_plugin_yaml_content(category, type) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/siba/generator.rb', line 76

def load_plugin_yaml_content(category, type)
  siba_file.run_this do
    path = get_plugin_yaml_path category, type
    begin
      Siba::OptionsLoader.load_erb path
    rescue Exception => ex
      raise "Failed to load options for #{InstalledPlugins.plugin_category_and_type(category, type)} plugin from file: #{path}. Error: #{ex.message}"
    end
  end
end

Instance Method Details

#generateObject

Generates yaml options file and returns its path



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
62
63
64
65
66
67
68
69
70
# File 'lib/siba/generator.rb', line 16

def generate
  siba_file.run_this do
    file_path = @name.gsub /\.yml$/, ""
    file_path += ".yml"
    file_path = siba_file.file_expand_path file_path
    if siba_file.file_file?(file_path) || siba_file.file_directory?(file_path)
      raise Siba::Error, "Options file already exists: #{file_path}"
    end

    options_data = []
    Siba::Plugins::PLUGINS_HASH.each do |category, types|
      type = nil
      if types.size > 1
        max_type_length = types.keys.max do |a,b|
          a.length <=> b.length
        end.length + 5

        siba_kernel.puts "\nChoose #{category} plugin:"
        types.keys.each_index do |i|
          type = types.keys[i]
          siba_kernel.puts "  #{i+1}. #{Siba::Plugins.plugin_type_and_description(category, type, max_type_length)}"
        end

        type = Siba::Generator.get_plugin_user_choice types.keys
        if type.nil?
          siba_kernel.puts "Cancelled by user"
          return
        end

        unless Siba::InstalledPlugins.installed? category, type
          siba_kernel.puts Siba::InstalledPlugins.install_gem_message(category, type)
          return
        end
      else
        type = types.keys.first
      end

      options = Siba::Generator.load_plugin_yaml_content category, type
      unless options =~ /^\s*type:/
        options = "type: #{type}\n" + options
      end

      options.gsub! /^/, "  "
      options = "#{category}:\n" + options
      options_data << options
    end

    file_data = options_data.join("\n")
    file_data = "# SIBA options file\n" + file_data
    dest_dir = File.dirname file_path
    siba_file.file_utils_mkpath(dest_dir) unless siba_file.file_directory?(dest_dir)
    Siba::FileHelper.write file_path, file_data
    file_path
  end
end