Class: Dapp::CLI::Command::Update

Inherits:
Dapp::CLI
  • Object
show all
Defined in:
lib/dapp/cli/command/update.rb

Constant Summary

Constants inherited from Dapp::CLI

SUBCOMMANDS

Instance Method Summary collapse

Methods inherited from Dapp::CLI

#initialize

Methods included from Helper::Cli

#cli_wrapper, #composite_options, #in_validate!, #list_msg_format, #parse_options, #parse_subcommand, #prepare_subcommand, #required_argument, #run_subcommand

Methods included from Helper::Trivia

class_to_lowercase, #class_to_lowercase, #delete_file, #kwargs, #make_path, #search_file_upward

Constructor Details

This class inherits a constructor from Dapp::CLI

Instance Method Details

#latest_beta_version(current_spec) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dapp/cli/command/update.rb', line 18

def latest_beta_version(current_spec)
  minor_version = current_spec.version.approximate_recommendation
  url = "https://rubygems.org/api/v1/versions/#{current_spec.name}.json"
  response = Excon.get(url)
  if response.status == 200
    JSON.parse(response.body)
      .reduce([]) { |versions, spec| versions << Gem::Version.new(spec['number']) }
      .reject { |spec_version| minor_version != spec_version.approximate_recommendation || current_spec.version >= spec_version }
      .first
  else
    warn "Cannot get `#{url}`: got bad http status `#{response.status}`"
  end
end

#run(_argv) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/dapp/cli/command/update.rb', line 5

def run(_argv)
  spec = Gem::Specification.find do |s|
    File.fnmatch(File.join(s.full_gem_path, '*'), __FILE__)
  end
  unless (latest_version = latest_beta_version(spec)).nil?
    try_lock do
      Gem.install(spec.name, latest_version)
    end
  end
rescue Gem::FilePermissionError => e
  raise Errno::EACCES, e.message
end

#try_lockObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dapp/cli/command/update.rb', line 32

def try_lock
  old_umask = File.umask(0)
  file = nil

  begin
    begin
      file = ::File.open('/tmp/dapp-update-running.lock', ::File::RDWR | ::File::CREAT, 0777)
    ensure
      File.umask(old_umask)
    end

    if file.flock(::File::LOCK_EX | ::File::LOCK_NB)
      yield
    else
      puts 'There are other active dapp update process, exiting without update'
    end
  ensure
    file.close unless file.nil?
  end
end