Class: Jsus::SourceFile

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SourceFile

Basic constructor.

You probably should use SourceFile.from_file instead of this one.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :package (Jsus::Package)

    package to assign source file to.

  • :relative_filename (String)

    used in Package to generate tree structure of the source files

  • :filename (String)

    full filename for the given source file

  • :content (String)

    file content of the source file

  • :pool (Jsus::Pool)

    owner pool for that file

  • :header (String)

    header of the file



33
34
35
36
37
# File 'lib/jsus/source_file.rb', line 33

def initialize(options = {})
  [:package, :header, :relative_filename, :filename, :content, :pool].each do |field|
    send("#{field}=", options[field]) if options[field]
  end
end

Instance Attribute Details

#filenameObject

Full filename



14
15
16
# File 'lib/jsus/source_file.rb', line 14

def filename
  @filename
end

#packageObject

Package owning the sourcefile



16
17
18
# File 'lib/jsus/source_file.rb', line 16

def package
  @package
end

#relative_filenameObject

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

Parameters:

  • filename (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

Returns:

Raises:



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, options = {})
  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]))
      options[:header]            = header
      options[:relative_filename] = filename
      options[:filename]          = File.expand_path(filename)
      options[:content]           = source
      new(options)
    else
      raise BadSourceFileException, "#{filename} is missing a header or header is invalid"
    end
  else
    raise BadSourceFileException, "Referenced #{filename} does not exist. #{options[:package] ? "Referenced from package #{options[: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

#contentString

Returns file contents, including extensions.

Returns:

  • (String)

    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.

Parameters:

  • new_value (String)

    file content



247
248
249
# File 'lib/jsus/source_file.rb', line 247

def content=(new_value)
  @content = new_value
end

#dependenciesArray Also known as: requires

Returns list of dependencies for given file.

Returns:

  • (Array)

    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.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :short (Boolean)

    whether inner dependencies should not prepend package name, e.g. 'Class' instead of 'Core/Class' when in package 'Core'.

    Note Doesn't change anything for external dependencies

Returns:

  • (Array)

    array with names of dependencies. Unordered.



107
108
109
# File 'lib/jsus/source_file.rb', line 107

def dependencies_names(options = {})
  dependencies.map {|d| d.name(options) }
end

#descriptionString

Returns description of the source file.

Returns:

  • (String)

    description of the source file.



86
87
88
# File 'lib/jsus/source_file.rb', line 86

def description
  header["description"]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/jsus/source_file.rb', line 307

def eql?(other)
  other.kind_of?(SourceFile) && filename == other.filename
end

#extendsObject

Bar.js in package Bar extends 'Core/Class'. That means its contents would be appended to the Foo.js when compiling the result.

Examples:

file Foo.js in package Core provides ['Class', 'Hash']. File



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.

Returns:

  • (Boolean)

    whether the source file is an extension.



157
158
159
# File 'lib/jsus/source_file.rb', line 157

def extension?
  extends && !extends.empty?
end

#extensionsArray

Returns new_value array of included extensions for given source.

Returns:

  • (Array)

    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

Parameters:

  • new_value (Array)

    list of extensions for given file



171
172
173
# File 'lib/jsus/source_file.rb', line 171

def extensions=(new_value)
  @extensions = new_value
end

#external_dependenciesArray

Returns array of external dependencies tags. Unordered.

Returns:

  • (Array)

    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_namesObject



120
121
122
# File 'lib/jsus/source_file.rb', line 120

def external_dependencies_names
  external_dependencies.map {|d| d.name }
end

#hashObject



312
313
314
# File 'lib/jsus/source_file.rb', line 312

def hash
  [self.class, filename].hash
end

#headerHash

Returns a header parsed from YAML-formatted source file first comment.

Returns:

  • (Hash)

    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.

Parameters:

  • new_header (String)

    header content



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_extensionsObject

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

#inspectString

Human readable description of source file.

Returns:

  • (String)


217
218
219
# File 'lib/jsus/source_file.rb', line 217

def inspect
  self.to_hash.inspect
end

#original_contentString

Returns Original file contents.

Returns:

  • (String)

    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.

Parameters:

  • tag_list (Enumerable)

    list of tags

Returns:

  • (Array)

    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
      tags = []
      tag_name.each do |pkg_name, sources|
        normalized_package_name = pkg_name.sub(/(.+)\/.*$/, "\\1")
        Array(sources).each do |source|
          tags << Tag.new([normalized_package_name, source].join("/"))
        end
      end
      tags
    end
  end.flatten
end

#poolJsus::Pool

A pool which the source file is assigned to. Used in #include_extensions!

Returns:



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.

Parameters:



289
290
291
292
# File 'lib/jsus/source_file.rb', line 289

def pool=(new_value)
  @pool = new_value
  @pool << self if @pool
end

#providesArray

Returns array with provides tags.

Returns:

  • (Array)

    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.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :short (Boolean)

    whether provides should not prepend package name, e.g. 'Class' instead of 'Core/Class' when in package 'Core'.

Returns:

  • (Array)

    array with provides names.



135
136
137
# File 'lib/jsus/source_file.rb', line 135

def provides_names(options = {})
  provides.map {|p| p.name(options)}
end

#replacesJsus::Tag

Returns tag for replaced file, if any.

Returns:

  • (Jsus::Tag)

    tag for replaced file, if any



141
142
143
# File 'lib/jsus/source_file.rb', line 141

def replaces
  @replaces
end

#required_filesArray

Returns array of files required by this files including all the filenames for extensions. SourceFile filename always goes first, all the extensions are unordered.

Returns:

  • (Array)

    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_hashHash

Returns hash containing basic info with dependencies/provides tags' names and description for source file.

Returns:

  • (Hash)

    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