Class: Rails::Hyperdrive::StackProfile

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

Constant Summary collapse

CATEGORIES_PATH =
File.expand_path("data/gem_categories.yml", __dir__)

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.



40
41
42
43
44
# File 'lib/rails/hyperdrive/stack_profile.rb', line 40

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.



38
39
40
# File 'lib/rails/hyperdrive/stack_profile.rb', line 38

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



38
39
40
# File 'lib/rails/hyperdrive/stack_profile.rb', line 38

def path
  @path
end

Class Method Details

.categoriesObject



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

def categories
  @categories ||= YAML.load_file(CATEGORIES_PATH).each_with_object({}) do |(cat, gems), acc|
    gems.each { |g| acc[g] = cat.to_sym }
  end
end

.currentObject



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

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

.default_lockfile_pathObject



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

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



25
26
27
# File 'lib/rails/hyperdrive/stack_profile.rb', line 25

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

.reset!Object



21
22
23
# File 'lib/rails/hyperdrive/stack_profile.rb', line 21

def reset!
  @current = nil
end

Instance Method Details

#parse!Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rails/hyperdrive/stack_profile.rb', line 46

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),
    test:       bucket(specs, :test),
    jobs:       bucket(specs, :jobs),
    frontend:   frontend_info(specs),
    auth:       bucket(specs, :auth),
    authz:      bucket(specs, :authz),
    db_gems:    bucket(specs, :db),
    gem_skills: gem_skills_info
  }
  self
end

#to_hObject



71
72
73
# File 'lib/rails/hyperdrive/stack_profile.rb', line 71

def to_h
  @data
end