Class: Rails::Hyperdrive::StackProfile

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/hyperdrive/stack_profile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, app_root: nil) ⇒ StackProfile

Returns a new instance of StackProfile.



32
33
34
35
36
# File 'lib/rails/hyperdrive/stack_profile.rb', line 32

def initialize(path:, app_root: nil)
  @path = path
  @app_root = app_root
  @data = empty_profile
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



30
31
32
# File 'lib/rails/hyperdrive/stack_profile.rb', line 30

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



30
31
32
# File 'lib/rails/hyperdrive/stack_profile.rb', line 30

def path
  @path
end

Class Method Details

.currentObject



9
10
11
# File 'lib/rails/hyperdrive/stack_profile.rb', line 9

def current
  @current ||= from_lockfile(default_lockfile_path)
end

.default_lockfile_pathObject



21
22
23
24
25
26
27
# File 'lib/rails/hyperdrive/stack_profile.rb', line 21

def default_lockfile_path
  if defined?(::Rails) && ::Rails.respond_to?(:root) && ::Rails.root
    ::Rails.root.join("Gemfile.lock").to_s
  else
    File.expand_path("Gemfile.lock", Dir.pwd)
  end
end

.from_lockfile(path, app_root: nil) ⇒ Object



17
18
19
# File 'lib/rails/hyperdrive/stack_profile.rb', line 17

def from_lockfile(path, app_root: nil)
  new(path: path, app_root: app_root).tap(&:parse!)
end

.reset!Object



13
14
15
# File 'lib/rails/hyperdrive/stack_profile.rb', line 13

def reset!
  @current = nil
end

Instance Method Details

#parse!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails/hyperdrive/stack_profile.rb', line 38

def parse!
  unless File.exist?(@path)
    @data = empty_profile.merge(error: "Gemfile.lock not found at #{@path}")
    return self
  end

  contents = File.read(@path)
  parser   = ::Bundler::LockfileParser.new(contents)
  specs    = parser.specs.each_with_object({}) { |s, h| h[s.name] = s.version.to_s }

  @data = {
    rails:               rails_info(specs),
    ruby:                ruby_info(parser),
    database:            database_info(specs),
    direct_dependencies: direct_dependencies(parser, specs),
    gem_skills:          gem_skills_info
  }
  self
end

#to_hObject



58
59
60
# File 'lib/rails/hyperdrive/stack_profile.rb', line 58

def to_h
  @data
end