|
|
|
@ -1,11 +1,14 @@ |
|
|
|
|
import type {CompilationSource, AstNode} from '@remix-project/remix-solidity' |
|
|
|
|
|
|
|
|
|
const IMPORT_SOLIDITY_REGEX = /^\s*import(\s+).*$/gm; |
|
|
|
|
const SPDX_SOLIDITY_REGEX = /^\s*\/\/ SPDX-License-Identifier:.*$/gm; |
|
|
|
|
|
|
|
|
|
export function getDependencyGraph(ast, target) { |
|
|
|
|
type Visited = { [key: string]: number } |
|
|
|
|
export function getDependencyGraph(ast: { [name: string]: CompilationSource }, target: string, remappings: string[]) { |
|
|
|
|
const graph = tsort(); |
|
|
|
|
const visited = {}; |
|
|
|
|
visited[target] = 1; |
|
|
|
|
_traverse(graph, visited, ast, target); |
|
|
|
|
_traverse(graph, visited, ast, target, remappings); |
|
|
|
|
return graph; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -21,32 +24,31 @@ export function concatSourceFiles(files: any[], sources: any) { |
|
|
|
|
return concat; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function _traverse(graph, visited, ast, name) { |
|
|
|
|
function _traverse(graph: Graph, visited: Visited, ast: { [name: string]: CompilationSource }, name: string, remappings: string[]) { |
|
|
|
|
let currentAst = null |
|
|
|
|
currentAst = ast[name].ast |
|
|
|
|
const dependencies = _getDependencies(currentAst); |
|
|
|
|
for (const dependency of dependencies) { |
|
|
|
|
const path = resolve(name, dependency); |
|
|
|
|
const path = resolve(name, dependency, remappings); |
|
|
|
|
if (path in visited) { |
|
|
|
|
// continue; // fixes wrong ordering of source in flattened file
|
|
|
|
|
} |
|
|
|
|
visited[path] = 1; |
|
|
|
|
graph.add(name, path); |
|
|
|
|
_traverse(graph, visited, ast, path); |
|
|
|
|
_traverse(graph, visited, ast, path, remappings); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function _getDependencies(ast) { |
|
|
|
|
function _getDependencies(ast: AstNode) { |
|
|
|
|
const dependencies = ast?.nodes |
|
|
|
|
.filter(node => node?.nodeType === 'ImportDirective') |
|
|
|
|
.map(node => node?.file); |
|
|
|
|
return dependencies; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TSORT
|
|
|
|
|
|
|
|
|
|
function tsort(initial?: any) { |
|
|
|
|
function tsort(initial?: any): Graph { |
|
|
|
|
const graph = new Graph(); |
|
|
|
|
|
|
|
|
|
if (initial) { |
|
|
|
@ -58,13 +60,14 @@ function tsort(initial?: any) { |
|
|
|
|
return graph; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class Graph { |
|
|
|
|
nodes: { [key: string]: any} |
|
|
|
|
constructor() { |
|
|
|
|
this.nodes = {} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function Graph() { |
|
|
|
|
this.nodes = {}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Add sorted items to the graph
|
|
|
|
|
Graph.prototype.add = function () { |
|
|
|
|
// Add sorted items to the graph
|
|
|
|
|
add (name, path) { |
|
|
|
|
const self = this; |
|
|
|
|
// eslint-disable-next-line prefer-rest-params
|
|
|
|
|
let items = [].slice.call(arguments); |
|
|
|
@ -86,11 +89,11 @@ Graph.prototype.add = function () { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return self; |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Depth first search
|
|
|
|
|
// As given in http://en.wikipedia.org/wiki/Topological_sorting
|
|
|
|
|
Graph.prototype.sort = function () { |
|
|
|
|
// Depth first search
|
|
|
|
|
// As given in http://en.wikipedia.org/wiki/Topological_sorting
|
|
|
|
|
sort () { |
|
|
|
|
const self = this; |
|
|
|
|
const nodes = Object.keys(this.nodes); |
|
|
|
|
|
|
|
|
@ -119,17 +122,26 @@ Graph.prototype.sort = function () { |
|
|
|
|
|
|
|
|
|
sorted.push(node); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Graph.prototype.isEmpty = function () { |
|
|
|
|
isEmpty () { |
|
|
|
|
const nodes = Object.keys(this.nodes); |
|
|
|
|
return nodes.length === 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// PATH
|
|
|
|
|
|
|
|
|
|
function resolve(parentPath, childPath) { |
|
|
|
|
function resolve(parentPath, childPath, remappings: string[]) { |
|
|
|
|
if (remappings && remappings.length) { |
|
|
|
|
for (const mapping of remappings) { |
|
|
|
|
if (mapping.indexOf('=') !== -1) { |
|
|
|
|
const split = mapping.split('=') |
|
|
|
|
childPath = childPath.replace(split[0].trim(), split[1].trim()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (_isAbsolute(childPath)) { |
|
|
|
|
return childPath; |
|
|
|
|
} |
|
|
|
|