class SC::Builder::Base

The base class extended by most builder classes. This contains some default functionality for handling loading and writing files. Usually you will want to consult the specific classes instead for more info.

Attributes

entry[RW]

entry the current builder is working on

Public Class Methods

build(entry, dst_path) click to toggle source

main entry called by build tasks

# File lib/sproutcore/builders/base.rb, line 34
def self.build(entry, dst_path)
  new(entry).build(dst_path)
end
new(entry=nil) click to toggle source
# File lib/sproutcore/builders/base.rb, line 25
def initialize(entry=nil)
  @entry =entry
end

Public Instance Methods

build(dst_path) click to toggle source

override this method in subclasses to actually do build

# File lib/sproutcore/builders/base.rb, line 30
def build(dst_path)
end
joinlines(lines) click to toggle source

joins the array of lines. this is where you can also do any final post-processing on the build

# File lib/sproutcore/builders/base.rb, line 60
def joinlines(lines)
  lines.is_a?(Array) ? lines.join : lines
end
read(src_path) click to toggle source

Reads the content of the source file. If the source file does not exist, returns an empty array.

# File lib/sproutcore/builders/base.rb, line 40
def read(src_path)
  if File.exist?(src_path) && !File.directory?(src_path)
    File.read(src_path)
  else
    ""
  end        
end
readlines(src_path) click to toggle source

Reads the lines from the source file. If the source file does not exist, returns empty array.

# File lib/sproutcore/builders/base.rb, line 50
def readlines(src_path)
  if File.exist?(src_path) && !File.directory?(src_path)
    File.readlines(src_path)
  else
    []
  end
end
replace_static_url(line) click to toggle source

Handles occurances of sc_static() or static_url()

# File lib/sproutcore/builders/base.rb, line 90
def replace_static_url(line)
  line.gsub!(sc_static_match) do | rsrc |
    entry_name = $2
    entry_name = "#{$2}:index.html" if $1 == 'sc_target'

    static_entry = entry.manifest.find_entry($2)

    if !static_entry
      SC.logger.warn "#{$2} was not found. Line: #{rsrc}"
      url = ''
    elsif $1 == 'sc_target'
      url = static_entry[:friendly_url] || static_entry.cacheable_url
    else
      url = static_entry.cacheable_url
    end

    static_url(url)
  end
end
sc_static_match() click to toggle source
# File lib/sproutcore/builders/base.rb, line 85
def sc_static_match
  /(sc_static|static_url|sc_target)\(\s*['"]([^"']*?)['"]\s*\)/
end
static_url(url='') click to toggle source

Generates the proper output for a given static url and a given target this is often overridden by subclasses. the default just wraps in quotes.

# File lib/sproutcore/builders/base.rb, line 113
def static_url(url='')
  "'#{url.gsub('"', '\"')}'"
end
writeline(dst_path, line) click to toggle source

writes the passed lines to the named file

# File lib/sproutcore/builders/base.rb, line 65
def writeline(dst_path, line)
  FileUtils.mkdir_p(File.dirname(dst_path))
  File.open(dst_path, 'w') do |f|
    f.write line
  end
end
writelinebinary(dst_path, line) click to toggle source

writes the passed lines to the named file as binary

# File lib/sproutcore/builders/base.rb, line 73
def writelinebinary(dst_path, line)
  FileUtils.mkdir_p(File.dirname(dst_path))
  File.open(dst_path, 'wb') do |f|
    f.write line
  end
end
writelines(dst_path, lines) click to toggle source

writes the passed lines to the named file

# File lib/sproutcore/builders/base.rb, line 81
def writelines(dst_path, lines)
  writeline(dst_path,joinlines(lines))
end