Class: SC::Builder::Base

Inherits:
Object
  • 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.

Direct Known Subclasses

Combine, Html, JavaScript, Minify, Sass, Strings, Stylesheet, TestIndex

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 tasls



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



50
51
52
# File 'lib/sproutcore/builders/base.rb', line 50

def joinlines(lines)
  lines * ""
end

#readlines(src_path) ⇒ Object

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



40
41
42
43
44
45
46
# File 'lib/sproutcore/builders/base.rb', line 40

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()



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sproutcore/builders/base.rb', line 63

def replace_static_url(line)
  line.gsub(/(sc_static|static_url|sc_target)\(\s*['"](.+)['"]\s*\)/) do | rsrc |
    entry_name = $2
    entry_name = "#{$2}:index.html" if $1 == 'sc_target'
    static_entry = entry.manifest.find_entry($2)
    
    if static_entry.nil?
      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

#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.



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

def static_url(url='')
  ["'", url.gsub('"','\"'),"'"].join('')
end

#writelines(dst_path, lines) ⇒ Object

writes the passed lines to the named file



55
56
57
58
59
60
# File 'lib/sproutcore/builders/base.rb', line 55

def writelines(dst_path, lines)
  FileUtils.mkdir_p(File.dirname(dst_path))
  f = File.open(dst_path, 'w')
  f.write joinlines(lines)
  f.close
end