Class: Jsus::SourceFile
- Inherits:
-
Object
- Object
- Jsus::SourceFile
- Defined in:
- lib/jsus/source_file.rb
Overview
SourceFile is a base for any Jsus operation.
It contains general info about source as well as file content.
Instance Attribute Summary collapse
-
#filename ⇒ Object
Full filename.
-
#package ⇒ Object
Package owning the sourcefile.
-
#relative_filename ⇒ Object
Filename relative to package root.
Class Method Summary collapse
-
.from_file(filename, options = {}) ⇒ Jsus::SourceFile
Initializes a SourceFile given the filename and options.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#content ⇒ String
File contents, including extensions.
- #content=(new_value) ⇒ Object private
-
#dependencies ⇒ Array
(also: #requires)
List of dependencies for given file.
-
#dependencies_names(options = {}) ⇒ Array
(also: #requires_names)
Array with names of dependencies.
-
#description ⇒ String
Description of the source file.
- #eql?(other) ⇒ Boolean
-
#extends ⇒ Object
Bar.js in package Bar extends 'Core/Class'.
-
#extension? ⇒ Boolean
Whether the source file is an extension.
-
#extensions ⇒ Array
New_value array of included extensions for given source.
- #extensions=(new_value) ⇒ Object
-
#external_dependencies ⇒ Array
Array of external dependencies tags.
- #external_dependencies_names ⇒ Object
- #hash ⇒ Object
-
#header ⇒ Hash
A header parsed from YAML-formatted source file first comment.
-
#header=(new_header) ⇒ Object
private
Parses header and gets info from it.
-
#include_extensions ⇒ Object
Looks up for extensions in the pool and then includes extensions for all the provides tag this source file has.
- #include_extensions! ⇒ Object
-
#initialize(options = {}) ⇒ SourceFile
constructor
Basic constructor.
-
#inspect ⇒ String
Human readable description of source file.
-
#original_content ⇒ String
Original file contents.
-
#parse_tag_list(tag_list) ⇒ Array
private
Normalized tags list.
-
#pool ⇒ Jsus::Pool
A pool which the source file is assigned to.
-
#pool=(new_value) ⇒ Object
private
Assigns an instance of Jsus::Pool to the source file.
-
#provides ⇒ Array
Array with provides tags.
-
#provides_names(options = {}) ⇒ Array
Array with provides names.
-
#replaces ⇒ Jsus::Tag
Tag for replaced file, if any.
-
#required_files ⇒ Array
Array of files required by this files including all the filenames for extensions.
-
#to_hash ⇒ Hash
Hash containing basic info with dependencies/provides tags' names and description for source file.
Constructor Details
#initialize(options = {}) ⇒ SourceFile
Basic constructor.
You probably should use SourceFile.from_file instead of this one.
33 34 35 36 37 |
# File 'lib/jsus/source_file.rb', line 33 def initialize( = {}) [:package, :header, :relative_filename, :filename, :content, :pool].each do |field| send("#{field}=", [field]) if [field] end end |
Instance Attribute Details
#filename ⇒ Object
Full filename
14 15 16 |
# File 'lib/jsus/source_file.rb', line 14 def filename @filename end |
#package ⇒ Object
Package owning the sourcefile
16 17 18 |
# File 'lib/jsus/source_file.rb', line 16 def package @package end |
#relative_filename ⇒ Object
Filename relative to package root
12 13 14 |
# File 'lib/jsus/source_file.rb', line 12 def relative_filename @relative_filename end |
Class Method Details
.from_file(filename, options = {}) ⇒ Jsus::SourceFile
Initializes a SourceFile given the filename and options
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/jsus/source_file.rb', line 49 def self.from_file(filename, = {}) if File.exists?(filename) source = File.open(filename, 'r:utf-8') {|f| f.read } bom = RUBY_VERSION =~ /1.9/ ? "\uFEFF" : "\xEF\xBB\xBF" source.gsub!(bom, "") yaml_data = source.match(%r(^/\*\s*(---.*?)\*/)m) if (yaml_data && yaml_data[1] && header = YAML.load(yaml_data[1])) [:header] = header [:relative_filename] = filename [:filename] = File.(filename) [:content] = source new() else raise BadSourceFileException, "#{filename} is missing a header or header is invalid" end else raise BadSourceFileException, "Referenced #{filename} does not exist. #{[:package] ? "Referenced from package #{[:package].name}" : ""}" end rescue Exception => e if !e.kind_of?(BadSourceFileException) # if we didn't raise the error; like in YAML, for example raise "Exception #{e.inspect} happened on #{filename}. Please take appropriate measures" else # if we did it, just reraise raise e end end |
Instance Method Details
#==(other) ⇒ Object
302 303 304 |
# File 'lib/jsus/source_file.rb', line 302 def ==(other) eql?(other) end |
#content ⇒ String
Returns file contents, including extensions.
253 254 255 256 |
# File 'lib/jsus/source_file.rb', line 253 def content include_extensions [@content, extensions.map {|e| e.content}].flatten.compact.join("\n") end |
#content=(new_value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
247 248 249 |
# File 'lib/jsus/source_file.rb', line 247 def content=(new_value) @content = new_value end |
#dependencies ⇒ Array Also known as: requires
Returns list of dependencies for given file.
92 93 94 |
# File 'lib/jsus/source_file.rb', line 92 def dependencies @dependencies end |
#dependencies_names(options = {}) ⇒ Array Also known as: requires_names
Returns array with names of dependencies. Unordered.
107 108 109 |
# File 'lib/jsus/source_file.rb', line 107 def dependencies_names( = {}) dependencies.map {|d| d.name() } end |
#description ⇒ String
Returns description of the source file.
86 87 88 |
# File 'lib/jsus/source_file.rb', line 86 def description header["description"] end |
#eql?(other) ⇒ Boolean
307 308 309 |
# File 'lib/jsus/source_file.rb', line 307 def eql?(other) other.kind_of?(SourceFile) && filename == other.filename end |
#extends ⇒ Object
Bar.js in package Bar extends 'Core/Class'. That means its contents would be appended to the Foo.js when compiling the result.
151 152 153 |
# File 'lib/jsus/source_file.rb', line 151 def extends @extends end |
#extension? ⇒ Boolean
Returns whether the source file is an extension.
157 158 159 |
# File 'lib/jsus/source_file.rb', line 157 def extension? extends && !extends.empty? end |
#extensions ⇒ Array
Returns new_value array of included extensions for given source.
163 164 165 166 167 |
# File 'lib/jsus/source_file.rb', line 163 def extensions @extensions ||= [] @extensions = @extensions.flatten.compact.uniq @extensions end |
#extensions=(new_value) ⇒ Object
171 172 173 |
# File 'lib/jsus/source_file.rb', line 171 def extensions=(new_value) @extensions = new_value end |
#external_dependencies ⇒ Array
Returns array of external dependencies tags. Unordered.
114 115 116 |
# File 'lib/jsus/source_file.rb', line 114 def external_dependencies dependencies.select {|d| d.external? } end |
#external_dependencies_names ⇒ Object
120 121 122 |
# File 'lib/jsus/source_file.rb', line 120 def external_dependencies_names external_dependencies.map {|d| d.name } end |
#hash ⇒ Object
312 313 314 |
# File 'lib/jsus/source_file.rb', line 312 def hash [self.class, filename].hash end |
#header ⇒ Hash
Returns a header parsed from YAML-formatted source file first comment.
79 80 81 82 |
# File 'lib/jsus/source_file.rb', line 79 def header self.header = {} unless @header @header end |
#header=(new_header) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses header and gets info from it.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/jsus/source_file.rb', line 224 def header=(new_header) @header = new_header # prepare defaults @header["description"] ||= "" # handle tags @dependencies = parse_tag_list(Array(@header["requires"])) @provides = parse_tag_list(Array(@header["provides"])) @extends = case @header["extends"] when Array then Tag.new(@header["extends"][0]) when String then Tag.new(@header["extends"]) else nil end @replaces = case @header["replaces"] when Array then Tag.new(@header["replaces"][0]) when String then Tag.new(@header["replaces"]) else nil end end |
#include_extensions ⇒ Object
Looks up for extensions in the pool and then includes extensions for all the provides tag this source file has. Caches the result.
180 181 182 |
# File 'lib/jsus/source_file.rb', line 180 def include_extensions @included_extensions ||= include_extensions! end |
#include_extensions! ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/jsus/source_file.rb', line 186 def include_extensions! if pool provides.each do |p| extensions << pool.lookup_extensions(p) end end end |
#inspect ⇒ String
Human readable description of source file.
217 218 219 |
# File 'lib/jsus/source_file.rb', line 217 def inspect self.to_hash.inspect end |
#original_content ⇒ String
Returns Original file contents.
260 261 262 |
# File 'lib/jsus/source_file.rb', line 260 def original_content @content end |
#parse_tag_list(tag_list) ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns normalized tags list.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/jsus/source_file.rb', line 267 def parse_tag_list(tag_list) tag_list.map do |tag_name| case tag_name when String Tag.new(tag_name, :package => package) when Hash = [] tag_name.each do |pkg_name, sources| normalized_package_name = pkg_name.sub(/(.+)\/.*$/, "\\1") Array(sources).each do |source| << Tag.new([normalized_package_name, source].join("/")) end end end end.flatten end |
#pool ⇒ Jsus::Pool
A pool which the source file is assigned to. Used in #include_extensions!
297 298 299 |
# File 'lib/jsus/source_file.rb', line 297 def pool @pool end |
#pool=(new_value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Assigns an instance of Jsus::Pool to the source file. Also performs push to that pool.
289 290 291 292 |
# File 'lib/jsus/source_file.rb', line 289 def pool=(new_value) @pool = new_value @pool << self if @pool end |
#provides ⇒ Array
Returns array with provides tags.
126 127 128 |
# File 'lib/jsus/source_file.rb', line 126 def provides @provides end |
#provides_names(options = {}) ⇒ Array
Returns array with provides names.
135 136 137 |
# File 'lib/jsus/source_file.rb', line 135 def provides_names( = {}) provides.map {|p| p.name()} end |
#replaces ⇒ Jsus::Tag
Returns tag for replaced file, if any.
141 142 143 |
# File 'lib/jsus/source_file.rb', line 141 def replaces @replaces end |
#required_files ⇒ Array
Returns array of files required by this files including all the filenames for extensions. SourceFile filename always goes first, all the extensions are unordered.
197 198 199 200 |
# File 'lib/jsus/source_file.rb', line 197 def required_files include_extensions [filename, extensions.map {|e| e.filename}].flatten end |
#to_hash ⇒ Hash
Returns hash containing basic info with dependencies/provides tags' names and description for source file.
206 207 208 209 210 211 212 |
# File 'lib/jsus/source_file.rb', line 206 def to_hash { "desc" => description, "requires" => dependencies_names(:short => true), "provides" => provides_names(:short => true) } end |