Ruby

1ファイルでsitespecにもwebサーバにもできるやつ

More than 5 years have passed since last update.

ruby test.rbだとSinatraがpumaで起動してrspec test.rbだとsitespecが走る。

1つにまとまってると便利かと思ったけど特にそうでもない。

# -- coding: utf-8

require "grill"

Grill.implant <<-G
gem "pry"
gem "rspec"
gem "sitespec"
gem "sinatra", ">1.4", :require => "sinatra/base"
gem "sinatra-contrib", :require => %w[
sinatra/reloader
]
gem "sprockets"
gem "sass"
gem "coffee-script"
gem "puma"

# gem "haml" とか
G

class App < Sinatra::Base
register Sinatra::Reloader

set :public_folder, File.expand_path("../src", __FILE__)

def sprockets
@sprockets ||=
begin
env = Sprockets::Environment.new
env.append_path "src/assets/css"
env.append_path "src/assets/js"
env
end
end

get "/" do
"hoge"
end

# namespace and regex generate broken routes
# https://github.com/sinatra/sinatra-contrib/issues/102
get %r!/assets/(.*)! do |path|
content_type sprockets.content_type_of(path)
halt 404 unless sprockets[path]
sprockets[path].to_s
end
end

case File.basename($0)
when "rspec"
Sitespec.configure do
self.application = App.new
self.build_path = "build"
self.raise_http_error = true
end

describe "Sitespec" do
include Sitespec

it "generates static site from your rack application & spec definition" do
get "/assets/test.js" # src/assets/js/test.js.coffee
get "/assets/test.css" # src/assets/css/test.css.scss
end
end
else
App.run!
end