Class: SC::Builder::Base

Inherits:
Object show all
Defined in:
lib/sproutcore/builders/base.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry = nil) ⇒ Base

Returns a new instance of Base.



25
26
27
# File 'lib/sproutcore/builders/base.rb', line 25

def initialize(entry=nil)
  @entry =entry
end

Instance Attribute Details

#entryObject

entry the current builder is working on



23
24
25
# File 'lib/sproutcore/builders/base.rb', line 23

def entry
  @entry
end

Class Method Details

.build(entry, dst_path) ⇒ Object

main entry called by build tasks



34
35
36
# File 'lib/sproutcore/builders/base.rb', line 34

def self.build(entry, dst_path)
  new(entry).build(dst_path)
end

Instance Method Details

#build(dst_path) ⇒ Object

override this method in subclasses to actually do build



30
31
# File 'lib/sproutcore/builders/base.rb', line 30

def build(dst_path)
end

#joinlines(lines) ⇒ Object

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



60
61
62
# File 'lib/sproutcore/builders/base.rb', line 60

def joinlines(lines)
  lines.is_a?(Array) ? lines.join : lines
end

#read(src_path) ⇒ Object

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



40
41
42
43
44
45
46
# 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) ⇒ Object

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



50
51
52
53
54
55
56
# 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) ⇒ Object

Handles occurances of sc_static() or static_url()



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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_matchObject



85
86
87
# File 'lib/sproutcore/builders/base.rb', line 85

def sc_static_match
  /(sc_static|static_url|sc_target)\(\s*['"]([^"']*?)['"]\s*\)/
end

#static_url(url = '') ⇒ Object

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.



113
114
115
# File 'lib/sproutcore/builders/base.rb', line 113

def static_url(url='')
  "'#{url.gsub('"', '\"')}'"
end

#writeline(dst_path, line) ⇒ Object

writes the passed lines to the named file



65
66
67
68
69
70
# 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) ⇒ Object

writes the passed lines to the named file as binary



73
74
75
76
77
78
# 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) ⇒ Object

writes the passed lines to the named file



81
82
83
# File 'lib/sproutcore/builders/base.rb', line 81

def writelines(dst_path, lines)
  writeline(dst_path,joinlines(lines))
end