|
1 | | -var fs = require('./fs.js'); |
2 | | -var File = require('./file.js'); |
3 | | -var Plugins = require('./plugins.js'); |
4 | | -var utils = require('../utils/utils.js'); |
5 | | -var path = require('path'); |
| 1 | +const fs = require('./fs.js'); |
| 2 | +const File = require('./file.js'); |
| 3 | +const Plugins = require('./plugins.js'); |
| 4 | +const utils = require('../utils/utils.js'); |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +const httpContractDir = '.embark/contracts/'; |
6 | 8 |
|
7 | 9 | var Config = function(options) { |
8 | 10 | this.env = options.env; |
@@ -142,17 +144,58 @@ Config.prototype.loadContractsConfigFile = function() { |
142 | 144 | this.contractsConfig = this._mergeConfig(configFilePath, configObject, this.env); |
143 | 145 | }; |
144 | 146 |
|
| 147 | +Config.prototype.getExternalContractUrl = function (contract) { |
| 148 | + let url; |
| 149 | + const RAW_URL = 'https://raw.githubusercontent.com/'; |
| 150 | + const MALFORMED_ERROR = 'Malformed Github URL for '; |
| 151 | + if (contract.file.startsWith('https://github')) { |
| 152 | + const match = contract.file.match(/https:\/\/github\.[a-z]+\/(.*)/); |
| 153 | + if (!match) { |
| 154 | + this.logger.error(MALFORMED_ERROR + contract.file); |
| 155 | + return ''; |
| 156 | + } |
| 157 | + url = `${RAW_URL}${match[1].replace('blob/', '')}`; |
| 158 | + } else if (contract.file.startsWith('git')) { |
| 159 | + // Match values |
| 160 | + // [0] entire input |
| 161 | + // [1] git:// |
| 162 | + // [2] user |
| 163 | + // [3] repository |
| 164 | + // [4] path |
| 165 | + // [5] branch |
| 166 | + const match = contract.file.match( |
| 167 | + /(git:\/\/)?github\.[a-z]+\/([a-zA-Z0-9_\-.]+)\/([a-zA-Z0-9_\-7]+)\/([a-zA-Z0-9_\-\/.]+)#?([a-zA-Z0-1_\-.]*)?/ |
| 168 | + ); |
| 169 | + if (!match) { |
| 170 | + this.logger.error(MALFORMED_ERROR + contract.file); |
| 171 | + return ''; |
| 172 | + } |
| 173 | + let branch = match[5]; |
| 174 | + if (!branch) { |
| 175 | + branch = 'master'; |
| 176 | + } |
| 177 | + url = `${RAW_URL}${match[2]}/${match[3]}/${branch}/${match[4]}`; |
| 178 | + } else { |
| 179 | + url = contract.file; |
| 180 | + } |
| 181 | + return url; |
| 182 | +}; |
| 183 | + |
145 | 184 | Config.prototype.loadExternalContractsFiles = function() { |
146 | 185 | let contracts = this.contractsConfig.contracts; |
147 | 186 | for (let contractName in contracts) { |
148 | 187 | let contract = contracts[contractName]; |
149 | 188 | if (!contract.file) { |
150 | 189 | continue; |
151 | 190 | } |
152 | | - if (fs.existsSync(contract.file)) { |
153 | | - this.contractsFiles.push(new File({filename: contract.file, type: "dapp_file", basedir: '', path: contract.file})); |
| 191 | + if (contract.file.startsWith('http') || contract.file.startsWith('git')) { |
| 192 | + const url = this.getExternalContractUrl(contract); |
| 193 | + const localFile = httpContractDir + path.basename(url); |
| 194 | + this.contractsFiles.push(new File({filename: localFile, type: File.types.http, basedir: '', path: url})); |
| 195 | + } else if (fs.existsSync(contract.file)) { |
| 196 | + this.contractsFiles.push(new File({filename: contract.file, type: File.types.dapp_file, basedir: '', path: contract.file})); |
154 | 197 | } else if (fs.existsSync(path.join('./node_modules/', contract.file))) { |
155 | | - this.contractsFiles.push(new File({filename: path.join('./node_modules/', contract.file), type: "dapp_file", basedir: '', path: path.join('./node_modules/', contract.file)})); |
| 198 | + this.contractsFiles.push(new File({filename: path.join('./node_modules/', contract.file), type: File.types.dapp_file, basedir: '', path: path.join('./node_modules/', contract.file)})); |
156 | 199 | } else { |
157 | 200 | this.logger.error("contract file not found: " + contract.file); |
158 | 201 | } |
@@ -216,6 +259,7 @@ Config.prototype.loadEmbarkConfigFile = function() { |
216 | 259 | }).map((dir) => { |
217 | 260 | return dir.split("*.")[0]; |
218 | 261 | }); |
| 262 | + this.contractDirectories.push(httpContractDir); |
219 | 263 |
|
220 | 264 | this.buildDir = this.embarkConfig.buildDir; |
221 | 265 | this.configDir = this.embarkConfig.config; |
@@ -258,7 +302,7 @@ Config.prototype.loadFiles = function(files) { |
258 | 302 | return (file[0] === '$' || file.indexOf('.') >= 0); |
259 | 303 | }).filter(function(file) { |
260 | 304 | let basedir = findMatchingExpression(file, files); |
261 | | - readFiles.push(new File({filename: file, type: "dapp_file", basedir: basedir, path: file})); |
| 305 | + readFiles.push(new File({filename: file, type: File.types.dapp_file, basedir: basedir, path: file})); |
262 | 306 | }); |
263 | 307 |
|
264 | 308 | var filesFromPlugins = []; |
@@ -291,7 +335,7 @@ Config.prototype.loadPluginContractFiles = function() { |
291 | 335 | contractsPlugins.forEach(function(plugin) { |
292 | 336 | plugin.contractsFiles.forEach(function(file) { |
293 | 337 | var filename = file.replace('./',''); |
294 | | - self.contractsFiles.push(new File({filename: filename, type: 'custom', resolver: function(callback) { |
| 338 | + self.contractsFiles.push(new File({filename: filename, type: File.types.custom, resolver: function(callback) { |
295 | 339 | callback(plugin.loadPluginFile(file)); |
296 | 340 | }})); |
297 | 341 | }); |
|
0 commit comments