Module: Dapp::Project::Dappfile

Included in:
Dapp::Project
Defined in:
lib/dapp/project/dappfile.rb

Overview

Dappfile

Instance Method Summary collapse

Instance Method Details

#build_configs ⇒ Object



6
7
8
9
10
11
12
# File 'lib/dapp/project/dappfile.rb', line 6

def build_configs
  @configs ||= begin
    dimgs(dappfile_path).flatten.tap do |dimgs|
      raise Error::Project, code: :no_such_dimg, data: { dimgs_patterns: dimgs_patterns.join(', ') } if dimgs.empty?
    end
  end
end

#dappfile_path ⇒ Object

Raises:



14
15
16
17
# File 'lib/dapp/project/dappfile.rb', line 14

def dappfile_path
  raise Error::Project, code: :dappfile_not_found unless (dappfile_path = search_up('Dappfile'))
  dappfile_path
end

#dimgs(dappfile_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dapp/project/dappfile.rb', line 39

def dimgs(dappfile_path)
  Config::DimgGroupMain.new(project: self) do |conf|
    begin
      conf.instance_eval File.read(dappfile_path), dappfile_path
    rescue SyntaxError, StandardError => e
      backtrace = e.backtrace.find { |line| line.start_with?(dappfile_path) }
      message = begin
        case e
        when NoMethodError
          e.message =~ /`.*'/
          "undefined method #{Regexp.last_match}"
        when NameError then e.message[/.*(?= for)/]
        else
          e.message
        end
      end
      message = "#{backtrace[/.*(?=:in)/]}: #{message}" if backtrace
      raise Error::Dappfile, code: :incorrect, data: { error: e.class.name, message: message }
    end
  end._dimg.select { |dimg| dimgs_patterns.any? { |pattern| dimg._name.nil? || File.fnmatch(pattern, dimg._name) } }.tap do |dimgs|
    dimgs.each { |dimg| dimg.send(:validate!) }
  end
end

#expand_path(path, number = 1) ⇒ Object



33
34
35
36
37
# File 'lib/dapp/project/dappfile.rb', line 33

def expand_path(path, number = 1)
  path = File.expand_path(path)
  number.times.each { path = File.dirname(path) }
  path
end

#search_up(file) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/dapp/project/dappfile.rb', line 19

def search_up(file)
  cdir = Pathname(work_dir)
  loop do
    if (path = cdir.join(file)).exist?
      return path.to_s
    end
    break if (cdir = cdir.parent).root?
  end
end

#work_dir ⇒ Object



29
30
31
# File 'lib/dapp/project/dappfile.rb', line 29

def work_dir
  File.expand_path(cli_options[:dir] || Dir.pwd)
end