aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-resolver
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-04-16 22:38:55 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-04-16 23:43:59 +0800
commitd8ef76fd5efe63ec8f6205a73494ce388e64391f (patch)
tree22a5740584029830e37961136e229938b8f735f4 /packages/sol-resolver
parente5cf41b3134d6cd5fdda995aa81ec23e51bb5145 (diff)
downloaddexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar.gz
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar.bz2
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar.lz
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar.xz
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar.zst
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.zip
Rename resolver to sol-resolver
Diffstat (limited to 'packages/sol-resolver')
-rw-r--r--packages/sol-resolver/package.json39
-rw-r--r--packages/sol-resolver/src/index.ts8
-rw-r--r--packages/sol-resolver/src/resolvers/enumerable_resolver.ts7
-rw-r--r--packages/sol-resolver/src/resolvers/fallthrough_resolver.ts21
-rw-r--r--packages/sol-resolver/src/resolvers/fs_resolver.ts19
-rw-r--r--packages/sol-resolver/src/resolvers/name_resolver.ts66
-rw-r--r--packages/sol-resolver/src/resolvers/npm_resolver.ts34
-rw-r--r--packages/sol-resolver/src/resolvers/resolver.ts14
-rw-r--r--packages/sol-resolver/src/resolvers/url_resolver.ts21
-rw-r--r--packages/sol-resolver/src/types.ts8
-rw-r--r--packages/sol-resolver/tsconfig.json7
-rw-r--r--packages/sol-resolver/tslint.json3
12 files changed, 247 insertions, 0 deletions
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"]
+}