class Widow

Public Class Methods

new(options = {}) click to toggle source
# File lib/widow.rb, line 28
def initialize options = {}
        log_level = options.delete(:debug) ? :debug : :info
        @logger = Logger.new(STDOUT, level: log_level)
        @logger.info "log level #{log_level}"
end
perform(method, options) click to toggle source
# File lib/widow.rb, line 22
def self.perform method, options
        widow = Widow.new(options)
        widow.public_send(method, options)
        widow.kill
end

Public Instance Methods

cut(options = {}) click to toggle source
# File lib/widow.rb, line 34
def cut options = {}
        options = {from: Pathname.pwd, to: Pathname.pwd}.merge options
        from = Pathname.new(options[:from])
        to = Pathname.new(options[:to])
        @logger.info "cut from #{from} to #{to}"
        exclude = Exclude.new from, @logger

        Find.find from do |path|
                path = Pathname.new path
                next if path == from
                if exclude.ignored? path
                        @logger.info "#{path} ignored"
                        next
                end
                begin
                        make from, path.relative_path_from(from), to
                rescue CompileError
                end
        end
        return
end
kill() click to toggle source

Called to clean up

# File lib/widow.rb, line 117
def kill
        @logger.close
        return
end
spin(options = {}) click to toggle source
# File lib/widow.rb, line 56
def spin options = {}
        options = {from: Pathname.pwd, to: Pathname.pwd, port: 8888, serve_source: false}.merge options
        from = Pathname.new(options[:from])
        to = Pathname.new(options[:to])
        @logger.info "spin from #{from} to #{to}"
        @logger.info "port #{options[:port]}"
        @logger.info "serve_source = #{options[:serve_source]}"
        exclude = Exclude.new from, @logger

        mime_to_ext = Hash.new { |hash, key| hash[key] = [] }
        Tilt.lazy_map.map do |ext, _|
                begin
                        tilt = Tilt[ext]
                        @logger.info "loaded template #{tilt} for extention #{ext}"
                        next [tilt.metadata[:mime_type] || tilt.default_mime_type, ext]
                rescue LoadError
                        next
                end
        end.compact.each do | mime_type, ext |
                mime_to_ext[mime_type] << ext
        end

        Rack::Handler.default.run(
                Proc.new do |env|
                        @logger.debug "#{env["PATH_INFO"]} requested"

                        file = Pathname.new env["PATH_INFO"][1..-1]
                        file += 'index.html' if (from + file).directory?
                        search = from + file.dirname + "#{file.basename file.extname}.{#{(mime_to_ext[mime_type file] + [file.extname[1..-1]]).join ?,}}"
                        @logger.debug "searching for \'#{search}\'"
                        original = Pathname.glob(search).first
                        file = to + file

                        if (exclude.ignored?(original) && !(options[:serve_source] && original.basename == file.basename))
                                @logger.info "#{file} ignored"
                                next file_not_found
                        elsif (original.nil? || !original.exist?)
                                @logger.debug "source not found"
                                next file_not_found
                        end
                        @logger.debug "#{original} found"

                        begin
                                if !file.exist? || file.mtime < original.mtime
                                        if original.basename == file.basename
                                                proxy from, original.relative_path_from(from), to
                                        else
                                                make from, original.relative_path_from(from), to
                                        end
                                end
                                next [200, {'Content-Type' => mime_type(file)}, [file.file? ? file.read : '']]
                        rescue CompileError => e
                                next [502, {'Content-Type' => 'text/html'}, [e.message]]
                        end
                end,
                Port: options[:port]
        )
        return
end

Private Instance Methods

default_ext(mime_type) click to toggle source
# File lib/widow.rb, line 199
def default_ext mime_type
        return (@@mime_map.find{ |e, t| t == mime_type } || ['html', nil])[0]
end
file_not_found() click to toggle source
# File lib/widow.rb, line 208
def file_not_found
        return [404, {'Content-Type' => 'text/html'}, ["File not found!"]]
end
make(root, file, to) click to toggle source

proxies or renders a file based on it's properties and tilt capacity throws a CompileError when the file fails to compile

# File lib/widow.rb, line 152
def make root, file, to
        unless Tilt.registered? file.extname[1..-1].to_s
                proxy root, file, to
                return
        end

        begin
                render root, file, to
        rescue LoadError
                @logger.warn "#{file} has a Tilt template that cannot be loaded"
                proxy root, file, to
        rescue CompileError => e
                @logger.error e.to_s
                raise e
        end
end
mime_type(filename) click to toggle source
# File lib/widow.rb, line 203
def mime_type filename
        name = Pathname.new(filename).extname[1..-1]
        return (@@mime_map.find{ |e, t| e == name } || [nil, 'text/plain'])[1]
end
proxy(root, file, to) click to toggle source

creates a carbon copy of a file elsewhere

# File lib/widow.rb, line 170
def proxy root, file, to
        src = root + file
        return if src.directory?

        obj = to + file
        obj.dirname.mkpath unless obj.dirname.exist?

        FileUtils.cp src, obj
        @logger.info "\t#{src} => #{obj}"
end
render(root, file, to) click to toggle source

Takes a file and tries to render it through tilt to the destination directory Throws load error when tilt template is not resolvable Throws a BuildError when on an internal error

# File lib/widow.rb, line 184
def render root, file, to
        tilt = Tilt.new root + file

        begin
                output = tilt.render
        rescue Exception => e
                raise CompileError.new tilt, e
        end

        dest = to + "#{file.basename file.extname}.#{default_ext tilt.metadata[:mime_type]}"
        dest.dirname.mkpath unless dest.dirname.exist?
        File.write(dest, output)
        @logger.info "\t#{tilt.file} => #{dest}"
end