Class: Jets::Gems::Extract::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/gems/extract/base.rb

Direct Known Subclasses

Gem

Defined Under Namespace

Classes: NotFound

Constant Summary collapse

@@log_level =

default level is :info

:info

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
# File 'lib/jets/gems/extract/base.rb', line 8

def initialize(name, options = {})
  @name = name
  @options = options
  ruby_folder = Jets::Gems.ruby_folder
  @downloads_root = options[:downloads_root] || "/tmp/jets/#{Jets.config.project_name}/serverlessgems/#{ruby_folder}"
end

Instance Attribute Details

#project_rootObject (readonly)

Returns the value of attribute project_root.



7
8
9
# File 'lib/jets/gems/extract/base.rb', line 7

def project_root
  @project_root
end

Instance Method Details

#clean_downloads(folder) ⇒ Object



15
16
17
18
19
# File 'lib/jets/gems/extract/base.rb', line 15

def clean_downloads(folder)
  path = "#{@downloads_root}/downloads/#{folder}"
  say "Removing cache: #{path}"
  FileUtils.rm_rf(path)
end

#download_file(url, dest) ⇒ Object

Returns the dest path



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jets/gems/extract/base.rb', line 37

def download_file(url, dest)
  if File.exist?(dest) && ENV["SG_FORCE_DOWNLOAD"].blank?
    say "File already downloaded #{dest}"
    return dest
  end

  say "Downloading..."
  downloaded = URI.open(url, "rb") { |read_file| read_file.read }

  FileUtils.mkdir_p(File.dirname(dest)) # ensure parent folder exists

  File.binwrite(dest, downloaded)

  dest
end

#log_level=(val) ⇒ Object

@@log_level = :debug # uncomment to debug



55
56
57
# File 'lib/jets/gems/extract/base.rb', line 55

def log_level=(val)
  @@log_level = val
end

#say(message, level = :info) ⇒ Object



59
60
61
62
# File 'lib/jets/gems/extract/base.rb', line 59

def say(message, level = :info)
  enabled = @@log_level == :debug || level == :debug
  puts(message) if enabled
end

#sh(command) ⇒ Object



29
30
31
32
33
34
# File 'lib/jets/gems/extract/base.rb', line 29

def sh(command)
  say "=> #{command}".color(:green)
  success = system(command)
  abort("Command Failed #{command}") unless success
  success
end

#unzip(zipfile_path, parent_folder_dest) ⇒ Object

Using ‘ > /dev/null 2>&1` to suppress stderr message:

lchmod (file attributes) error: Function not implemented


25
26
27
# File 'lib/jets/gems/extract/base.rb', line 25

def unzip(zipfile_path, parent_folder_dest)
  sh("cd #{parent_folder_dest} && unzip -qo #{zipfile_path} > /dev/null 2>&1")
end