Module: Packr::FileSystem

Defined in:
lib/packr/file_system.rb

Class Method Summary collapse

Class Method Details

.bundle(source_paths, output_path, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/packr/file_system.rb', line 4

def self.bundle(source_paths, output_path, options)
  output = output_path && File.expand_path(output_path)
  
  sections = source_paths.map do |source|
    path = File.expand_path(source)
    relative = relative_path(path, output)
    {:code => File.read(path), :source => relative}
  end
  
  packed = Packr.pack(sections,
    :minify       => options[:minify],
    :shrink_vars  => options[:shrink_vars],
    :private      => options[:private],
    :base62       => options[:base62],
    :protect      => options[:protect],
    :header       => options[:header],
    :output_file  => output
  )
  source_map = packed.source_map
  
  if output
    FileUtils.mkdir_p(File.dirname(output))
    File.open(output, 'w') { |f| f.write(packed) }
    File.open(source_map.filename, 'w') { |f| f.write(source_map.to_s) }
  end
  
  packed
end

.relative_path(source, target) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/packr/file_system.rb', line 33

def self.relative_path(source, target)
  return source unless target
  
  target_parts = target.split('/')
  source_parts = source.split('/')
  
  while target_parts.first == source_parts.first
    target_parts.shift
    source_parts.shift
  end
  
  ('../' * (target_parts.size-1)) + source_parts.join('/')
end