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

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

Direct Known Subclasses

Gem, Ruby

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
14
# File 'lib/jets/gems/extract/base.rb', line 8

def initialize(name, options={})
  @name = name
  @options = options

  @downloads_root = options[:downloads_root] || "/tmp/jets/#{Jets.config.project_name}/lambdagems"
  @source_url = options[:source_url] || Jets.default_gems_source
end

Instance Attribute Details

#source_urlObject (readonly)

Returns the value of attribute source_url.



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

def source_url
  @source_url
end

Instance Method Details

#clean_downloads(folder) ⇒ Object



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

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

#download_file(source_url, dest) ⇒ Object

Returns the dest path



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jets/gems/extract/base.rb', line 39

def download_file(source_url, dest)
  say "Url #{source_url}"
  return unless url_exists?(source_url)

  if File.exist?(dest)
    say "File already downloaded #{dest}"
    return dest
  end

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

  File.open(dest, 'wb') do |saved_file|
    open(source_url, 'rb') do |read_file|
      saved_file.write(read_file.read)
    end
  end
  dest
end

#log_level=(val) ⇒ Object

@@log_level = :debug # uncomment to debug



65
66
67
# File 'lib/jets/gems/extract/base.rb', line 65

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

#project_rootObject



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

def project_root
  @options[:project_root] || "."
end

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



69
70
71
72
# File 'lib/jets/gems/extract/base.rb', line 69

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

#sh(command) ⇒ Object



26
27
28
29
30
31
# File 'lib/jets/gems/extract/base.rb', line 26

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

#unzip(zipfile_path, parent_folder_dest) ⇒ Object



22
23
24
# File 'lib/jets/gems/extract/base.rb', line 22

def unzip(zipfile_path, parent_folder_dest)
  sh("cd #{parent_folder_dest} && unzip -qo #{zipfile_path}")
end

#url_exists?(url) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/jets/gems/extract/base.rb', line 33

def url_exists?(url)
  exist = Jets::Gems::Exist.new(@options)
  exist.url_exists?(url)
end