Module: Dapp::Project::Dappfile

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

Overview

Dappfile

Instance Method Summary collapse

Instance Method Details

#apps(dappfile_path, app_filters:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dapp/project/dappfile.rb', line 55

def apps(dappfile_path, app_filters:)
  config = Config::Main.new(dappfile_path: dappfile_path, 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 = e.is_a?(NoMethodError) ? e.message[/.*(?= for)/] : e.message
      message = "#{backtrace[/.*(?=:in)/]}: #{message}" if backtrace
      raise Error::Dappfile, code: :incorrect, data: { error: e.class.name, message: message }
    end
  end
  config._apps.select { |app| app_filters.any? { |pattern| File.fnmatch(pattern, app._name) } }.tap do |apps|
    apps.each { |app| app.send(:validate!) }
  end
end

#build_configsObject



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

def build_configs
  @configs ||= begin
    dappfiles.map { |dappfile| apps(dappfile, app_filters: apps_patterns) }.flatten.tap do |apps|
      raise Error::Project, code: :no_such_app, data: { apps_patterns: apps_patterns.join(', ') } if apps.empty?
    end
  end
end

#dappfile_pathObject



22
23
24
# File 'lib/dapp/project/dappfile.rb', line 22

def dappfile_path
  File.join [cli_options[:dir], 'Dappfile'].compact
end

#dappfilesObject



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

def dappfiles
  if File.exist?(dappfile_path)                 then [dappfile_path]
  elsif !dapps_dappfiles_pathes.empty?          then dapps_dappfiles_pathes
  elsif (dappfile_path = search_up('Dappfile')) then [dappfile_path]
  else raise Error::Project, code: :dappfile_not_found
  end
end

#dapps_dappfiles_pathesObject



26
27
28
29
30
31
32
33
# File 'lib/dapp/project/dappfile.rb', line 26

def dapps_dappfiles_pathes
  path = []
  path << cli_options[:dir]
  path << '.dapps' unless File.basename(work_dir) == '.dapps'
  path << '*'
  path << 'Dappfile'
  Dir.glob(File.join(path.compact))
end

#expand_path(path, number = 1) ⇒ Object



49
50
51
52
53
# File 'lib/dapp/project/dappfile.rb', line 49

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

#search_up(file) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/dapp/project/dappfile.rb', line 35

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_dirObject



45
46
47
# File 'lib/dapp/project/dappfile.rb', line 45

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