Skip to content

Commit

Permalink
feat: import resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
alaibe authored and iurimatias committed Jan 25, 2019
1 parent 0d8f233 commit 29db66b
Show file tree
Hide file tree
Showing 31 changed files with 392 additions and 426 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
"@types/node": "10.11.7",
"@types/os-locale": "2.1.0",
"@types/pretty-ms": "3.2.0",
"@types/request": "2.48.1",
"@types/web3": "1.0.12",
"babel-plugin-dynamic-import-node": "2.2.0",
"chai": "4.1.2",
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/cmd_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ class EmbarkController {
engine.startService("deployment", {
trackContracts: false,
compileOnceOnly: true,
disableOptimizations: options.coverage
isCoverage: options.coverage
});
engine.startService("storage");
engine.startService("codeGenerator");
Expand Down
18 changes: 9 additions & 9 deletions src/lib/core/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fs = require('./fs.js');
const File = require('./file.js');
const Plugins = require('./plugins.js');
const utils = require('../utils/utils.js');
const path = require('path');
Expand All @@ -11,6 +10,7 @@ const cloneDeep = require('lodash.clonedeep');
import { replaceZeroAddressShorthand } from '../utils/addressUtils';
import { unitRegex } from "../utils/regexConstants";
import * as utilsContractsConfig from "../utils/contractsConfig";
import { File, Types } from "./file";

const DEFAULT_CONFIG_PATH = 'config/';

Expand Down Expand Up @@ -59,13 +59,13 @@ var Config = function(options) {
resolver = resolver || function(callback) {
callback(fs.readFileSync(filename).toString());
};
self.contractsFiles.push(new File({filename, type: File.types.custom, path: filename, resolver}));
self.contractsFiles.push(new File({path: filename, type: Types.custom, resolver}));
});

self.events.on('file-remove', (fileType, removedPath) => {
if(fileType !== 'contract') return;
const normalizedPath = path.normalize(removedPath);
self.contractsFiles = self.contractsFiles.filter(file => path.normalize(file.filename) !== normalizedPath);
self.contractsFiles = self.contractsFiles.filter(file => path.normalize(file.path) !== normalizedPath);
});
};

Expand Down Expand Up @@ -127,7 +127,7 @@ Config.prototype.loadContractFiles = function() {
if (!this.contractFiles || newContractsFiles.length !== this.contractFiles.length || !deepEqual(newContractsFiles, this.contractFiles)) {
this.contractsFiles = this.contractsFiles.concat(newContractsFiles).filter((file, index, arr) => {
return !arr.some((file2, index2) => {
return file.filename === file2.filename && index < index2;
return file.path === file2.path && index < index2;
});
});
}
Expand Down Expand Up @@ -369,11 +369,11 @@ Config.prototype.loadExternalContractsFiles = function() {
return this.logger.error(__("HTTP contract file not found") + ": " + contract.file);
}
const localFile = fileObj.filePath;
this.contractsFiles.push(new File({filename: localFile, type: File.types.http, basedir: '', path: fileObj.url, storageConfig: storageConfig}));
this.contractsFiles.push(new File({path: localFile, type: Types.http, basedir: '', externalUrl: fileObj.url, storageConfig: storageConfig}));
} else if (fs.existsSync(contract.file)) {
this.contractsFiles.push(new File({filename: contract.file, type: File.types.dapp_file, basedir: '', path: contract.file, storageConfig: storageConfig}));
this.contractsFiles.push(new File({path: contract.file, type: Types.dappFile, basedir: '', storageConfig: storageConfig}));
} else if (fs.existsSync(path.join('./node_modules/', contract.file))) {
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), storageConfig: storageConfig}));
this.contractsFiles.push(new File({path: path.join('./node_modules/', contract.file), type: Types.dappFile, basedir: '', storageConfig: storageConfig}));
} else {
this.logger.error(__("contract file not found") + ": " + contract.file);
}
Expand Down Expand Up @@ -571,7 +571,7 @@ Config.prototype.loadFiles = function(files) {
return (file[0] === '$' || file.indexOf('.') >= 0);
}).filter(function(file) {
let basedir = findMatchingExpression(file, files);
readFiles.push(new File({filename: file, type: File.types.dapp_file, basedir: basedir, path: file, storageConfig: storageConfig}));
readFiles.push(new File({path: file, type: Types.dappFile, basedir: basedir, storageConfig: storageConfig}));
});

var filesFromPlugins = [];
Expand Down Expand Up @@ -605,7 +605,7 @@ Config.prototype.loadPluginContractFiles = function() {
contractsPlugins.forEach(function(plugin) {
plugin.contractsFiles.forEach(function(file) {
var filename = file.replace('./','');
self.contractsFiles.push(new File({filename: filename, pluginPath: plugin.pluginPath, type: File.types.custom, path: filename, storageConfig: storageConfig, resolver: function(callback) {
self.contractsFiles.push(new File({path: filename, pluginPath: plugin.pluginPath, type: Types.custom, storageConfig: storageConfig, resolver: function(callback) {
callback(plugin.loadPluginFile(file));
}}));
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class Engine {
}

setupCompilerAndContractsManagerService(options) {
this.registerModule('compiler', {plugins: this.plugins, disableOptimizations: options.disableOptimizations});
this.registerModule('compiler', {plugins: this.plugins, isCoverage: options.isCoverage});
this.registerModule('solidity', {ipc: this.ipc, useDashboard: this.useDashboard});
this.registerModule('vyper');
this.registerModule('contracts_manager', {plugins: this.plugins, compileOnceOnly: options.compileOnceOnly});
Expand Down
210 changes: 0 additions & 210 deletions src/lib/core/file.js

This file was deleted.

77 changes: 77 additions & 0 deletions src/lib/core/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as path from "path";

const fs = require("./fs.js");
const utils = require("../utils/utils");

export enum Types {
embarkInternal = "embark_internal",
dappFile = "dapp_file",
custom = "custom",
http = "http",
}

interface ImportRemapping {
prefix: string;
target: string;
}

export class File {
public type: Types;
public externalUrl: string = "";
public path: string;
public basedir: string;
public resolver: (callback: (content: string) => void) => void;
public pluginPath: string;
public storageConfig: any;
public providerUrl: string;
public importRemappings: ImportRemapping[] = [];

constructor(options: any) {
this.type = options.type;

this.basedir = options.basedir;
this.resolver = options.resolver;
this.pluginPath = options.pluginPath ? options.pluginPath : "";
this.storageConfig = options.storageConfig;
this.providerUrl = "";

if (this.type === Types.http) {
const external = utils.getExternalContractUrl(options.externalUrl, this.providerUrl);
this.externalUrl = external.url;
this.path = external.filePath;
} else {
this.path = options.path.replace(/\\/g, "/");
}
}

public get content(): Promise<string> {
return new Promise<string>((resolve) => {
switch (this.type) {
case Types.embarkInternal: {
const content = fs.readFileSync(fs.embarkPath(path.join("dist", this.path)), "utf-8");
return resolve(content);
}

case Types.dappFile: {
const content = fs.readFileSync(this.path, "utf-8").toString();
return resolve(content);
}

case Types.custom: {
return this.resolver((content: string) => {
resolve(content);
});
}

case Types.http: {
fs.ensureFileSync(this.path);
return utils.downloadFile(this.externalUrl, this.path, () => {
const content = fs.readFileSync(this.path, "utf-8");
resolve(content);
});
}
}
});
}

}
Loading

0 comments on commit 29db66b

Please sign in to comment.