aboutsummaryrefslogtreecommitdiffstats
path: root/packages/deployer/src/utils/compiler.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-04-11 18:00:30 +0800
committerFabio Berger <me@fabioberger.com>2018-04-11 18:00:30 +0800
commit29dc22e208080fa8ff0871b98b530a2deb251a73 (patch)
treedb372a34e85ac8caf4a3f3ed9d7591452df9cb1a /packages/deployer/src/utils/compiler.ts
parent6f72fed8b5b37fac5096413b363b533e0a29f7b5 (diff)
parentc44f9e56ada898ffd0d0e57aa228006977fb238c (diff)
downloaddexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar.gz
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar.bz2
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar.lz
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar.xz
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.tar.zst
dexon-sol-tools-29dc22e208080fa8ff0871b98b530a2deb251a73.zip
Merge branch 'development' into removeMigrateStep
* development: Fix lint error Fix documentation links in some READMEs Fix relative link Add step to publishing that upload staging doc jsons, deploys staging website, opens every docs page and asks the dev to confirm that each one renders properly before publishing Fix web3Wrapper build command Add top-level `yarn lerna:stage_docs` to upload docJsons to the staging S3 bucket for all packages with a docs page Added a detailed description of `renameOverloadedMethods` (special thanks to @fabioberger). Updated Javascript styles in the Abi-Gen and Utils packages, around support for function overloading. Updated deployer to accept a list of contract directories as input. Contract directories are namespaced to a void clashes. Also in this commit is a fix for overloading contract functions. Refactor publish script to have it's main execution body be lean and discrete steps # Conflicts: # packages/contracts/package.json # packages/deployer/package.json
Diffstat (limited to 'packages/deployer/src/utils/compiler.ts')
-rw-r--r--packages/deployer/src/utils/compiler.ts65
1 files changed, 56 insertions, 9 deletions
diff --git a/packages/deployer/src/utils/compiler.ts b/packages/deployer/src/utils/compiler.ts
index d5137d394..600495693 100644
--- a/packages/deployer/src/utils/compiler.ts
+++ b/packages/deployer/src/utils/compiler.ts
@@ -1,3 +1,4 @@
+import { AbiType, ContractAbi, MethodAbi } from '@0xproject/types';
import { logUtils } from '@0xproject/utils';
import * as _ from 'lodash';
import * as path from 'path';
@@ -5,9 +6,49 @@ import * as solc from 'solc';
import { constants } from './constants';
import { fsWrapper } from './fs_wrapper';
-import { ContractArtifact, ContractSources } from './types';
+import { ContractArtifact, ContractSources, FunctionNameToSeenCount } from './types';
/**
+ * Constructs a system-wide unique identifier for a source file.
+ * @param directoryNamespace Namespace of the source file's root contract directory.
+ * @param sourceFilePath Path to a source file, relative to contractBaseDir.
+ * @return sourceFileId A system-wide unique identifier for the source file.
+ */
+export function constructUniqueSourceFileId(directoryNamespace: string, sourceFilePath: string): string {
+ const namespacePrefix = !_.isEmpty(directoryNamespace) ? `/${directoryNamespace}` : '';
+ const sourceFilePathNoLeadingSlash = sourceFilePath.replace(/^\/+/g, '');
+ const sourceFileId = `${namespacePrefix}/${sourceFilePathNoLeadingSlash}`;
+ return sourceFileId;
+}
+/**
+ * Constructs a system-wide unique identifier for a dependency file.
+ * @param dependencyFilePath Path from a sourceFile to a dependency.
+ * @param contractBaseDir Base contracts directory of search tree.
+ * @return sourceFileId A system-wide unique identifier for the source file.
+ */
+export function constructDependencyFileId(dependencyFilePath: string, sourceFilePath: string): string {
+ if (_.startsWith(dependencyFilePath, '/')) {
+ // Path of the form /namespace/path/to/dependency.sol
+ return dependencyFilePath;
+ } else {
+ // Dependency is relative to the source file: ./dependency.sol, ../../some/path/dependency.sol, etc.
+ // Join the two paths to construct a valid source file id: /namespace/path/to/dependency.sol
+ return path.join(path.dirname(sourceFilePath), dependencyFilePath);
+ }
+}
+/**
+ * Constructs a system-wide unique identifier for a contract.
+ * @param directoryNamespace Namespace of the source file's root contract directory.
+ * @param sourceFilePath Path to a source file, relative to contractBaseDir.
+ * @return sourceFileId A system-wide unique identifier for contract.
+ */
+export function constructContractId(directoryNamespace: string, sourceFilePath: string): string {
+ const namespacePrefix = !_.isEmpty(directoryNamespace) ? `${directoryNamespace}:` : '';
+ const sourceFileName = path.basename(sourceFilePath, constants.SOLIDITY_FILE_EXTENSION);
+ const contractId = `${namespacePrefix}${sourceFileName}`;
+ return contractId;
+}
+/**
* Gets contract data on network or returns if an artifact does not exist.
* @param artifactsDir Path to the artifacts directory.
* @param fileName Name of contract file.
@@ -82,9 +123,10 @@ export function getNormalizedErrMsg(errMsg: string): string {
/**
* Parses the contract source code and extracts the dendencies
* @param source Contract source code
+ * @param sourceFilePath File path of the source code.
* @return List of dependendencies
*/
-export function parseDependencies(source: string): string[] {
+export function parseDependencies(source: string, sourceFileId: string): string[] {
// TODO: Use a proper parser
const IMPORT_REGEX = /(import\s)/;
const DEPENDENCY_PATH_REGEX = /"([^"]+)"/; // Source: https://github.com/BlockChainCompany/soljitsu/blob/master/lib/shared.js
@@ -95,8 +137,8 @@ export function parseDependencies(source: string): string[] {
const dependencyMatch = line.match(DEPENDENCY_PATH_REGEX);
if (!_.isNull(dependencyMatch)) {
const dependencyPath = dependencyMatch[1];
- const basenName = path.basename(dependencyPath);
- dependencies.push(basenName);
+ const dependencyId = constructDependencyFileId(dependencyPath, sourceFileId);
+ dependencies.push(dependencyId);
}
}
});
@@ -107,14 +149,19 @@ export function parseDependencies(source: string): string[] {
* Callback to resolve dependencies with `solc.compile`.
* Throws error if contractSources not yet initialized.
* @param contractSources Source codes of contracts.
- * @param importPath Path to an imported dependency.
+ * @param sourceFileId ID of the source file.
+ * @param importPath Path of dependency source file.
* @return Import contents object containing source code of dependency.
*/
-export function findImportIfExist(contractSources: ContractSources, importPath: string): solc.ImportContents {
- const fileName = path.basename(importPath);
- const source = contractSources[fileName];
+export function findImportIfExist(
+ contractSources: ContractSources,
+ sourceFileId: string,
+ importPath: string,
+): solc.ImportContents {
+ const dependencyFileId = constructDependencyFileId(importPath, sourceFileId);
+ const source = contractSources[dependencyFileId];
if (_.isUndefined(source)) {
- throw new Error(`Contract source not found for ${fileName}`);
+ throw new Error(`Contract source not found for ${dependencyFileId}`);
}
const importContents: solc.ImportContents = {
contents: source,