Class: Contextizer::Providers::Ruby::ProjectInfo

Inherits:
BaseProvider
  • Object
show all
Defined in:
lib/contextizer/providers/ruby/project_info.rb

Class Method Summary collapse

Class Method Details

.call(context:, config:) ⇒ Object



7
8
9
10
# File 'lib/contextizer/providers/ruby/project_info.rb', line 7

def self.call(context:, config:)
  project_info = detect_project_info(context.target_path)
  context.[:project] = project_info
end

.detect_gem_version(path) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/contextizer/providers/ruby/project_info.rb', line 25

def self.detect_gem_version(path)
  version_file = Dir.glob(File.join(path, "lib", "**", "version.rb")).first
  return "N/A" unless version_file

  content = File.read(version_file)
  match = content.match(/VERSION\s*=\s*["'](.+?)["']/)
  match ? match[1] : "N/A"
end

.detect_project_info(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/contextizer/providers/ruby/project_info.rb', line 13

def self.detect_project_info(path)
  if Dir.glob(File.join(path, "*.gemspec")).any?
    return { type: "Gem", version: detect_gem_version(path) }
  end

  if File.exist?(File.join(path, "bin", "rails"))
    return { type: "Rails", version: detect_rails_version(path) }
  end

  { type: "Directory", version: "N/A" }
end

.detect_rails_version(path) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/contextizer/providers/ruby/project_info.rb', line 34

def self.detect_rails_version(path)
  gemfile_lock = File.join(path, "Gemfile.lock")
  return "N/A" unless File.exist?(gemfile_lock)

  content = File.read(gemfile_lock)
  match = content.match(/^\s{4}rails\s\((.+?)\)/)
  match ? match[1] : "N/A"
end