| 14 | const ABI_FILE_SUFFIX = '-abi.json'; |
| 15 | |
| 16 | export class ProgramProvider implements TreeDataProvider<Function | Program> { |
| 17 | private _onDidChangeTreeData: EventEmitter<Program | undefined | void> = |
| 18 | new EventEmitter<Program | undefined | void>(); |
| 19 | readonly onDidChangeTreeData: Event<Program | undefined | void> = |
| 20 | this._onDidChangeTreeData.event; |
| 21 | |
| 22 | constructor( |
| 23 | private workspaceRoot: string | undefined, |
| 24 | readonly type: ProgramType |
| 25 | ) {} |
| 26 | |
| 27 | refresh(): void { |
| 28 | this._onDidChangeTreeData.fire(); |
| 29 | } |
| 30 | |
| 31 | getTreeItem(element: TreeItem): TreeItem { |
| 32 | return element; |
| 33 | } |
| 34 | |
| 35 | getChildren(contract?: Function | Program): ProviderResult<Program[]> { |
| 36 | if (!this.workspaceRoot) { |
| 37 | window.showInformationMessage('No contract in empty workspace'); |
| 38 | return Promise.resolve([]); |
| 39 | } |
| 40 | |
| 41 | return contract |
| 42 | ? Promise.resolve(contract['children']) |
| 43 | : this.getPrograms(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Reads contracts from the ABIs. |
| 48 | * @returns array of contracts |
| 49 | */ |
| 50 | private async getPrograms(): Promise<Program[]> { |
| 51 | const allFiles = getAllFiles(this.workspaceRoot, []); |
| 52 | const swayFilePaths = allFiles.filter(file => file.endsWith('.sw')); |
| 53 | const forcTomlFilePaths = allFiles.filter(file => |
| 54 | file.endsWith('Forc.toml') |
| 55 | ); |
| 56 | const abiFilePaths = allFiles.filter(file => |
| 57 | file.endsWith(ABI_FILE_SUFFIX) |
| 58 | ); |
| 59 | const programs = abiFilePaths.map(filepath => { |
| 60 | const contractName = path |
| 61 | .parse(filepath) |
| 62 | .base.replace(ABI_FILE_SUFFIX, ''); |
| 63 | |
| 64 | // This is assuming that there is only one sway contract in the same directory as a Forc.toml |
| 65 | const forcFilePath = forcTomlFilePaths.find(forcFilePath => |
| 66 | fs.readFileSync(forcFilePath).toString().includes(contractName) |
| 67 | ); |
| 68 | |
| 69 | // Find out the program type of the sway file |
| 70 | const swayFilePath = swayFilePaths.find(swayFilePath => { |
| 71 | return swayFilePath.startsWith(path.parse(forcFilePath).dir); |
| 72 | }); |
| 73 | const swayFileMatchesType = fs |
nothing calls this directly
no outgoing calls
no test coverage detected