Class: Alula::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/alula/cli.rb

Constant Summary collapse

TEMPLATE_DIR =
File.expand_path(File.join(File.dirname(__FILE__), *%w[.. .. template]))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_optionsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/alula/cli.rb', line 15

def self.generate_options
  # option :development, :type => :boolean, :default => true,
  #   :desc => "Generate site using development settings. Keeps all assets and HTML uncompressed."
  # option :production, :type => :boolean, :default => false,
  #   :desc => "Generate site using production settings. Compresses all assets and HTML."
  option :environment, :type => :string, :default => 'development', :aliases => "-e",
    :desc => "Environment where site is built"
  option :verbose, :type => :boolean, :default => false, :aliases => "-v",
    :desc => "Be verbose during site generation."
  option :test, :type => :boolean, :default => false, :aliases => "-t",
    :desc => "Turn on some testing features, i.e. doesn't upload/convert all files etc."
  option :debug, :type => :boolean, :default => false, :aliases => "-d"
end

Instance Method Details

#deploy(*args) ⇒ Object



80
81
82
83
84
85
# File 'lib/alula/cli.rb', line 80

def deploy(*args)
  unless args.empty?
    args.each {|a| puts "Unknown option #{a} given."}; exit
  end
  site.deploy
end

#generate(*args) ⇒ Object



71
72
73
74
75
76
# File 'lib/alula/cli.rb', line 71

def generate(*args)
  unless args.empty?
    args.each {|a| puts "Unknown option #{a} given."}; exit
  end
  site.generate
end

#new(path = ".") ⇒ Object



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
# File 'lib/alula/cli.rb', line 37

def new(path = ".")
  puts "Create blog at #{path}"
  
  # Init directories
  %w{content
    content/attachments content/pages content/posts content/static
    custom
    custom/images custom/javascripts custom/stylesheets}.each do |dir|
    empty_directory File.join(path, dir)
    create_file File.join(path, dir, ".gitkeep")
  end
  Dir[File.join(TEMPLATE_DIR, "**/*")].each do |tpl|
    name = tpl.gsub("#{TEMPLATE_DIR}/", '')
    if tpl[/\.erb$/]
      template tpl, File.join(path, name.gsub(/\.erb$/, ''))
    else
      copy_file tpl, File.join(path, name)
    end
  end
  
  inside File.expand_path(path) do
    # Try to find git
    git=%x{/usr/bin/which git}.strip
    if File.executable?(git) and !File.directory?(".git")
      run "#{git} init"
    end

    # Run bundle command
    run "bundle install"
  end
end

#preview(*args) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/alula/cli.rb', line 91

def preview(*args)
  unless args.empty?
    args.each {|a| puts "Unknown option #{a} given."}; exit
  end
  site.generate unless options['skip-generate']
        
  # Start webserver
  begin
    require 'thin'
    require 'alula/support/commonlogger'
    
    public_path = site.storage.path(:public)
    s = Thin::Server.start('0.0.0.0', site.config.port) do
      @root = File.expand_path(public_path)
      use CommonLogger
      run Proc.new { |env|
        path = Rack::Utils.unescape(env['PATH_INFO'])
        index_file = File.join(@root, path, "index.html")
        
        if File.exists?(index_file)
          [200, {'Content-Type' => 'text/html'}, [File.read(index_file)]]
        else
          Rack::Directory.new(@root).call(env)
        end
      }
    end
  rescue LoadError => e
    puts "Please install thin gem to use preview functionality (gem install thin)."
  end
end

#versionObject



30
31
32
# File 'lib/alula/cli.rb', line 30

def version
  puts "Alula #{Alula::VERSION::STRING}"
end