Module: Rpack::Packer

Included in:
Rpack
Defined in:
lib/packer.rb

Instance Method Summary collapse

Instance Method Details

#get_pack_list(verbose = true) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/packer.rb', line 22

def get_pack_list(verbose=true)
   list = {}
   extracted = {}

   for pattern in @patterns
      @singular   = pattern.singularize
      @plural     = singular.pluralize
      puts "processing #{@plural} ..." if verbose

      for option in @parser.options
         config   = @config[option]
         paths    = config["paths"]
         key      = config["plural"] ? @plural : @singular
         suffix   = config["suffix"]
         dir      = config["dir"] 
         inside   = config["inside"]
         extract  = config["extract"]
         begin_p  = config["begin_pattern"]
         end_p    = config["end_pattern"]

         list[option] ||= []

         for path in paths
            file     = File.expand_path("#{@basedir}/#{path}#{inside ? '' : key}#{suffix}")
            flist    = dir ? Dir.glob(File.expand_path("#{file}/**")) : Dir.glob(file)
            flist.sort!
            for f in flist
               incfile = true
               next if !File.exist?(f)
               contents = File.readlines(f)
               if inside
                  insidecontent = extract_contents(contents,key)
                  next if insidecontent.size<1
               end
               if extract
                  contents = extract_contents(contents,key,begin_p,end_p)
                  incfile  = contents.size>0
                  if incfile && extracted[f]
                     contents = extracted[f]+contents
                  end
               end
               extracted[f] = contents
               list[option] << f if incfile
            end
         end
      end
   end
   [list,extracted]
end

#packObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/packer.rb', line 3

def pack
   filename = @parser.package || "#{@patterns.sort.join}.zip"
   puts "Using basedir #{@basedir}"
   puts "Packing to #{filename} ..."

   zip = Zip::ZipOutputStream.open(filename)
   list, extracted = get_pack_list

   for key,value in list 
      for file in value.sort
         content  = extracted[file]
         entry    = file.gsub(@basedir,"").gsub(/^\//,"")
         zip.put_next_entry(entry)
         zip.write content.join
      end
   end
   zip.close
end