Module: Dapp::Dapp::Dappfile

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

Defined Under Namespace

Modules: Error

Instance Method Summary collapse

Instance Method Details

#configObject



38
39
40
41
42
43
44
45
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/dapp/dapp/dappfile.rb', line 38

def config
  @config ||= begin
    config = nil

    dappfile_yml = path("dappfile.yml").to_s
    dappfile_yaml = path("dappfile.yaml").to_s
    dappfile_ruby = path("Dappfile").to_s

    if ENV["DAPP_LOAD_CONFIG_PATH"]
      config = YAML.load_file ENV["DAPP_LOAD_CONFIG_PATH"]
    elsif File.exist? dappfile_yml
      config = load_dappfile_yml(dappfile_yml)
    elsif File.exist? dappfile_yaml
      config = load_dappfile_yml(dappfile_yaml)
    elsif File.exist? dappfile_ruby
      config = load_dappfile_ruby(dappfile_ruby)
    else
      raise ::Dapp::Error::Dapp, code: :dappfile_not_found
    end

    if ENV["DAPP_DUMP_CONFIG"]
      puts "-- DAPP_DUMP_CONFIG BEGIN"
      puts YAML.dump(config)
      puts "-- DAPP_DUMP_CONFIG END"
    end

    config.tap do
      config.after_parsing!
      config.validate!
    end
  end # begin
end

#dappfile_exists?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/dapp/dapp/dappfile.rb', line 31

def dappfile_exists?
  File.exist?(path("dappfile.yml")) ||
    File.exist?(path("dappfile.yaml")) ||
      File.exist?(path("Dappfile")) ||
        ENV["DAPP_LOAD_CONFIG_PATH"]
end

#download_dappfile_yml_bin(dappfile_yml_bin_path) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dapp/dapp/dappfile.rb', line 124

def download_dappfile_yml_bin(dappfile_yml_bin_path)
  lock("downloader.bin.dappfile-yml", default_timeout: 1800) do
    return if File.exists? dappfile_yml_bin_path

    log_process("Downloading dappfile-yml dapp dependency") do
      location = URI("https://dl.bintray.com/dapp/ruby2go/#{::Dapp::VERSION}/dappfile-yml")

      tmp_bin_path = File.join(self.class.tmp_base_dir, "dappfile-yml-#{SecureRandom.uuid}")
      ::Dapp::Downloader.download(location, tmp_bin_path, show_progress: true, progress_titile: dappfile_yml_bin_path)

      checksum_location = URI("https://dl.bintray.com/dapp/ruby2go/#{::Dapp::VERSION}/dappfile-yml.sha")
      tmp_bin_checksum_path = tmp_bin_path + ".checksum"
      ::Dapp::Downloader.download(checksum_location, tmp_bin_checksum_path)

      if Digest::SHA256.hexdigest(File.read(tmp_bin_path)) != File.read(tmp_bin_checksum_path).strip
        raise ::Dapp::Error::Dapp, code: :download_failed_bad_dappfile_yml_checksum, data: {url: location.to_s, checksum_url: checksum_location.to_s}
      end

      File.chmod(0755, tmp_bin_path)
      FileUtils.mkdir_p File.dirname(dappfile_yml_bin_path)
      FileUtils.mv tmp_bin_path, dappfile_yml_bin_path
    end # log_process
  end # lock
end

#expand_path(path, number = 1) ⇒ Object



25
26
27
28
29
# File 'lib/dapp/dapp/dappfile.rb', line 25

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

#load_dappfile_ruby(dappfile_path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dapp/dapp/dappfile.rb', line 71

def load_dappfile_ruby(dappfile_path)
  ::Dapp::Config::Config.new(dapp: self).tap do |config|
    begin
      config.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 ::Dapp::Error::Dappfile, code: :incorrect, data: { error: e.class.name, message: message }
    end # begin-rescue
  end
end

#load_dappfile_yml(dappfile_path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dapp/dapp/dappfile.rb', line 93

def load_dappfile_yml(dappfile_path)
  if dappfile_yml_bin_path = ENV["DAPP_BIN_DAPPFILE_YML"]
    unless File.exists? dappfile_yml_bin_path
      raise ::Dapp::Error::Dapp, code: :dappfile_yml_bin_path_not_found, data: {path: dappfile_yml_bin_path}
    end
  else
    dappfile_yml_bin_path = File.join(::Dapp::Dapp.home_dir, "bin", "dappfile-yml", ::Dapp::VERSION, "dappfile-yml")
    unless File.exists? dappfile_yml_bin_path
      download_dappfile_yml_bin(dappfile_yml_bin_path)
    end
  end

  cmd_res = shellout "#{dappfile_yml_bin_path} -dappfile #{dappfile_path}"

  raw_json_response = nil
  if cmd_res.exitstatus == 0
    raw_json_response = cmd_res.stdout
  elsif cmd_res.exitstatus == 16
    raw_json_response = cmd_res.stderr
  else
    shellout_cmd_should_succeed! cmd_res
  end

  response = JSON.parse(raw_json_response)

  log_warning(response["warning"]) unless response["warning"].empty?
  raise ::Dapp::Dapp::Error::DappfileYmlErrorResponse.new(response["error"], response) if response["error"]

  YAML.load response["dappConfig"]
end

#local_git_artifact_exclude_paths(&blk) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/dapp/dapp/dappfile.rb', line 15

def local_git_artifact_exclude_paths(&blk)
  super do |exclude_paths|
    exclude_paths << 'Dappfile'
    exclude_paths << "dappfile.yml"
    exclude_paths << "dappfile.yaml"

    yield exclude_paths if block_given?
  end
end