diff options
Diffstat (limited to 'packages/deployer/src/utils/compiler.ts')
-rw-r--r-- | packages/deployer/src/utils/compiler.ts | 87 |
1 files changed, 12 insertions, 75 deletions
diff --git a/packages/deployer/src/utils/compiler.ts b/packages/deployer/src/utils/compiler.ts index 600495693..c571b2581 100644 --- a/packages/deployer/src/utils/compiler.ts +++ b/packages/deployer/src/utils/compiler.ts @@ -1,4 +1,4 @@ -import { AbiType, ContractAbi, MethodAbi } from '@0xproject/types'; +import { ContractSource, ContractSources } from '@0xproject/sol-resolver'; import { logUtils } from '@0xproject/utils'; import * as _ from 'lodash'; import * as path from 'path'; @@ -6,60 +6,19 @@ import * as solc from 'solc'; import { constants } from './constants'; import { fsWrapper } from './fs_wrapper'; -import { ContractArtifact, ContractSources, FunctionNameToSeenCount } from './types'; +import { ContractArtifact } 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. + * @param contractName Name of contract. * @return Contract data on network or undefined. */ export async function getContractArtifactIfExistsAsync( artifactsDir: string, - fileName: string, + contractName: string, ): Promise<ContractArtifact | void> { let contractArtifact; - const contractName = path.basename(fileName, constants.SOLIDITY_FILE_EXTENSION); const currentArtifactPath = `${artifactsDir}/${contractName}.json`; try { const opts = { @@ -69,7 +28,7 @@ export async function getContractArtifactIfExistsAsync( contractArtifact = JSON.parse(contractArtifactString); return contractArtifact; } catch (err) { - logUtils.log(`Artifact for ${fileName} does not exist`); + logUtils.log(`Artifact for ${contractName} does not exist`); return undefined; } } @@ -123,11 +82,11 @@ 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, sourceFileId: string): string[] { +export function parseDependencies(contractSource: ContractSource): string[] { // TODO: Use a proper parser + const source = contractSource.source; const IMPORT_REGEX = /(import\s)/; const DEPENDENCY_PATH_REGEX = /"([^"]+)"/; // Source: https://github.com/BlockChainCompany/soljitsu/blob/master/lib/shared.js const dependencies: string[] = []; @@ -136,35 +95,13 @@ export function parseDependencies(source: string, sourceFileId: string): string[ if (!_.isNull(line.match(IMPORT_REGEX))) { const dependencyMatch = line.match(DEPENDENCY_PATH_REGEX); if (!_.isNull(dependencyMatch)) { - const dependencyPath = dependencyMatch[1]; - const dependencyId = constructDependencyFileId(dependencyPath, sourceFileId); - dependencies.push(dependencyId); + let dependencyPath = dependencyMatch[1]; + if (dependencyPath.startsWith('.')) { + dependencyPath = path.join(path.dirname(contractSource.path), dependencyPath); + } + dependencies.push(dependencyPath); } } }); return dependencies; } - -/** - * Callback to resolve dependencies with `solc.compile`. - * Throws error if contractSources not yet initialized. - * @param contractSources Source codes of contracts. - * @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, - 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 ${dependencyFileId}`); - } - const importContents: solc.ImportContents = { - contents: source, - }; - return importContents; -} |