Skip to content

Commit

Permalink
lissio: add a builder
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Feb 1, 2014
1 parent 8237eed commit 9f90b96
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 64 deletions.
172 changes: 111 additions & 61 deletions bin/lissio
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,13 @@ require 'fileutils'
require 'yaml'

require 'lissio'
require 'opal/browser'

class CLI < Thor
desc "start", "start the lissio server"

option :server, type: :string
option :port, type: :numeric
option :host, type: :string
option :require, type: :array, default: []
option :use, type: :array, default: []
option :path, type: :array, default: ['app', 'js', 'opal']
option :static, type: :array, default: ['/css', '/fonts', '/img']
option :index, type: :string
option :debug, type: :boolean

def start
options = self.options.dup

if File.readable? 'lissio.yml'
YAML.load_file('lissio.yml').tap {|opts|
%w[server port host index debug].each {|name|
if opts.has_key?(name) && !options.has_key?(name)
options[name] = opts[name]
end
}

%w[use require path static].each {|name|
if opts.has_key? name
options[name] = Array(opts[name])
end
}
}
end

options[:require].each {|r|
require r
}

options[:use].each {|u|
Opal.use_gem u
}

Rack::Server.start(
Host: options[:host] || '0.0.0.0',
Port: options[:port] || 9292,
server: options[:server],

app: Lissio::Server.new {|s|
options[:path].each {|path|
s.append_path path
}

s.static = options[:static]
desc "new [PATH]", "create a new lissio application"

if index = options[:index]
s.index = index
end
option :adapter, type: :string
option :components, type: :string

if options[:debug]
s.debug = true
end
})
end

desc "new [PATH]", "create a new lissio application"
def new(path = '.')
FileUtils.mkpath path
FileUtils.cd path do
Expand Down Expand Up @@ -131,6 +73,114 @@ class CLI < Thor
end
end
end

desc "start", "start the lissio server"

option :server, aliases: '-S', type: :string
option :port, aliases: '-p', type: :numeric
option :host, type: :string
option :main, aliases: '-m', type: :string
option :require, aliases: '-r', type: :array, default: []
option :use, aliases: '-u', type: :array, default: []
option :path, type: :array, default: ['app', 'js', 'opal']
option :static, aliases: '-s', type: :array, default: ['/css', '/fonts', '/img']
option :index, aliases: '-i', type: :string
option :debug, aliases: '-d', type: :boolean

def start
options = prepare

Rack::Server.start(
Host: options[:host] || '0.0.0.0',
Port: options[:port] || 9292,
server: options[:server],

app: Lissio::Server.new {|s|
options[:path].each {|path|
s.append_path path
}

s.static = options[:static]

if main = options[:main]
s.main = main
end

if index = options[:index]
s.index = index
end

if options[:debug]
s.debug = true
end
})
end

desc "build [PATH]", "create a server-less distribution of the application"

option :main, aliases: '-m', type: :string
option :require, aliases: '-r', type: :array, default: []
option :use, aliases: '-u', type: :array, default: []
option :path, type: :array, default: ['app', 'js', 'opal']
option :index, aliases: '-i', type: :string
option :force, aliases: '-f', type: :boolean, default: false

def build(output = 'index.html')
if !options[:force] && File.exists?(output)
raise ArgumentError, "'#{output}' already exists"
end

options = prepare

File.open output, 'w' do |f|
builder = Lissio::Builder.new {|b|
options[:path].each {|path|
b.append_path path
}

if main = options[:main]
b.main = main
end

if index = options[:index]
b.index = index
end
}

f.write builder.build
end
end

private
def prepare
options = self.options.dup

if File.readable? 'lissio.yml'
YAML.load_file('lissio.yml').tap {|opts|
%w[server port host index debug main].each {|name|
if opts.has_key?(name) && !options.has_key?(name)
options[name] = opts[name]
end
}

%w[use require path static].each {|name|
if opts.has_key? name
options[name] = Array(opts[name])
end
}
}
end

options[:require].each {|r|
require r
}

options[:use].each {|u|
Opal.use_gem u
}

options
end
end

CLI.start(ARGV)
2 changes: 2 additions & 0 deletions lib/lissio.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'opal'
require 'opal/browser'

require 'lissio/server'
require 'lissio/builder'

Opal.append_path File.expand_path('../../opal', __FILE__)
49 changes: 49 additions & 0 deletions lib/lissio/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'opal/sprockets/environment'
require 'forwardable'

module Lissio

class Builder
extend Forwardable

attr_accessor :debug, :index, :main, :static, :source_maps, :sprockets
def_delegators :@sprockets, :append_path, :use_gem

def initialize(&block)
@sprockets = Opal::Environment.new
@main = 'app'

block.call(self) if block
end

def build
source = if @path
unless File.exist?(@path)
raise "index does not exist: #{@path}"
end

File.read @path
elsif File.exist? 'index.html.erb'
File.read 'index.html.erb'
else
<<-HTML
<!DOCTYPE html>
<html>
<head>
<%= lissio %>
</head>
<body>
</body>
</html>
HTML
end

::ERB.new(source).result binding
end

def lissio(source = main)
"<script>#{@sprockets[source].to_s}</script>"
end
end

end
5 changes: 2 additions & 3 deletions lib/lissio/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ def html
end

File.read @path
elsif File.exist? 'index.html'
File.read 'index.html'
elsif File.exist? 'index.html.erb'
File.read 'index.html.erb'
else
Expand Down Expand Up @@ -101,8 +99,9 @@ def lissio(source = @server.main)
attr_accessor :debug, :index, :main, :static, :source_maps, :sprockets
def_delegators :@sprockets, :append_path, :use_gem

def initialize(options = {}, &block)
def initialize(&block)
@sprockets = Opal::Environment.new
@main = 'app'

block.call(self) if block
end
Expand Down

0 comments on commit 9f90b96

Please sign in to comment.