Class: SkadateGems::Dev::Bundle

Inherits:
Object
  • Object
show all
Defined in:
lib/skadategems/dev/bundle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(skadate) ⇒ Bundle

Bundler instance initializer.

Parameters:



16
17
18
19
20
21
22
# File 'lib/skadategems/dev/bundle.rb', line 16

def initialize(skadate)
  @source     = skadate.source
  @internal_c = @source.internal_c
  @external_c = @source.external_c
  @userfiles  = @source.userfiles
  @themes_dir = @source.filename('layout/themes/')
end

Instance Attribute Details

#layout_themeObject

Returns the value of attribute layout_theme.



11
12
13
# File 'lib/skadategems/dev/bundle.rb', line 11

def layout_theme
  @layout_theme
end

Instance Method Details

#gzip(filename = nil) ⇒ StringIO|Fixnum

gzips the underlying string in the given ‘StringIO`, returning a new `StringIO` representing the compressed file.

Parameters:

  • filename (String) (defaults to: nil)

    optional output filename

Returns:

  • (StringIO|Fixnum)


94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/skadategems/dev/bundle.rb', line 94

def gzip(filename = nil)
  gz = StringIO.new('')
  z = Zlib::GzipWriter.new(gz)
  z.write(tar.string)
  z.close # this is necessary!

  if filename
    File.binwrite(filename, gz.string)
  else
    StringIO.new(gz.string)
  end
end

#should_include?(filename) ⇒ boolean

Filter files by their filename.

Parameters:

  • filename (String)

    absolute path to a file

Returns:

  • (boolean)

    true if given filename should be bundled



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/skadategems/dev/bundle.rb', line 28

def should_include?(filename)
  return false if filename.start_with?(@internal_c, @external_c, @userfiles) ||
      filename.end_with?('.tar', '.gz', '.zip', '.swp', '~', 'Thumbs.db', 'desktop.ini')

  if filename.start_with?(@themes_dir) && layout_theme &&
    !filename.start_with?(File.join(@themes_dir, layout_theme))
    return false
  end

  true
end

#tarStringIO

Create a tar file in memory.

Returns:

  • (StringIO)

    whose underlying String is the contents of the tar file



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/skadategems/dev/bundle.rb', line 43

def tar
  return @tar if @tar

  string_io = StringIO.new('')
  Gem::Package::TarWriter.new(string_io) do |tar|

    htaccess = File.join(@source.root, '.htaccess')
    if File.exists?(htaccess)
      tar.add_file '.htaccess', 0644 do |tar_file|
        File.open(htaccess, 'rb') { |file| tar_file.write(file.read) }
      end
    end

    Dir[File.join(@source.root, '**/*')].each do |filename|
      next unless should_include?(filename)

      if File.directory?(filename)
        tar.mkdir (relative_path filename), 0755

        htaccess = File.join(filename, '.htaccess')
        if File.exists?(htaccess)
          filename = htaccess # iteration continues...
        else
          next
        end
      end

      tar.add_file (relative_path filename), 0644 do |tar_file|
        File.open(filename, 'rb') { |file| tar_file.write(file.read) }
      end
    end

    [ (int_c = relative_path @internal_c),
      "#{int_c}/cache",
      "#{int_c}/components",
      "#{int_c}/forms",
      "#{int_c}/lang",
      relative_path(@external_c),
      relative_path(@userfiles),
    ].each { |dir| tar.mkdir dir, 0777 }
  end

  string_io.rewind
  @tar = string_io
end