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.



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

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

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

Instance Method Details

#check_file_existence!(path, additional_message = nil) ⇒ Object



71
72
73
74
75
76
# File 'lib/safedep/runner.rb', line 71

def check_file_existence!(path, additional_message = nil)
  return if File.exist?(path)
  message = "#{path} is not found."
  message << ' ' + additional_message if additional_message
  fail Error, message
end

#dependenciesObject



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

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

#gemfileObject



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

def gemfile
  @gemfile ||= begin
    check_file_existence!(GEMFILE_PATH)
    Gemologist::Gemfile.new(GEMFILE_PATH)
  end
end

#gemfile_lockObject



64
65
66
67
68
69
# File 'lib/safedep/runner.rb', line 64

def gemfile_lock
  @gemfile_lock ||= begin
    check_file_existence!(GEMFILE_LOCK_PATH, 'Please run `bundle install`.')
    Safedep::GemfileLock.new(GEMFILE_LOCK_PATH)
  end
end

#gemfilesObject



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

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

#gemspecObject



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

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

#runObject



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

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)


78
79
80
81
82
# File 'lib/safedep/runner.rb', line 78

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



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

def validate!
  gemfile_lock
  gemfile
end

#version_specifiers(version) ⇒ Object



84
85
86
# File 'lib/safedep/runner.rb', line 84

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