Class: Safedep::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/safedep/runner.rb

Constant Summary collapse

GEMFILE_PATH =
'Gemfile'.freeze
GEMFILE_LOCK_PATH =
'Gemfile.lock'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = Configuration.new) ⇒ Runner

Returns a new instance of Runner.



15
16
17
# File 'lib/safedep/runner.rb', line 15

def initialize(configuration = Configuration.new)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



13
14
15
# File 'lib/safedep/runner.rb', line 13

def configuration
  @configuration
end

Instance Method Details

#dependenciesObject



43
44
45
# File 'lib/safedep/runner.rb', line 43

def dependencies
  @dependencies ||= gemfiles.map(&:dependencies).reduce(:+)
end

#gemfileObject



58
59
60
61
62
63
# File 'lib/safedep/runner.rb', line 58

def gemfile
  @gemfile ||= begin
    fail Error, "#{GEMFILE_PATH} is not found." unless File.exist?(GEMFILE_PATH)
    Gemfile.new(GEMFILE_PATH)
  end
end

#gemfile_lockObject



65
66
67
68
69
70
71
72
73
# File 'lib/safedep/runner.rb', line 65

def gemfile_lock
  @gemfile_lock ||= begin
    unless File.exist?(GEMFILE_LOCK_PATH)
      fail Error, "#{GEMFILE_LOCK_PATH} is not found. Please run `bundle install`."
    end

    GemfileLock.new(GEMFILE_LOCK_PATH)
  end
end

#gemfilesObject



47
48
49
# File 'lib/safedep/runner.rb', line 47

def gemfiles
  @gemfiles ||= [gemspec, gemfile].compact
end

#gemspecObject



51
52
53
54
55
56
# File 'lib/safedep/runner.rb', line 51

def gemspec
  @gemspec ||= begin
    path = Dir['*.gemspec'].first
    Gemspec.new(path) if path
  end
end

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/safedep/runner.rb', line 19

def run
  validate!

  dependencies.each do |dep|
    next if should_ignore?(dep)

    lockfile_dep = gemfile_lock.find_dependency(dep.name)

    unless lockfile_dep
      fail Error, "#{dep.name.inspect} definition is not found in #{gemfile_lock.path}. " \
                  'Please run `bundle install`.'
    end

    dep.version_specifiers = version_specifiers(lockfile_dep.version)
  end

  gemfiles.each(&:rewrite!)
end

#should_ignore?(dependency) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/safedep/runner.rb', line 75

def should_ignore?(dependency)
  return true unless dependency.version_specifiers.empty?
  return true unless (dependency.groups & configuration.skipped_groups).empty?
  [:git, :github, :path].any? { |key| dependency.options[key] }
end

#validate!Object



38
39
40
41
# File 'lib/safedep/runner.rb', line 38

def validate!
  gemfile_lock
  gemfile
end

#version_specifiers(version) ⇒ Object



81
82
83
# File 'lib/safedep/runner.rb', line 81

def version_specifiers(version)
  Policy::SemVer.version_specifiers(version)
end