Skip to content

Commit

Permalink
refactor ts/yaml loaders and extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
bz888 committed Oct 20, 2023
1 parent 629e325 commit d72f2ad
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 180 deletions.
2 changes: 1 addition & 1 deletion packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
### Added
- ts-manifest compatibility with `codegen:generate` command (#2111)

## [4.0.4] - 2023-10-17
Expand Down
99 changes: 64 additions & 35 deletions packages/cli/src/commands/codegen/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import fs, {lstatSync} from 'fs';
import path from 'path';
import {EventFragment, FunctionFragment} from '@ethersproject/abi/src.ts/fragments';
import {Command, Flags} from '@oclif/core';
import {DEFAULT_MANIFEST, DEFAULT_TS_MANIFEST, extensionIsTs, getProjectRootAndManifest} from '@subql/common';
import {DEFAULT_MANIFEST, DEFAULT_TS_MANIFEST, extensionIsTs} from '@subql/common';
import {SubqlRuntimeDatasource as EthereumDs} from '@subql/types-ethereum';
import {parseContractPath} from 'typechain';
import {Document, Node} from 'yaml';
import {
constructMethod,
filterExistingMethods,
filterObjectsByStateMutability,
generateHandlers,
generateManifest,
generateManifestTs,
generateManifestYaml,
getAbiInterface,
getManifestData,
prepareAbiDirectory,
prepareInputFragments,
tsExtractor,
yamlExtractor,
} from '../../controller/generate-controller';
import {extractFromTs} from '../../utils';

Expand Down Expand Up @@ -49,8 +51,7 @@ export default class Generate extends Command {
async run(): Promise<void> {
const {flags} = await this.parse(Generate);
const {abiPath, address, events, file, functions, startBlock} = flags;
let manifest: string;
let root: string;
let manifest: string, root: string;
let isTs: boolean;

const projectPath = path.resolve(file ?? process.cwd());
Expand Down Expand Up @@ -87,40 +88,68 @@ export default class Generate extends Command {
const eventsFragments = abiInterface.events;
const functionFragments = filterObjectsByStateMutability(abiInterface.functions);

let existingManifest: Document<Node<unknown>, true> | string;
let existingDs: EthereumDs[] | string;
// ts check if the file is ts
if (isTs) {
existingManifest = await fs.promises.readFile(manifest, 'utf8');

const extractedDatasources = extractFromTs(existingManifest, {
dataSources: undefined,
});
existingDs = extractedDatasources.dataSources as string;
} else {
// yaml
existingManifest = await getManifestData(manifest);
existingDs = ((existingManifest.get('dataSources') as any)?.toJSON() as EthereumDs[]) ?? [];
}

const selectedEvents = await prepareInputFragments('event', events, eventsFragments, abiName);
const selectedFunctions = await prepareInputFragments('function', functions, functionFragments, abiName);

const [cleanEvents, cleanFunctions] = filterExistingMethods(selectedEvents, selectedFunctions, existingDs, address);

const constructedEvents: SelectedMethod[] = constructMethod<EventFragment>(cleanEvents);
const constructedFunctions: SelectedMethod[] = constructMethod<FunctionFragment>(cleanFunctions);
let cleanEvents: Record<string, EventFragment>,
cleanFunctions: Record<string, FunctionFragment>,
constructedEvents: SelectedMethod[],
constructedFunctions: SelectedMethod[];

try {
const userInput: UserInput = {
startBlock: startBlock,
functions: constructedFunctions,
events: constructedEvents,
abiPath: `./abis/${abiFileName}`,
address: address,
};

await generateManifest(manifest, userInput, existingManifest);
if (isTs) {
const existingManifest = await fs.promises.readFile(manifest, 'utf8');
const extractedDatasources = extractFromTs(existingManifest, {
dataSources: undefined,
});
const existingDs = extractedDatasources.dataSources as string;
const [cleanEvents, cleanFunctions] = filterExistingMethods(
selectedEvents,
selectedFunctions,
existingDs,
address,
tsExtractor
);

constructedEvents = constructMethod<EventFragment>(cleanEvents);
constructedFunctions = constructMethod<FunctionFragment>(cleanFunctions);

const userInput: UserInput = {
startBlock: startBlock,
functions: constructedFunctions,
events: constructedEvents,
abiPath: `./abis/${abiFileName}`,
address: address,
};

await generateManifestTs(manifest, userInput, existingManifest);
} else {
// yaml
const existingManifest = await getManifestData(manifest);
const existingDs = ((existingManifest.get('dataSources') as any)?.toJSON() as EthereumDs[]) ?? [];

[cleanEvents, cleanFunctions] = filterExistingMethods(
selectedEvents,
selectedFunctions,
existingDs,
address,
yamlExtractor
);

constructedEvents = constructMethod<EventFragment>(cleanEvents);
constructedFunctions = constructMethod<FunctionFragment>(cleanFunctions);

const userInput: UserInput = {
startBlock: startBlock,
functions: constructedFunctions,
events: constructedEvents,
abiPath: `./abis/${abiFileName}`,
address: address,
};

await generateManifestYaml(manifest, userInput, existingManifest);
}

await generateHandlers([constructedEvents, constructedFunctions], root, abiName);

this.log('-----------Generated-----------');
Expand All @@ -132,7 +161,7 @@ export default class Generate extends Command {
});
this.log('-------------------------------');
} catch (e) {
throw new Error(e.message);
this.error(e);
}
}
}
1 change: 1 addition & 0 deletions packages/cli/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export const BASE_TEMPLATE_URl = 'https://templates.subquery.network';

// Regex for cold tsManifest
export const ENDPOINT_REG = /endpoint:\s*(\[[^\]]+\]|['"`][^'"`]+['"`])/;
export const ADDRESS_REG = /address\s*:\s*['"]([^'"]+)['"]/;
Loading

0 comments on commit d72f2ad

Please sign in to comment.