Class: Jsus::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/jsus/package.rb

Overview

Package is a (self-contained) unit with all the info required to build a javascript package.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Package

Constructors

Creates a package from given directory.

Parameters:

  • directory (String)

    path to directory containing a package

Raises:

  • (RuntimeError)

    when the given directory doesn't contain a package.yml or package.json file with meta info.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jsus/package.rb', line 21

def initialize(directory)
  self.directory          = File.expand_path(directory)
  @header = if File.exists?(File.join(directory, 'package.yml'))
    YAML.load_file(File.join(directory, 'package.yml'))
  elsif File.exists?(File.join(directory, 'package.json'))
    JSON.load(Jsus::Util.read_file(File.join(directory, 'package.json')))
  else
    Jsus.logger.fatal "Directory #{directory} does not contain a valid package.yml / package.json file!"
    raise "Directory #{directory} does not contain a valid package.yml / package.json file!"
  end
  Dir.chdir(directory) do
    files.each do |filename|
      source_file = SourceFile.from_file(filename, :namespace => name)
      source_file.package = self
      if source_file
        if source_file.extension?
          extensions << source_file
        else
          source_files << source_file
        end
      else
        Jsus.logger.warn "#{filename} is not found for #{name}"
      end
    end
  end
end

Instance Attribute Details

#directoryObject

directory which this package resides in (full path)



9
10
11
# File 'lib/jsus/package.rb', line 9

def directory
  @directory
end

#poolObject

an instance of Jsus::Pool



11
12
13
# File 'lib/jsus/package.rb', line 11

def pool
  @pool
end

Instance Method Details

#dependenciesArray Also known as: requires, requirements

Returns array dependencies tags for all source files in the package.

Returns:

  • (Array)

    array dependencies tags for all source files in the package



90
91
92
93
94
# File 'lib/jsus/package.rb', line 90

def dependencies
  result = source_files.map {|source| source.dependencies }.flatten
  result -= provides
  result
end

#descriptionString

Returns a package description.

Returns:

  • (String)

    a package description.



65
66
67
# File 'lib/jsus/package.rb', line 65

def description
  header["description"] ||= ""
end

#extensionsArray

Array with extensions

Returns:

  • (Array)


129
130
131
# File 'lib/jsus/package.rb', line 129

def extensions
  @extensions ||= []
end

#filenameString

Returns a filename for compiled package.

Returns:

  • (String)

    a filename for compiled package.



71
72
73
# File 'lib/jsus/package.rb', line 71

def filename
  header["filename"] ||= Jsus::Util::Inflection.snake_case(name) + ".js"
end

#filesArray

Returns a list of sources filenames.

Returns:

  • (Array)

    a list of sources filenames.



77
78
79
# File 'lib/jsus/package.rb', line 77

def files
  header["files"] = header["files"] || header["sources"] || []
end

#headerHash

Returns parsed package header.

Returns:

  • (Hash)

    parsed package header.



53
54
55
# File 'lib/jsus/package.rb', line 53

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

Parameters:

  • new_header (Hash)

    parsed header



138
139
140
# File 'lib/jsus/package.rb', line 138

def header=(new_header)
  @header = new_header
end

#nameString

Returns a package name.

Returns:

  • (String)

    a package name.



59
60
61
# File 'lib/jsus/package.rb', line 59

def name
  header["name"] ||= ""
end

#providesArray Also known as: provisions

Returns an array of provided tags.

Returns:

  • (Array)

    an array of provided tags



83
84
85
# File 'lib/jsus/package.rb', line 83

def provides
  source_files.map {|s| s.provides }.flatten
end

#required_filesArray

Lists the required files for the package.

Returns:

  • (Array)

    ordered list of full paths to required files.



101
102
103
# File 'lib/jsus/package.rb', line 101

def required_files
  source_files.map {|s| s.required_files }.flatten
end

#source_filesArray

Array with source files

Returns:

  • (Array)


122
123
124
# File 'lib/jsus/package.rb', line 122

def source_files
  @source_files ||= []
end

#to_hashHash

Hash representation of the package.

Returns:

  • (Hash)


108
109
110
111
112
113
114
115
116
# File 'lib/jsus/package.rb', line 108

def to_hash
  {
    name => {
      :description => description,
      :provides    => provides.map {|tag| tag.name },
      :requires    => requires.map {|tag| tag.namespace == name ? tag.name : tag.full_name }
    }
  }
end