Class: Moka::Generators::MokaGroupGenerator

Inherits:
Thor::Group
  • Object
show all
Includes:
SiteTree, Thor::Actions
Defined in:
lib/commands/group/group_generator.rb

Constant Summary

Constants included from SiteTree

SiteTree::RESERVED_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



19
20
21
# File 'lib/commands/group/group_generator.rb', line 19

def self.source_root
  File.expand_path('template/', File.dirname(__FILE__))
end

Instance Method Details

#create_group_dir_and_filesObject



59
60
61
62
63
64
65
# File 'lib/commands/group/group_generator.rb', line 59

def create_group_dir_and_files
  unless options[:template].nil?
    directory(File.expand_path("project/site/#{@template_group}", MOKA_ROOT), File.expand_path("project/site/#{@group}", MOKA_ROOT))
  else
    directory("groupdir", File.expand_path("project/site/#{@group}", MOKA_ROOT))
  end
end

#delete_variables_using_reserved_nameObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/commands/group/group_generator.rb', line 67

def delete_variables_using_reserved_name
  unless options[:vars].nil?
    options[:vars].each do |var_name, var_value|
      if Moka::SiteTree::RESERVED_NAMES.include? var_name.to_s
        say_status "WARNING:", "variable '#{var_name}' is a reserved word, and will not be set as a variable", :yellow
      end
    end
    options[:vars].delete_if {|var_name, var_value| Moka::SiteTree::RESERVED_NAMES.include? var_name.to_s }
  end
end

#dump_variablesObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/commands/group/group_generator.rb', line 78

def dump_variables
  if File.exists? File.expand_path("project/site/#{@group}/variables.yml", MOKA_ROOT)
    @group_variables = YAML.load_file(File.expand_path("project/site/#{@group}/variables.yml", MOKA_ROOT))
  else
    @group_variables = {}
  end
  unless options[:vars].nil?
    @group_variables.merge! options[:vars]
    f = File.open( File.expand_path("project/site/#{@group}/variables.yml", MOKA_ROOT), 'w' ) do |out|
      YAML.dump( @group_variables, out )
    end
    say_status "update", "project/site/#{@group}/variables.yml", :green
  end
end

#load_manifestObject



23
24
25
26
# File 'lib/commands/group/group_generator.rb', line 23

def load_manifest
  @manifest = YAML.load_file(File.expand_path("manifest.yml", MOKA_ROOT))
  @site = SiteNode.new("site", @manifest["site"])
end

#notify_about_creationObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/commands/group/group_generator.rb', line 93

def notify_about_creation
  say ""
  if options[:template].nil?
    say "Generated group #{@group}"
  else
    say "Generated group #{@group} using group #{@template_group} as a template"
  end
  say ""
  say " Group Parameters:"
  @group_params.each do |param_name, param_value|
    say "  #{param_name} = #{param_value}"
  end

  say ""
  say " Group Variables:"
  @group_variables.each do |var_name, var_value|
    puts "  #{var_name} = #{var_value}"
  end
  say ""
end

#update_manifestObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/commands/group/group_generator.rb', line 36

def update_manifest
  unless options[:template].nil?
    @template_group = options[:template]
    if @site.find_group(@template_group).nil?
      say_status "ERROR:", "cannot find group '#{@template_group}'", :red
      exit
    end
    template_manifest = Moka::Utilities.deepcopy(@manifest["site"][@template_group])
    @site.find_group(@template_group).pages.each do |page|
      template_manifest[page.name]["path"] = File.join "#{@group}", "#{page.name}.#{@site.respond_to?(:default_extension) ? @site.default_extension : "html"}"
    end
  else
    template_manifest = {}
  end
  @group_params = {"order" => Time.new.utc.to_i}
  @manifest["site"][@group] = template_manifest.merge @group_params
  # dump manifest
  f = File.open( File.expand_path('manifest.yml', MOKA_ROOT), 'w' ) do |out|
    YAML.dump( @manifest, out )
  end
  say_status "update", "manifest.yml", :green
end

#verify_if_group_existsObject



28
29
30
31
32
33
34
# File 'lib/commands/group/group_generator.rb', line 28

def verify_if_group_exists
  @group = name
  unless @site.find_group(@group).nil?
    say_status "ERROR:", "group '#{@group}' already exists!", :red
    exit
  end
end