From d8ef76fd5efe63ec8f6205a73494ce388e64391f Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 16 Apr 2018 16:38:55 +0200 Subject: Rename resolver to sol-resolver --- packages/deployer/package.json | 2 +- packages/deployer/src/compiler.ts | 2 +- packages/deployer/src/utils/compiler.ts | 2 +- packages/deployer/test/compiler_utils_test.ts | 29 ++++++---- .../deployer/test/fixtures/contracts/Exchange.sol | 2 +- .../test/fixtures/contracts/TokenTransferProxy.sol | 2 +- packages/resolver/package.json | 39 ------------- packages/resolver/src/index.ts | 8 --- .../resolver/src/resolvers/enumerable_resolver.ts | 7 --- .../resolver/src/resolvers/fallthrough_resolver.ts | 21 ------- packages/resolver/src/resolvers/fs_resolver.ts | 19 ------- packages/resolver/src/resolvers/name_resolver.ts | 66 ---------------------- packages/resolver/src/resolvers/npm_resolver.ts | 34 ----------- packages/resolver/src/resolvers/resolver.ts | 14 ----- packages/resolver/src/resolvers/url_resolver.ts | 21 ------- packages/resolver/src/types.ts | 8 --- packages/resolver/tsconfig.json | 7 --- packages/resolver/tslint.json | 3 - packages/sol-cov/src/collect_contract_data.ts | 1 + packages/sol-resolver/package.json | 39 +++++++++++++ packages/sol-resolver/src/index.ts | 8 +++ .../src/resolvers/enumerable_resolver.ts | 7 +++ .../src/resolvers/fallthrough_resolver.ts | 21 +++++++ packages/sol-resolver/src/resolvers/fs_resolver.ts | 19 +++++++ .../sol-resolver/src/resolvers/name_resolver.ts | 66 ++++++++++++++++++++++ .../sol-resolver/src/resolvers/npm_resolver.ts | 34 +++++++++++ packages/sol-resolver/src/resolvers/resolver.ts | 14 +++++ .../sol-resolver/src/resolvers/url_resolver.ts | 21 +++++++ packages/sol-resolver/src/types.ts | 8 +++ packages/sol-resolver/tsconfig.json | 7 +++ packages/sol-resolver/tslint.json | 3 + 31 files changed, 271 insertions(+), 263 deletions(-) delete mode 100644 packages/resolver/package.json delete mode 100644 packages/resolver/src/index.ts delete mode 100644 packages/resolver/src/resolvers/enumerable_resolver.ts delete mode 100644 packages/resolver/src/resolvers/fallthrough_resolver.ts delete mode 100644 packages/resolver/src/resolvers/fs_resolver.ts delete mode 100644 packages/resolver/src/resolvers/name_resolver.ts delete mode 100644 packages/resolver/src/resolvers/npm_resolver.ts delete mode 100644 packages/resolver/src/resolvers/resolver.ts delete mode 100644 packages/resolver/src/resolvers/url_resolver.ts delete mode 100644 packages/resolver/src/types.ts delete mode 100644 packages/resolver/tsconfig.json delete mode 100644 packages/resolver/tslint.json create mode 100644 packages/sol-resolver/package.json create mode 100644 packages/sol-resolver/src/index.ts create mode 100644 packages/sol-resolver/src/resolvers/enumerable_resolver.ts create mode 100644 packages/sol-resolver/src/resolvers/fallthrough_resolver.ts create mode 100644 packages/sol-resolver/src/resolvers/fs_resolver.ts create mode 100644 packages/sol-resolver/src/resolvers/name_resolver.ts create mode 100644 packages/sol-resolver/src/resolvers/npm_resolver.ts create mode 100644 packages/sol-resolver/src/resolvers/resolver.ts create mode 100644 packages/sol-resolver/src/resolvers/url_resolver.ts create mode 100644 packages/sol-resolver/src/types.ts create mode 100644 packages/sol-resolver/tsconfig.json create mode 100644 packages/sol-resolver/tslint.json (limited to 'packages') diff --git a/packages/deployer/package.json b/packages/deployer/package.json index d9f2a4a4f..d529382ac 100644 --- a/packages/deployer/package.json +++ b/packages/deployer/package.json @@ -68,7 +68,7 @@ "zeppelin-solidity": "1.8.0" }, "dependencies": { - "@0xproject/resolver": "^0.0.1", + "@0xproject/sol-resolver": "^0.0.1", "@0xproject/json-schemas": "^0.7.20", "@0xproject/types": "^0.6.0", "@0xproject/typescript-typings": "^0.1.0", diff --git a/packages/deployer/src/compiler.ts b/packages/deployer/src/compiler.ts index dfbe469a7..a3c8004ec 100644 --- a/packages/deployer/src/compiler.ts +++ b/packages/deployer/src/compiler.ts @@ -8,7 +8,7 @@ import { NPMResolver, Resolver, URLResolver, -} from '@0xproject/resolver'; +} from '@0xproject/sol-resolver'; import { ContractAbi } from '@0xproject/types'; import { logUtils, promisify } from '@0xproject/utils'; import chalk from 'chalk'; diff --git a/packages/deployer/src/utils/compiler.ts b/packages/deployer/src/utils/compiler.ts index 79dce5d73..c571b2581 100644 --- a/packages/deployer/src/utils/compiler.ts +++ b/packages/deployer/src/utils/compiler.ts @@ -1,4 +1,4 @@ -import { ContractSource, ContractSources } from '@0xproject/resolver'; +import { ContractSource, ContractSources } from '@0xproject/sol-resolver'; import { logUtils } from '@0xproject/utils'; import * as _ from 'lodash'; import * as path from 'path'; diff --git a/packages/deployer/test/compiler_utils_test.ts b/packages/deployer/test/compiler_utils_test.ts index 7e7ae1200..e2b95f289 100644 --- a/packages/deployer/test/compiler_utils_test.ts +++ b/packages/deployer/test/compiler_utils_test.ts @@ -47,24 +47,31 @@ describe('Compiler utils', () => { }); describe('#parseDependencies', () => { it('correctly parses Exchange dependencies', async () => { - const exchangeSource = await fsWrapper.readFileAsync(`${__dirname}/fixtures/contracts/Exchange.sol`, { + const path = `${__dirname}/fixtures/contracts/Exchange.sol`; + const source = await fsWrapper.readFileAsync(path, { encoding: 'utf8', }); - expect(parseDependencies(exchangeSource)).to.be.deep.equal(['ERC20', 'TokenTransferProxy', 'SafeMath']); + expect(parseDependencies({ source, path })).to.be.deep.equal([ + 'zeppelin-solidity/contracts/token/ERC20/ERC20.sol', + '/home/circleci/repo/packages/deployer/lib/test/fixtures/contracts/TokenTransferProxy.sol', + '/home/circleci/repo/packages/deployer/lib/test/fixtures/contracts/base/SafeMath.sol', + ]); }); it('correctly parses TokenTransferProxy dependencies', async () => { - const exchangeSource = await fsWrapper.readFileAsync( - `${__dirname}/fixtures/contracts/TokenTransferProxy.sol`, - { - encoding: 'utf8', - }, - ); - expect(parseDependencies(exchangeSource)).to.be.deep.equal(['Ownable', 'ERC20']); + const path = `${__dirname}/fixtures/contracts/TokenTransferProxy.sol`; + const source = await fsWrapper.readFileAsync(path, { + encoding: 'utf8', + }); + expect(parseDependencies({ source, path })).to.be.deep.equal([ + 'zeppelin-solidity/contracts/ownership/Ownable.sol', + 'zeppelin-solidity/contracts/token/ERC20/ERC20.sol', + ]); }); // TODO: For now that doesn't work. This will work after we switch to a grammar-based parser it.skip('correctly parses commented out dependencies', async () => { - const contractWithCommentedOutDependencies = `// import "./TokenTransferProxy.sol";`; - expect(parseDependencies(contractWithCommentedOutDependencies)).to.be.deep.equal([]); + const path = ''; + const source = `// import "./TokenTransferProxy.sol";`; + expect(parseDependencies({ path, source })).to.be.deep.equal([]); }); }); }); diff --git a/packages/deployer/test/fixtures/contracts/Exchange.sol b/packages/deployer/test/fixtures/contracts/Exchange.sol index 1249d21ed..e3725335b 100644 --- a/packages/deployer/test/fixtures/contracts/Exchange.sol +++ b/packages/deployer/test/fixtures/contracts/Exchange.sol @@ -1,6 +1,6 @@ /* - Copyright 2017 ZeroEx Intl. + Copyright 2018 ZeroEx Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/packages/deployer/test/fixtures/contracts/TokenTransferProxy.sol b/packages/deployer/test/fixtures/contracts/TokenTransferProxy.sol index 1ba8a96de..44570d459 100644 --- a/packages/deployer/test/fixtures/contracts/TokenTransferProxy.sol +++ b/packages/deployer/test/fixtures/contracts/TokenTransferProxy.sol @@ -1,6 +1,6 @@ /* - Copyright 2017 ZeroEx Intl. + Copyright 2018 ZeroEx Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/packages/resolver/package.json b/packages/resolver/package.json deleted file mode 100644 index 107405cf8..000000000 --- a/packages/resolver/package.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "name": "@0xproject/resolver", - "version": "0.0.1", - "description": "Import resolver for smart contracts dependencies", - "main": "lib/index.js", - "types": "lib/index.d.ts", - "scripts": { - "build:watch": "tsc -w", - "build": "yarn clean && tsc && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", - "clean": "shx rm -rf lib scripts", - "lint": "tslint --project .", - "manual:postpublish": "yarn build; node ./scripts/postpublish.js" - }, - "repository": { - "type": "git", - "url": "https://github.com/0xProject/0x-monorepo.git" - }, - "license": "Apache-2.0", - "bugs": { - "url": "https://github.com/0xProject/0x-monorepo/issues" - }, - "homepage": "https://github.com/0xProject/0x-monorepo/packages/resolver/README.md", - "devDependencies": { - "@0xproject/monorepo-scripts": "^0.1.16", - "@0xproject/tslint-config": "^0.4.14", - "copyfiles": "^1.2.0", - "shx": "^0.2.2", - "tslint": "5.8.0", - "typescript": "2.7.1" - }, - "dependencies": { - "@0xproject/typescript-typings": "^0.0.3", - "@0xproject/types": "^0.6.0", - "lodash": "^4.17.4" - }, - "publishConfig": { - "access": "public" - } -} diff --git a/packages/resolver/src/index.ts b/packages/resolver/src/index.ts deleted file mode 100644 index cd6ec42ec..000000000 --- a/packages/resolver/src/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -export { ContractSource, ContractSources } from './types'; -export { FallthroughResolver } from './resolvers/fallthrough_resolver'; -export { URLResolver } from './resolvers/url_resolver'; -export { NPMResolver } from './resolvers/npm_resolver'; -export { FSResolver } from './resolvers/fs_resolver'; -export { NameResolver } from './resolvers/name_resolver'; -export { EnumerableResolver } from './resolvers/enumerable_resolver'; -export { Resolver } from './resolvers/resolver'; diff --git a/packages/resolver/src/resolvers/enumerable_resolver.ts b/packages/resolver/src/resolvers/enumerable_resolver.ts deleted file mode 100644 index 0efa43e7c..000000000 --- a/packages/resolver/src/resolvers/enumerable_resolver.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { ContractSource } from '../types'; - -import { Resolver } from './resolver'; - -export abstract class EnumerableResolver extends Resolver { - public abstract getAll(): ContractSource[]; -} diff --git a/packages/resolver/src/resolvers/fallthrough_resolver.ts b/packages/resolver/src/resolvers/fallthrough_resolver.ts deleted file mode 100644 index 8f6af9be0..000000000 --- a/packages/resolver/src/resolvers/fallthrough_resolver.ts +++ /dev/null @@ -1,21 +0,0 @@ -import * as _ from 'lodash'; - -import { ContractSource } from '../types'; - -import { Resolver } from './resolver'; - -export class FallthroughResolver extends Resolver { - private _resolvers: Resolver[] = []; - public appendResolver(resolver: Resolver): void { - this._resolvers.push(resolver); - } - public resolveIfExists(importPath: string): ContractSource | undefined { - for (const resolver of this._resolvers) { - const contractSourceIfExists = resolver.resolveIfExists(importPath); - if (!_.isUndefined(contractSourceIfExists)) { - return contractSourceIfExists; - } - } - return undefined; - } -} diff --git a/packages/resolver/src/resolvers/fs_resolver.ts b/packages/resolver/src/resolvers/fs_resolver.ts deleted file mode 100644 index 4f05fba88..000000000 --- a/packages/resolver/src/resolvers/fs_resolver.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as fs from 'fs'; - -import { ContractSource } from '../types'; - -import { Resolver } from './resolver'; - -export class FSResolver extends Resolver { - // tslint:disable-next-line:prefer-function-over-method - public resolveIfExists(importPath: string): ContractSource | undefined { - if (fs.existsSync(importPath)) { - const fileContent = fs.readFileSync(importPath).toString(); - return { - source: fileContent, - path: importPath, - }; - } - return undefined; - } -} diff --git a/packages/resolver/src/resolvers/name_resolver.ts b/packages/resolver/src/resolvers/name_resolver.ts deleted file mode 100644 index 6849b7610..000000000 --- a/packages/resolver/src/resolvers/name_resolver.ts +++ /dev/null @@ -1,66 +0,0 @@ -import * as fs from 'fs'; -import * as _ from 'lodash'; -import * as path from 'path'; - -import { ContractSource } from '../types'; - -import { EnumerableResolver } from './enumerable_resolver'; - -export class NameResolver extends EnumerableResolver { - private _contractsDir: string; - constructor(contractsDir: string) { - super(); - this._contractsDir = contractsDir; - } - public resolveIfExists(lookupContractName: string): ContractSource | undefined { - const SOLIDITY_FILE_EXTENSION = '.sol'; - let contractSource: ContractSource | undefined; - const onFile = (filePath: string) => { - const contractName = path.basename(filePath, SOLIDITY_FILE_EXTENSION); - if (contractName === lookupContractName) { - const source = fs.readFileSync(filePath).toString(); - contractSource = { - source, - path: filePath, - }; - return true; - } - return undefined; - }; - this._traverseContractsDir(this._contractsDir, onFile); - return contractSource; - } - public getAll(): ContractSource[] { - const SOLIDITY_FILE_EXTENSION = '.sol'; - const contractSources: ContractSource[] = []; - const onFile = (filePath: string) => { - const contractName = path.basename(filePath, SOLIDITY_FILE_EXTENSION); - const source = fs.readFileSync(filePath).toString(); - const contractSource = { - source, - path: filePath, - }; - contractSources.push(contractSource); - }; - this._traverseContractsDir(this._contractsDir, onFile); - return contractSources; - } - // tslint:disable-next-line:prefer-function-over-method - private _traverseContractsDir(dirPath: string, onFile: (filePath: string) => true | void): boolean { - let dirContents: string[] = []; - try { - dirContents = fs.readdirSync(dirPath); - } catch (err) { - throw new Error(`No directory found at ${dirPath}`); - } - for (const fileName of dirContents) { - const entryPath = path.join(dirPath, fileName); - const isDirectory = fs.lstatSync(entryPath).isDirectory(); - const isComplete = isDirectory ? this._traverseContractsDir(entryPath, onFile) : onFile(entryPath); - if (isComplete) { - return isComplete; - } - } - return false; - } -} diff --git a/packages/resolver/src/resolvers/npm_resolver.ts b/packages/resolver/src/resolvers/npm_resolver.ts deleted file mode 100644 index 2c0a44aad..000000000 --- a/packages/resolver/src/resolvers/npm_resolver.ts +++ /dev/null @@ -1,34 +0,0 @@ -import * as fs from 'fs'; -import * as path from 'path'; - -import { ContractSource } from '../types'; - -import { Resolver } from './resolver'; - -export class NPMResolver extends Resolver { - private _packagePath: string; - constructor(packagePath: string) { - super(); - this._packagePath = packagePath; - } - public resolveIfExists(importPath: string): ContractSource | undefined { - if (!importPath.startsWith('/')) { - const [packageName, ...other] = importPath.split('/'); - const pathWithinPackage = path.join(...other); - let currentPath = this._packagePath; - const ROOT_PATH = '/'; - while (currentPath !== ROOT_PATH) { - const lookupPath = path.join(currentPath, 'node_modules', packageName, pathWithinPackage); - if (fs.existsSync(lookupPath)) { - const fileContent = fs.readFileSync(lookupPath).toString(); - return { - source: fileContent, - path: lookupPath, - }; - } - currentPath = path.dirname(currentPath); - } - } - return undefined; - } -} diff --git a/packages/resolver/src/resolvers/resolver.ts b/packages/resolver/src/resolvers/resolver.ts deleted file mode 100644 index 7edc9a85d..000000000 --- a/packages/resolver/src/resolvers/resolver.ts +++ /dev/null @@ -1,14 +0,0 @@ -import * as _ from 'lodash'; - -import { ContractSource } from '../types'; - -export abstract class Resolver { - public abstract resolveIfExists(importPath: string): ContractSource | undefined; - public resolve(importPath: string): ContractSource { - const contractSourceIfExists = this.resolveIfExists(importPath); - if (_.isUndefined(contractSourceIfExists)) { - throw new Error(`Failed to resolve ${importPath}`); - } - return contractSourceIfExists; - } -} diff --git a/packages/resolver/src/resolvers/url_resolver.ts b/packages/resolver/src/resolvers/url_resolver.ts deleted file mode 100644 index 180b0c9f6..000000000 --- a/packages/resolver/src/resolvers/url_resolver.ts +++ /dev/null @@ -1,21 +0,0 @@ -import * as fs from 'fs'; - -import { ContractSource } from '../types'; - -import { Resolver } from './resolver'; - -export class URLResolver extends Resolver { - // tslint:disable-next-line:prefer-function-over-method - public resolveIfExists(importPath: string): ContractSource | undefined { - const FILE_URL_PREXIF = 'file://'; - if (importPath.startsWith(FILE_URL_PREXIF)) { - const filePath = importPath.substr(FILE_URL_PREXIF.length); - const fileContent = fs.readFileSync(filePath).toString(); - return { - source: fileContent, - path: importPath, - }; - } - return undefined; - } -} diff --git a/packages/resolver/src/types.ts b/packages/resolver/src/types.ts deleted file mode 100644 index 41492622d..000000000 --- a/packages/resolver/src/types.ts +++ /dev/null @@ -1,8 +0,0 @@ -export interface ContractSource { - source: string; - path: string; -} - -export interface ContractSources { - [key: string]: ContractSource; -} diff --git a/packages/resolver/tsconfig.json b/packages/resolver/tsconfig.json deleted file mode 100644 index f5f4b37c2..000000000 --- a/packages/resolver/tsconfig.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "../../tsconfig", - "compilerOptions": { - "outDir": "lib" - }, - "include": ["src/**/*"] -} diff --git a/packages/resolver/tslint.json b/packages/resolver/tslint.json deleted file mode 100644 index ffaefe83a..000000000 --- a/packages/resolver/tslint.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": ["@0xproject/tslint-config"] -} diff --git a/packages/sol-cov/src/collect_contract_data.ts b/packages/sol-cov/src/collect_contract_data.ts index da56e8d0d..1d8bc7178 100644 --- a/packages/sol-cov/src/collect_contract_data.ts +++ b/packages/sol-cov/src/collect_contract_data.ts @@ -12,6 +12,7 @@ export const collectContractsData = (artifactsPath: string, sourcesPath: string, const artifact = JSON.parse(fs.readFileSync(artifactFileName).toString()); const sources = artifact.networks[networkId].sources; const contractName = artifact.contract_name; + // We don't compute coverage for dependencies const sourceCodes = _.map(sources, (source: string) => fs.readFileSync(source).toString()); if (_.isUndefined(artifact.networks[networkId])) { throw new Error(`No ${contractName} artifacts found for networkId ${networkId}`); diff --git a/packages/sol-resolver/package.json b/packages/sol-resolver/package.json new file mode 100644 index 000000000..ae1a94f3e --- /dev/null +++ b/packages/sol-resolver/package.json @@ -0,0 +1,39 @@ +{ + "name": "@0xproject/sol-resolver", + "version": "0.0.1", + "description": "Import resolver for smart contracts dependencies", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "scripts": { + "build:watch": "tsc -w", + "build": "yarn clean && tsc && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", + "clean": "shx rm -rf lib scripts", + "lint": "tslint --project .", + "manual:postpublish": "yarn build; node ./scripts/postpublish.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/0xProject/0x-monorepo.git" + }, + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/0xProject/0x-monorepo/issues" + }, + "homepage": "https://github.com/0xProject/0x-monorepo/packages/resolver/README.md", + "devDependencies": { + "@0xproject/monorepo-scripts": "^0.1.16", + "@0xproject/tslint-config": "^0.4.14", + "copyfiles": "^1.2.0", + "shx": "^0.2.2", + "tslint": "5.8.0", + "typescript": "2.7.1" + }, + "dependencies": { + "@0xproject/typescript-typings": "^0.0.3", + "@0xproject/types": "^0.6.0", + "lodash": "^4.17.4" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/sol-resolver/src/index.ts b/packages/sol-resolver/src/index.ts new file mode 100644 index 000000000..cd6ec42ec --- /dev/null +++ b/packages/sol-resolver/src/index.ts @@ -0,0 +1,8 @@ +export { ContractSource, ContractSources } from './types'; +export { FallthroughResolver } from './resolvers/fallthrough_resolver'; +export { URLResolver } from './resolvers/url_resolver'; +export { NPMResolver } from './resolvers/npm_resolver'; +export { FSResolver } from './resolvers/fs_resolver'; +export { NameResolver } from './resolvers/name_resolver'; +export { EnumerableResolver } from './resolvers/enumerable_resolver'; +export { Resolver } from './resolvers/resolver'; diff --git a/packages/sol-resolver/src/resolvers/enumerable_resolver.ts b/packages/sol-resolver/src/resolvers/enumerable_resolver.ts new file mode 100644 index 000000000..0efa43e7c --- /dev/null +++ b/packages/sol-resolver/src/resolvers/enumerable_resolver.ts @@ -0,0 +1,7 @@ +import { ContractSource } from '../types'; + +import { Resolver } from './resolver'; + +export abstract class EnumerableResolver extends Resolver { + public abstract getAll(): ContractSource[]; +} diff --git a/packages/sol-resolver/src/resolvers/fallthrough_resolver.ts b/packages/sol-resolver/src/resolvers/fallthrough_resolver.ts new file mode 100644 index 000000000..8f6af9be0 --- /dev/null +++ b/packages/sol-resolver/src/resolvers/fallthrough_resolver.ts @@ -0,0 +1,21 @@ +import * as _ from 'lodash'; + +import { ContractSource } from '../types'; + +import { Resolver } from './resolver'; + +export class FallthroughResolver extends Resolver { + private _resolvers: Resolver[] = []; + public appendResolver(resolver: Resolver): void { + this._resolvers.push(resolver); + } + public resolveIfExists(importPath: string): ContractSource | undefined { + for (const resolver of this._resolvers) { + const contractSourceIfExists = resolver.resolveIfExists(importPath); + if (!_.isUndefined(contractSourceIfExists)) { + return contractSourceIfExists; + } + } + return undefined; + } +} diff --git a/packages/sol-resolver/src/resolvers/fs_resolver.ts b/packages/sol-resolver/src/resolvers/fs_resolver.ts new file mode 100644 index 000000000..4f05fba88 --- /dev/null +++ b/packages/sol-resolver/src/resolvers/fs_resolver.ts @@ -0,0 +1,19 @@ +import * as fs from 'fs'; + +import { ContractSource } from '../types'; + +import { Resolver } from './resolver'; + +export class FSResolver extends Resolver { + // tslint:disable-next-line:prefer-function-over-method + public resolveIfExists(importPath: string): ContractSource | undefined { + if (fs.existsSync(importPath)) { + const fileContent = fs.readFileSync(importPath).toString(); + return { + source: fileContent, + path: importPath, + }; + } + return undefined; + } +} diff --git a/packages/sol-resolver/src/resolvers/name_resolver.ts b/packages/sol-resolver/src/resolvers/name_resolver.ts new file mode 100644 index 000000000..6849b7610 --- /dev/null +++ b/packages/sol-resolver/src/resolvers/name_resolver.ts @@ -0,0 +1,66 @@ +import * as fs from 'fs'; +import * as _ from 'lodash'; +import * as path from 'path'; + +import { ContractSource } from '../types'; + +import { EnumerableResolver } from './enumerable_resolver'; + +export class NameResolver extends EnumerableResolver { + private _contractsDir: string; + constructor(contractsDir: string) { + super(); + this._contractsDir = contractsDir; + } + public resolveIfExists(lookupContractName: string): ContractSource | undefined { + const SOLIDITY_FILE_EXTENSION = '.sol'; + let contractSource: ContractSource | undefined; + const onFile = (filePath: string) => { + const contractName = path.basename(filePath, SOLIDITY_FILE_EXTENSION); + if (contractName === lookupContractName) { + const source = fs.readFileSync(filePath).toString(); + contractSource = { + source, + path: filePath, + }; + return true; + } + return undefined; + }; + this._traverseContractsDir(this._contractsDir, onFile); + return contractSource; + } + public getAll(): ContractSource[] { + const SOLIDITY_FILE_EXTENSION = '.sol'; + const contractSources: ContractSource[] = []; + const onFile = (filePath: string) => { + const contractName = path.basename(filePath, SOLIDITY_FILE_EXTENSION); + const source = fs.readFileSync(filePath).toString(); + const contractSource = { + source, + path: filePath, + }; + contractSources.push(contractSource); + }; + this._traverseContractsDir(this._contractsDir, onFile); + return contractSources; + } + // tslint:disable-next-line:prefer-function-over-method + private _traverseContractsDir(dirPath: string, onFile: (filePath: string) => true | void): boolean { + let dirContents: string[] = []; + try { + dirContents = fs.readdirSync(dirPath); + } catch (err) { + throw new Error(`No directory found at ${dirPath}`); + } + for (const fileName of dirContents) { + const entryPath = path.join(dirPath, fileName); + const isDirectory = fs.lstatSync(entryPath).isDirectory(); + const isComplete = isDirectory ? this._traverseContractsDir(entryPath, onFile) : onFile(entryPath); + if (isComplete) { + return isComplete; + } + } + return false; + } +} diff --git a/packages/sol-resolver/src/resolvers/npm_resolver.ts b/packages/sol-resolver/src/resolvers/npm_resolver.ts new file mode 100644 index 000000000..2c0a44aad --- /dev/null +++ b/packages/sol-resolver/src/resolvers/npm_resolver.ts @@ -0,0 +1,34 @@ +import * as fs from 'fs'; +import * as path from 'path'; + +import { ContractSource } from '../types'; + +import { Resolver } from './resolver'; + +export class NPMResolver extends Resolver { + private _packagePath: string; + constructor(packagePath: string) { + super(); + this._packagePath = packagePath; + } + public resolveIfExists(importPath: string): ContractSource | undefined { + if (!importPath.startsWith('/')) { + const [packageName, ...other] = importPath.split('/'); + const pathWithinPackage = path.join(...other); + let currentPath = this._packagePath; + const ROOT_PATH = '/'; + while (currentPath !== ROOT_PATH) { + const lookupPath = path.join(currentPath, 'node_modules', packageName, pathWithinPackage); + if (fs.existsSync(lookupPath)) { + const fileContent = fs.readFileSync(lookupPath).toString(); + return { + source: fileContent, + path: lookupPath, + }; + } + currentPath = path.dirname(currentPath); + } + } + return undefined; + } +} diff --git a/packages/sol-resolver/src/resolvers/resolver.ts b/packages/sol-resolver/src/resolvers/resolver.ts new file mode 100644 index 000000000..7edc9a85d --- /dev/null +++ b/packages/sol-resolver/src/resolvers/resolver.ts @@ -0,0 +1,14 @@ +import * as _ from 'lodash'; + +import { ContractSource } from '../types'; + +export abstract class Resolver { + public abstract resolveIfExists(importPath: string): ContractSource | undefined; + public resolve(importPath: string): ContractSource { + const contractSourceIfExists = this.resolveIfExists(importPath); + if (_.isUndefined(contractSourceIfExists)) { + throw new Error(`Failed to resolve ${importPath}`); + } + return contractSourceIfExists; + } +} diff --git a/packages/sol-resolver/src/resolvers/url_resolver.ts b/packages/sol-resolver/src/resolvers/url_resolver.ts new file mode 100644 index 000000000..180b0c9f6 --- /dev/null +++ b/packages/sol-resolver/src/resolvers/url_resolver.ts @@ -0,0 +1,21 @@ +import * as fs from 'fs'; + +import { ContractSource } from '../types'; + +import { Resolver } from './resolver'; + +export class URLResolver extends Resolver { + // tslint:disable-next-line:prefer-function-over-method + public resolveIfExists(importPath: string): ContractSource | undefined { + const FILE_URL_PREXIF = 'file://'; + if (importPath.startsWith(FILE_URL_PREXIF)) { + const filePath = importPath.substr(FILE_URL_PREXIF.length); + const fileContent = fs.readFileSync(filePath).toString(); + return { + source: fileContent, + path: importPath, + }; + } + return undefined; + } +} diff --git a/packages/sol-resolver/src/types.ts b/packages/sol-resolver/src/types.ts new file mode 100644 index 000000000..41492622d --- /dev/null +++ b/packages/sol-resolver/src/types.ts @@ -0,0 +1,8 @@ +export interface ContractSource { + source: string; + path: string; +} + +export interface ContractSources { + [key: string]: ContractSource; +} diff --git a/packages/sol-resolver/tsconfig.json b/packages/sol-resolver/tsconfig.json new file mode 100644 index 000000000..f5f4b37c2 --- /dev/null +++ b/packages/sol-resolver/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig", + "compilerOptions": { + "outDir": "lib" + }, + "include": ["src/**/*"] +} diff --git a/packages/sol-resolver/tslint.json b/packages/sol-resolver/tslint.json new file mode 100644 index 000000000..ffaefe83a --- /dev/null +++ b/packages/sol-resolver/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": ["@0xproject/tslint-config"] +} -- cgit v1.2.3