First pass at combining views and public folders

This commit is contained in:
Thomas Reynolds 2011-05-30 22:33:11 -07:00
parent ba2cbb0ceb
commit 993be281c7
62 changed files with 57 additions and 91 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ pkg
.tmp .tmp
Gemfile.lock Gemfile.lock
docs docs
.rvmrc

4
CHANGELOG Normal file
View file

@ -0,0 +1,4 @@
2.0.0
=====
- Combine views/ and public/ into a single source/ folder.

View file

@ -1,22 +1,5 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# Non-blocking site rebuilding
trap("TSTP") do
fork do
require "open3"
first_run = true
Open3.popen3(%Q{cd "#{Dir.pwd}" && #{File.join(File.dirname(__FILE__), "mm-build")} | grep FORCED}) do |stdin, stdout, stderr|
puts "\n== Building the site..."
stdout.readlines.each do |l|
puts "== Updated:" if first_run
puts " " + l.split("[FORCED]").last.chomp
first_run = false
end
puts "== Build complete"
end
end
end if Signal.list.has_key? "TSTP"
require 'optparse' require 'optparse'
# Require Middleman # Require Middleman
@ -25,6 +8,7 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
env = ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development' env = ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development'
options = { :Port => 4567, :AccessLog => [] } options = { :Port => 4567, :AccessLog => [] }
# TODO: Switch to Thor
OptionParser.new { |opts| OptionParser.new { |opts|
opts.banner = "Usage: mm-server [rack options]" opts.banner = "Usage: mm-server [rack options]"
opts.separator "" opts.separator ""
@ -51,10 +35,9 @@ ENV['RACK_ENV'] = env
while (!@found_root && (@path_parts.length > 0)) while (!@found_root && (@path_parts.length > 0))
@current_path = File.join(*@path_parts) @current_path = File.join(*@path_parts)
public_folder = File.join(@current_path, "public") source_folder = File.join(@current_path, "source")
views_folder = File.join(@current_path, "views")
if File.exists?(public_folder) && File.exists?(views_folder) if File.exists?(source_folder)
@found_root = true @found_root = true
next next
end end
@ -67,20 +50,25 @@ if !@found_root
exit exit
end end
# If the old init.rb exists, use it, but issue warning # If the old init.rb exists, issue warning
old_config = File.join(@current_path, "init.rb") old_config = File.join(@current_path, "init.rb")
if File.exists? old_config if File.exists? old_config
$stderr.puts "== Error: The init.rb file (deprecated) needs to be be renamed to config.rb" $stderr.puts "== Error: The init.rb file (deprecated) needs to be be renamed to config.rb"
exit exit
end end
# If the old directories exists, use it, but issue warning
old_views = File.join(@current_path, "views")
old_public = File.join(@current_path, "public")
if File.exists?(old_views) || File.exists?(old_public)
$stderr.puts "== Error: The views and public folders are have been combined. Create a new 'source' folder, add the contents of views and public to it and then remove the empty views and public folders."
exit
end
# require 'shotgun'
# config = File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'config.ru')
# app = Shotgun.new(config, &lambda { |inner_app| Middleman::Server })
Middleman::Server.root = @current_path Middleman::Server.root = @current_path
app = Middleman::Server.new app = Middleman::Server.new
# TODO: Remove this
require 'rubygems' require 'rubygems'
unless defined?(JRUBY_VERSION) unless defined?(JRUBY_VERSION)

View file

@ -26,26 +26,4 @@ end
Then /^"([^"]*)" should not exist$/ do |target_file| Then /^"([^"]*)" should not exist$/ do |target_file|
target = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "fixtures", "test-app", "build", target_file) target = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "fixtures", "test-app", "build", target_file)
File.exists?(target).should be_false File.exists?(target).should be_false
end end
# require 'fileutils'
#
# describe "Builder" do
# def project_file(*parts)
# File.expand_path(File.join(File.dirname(__FILE__), "..", *parts))
# end
#
# before :all do
# @root_dir = project_file("spec", "fixtures", "sample")
# end
#
# before :each do
# build_cmd = project_file("bin", "mm-build")
# `cd #{@root_dir} && MM_DIR="#{@root_dir}" #{build_cmd}`
# end
#
# after :each do
# FileUtils.rm_rf(File.join(@root_dir, "build"))
# end

View file

@ -19,7 +19,7 @@ end
Then /^empty directories should exist at "([^\"]*)"$/ do |dirname| Then /^empty directories should exist at "([^\"]*)"$/ do |dirname|
target = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "fixtures", dirname) target = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "fixtures", dirname)
%w(views/stylesheets public/stylesheets public/javascripts public/images).each do |d| %w(source/stylesheets source/javascripts source/images).each do |d|
File.exists?("#{target}/#{d}").should be_true File.exists?("#{target}/#{d}").should be_true
end end
end end

View file

Before

Width:  |  Height:  |  Size: 43 B

After

Width:  |  Height:  |  Size: 43 B

View file

@ -0,0 +1 @@
// Success

View file

@ -8,7 +8,7 @@ module Middleman
def tilt_template(source, *args, &block) def tilt_template(source, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {} config = args.last.is_a?(Hash) ? args.pop : {}
destination = args.first || source destination = args.first || source
source = File.expand_path(find_in_source_paths(source.to_s)) source = File.expand_path(find_in_source_paths(source.to_s))
context = instance_eval('binding') context = instance_eval('binding')
@ -33,6 +33,7 @@ module Middleman
super super
Middleman::Server.new Middleman::Server.new
if options.has_key?("relative") && options["relative"] if options.has_key?("relative") && options["relative"]
Middleman::Server.activate :relative_assets Middleman::Server.activate :relative_assets
end end
@ -41,17 +42,12 @@ module Middleman
def source_paths def source_paths
@source_paths ||= [ @source_paths ||= [
Middleman::Server.public, Middleman::Server.root
Middleman::Server.views
] ]
end end
def build_static_files def build_all_files
action Directory.new(self, Middleman::Server.public, :public, Middleman::Server.build_dir, { :force => true }) action Directory.new(self, Middleman::Server.views, Middleman::Server.build_dir, { :force => true })
end
def build_dynamic_files
action Directory.new(self, Middleman::Server.views, :dynamic, Middleman::Server.build_dir, { :force => true })
end end
@@hooks = {} @@hooks = {}
@ -69,8 +65,7 @@ module Middleman
class Directory < ::Thor::Actions::EmptyDirectory class Directory < ::Thor::Actions::EmptyDirectory
attr_reader :source attr_reader :source
def initialize(base, source, mode, destination=nil, config={}, &block) def initialize(base, source, destination=nil, config={}, &block)
@mode = mode
@source = File.expand_path(base.find_in_source_paths(source.to_s)) @source = File.expand_path(base.find_in_source_paths(source.to_s))
@block = block @block = block
super(base, destination, { :recursive => true }.merge(config)) super(base, destination, { :recursive => true }.merge(config))
@ -96,22 +91,20 @@ module Middleman
end end
next if file_source.include?('layout') next if file_source.include?('layout')
# Skip partials prefixed with an underscore
next unless file_source.split('/').select { |p| p[0,1] == '_' }.empty? next unless file_source.split('/').select { |p| p[0,1] == '_' }.empty?
file_extension = File.extname(file_source) file_extension = File.extname(file_source)
file_destination = File.join(given_destination, file_source.gsub(source, '.')) file_destination = File.join(given_destination, file_source.gsub(source, '.'))
file_destination.gsub!('/./', '/') file_destination.gsub!('/./', '/')
handled_by_tilt = ::Tilt.mappings.keys.include?(file_extension.gsub(/^\./, ""))
if handled_by_tilt || (file_extension == ".js")
new_file_extension = (file_extension == ".js") ? ".js" : ""
next if @mode == :dynamic && file_source.split('/').last.split('.').length < 3
file_destination.gsub!(file_extension, new_file_extension) handled_by_tilt = ::Tilt.mappings.has_key?(file_extension.gsub(/^\./, ""))
destination = base.tilt_template(file_source, file_destination, config, &@block) if handled_by_tilt
else file_destination.gsub!(file_extension, "")
destination = base.copy_file(file_source, file_destination, config, &@block)
end end
destination = base.tilt_template(file_source, file_destination, config, &@block)
end end
end end

View file

@ -15,8 +15,6 @@ module Middleman
set :logging, false set :logging, false
set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
# Import padrino helper methods
# Middleman-specific options # Middleman-specific options
set :index_file, "index.html" # What file responds to folder requests set :index_file, "index.html" # What file responds to folder requests
# Such as the homepage (/) or subfolders (/about/) # Such as the homepage (/) or subfolders (/about/)
@ -30,9 +28,14 @@ module Middleman
set :build_dir, "build" # Which folder are builds output to set :build_dir, "build" # Which folder are builds output to
set :http_prefix, nil # During build, add a prefix for absolute paths set :http_prefix, nil # During build, add a prefix for absolute paths
set :static, false
set :views, "source"
# Disable Padrino cache buster until explicitly enabled
set :asset_stamp, false
# Use Padrino Helpers # Use Padrino Helpers
register Padrino::Helpers register Padrino::Helpers
set :asset_stamp, false # Disable Padrino cache buster until explicitly enabled
# Activate custom features # Activate custom features
register Middleman::Features register Middleman::Features
@ -140,11 +143,12 @@ module Middleman
# Normalize the path and add index if we're looking at a directory # Normalize the path and add index if we're looking at a directory
path = self.class.path_to_index(request.path) path = self.class.path_to_index(request.path)
static_path = File.join(Middleman::Server.public, path) extensionless_path, template_engine = resolve_template(path)
# if File.exists? static_path
# send_file static_path if !::Tilt.mappings.has_key?(template_engine.to_s)
# return send_file File.join(Middleman::Server.views, path)
# end return
end
old_layout = settings.current_layout old_layout = settings.current_layout
settings.layout(options[:layout]) if !options[:layout].nil? settings.layout(options[:layout]) if !options[:layout].nil?
@ -182,9 +186,6 @@ class Middleman::Server
Middleman::Server.class_eval File.read(local_config) Middleman::Server.class_eval File.read(local_config)
set :app_file, File.expand_path(local_config) set :app_file, File.expand_path(local_config)
end end
use ::Rack::ConditionalGet
use ::Rack::Static, :urls => ["/#{self.images_dir}"], :root => "public"
@@run_after_features.each { |block| class_eval(&block) } @@run_after_features.each { |block| class_eval(&block) }

View file

@ -6,10 +6,10 @@ class Middleman::Templates::Default < Middleman::Templates::Base
def build_scaffold def build_scaffold
template "config.tt", File.join(location, "config.rb") template "config.tt", File.join(location, "config.rb")
template "config.ru", File.join(location, "config.ru") template "config.ru", File.join(location, "config.ru")
directory "views", File.join(location, "views") directory "source", File.join(location, "source")
empty_directory File.join(location, "public", options[:css_dir]) empty_directory File.join(location, "source", options[:css_dir])
empty_directory File.join(location, "public", options[:js_dir]) empty_directory File.join(location, "source", options[:js_dir])
empty_directory File.join(location, "public", options[:images_dir]) empty_directory File.join(location, "source", options[:images_dir])
end end
end end

View file

@ -5,8 +5,8 @@ class Middleman::Templates::Html5 < Middleman::Templates::Base
def build_scaffold def build_scaffold
template "config.tt", File.join(location, "config.rb") template "config.tt", File.join(location, "config.rb")
directory "public", File.join(location, "public") directory "source", File.join(location, "source")
empty_directory File.join(location, "views") empty_directory File.join(location, "source")
end end
end end

View file

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View file

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -6,10 +6,10 @@ class Middleman::Templates::Xhtml < Middleman::Templates::Base
def build_scaffold def build_scaffold
template "config.tt", File.join(location, "config.rb") template "config.tt", File.join(location, "config.rb")
template "config.ru", File.join(location, "config.ru") template "config.ru", File.join(location, "config.ru")
directory "views", File.join(location, "views") directory "source", File.join(location, "source")
empty_directory File.join(location, "public", options[:css_dir]) empty_directory File.join(location, "source", options[:css_dir])
empty_directory File.join(location, "public", options[:js_dir]) empty_directory File.join(location, "source", options[:js_dir])
empty_directory File.join(location, "public", options[:images_dir]) empty_directory File.join(location, "source", options[:images_dir])
end end
end end

View file

@ -1,3 +1,3 @@
module Middleman module Middleman
VERSION = "1.2.7" VERSION = "2.0.0.beta1"
end end