Class: Linecook::Package

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = {}) ⇒ Package

Returns a new instance of Package.



20
21
22
23
24
25
# File 'lib/linecook/package.rb', line 20

def initialize(env={})
  @env = env
  @registry = {}
  @export_options = {}
  @default_export_options  = {}
end

Instance Attribute Details

#default_export_optionsObject (readonly)

A hash of default export options.



18
19
20
# File 'lib/linecook/package.rb', line 18

def default_export_options
  @default_export_options
end

#envObject (readonly)

The package environment



7
8
9
# File 'lib/linecook/package.rb', line 7

def env
  @env
end

#export_optionsObject (readonly)

A hash of (path, Hash) pairs identifing export options for a package file. See on_export.



15
16
17
# File 'lib/linecook/package.rb', line 15

def export_options
  @export_options
end

#registryObject (readonly)

A registry of (path, source_path) pairs recording what files are included in the package.



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

def registry
  @registry
end

Instance Method Details

#add(path, options = {}) ⇒ Object

Generates a tempfile and registers it into the package at the specified path. Returns the open tempfile.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/linecook/package.rb', line 71

def add(path, options={})
  options  = {
    :move => true
  }.merge(options)

  # preserve a reference to tempfile in options so that it will not be
  # unlinked before it can be moved into the package during export
  tempfile = Tempfile.new File.basename(path)
  options[:tempfile] = tempfile

  if block_given?
    begin
      yield tempfile
    ensure
      tempfile.close
    end
  end

  register path, tempfile.path, options
  tempfile
end

#add_dir(path, options = {}) ⇒ Object

Adds an empty dir at path. Returns nil.



94
95
96
# File 'lib/linecook/package.rb', line 94

def add_dir(path, options={})
  register path, :dir, options
end

#content(path, length = nil, offset = nil) ⇒ Object

Returns the content to be added to the package at the path. Returns nil if nothing is registered.



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

def content(path, length=nil, offset=nil)
  source = source_path(path)
  source ? File.read(source, length, offset) : nil
end

#export(dir) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/linecook/package.rb', line 143

def export(dir)
  registry.keys.sort.each do |path|
    target_path = File.join(dir, path)
    source_path = registry[path]
    options     = export_options[path] || default_export_options

    if source_path != target_path
      if File.exists?(target_path)
        if block_given?
          unless yield(source_path, target_path)
            next
          end
        else
          raise "already exists: #{target_path.inspect}"
        end
      end

      target_dir = File.dirname(target_path)
      FileUtils.mkdir_p(target_dir)

      case source_path
      when :file
        FileUtils.touch target_path
      when :dir
        FileUtils.mkdir target_path
      else
        if File.directory?(source_path)
          export_dir(source_path, target_path, options)
        else
          export_file(source_path, target_path, options)
        end
      end
    end

    if mode = options[:mode]
      FileUtils.chmod(mode, target_path)
    end

    registry[path] = target_path
  end

  registry
end

#next_path(path = 'file') ⇒ Object

Increments path until an unregistered path is found and returns the result in the format “path.count”.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/linecook/package.rb', line 128

def next_path(path='file')
  count = 0
  registry.each_key do |current|
    if current.index(path) == 0
      count += 1
    end
  end

  if count > 0
    path = "#{path}.#{count}"
  end

  path
end

#on_export(path, options = {}) ⇒ Object

Sets export options for the package file. Available options (as symbols):

move:: When set to true the source will be moved into place
       rather than copied (the default)
mode:: Sets the mode of the package file

Unless specified, the values in default_export_options will be used.



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

def on_export(path, options={})
  export_options[path] = default_export_options.merge(options)
end

#paths(source_path) ⇒ Object

Returns an array of paths that the source path is registered to.



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

def paths(source_path)
  source = resolve_source_path(source_path)

  paths = []
  registry.each_pair do |path, current|
    if current == source
      paths << path
    end
  end
  paths
end

#register(path, source_path, options = {}) ⇒ Object

Registers a file into the package with the specified export options. The source path should be the path to a file or directory to include. To make an empty file or directory use :file or :dir as the source_path.

Raises an error if a source is already registered at the path.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/linecook/package.rb', line 32

def register(path, source_path, options={})
  package_path = path.to_s.strip

  if package_path.empty?
    raise "invalid package path: #{path.inspect}"
  end

  if registry.has_key?(package_path)
    raise "already registered: #{path.inspect}"
  end

  source_path = resolve_source_path(source_path)
  registry[package_path] = source_path
  on_export(package_path, options)

  source_path
end

#source_path(path) ⇒ Object

Returns the source path registered at the path, or nil if no source is registered.



102
103
104
# File 'lib/linecook/package.rb', line 102

def source_path(path)
  registry[path]
end

#unregister(path) ⇒ Object Also known as: rm

Removes a file from the package. Returns the source path if one was registered.



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

def unregister(path)
  registry.delete(path)
  export_options.delete(path)
end