Module: Rack::Blogengine::CommandLineInterface

Defined in:
lib/rack/blogengine/command_line_interface.rb

Overview

This Module handles all cli input (running the app, generate folder skeleton)

Author:

  • benny

Class Method Summary collapse

Class Method Details

.build_rack_app(target, config) ⇒ Rack::Builder

Build rack app via Rack::Builder

Parameters:

  • target (String)
    The Targetfolder where all relevant files are located
  • config (Hash)
    Config via get_config -> parses in config.yml

Returns:

  • (Rack::Builder)

    Rack Application



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

def build_rack_app(target, config)
  Rack::Builder.new do
    map '/assets' do
      run Rack::Directory.new("#{target}/assets")
    end

    if config['Usage'] == 'yes'
      use Rack::Auth::Basic, 'Protected Area' do |username, password|
        username == config['Username'] && password == config['Password']
      end
    end

    # Parse in all Documents in cli.run(target)
    # -> $documents are parsed in only once and then cached via a global variable
    # Todo Cache without global variable?
    # Global Variable replaced with module instance variable
    Rack::Blogengine.documents = DocumentParser.parse_in_documents(target)

    run Application.new
  end
end

.generate(folder) ⇒ Object

Command to generate the folder skeleton

Parameters:

  • folder (String)


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
71
72
73
74
75
76
77
78
79
# File 'lib/rack/blogengine/command_line_interface.rb', line 44

def generate(folder)
  puts "\tGenerating folder skeleton\n"
  system("mkdir #{folder}")
  system("mkdir #{folder}/assets")
  system("mkdir #{folder}/assets/layout")
  system("mkdir #{folder}/assets/js")
  system("mkdir #{folder}/assets/style")
  system("mkdir #{folder}/assets/images")
  system("mkdir #{folder}/operator")

  puts "\n\tSetting up essential Files\n"

  # SET UP operator.rb
  setup('operator.rb', "#{folder}/operator", true)

  # SET UP config.yml
  setup('config.yml', "#{folder}", true)

  # SET UP index.content
  setup('index.content', "#{folder}", true)

  # SET UP layout.html
  setup('layout.html', "#{folder}/assets/layout", true)

  # SET UP style.css
  setup('style.css', "#{folder}/assets/style", false)

  # SET UP script.js
  setup('script.js', "#{folder}/assets/js", false)

  # SET UP Gemfile
  setup('Gemfile', "#{folder}", true)

  puts "\n\tSetup finished! Have Fun\n"
  puts "\tTo test it type rack-blogengine run #{folder}"
end

.get_config(target) ⇒ Hash

Get YAML Config settings for Server.start && HTTPauth

Parameters:

  • target (String)

Returns:

  • (Hash)

    Config



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rack/blogengine/command_line_interface.rb', line 132

def get_config(target)
  config_yaml = YAML.load(::File.open("#{target}/config.yml"))

  port = config_yaml['Port']
  server = config_yaml['Server']
  username = config_yaml['HTTPauth']['username'].to_s.strip
  password = config_yaml['HTTPauth']['password'].to_s.strip
  usage = config_yaml['HTTPauth']['usage']
  pygments_style = config_yaml['Pygments']['style']
  pygments_seperator = config_yaml['Pygments']['seperator']

  Rack::Blogengine.config = { 'Port' => port,
                              'Server' => server,
                              'Username' => username,
                              'Password' => password,
                              'Usage' => usage,
                              'pygments_style' => pygments_style,
                              'pygments_seperator' => pygments_seperator }
end

.method_missing(name, *args) ⇒ Object

Handle unavailable methods

Parameters:

  • name (String)
    called Methodname
  • *args (Array)
    Available args


16
17
18
19
20
21
22
23
# File 'lib/rack/blogengine/command_line_interface.rb', line 16

def method_missing(name, *args)
  puts "Command #{name} not available"
  print "Available Commands are: \n\n"
  self.methods(false).each do |method|
    print "\t #{method}\n" unless method == :method_missing # || method == :setup || method == :getConfig
  end
  print "\n"
end

.run(target) ⇒ Object

Method to run the rack Application

Parameters:

  • target (String)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rack/blogengine/command_line_interface.rb', line 27

def run(target)
  if target.empty?
    print 'Specify a targetfolder!'
  else
    if Dir.exists?("#{target}")
      config = get_config(target)
      app = build_rack_app(target, config)

      Rack::Server.start(app: app, Port: config['Port'], server: config['Server'])
    else
      print "#{target} is not a folder!"
    end
  end
end

.setup(name, path, essential) ⇒ Object

Helper method for generate to set up all essential files

Parameters:

  • name (String)
  • path (String)
  • essential (boolean)


119
120
121
122
123
124
125
126
127
# File 'lib/rack/blogengine/command_line_interface.rb', line 119

def setup(name, path, essential)
  puts "\tSet up #{path}/#{name}\n"
  system("touch #{path}/#{name}")
  if essential
    assetspath = "#{Rack::Blogengine.root}/assets/#{name}"
    content = IO.read(assetspath)
    ::File.open("#{path}/#{name}", 'w') { |file| file.write(content) }
  end
end

.versionString

Display Version

Returns:

  • (String)

    VERSION



83
84
85
# File 'lib/rack/blogengine/command_line_interface.rb', line 83

def version
  puts "\n\tVERSION: #{Rack::Blogengine::VERSION}\n"
end