Class: Grache::Packer

Inherits:
Object
  • Object
show all
Defined in:
lib/grache/packer/packer.rb

Constant Summary collapse

DEFAULT_INSTALL_OPTIONS =
{
}
DEFAULT_PACK_OPTIONS =
{
  dir: '.'
}
DEFAULT_ZIP_OPTIONS =
{
}
CMDS =
{
  # gemfile path
  'bundle-install-no-deployment' => 'bundle install --gemfile=%s --no-deployment',

  'bundle-install-deployment' => 'bundle install --gemfile=%s --deployment',

  # gemfile path
  # deployment path
  'bundle-install-deployment-path' => 'bundle install --gemfile=%s --deployment --path %s',

  # gemfile path
  'bundle-pack' => 'bundle pack --gemfile=%s --all',

  # gemspec path
  'gem-build' => 'gem build %s' # spec path
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exec_cmd(cmd) ⇒ Object



48
49
50
51
# File 'lib/grache/packer/packer.rb', line 48

def exec_cmd(cmd)
  puts cmd
  system(cmd)
end

.find_gemfile(path) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/grache/packer/packer.rb', line 53

def find_gemfile(path)
  gemfile_path = File.join(path, 'Gemfile')
  return gemfile_path if File.exist?(gemfile_path)

  new_path = File.expand_path(File.join(path, '..'))
  return nil if new_path == path

  find_gemfile(new_path)
end

.get_checksum(path) ⇒ Object



63
64
65
66
67
# File 'lib/grache/packer/packer.rb', line 63

def get_checksum(path)
  content = File.open(path).read
  # TODO: Return checksum here
  content
end

.get_gemfile_meta(path) ⇒ Object



69
70
# File 'lib/grache/packer/packer.rb', line 69

def get_gemfile_meta(path)
end

.install(opts = DEFAULT_INSTALL_OPTIONS) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/grache/packer/packer.rb', line 72

def install(opts = DEFAULT_INSTALL_OPTIONS)
  opts = DEFAULT_INSTALL_OPTIONS.merge(opts)

  puts "Installing pack: #{JSON.pretty_generate(opts)}"

  dir = opts[:dir]
  fail ArgumentError, 'No directory specified' unless dir

  dir = File.expand_path(dir)

  gemfile = find_gemfile(dir)
  return unless gemfile

  gem_dir = File.dirname(gemfile)
  vendor_dir = File.join(gem_dir, 'vendor/')

  unless File.directory?(vendor_dir)
    puts "Creating #{vendor_dir}"
    FileUtils.mkdir_p vendor_dir
  end

  FileUtils.rm_rf File.join(vendor_dir, 'cache')

  gemfile_lock = "#{gemfile}.lock"
  sha = Digest::SHA2.file(gemfile_lock).hexdigest

  uri = URI.parse("https://gdc-ms-grache.s3.amazonaws.com/grache-#{sha}.zip")
  puts "Looking for #{uri.to_s}"

  name = uri.path.split('/').last
  FileUtils.rm_rf name if File.exists?(name)

  begin
    Net::HTTP.start(uri.host) do |http|
      resp = http.get(uri.path)
      if(resp.code == '200')
        open(name, 'wb') do |file|
          file.write(resp.body)
        end
      else
        Packer.pack(opts)
        return
      end
    end
  rescue => e
    puts "ERROR: #{e.inspect}"
  end

  Zip::File.open(name) do |zip_file|
    # Handle entries one by one
    zip_file.each do |entry|
      # Extract to file/directory/symlink
      puts "Extracting #{entry.name}"
      out_path = "vendor/#{entry.name}"
      entry.extract(out_path)
    end
  end

  puts "Removing old #{name}"
  FileUtils.rm_rf name
end

.pack(opts = DEFAULT_PACK_OPTIONS) ⇒ Object



134
135
136
137
138
139
140
141
142
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
# File 'lib/grache/packer/packer.rb', line 134

def pack(opts = DEFAULT_PACK_OPTIONS)
  opts = DEFAULT_PACK_OPTIONS.merge(opts)

  dir = opts[:dir]
  fail ArgumentError, 'No directory specified' unless dir

  dir = File.expand_path(dir)
  puts "Packing #{dir}"

  gemfile = find_gemfile(dir)
  return unless gemfile

  puts "Gemfile located at #{gemfile}" if gemfile
  gem_dir = File.dirname(gemfile)

  # TODO: Read gemfile programatically
  uses_gemspec = File.open(gemfile).read.index(/^gemspec$/) != nil
  if(uses_gemspec)
    tmp = File.join(gem_dir, '.gemspec')
    gemspecfile = tmp if File.exist?(tmp)
    gemspecfile = Dir.glob('*.gemspec').first if gemspecfile.nil?
  end

  gemfile_lock = "#{gemfile}.lock"
  unless File.exists?(gemfile_lock)
    cmd = CMDS['bundle-install-no-deployment'] % [gemfile]
    exec_cmd(cmd)
  end

  if uses_gemspec
    cmd = CMDS['gem-build'] % gemspecfile
    exec_cmd(cmd)
  end

  cache_dir = File.join(gem_dir, 'vendor')
  if File.directory?(cache_dir)
    puts "Deleting cache #{cache_dir}"
    FileUtils.rm_rf cache_dir
  end

  cmd = CMDS['bundle-pack'] % gemfile
  exec_cmd(cmd)
end

.zip(opts = DEFAULT_ZIP_OPTIONS) ⇒ Object



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
205
206
207
208
209
# File 'lib/grache/packer/packer.rb', line 178

def zip(opts = DEFAULT_ZIP_OPTIONS)
  opts = DEFAULT_ZIP_OPTIONS.merge(opts)

  puts "Zipping pack: #{JSON.pretty_generate(opts)}"

  dir = opts[:dir]
  fail ArgumentError, 'No directory specified' unless dir

  dir = File.expand_path(dir)
  puts "Zipping #{dir}"

  gemfile = find_gemfile(dir)
  return unless gemfile

  gem_dir = File.dirname(gemfile)
  vendor_dir = File.join(gem_dir, 'vendor/')

  unless File.directory?(vendor_dir)
    puts "Vendor directory does not exists. Run 'grache pack build' first!"
    return
  end

  gemfile_lock = "#{gemfile}.lock"
  sha = Digest::SHA2.file(gemfile_lock).hexdigest

  archive = "grache-#{sha}.zip"
  FileUtils.rm archive, :force => true

  ZipGenerator.new(vendor_dir, archive).write

  puts "Created #{archive}"
end

Instance Method Details

#find_gemfile(path) ⇒ Object



213
214
215
# File 'lib/grache/packer/packer.rb', line 213

def find_gemfile(path)
  Packer.find_gemfile(path)
end

#get_checksum(path) ⇒ Object



217
218
219
# File 'lib/grache/packer/packer.rb', line 217

def get_checksum(path)
  Packer.get_checksum(path)
end

#install(opts = DEFAULT_INSTALL_OPTIONS) ⇒ Object



221
222
223
224
225
# File 'lib/grache/packer/packer.rb', line 221

def install(opts = DEFAULT_INSTALL_OPTIONS)
  opts = DEFAULT_PACK_OPTIONS.merge(opts)

  Packer.install(opts)
end

#pack(opts = DEFAULT_PACK_OPTIONS) ⇒ Object



227
228
229
230
231
# File 'lib/grache/packer/packer.rb', line 227

def pack(opts = DEFAULT_PACK_OPTIONS)
  opts = DEFAULT_PACK_OPTIONS.merge(opts)

  Packer.pack(opts)
end

#zip(opts = DEFAULT_ZIP_OPTIONS) ⇒ Object



233
234
235
236
237
# File 'lib/grache/packer/packer.rb', line 233

def zip(opts = DEFAULT_ZIP_OPTIONS)
  opts = DEFAULT_PACK_OPTIONS.merge(opts)

  Packer.zip(opts)
end