Guest User

Untitled

a guest
Jun 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. # ===========================================================================
  2. # Project: Abbot - SproutCore Build Tools
  3. # Copyright: ©2009 Apple Inc.
  4. # portions copyright @2006-2009 Sprout Systems, Inc.
  5. # and contributors
  6. # ===========================================================================
  7.  
  8. module SC
  9.  
  10. # Builder classes implement the more complex algorithms for building
  11. # resources in SproutCore such as building HTML, JavaScript or CSS.
  12. # Builders are usually invoked from within build tasks which are, in-turn,
  13. # selected by the manifest.
  14. #
  15. module Builder
  16.  
  17. # The base class extended by most builder classes. This contains some
  18. # default functionality for handling loading and writing files. Usually
  19. # you will want to consult the specific classes instead for more info.
  20. #
  21. class Base
  22. # entry the current builder is working on
  23. attr_accessor :entry
  24.  
  25. def initialize(entry=nil)
  26. @entry =entry
  27. end
  28.  
  29. # override this method in subclasses to actually do build
  30. def build(dst_path)
  31. end
  32.  
  33. # main entry called by build tasks
  34. def self.build(entry, dst_path)
  35. new(entry).build(dst_path)
  36. end
  37.  
  38. # Reads the lines from the source file. If the source file does not
  39. # exist, returns empty array.
  40. def readlines(src_path)
  41. if File.exist?(src_path) && !File.directory?(src_path)
  42. File.readlines(src_path)
  43. else
  44. []
  45. end
  46. end
  47.  
  48. # joins the array of lines. this is where you can also do any final
  49. # post-processing on the build
  50. def joinlines(lines)
  51. lines.is_a?(Array) ? lines.join : lines
  52. end
  53.  
  54. # writes the passed lines to the named file
  55. def writelines(dst_path, lines)
  56. FileUtils.mkdir_p(File.dirname(dst_path))
  57. File.open(dst_path, 'w') do |f|
  58. f.write joinlines(lines)
  59. end
  60. end
  61.  
  62. # Handles occurances of sc_static() or static_url()
  63. def replace_static_url(line)
  64. line.gsub!(/(sc_static|static_url|sc_target)\(\s*['"](.+?)['"]\s*\)/) do | rsrc |
  65. entry_name = $2
  66. entry_name = "#{$2}:index.html" if $1 == 'sc_target'
  67. static_entry = entry.manifest.find_entry($2)
  68.  
  69. if !static_entry
  70. url = ''
  71. elsif $1 == 'sc_target'
  72. url = static_entry[:friendly_url] || static_entry.cacheable_url
  73. else
  74. url = static_entry.cacheable_url
  75. end
  76.  
  77. static_url(url)
  78. end
  79. end
  80.  
  81. # Generates the proper output for a given static url and a given target
  82. # this is often overridden by subclasses. the default just wraps in
  83. # quotes.
  84. def static_url(url='')
  85. "'#{url.gsub('"', '\"')}'"
  86. end
  87. end # class
  88.  
  89. end # module Builder
  90. end # module SC
Add Comment
Please, Sign In to add comment