Class: OSGi::RootFilter

Inherits:
Buildr::Filter
  • Object
show all
Defined in:
lib/buildr4osgi/osgi/packaging.rb

Instance Method Summary collapse

Instance Method Details

#pattern_match(file, pattern) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/buildr4osgi/osgi/packaging.rb', line 152

def pattern_match(file, pattern)
  case
  when pattern.is_a?(Regexp)
    return file.match(pattern)
  when pattern.is_a?(String)
    return File.fnmatch(pattern, file)
  when pattern.is_a?(Proc)
    return pattern.call(file)
  else
    raise "Cannot interpret pattern #{pattern}"
  end
end

#runObject

:call-seq:

run => boolean

Runs the filter.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/buildr4osgi/osgi/packaging.rb', line 168

def run
  sources.each { |source| raise "Source directory #{source} doesn't exist" unless File.exist?(source.to_s) }
  raise 'No target directory specified, where am I going to copy the files to?' if target.nil?

  copy_map = sources.flatten.map(&:to_s).inject({}) do |map, source|
    files = Util.recursive_with_dot_files(source).
      map { |file| Util.relative_path(file, source) }.
      select { |file| @include.empty? || @include.any? { |pattern| pattern_match(file, pattern) } }.
      reject { |file| @exclude.any? { |pattern| pattern_match(file, pattern) } }
    files.each do |file|
      src, dest = File.expand_path(file, source), File.expand_path(file, target.to_s)
      map[file] = src if !File.exist?(dest) || File.stat(src).mtime >= File.stat(dest).mtime
    end
    map
  end
    
  mkpath target.to_s
  return false if copy_map.empty?

  copy_map.each do |path, source|
    dest = File.expand_path(path, target.to_s)
    if File.directory?(source)
      mkpath dest
    else
      mkpath File.dirname(dest)
      if @mapper.mapper_type
        mapped = @mapper.transform(File.open(source, 'rb') { |file| file.read }, path)
        File.open(dest, 'wb') { |file| file.write mapped }
      else # no mapping
        cp source, dest
        File.chmod(0664, dest)
      end
    end
  end
  touch target.to_s
  true
end