Class: Gembrew::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/gembrew/config.rb

Defined Under Namespace

Classes: Dependency

Constant Summary collapse

DEPENDENCY_TAGS =
%w[:system_on_macos].freeze
DIRECTORY =
'gembrew'
ALLOWED_KEYS =
%w[
  gem version source output desc homepage license executable dependencies test test_from_file
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, project_path: nil) ⇒ Config

Returns a new instance of Config.



39
40
41
42
43
44
# File 'lib/gembrew/config.rb', line 39

def initialize(path, project_path: nil)
  @path = Pathname(path).expand_path
  @project_path = project_path ? Pathname(project_path).expand_path : @path.dirname.parent
  @data = load_data
  validate
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/gembrew/config.rb', line 17

def path
  @path
end

#project_pathObject (readonly)

Returns the value of attribute project_path.



17
18
19
# File 'lib/gembrew/config.rb', line 17

def project_path
  @project_path
end

Class Method Details

.all(project_path = Pathname.pwd) ⇒ Object

Raises:



19
20
21
22
23
24
25
# File 'lib/gembrew/config.rb', line 19

def self.all(project_path = Pathname.pwd)
  project_path = Pathname(project_path).expand_path
  paths = (project_path/DIRECTORY).glob('*.yml').sort
  raise Error, "No gem configurations found in #{project_path/DIRECTORY}" if paths.empty?

  paths.map { |path| new path, project_path: project_path }
end

.find(name, project_path = Pathname.pwd) ⇒ Object



27
28
29
30
31
# File 'lib/gembrew/config.rb', line 27

def self.find(name, project_path = Pathname.pwd)
  validate_name! name
  project_path = Pathname(project_path).expand_path
  new project_path/DIRECTORY/"#{name}.yml", project_path: project_path
end

.validate_name!(name) ⇒ Object

Raises:



33
34
35
36
37
# File 'lib/gembrew/config.rb', line 33

def self.validate_name!(name)
  return name if name&.match?(/\A[a-zA-Z0-9._-]+\z/)

  raise Error, "Invalid gem name: #{name.inspect}"
end

Instance Method Details

#dependenciesObject



60
61
62
# File 'lib/gembrew/config.rb', line 60

def dependencies
  @dependencies ||= data.fetch('dependencies', []).map { |value| parse_dependency value }
end

#descriptionObject



55
# File 'lib/gembrew/config.rb', line 55

def description = data['desc']

#executableObject



58
# File 'lib/gembrew/config.rb', line 58

def executable = data['executable']

#gem_nameObject



47
# File 'lib/gembrew/config.rb', line 47

def gem_name = data.fetch('gem').to_s

#homepageObject



56
# File 'lib/gembrew/config.rb', line 56

def homepage = data['homepage']

#licenseObject



57
# File 'lib/gembrew/config.rb', line 57

def license = data['license']

#nameObject



46
# File 'lib/gembrew/config.rb', line 46

def name = path.basename('.yml').to_s

#output_pathObject



54
# File 'lib/gembrew/config.rb', line 54

def output_path = (project_path/(data['output'] || "Formula/#{name}.rb")).expand_path

#sourceObject



49
# File 'lib/gembrew/config.rb', line 49

def source = data.fetch('source')

#source_gemspecObject



53
# File 'lib/gembrew/config.rb', line 53

def source_gemspec = source['gemspec'] || "#{gem_name}.gemspec"

#source_repoObject



51
# File 'lib/gembrew/config.rb', line 51

def source_repo = source['repo']

#source_tagObject



52
# File 'lib/gembrew/config.rb', line 52

def source_tag = source['tag'] || "v#{version}"

#source_typeObject



50
# File 'lib/gembrew/config.rb', line 50

def source_type = source.fetch('type')

#test_bodyObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/gembrew/config.rb', line 64

def test_body
  @test_body ||= if data.has_key?('test')
    data.fetch('test').to_s
  else
    test_path = project_path/data.fetch('test_from_file')
    raise Error, "Test file does not exist: #{test_path}" unless test_path.file?

    test_path.read
  end
end

#versionObject



48
# File 'lib/gembrew/config.rb', line 48

def version = data['version']&.to_s