Class: Homebrew::DevCmd::BumpUnversionedCasks

Inherits:
AbstractCommand show all
Includes:
SystemCommand::Mixin
Defined in:
dev-cmd/bump-unversioned-casks.rb

Instance Attribute Summary

Attributes inherited from AbstractCommand

#args

Instance Method Summary collapse

Methods included from SystemCommand::Mixin

#system_command, #system_command!

Methods inherited from AbstractCommand

command, command_name, #initialize, parser

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#runvoid

This method returns an undefined value.



32
33
34
35
36
37
38
39
40
41
42
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
# File 'dev-cmd/bump-unversioned-casks.rb', line 32

def run
  Homebrew.install_bundler_gems!(groups: ["bump_unversioned_casks"])

  state_file = if args.state_file.present?
    Pathname(T.must(args.state_file)).expand_path
  else
    HOMEBREW_CACHE/"bump_unversioned_casks.json"
  end
  state_file.dirname.mkpath

  state = state_file.exist? ? JSON.parse(state_file.read) : {}

  casks = args.named.to_paths(only: :cask, recurse_tap: true).map { |path| Cask::CaskLoader.load(path) }

  unversioned_casks = casks.select do |cask|
    cask.url&.unversioned? && !cask.livecheckable? && !cask.discontinued?
  end

  ohai "Unversioned Casks: #{unversioned_casks.count} (#{state.size} cached)"

  checked, unchecked = unversioned_casks.partition { |c| state.key?(c.full_name) }

  queue = Queue.new

  # Start with random casks which have not been checked.
  unchecked.shuffle.each do |c|
    queue.enq c
  end

  # Continue with previously checked casks, ordered by when they were last checked.
  checked.sort_by { |c| state.dig(c.full_name, "check_time") }.each do |c|
    queue.enq c
  end

  limit = args.limit.presence&.to_i
  end_time = Time.now + (limit * 60) if limit

  until queue.empty? || (end_time && end_time < Time.now)
    cask = queue.deq

    key = cask.full_name

    new_state = bump_unversioned_cask(cask, state: state.fetch(key, {}))

    next unless new_state

    state[key] = new_state

    state_file.atomic_write JSON.pretty_generate(state) unless args.dry_run?
  end
end