From b25e37a89fbd1cb7c4e25ba8b49cf8e30bc7dc34 Mon Sep 17 00:00:00 2001 From: filip mertens Date: Wed, 8 Mar 2023 16:18:32 +0100 Subject: [PATCH] refactor --- apps/remixdocgen/src/app/App.tsx | 30 ++++++--- apps/remixdocgen/src/app/docgen-client.ts | 62 +++++++++++++++++-- .../{plugins => }/docgen/common/helpers.ts | 0 .../{plugins => }/docgen/common/properties.ts | 0 .../src/app/{plugins => }/docgen/config.ts | 0 .../src/app/{plugins => }/docgen/doc-item.ts | 0 .../src/app/{plugins => }/docgen/render.ts | 0 .../src/app/{plugins => }/docgen/site.ts | 0 .../src/app/{plugins => }/docgen/templates.ts | 0 .../docgen/themes/markdown/common.hbs | 0 .../docgen/themes/markdown/contract.hbs | 0 .../docgen/themes/markdown/enum.hbs | 0 .../docgen/themes/markdown/error.hbs | 0 .../docgen/themes/markdown/event.hbs | 0 .../docgen/themes/markdown/function.hbs | 0 .../docgen/themes/markdown/helpers.ts | 0 .../docgen/themes/markdown/modifier.hbs | 0 .../docgen/themes/markdown/page.hbs | 0 .../docgen/themes/markdown/struct.hbs | 0 .../markdown/user-defined-value-type.hbs | 0 .../docgen/themes/markdown/variable.hbs | 0 .../{plugins => }/docgen/utils/ItemError.ts | 0 .../docgen/utils/arrays-equal.ts | 0 .../docgen/utils/assert-equal-types.ts | 0 .../app/{plugins => }/docgen/utils/clone.ts | 0 .../docgen/utils/ensure-array.ts | 0 .../app/{plugins => }/docgen/utils/execall.ts | 0 .../{plugins => }/docgen/utils/is-child.ts | 0 .../{plugins => }/docgen/utils/item-type.ts | 0 .../{plugins => }/docgen/utils/map-keys.ts | 0 .../{plugins => }/docgen/utils/map-values.ts | 0 .../docgen/utils/memoized-getter.ts | 0 .../app/{plugins => }/docgen/utils/natspec.ts | 0 .../docgen/utils/read-item-docs.ts | 0 .../app/{plugins => }/docgen/utils/scope.ts | 0 apps/remixdocgen/src/app/plugins/docgen.ts | 44 ------------- 36 files changed, 76 insertions(+), 60 deletions(-) rename apps/remixdocgen/src/app/{plugins => }/docgen/common/helpers.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/common/properties.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/config.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/doc-item.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/render.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/site.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/templates.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/common.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/contract.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/enum.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/error.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/event.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/function.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/helpers.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/modifier.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/page.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/struct.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/user-defined-value-type.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/themes/markdown/variable.hbs (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/ItemError.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/arrays-equal.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/assert-equal-types.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/clone.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/ensure-array.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/execall.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/is-child.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/item-type.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/map-keys.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/map-values.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/memoized-getter.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/natspec.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/read-item-docs.ts (100%) rename apps/remixdocgen/src/app/{plugins => }/docgen/utils/scope.ts (100%) delete mode 100644 apps/remixdocgen/src/app/plugins/docgen.ts diff --git a/apps/remixdocgen/src/app/App.tsx b/apps/remixdocgen/src/app/App.tsx index 94896b410e..dfbc3c43cd 100644 --- a/apps/remixdocgen/src/app/App.tsx +++ b/apps/remixdocgen/src/app/App.tsx @@ -5,6 +5,9 @@ import { import './App.css' import { DocGenClient } from './docgen-client' +import { Build } from './docgen/site' + +export const client = new DocGenClient() export const getNewContractNames = (compilationResult: CompilationResult) => { const compiledContracts = compilationResult.contracts @@ -18,28 +21,35 @@ export const getNewContractNames = (compilationResult: CompilationResult) => { } const App = () => { - const client = useMemo(() => new DocGenClient(), []) + const [themeType, setThemeType] = useState('dark'); + const [hasBuild, setHasBuild] = useState(false); + const [fileName, setFileName] = useState(''); useEffect(() => { const watchThemeSwitch = async () => { - //await client.call("manager" as any, "activatePlugin", "ethdoc-viewer"); - - const currentTheme = await client.call('theme', 'currentTheme'); - - setThemeType(currentTheme.brightness || currentTheme.quality); + client.eventEmitter.on('themeChanged', (theme: string) => { + console.log('themeChanged', theme) + setThemeType(theme) + }) + client.eventEmitter.on('compilationFinished', (build: Build, fileName: string) => { + setHasBuild(true) + setFileName(fileName) + }) + client.eventEmitter.on('docsGenerated', (docs: string[]) => { + console.log('docsGenerated', docs) + }) - client.on("theme", "themeChanged", (theme: any) => { - setThemeType(theme.quality); - }); }; watchThemeSwitch(); - }, [client]); + }, []); return (

Remix Docgen

+ {fileName &&

File: {fileName}

} + {hasBuild && }
) }; diff --git a/apps/remixdocgen/src/app/docgen-client.ts b/apps/remixdocgen/src/app/docgen-client.ts index 10df133d24..ad455f7ae8 100644 --- a/apps/remixdocgen/src/app/docgen-client.ts +++ b/apps/remixdocgen/src/app/docgen-client.ts @@ -1,22 +1,72 @@ import { PluginClient } from '@remixproject/plugin' +import { CompilationResult, SourceWithTarget } from '@remixproject/plugin-api' import { createClient } from '@remixproject/plugin-webview' -import { DocGen } from './plugins/docgen' +import EventEmitter from 'events' +import { Config, defaults } from './docgen/config' +import { Build, buildSite } from './docgen/site' +import { loadTemplates } from './docgen/templates' +import {SolcInput, SolcOutput} from 'solidity-ast/solc' +import { render } from './docgen/render' export class DocGenClient extends PluginClient { - private client - private docgen + private currentTheme + public eventEmitter: EventEmitter + private build: Build constructor() { super() - this.client = createClient(this) + this.eventEmitter = new EventEmitter() + createClient(this) // this.docgen = new DocGen() this.methods = ['generateDocs', 'publishDocs'] + this.onload().then(async () => { + console.log('docgen client loaded') + await this.setListeners() + }) } - loadClient() { - this.client.onload() + + async setListeners() { + this.currentTheme = await this.call('theme', 'currentTheme'); + this.on("theme", "themeChanged", (theme: any) => { + this.currentTheme = theme + this.eventEmitter.emit('themeChanged', this.currentTheme) + }); + this.eventEmitter.emit('themeChanged', this.currentTheme) + + this.on('solidity', 'compilationFinished', (fileName: string, source: SourceWithTarget, languageVersion: string, data: CompilationResult) => { + console.log('docgen client compilationFinished', data, source) + const input: SolcInput = { + sources: source.sources + } + const output: SolcOutput = { + sources: data.sources as any + } + this.build = { + input: input, + output: output + } + + this.eventEmitter.emit('compilationFinished', this.build, fileName) + + }) } + + async docgen(builds: Build[], userConfig?: Config): Promise { + const config = { ...defaults, ...userConfig } + const templates = await loadTemplates(config.theme, config.root, config.templates) + const site = buildSite(builds, config, templates.properties ?? {}) + const renderedSite = render(site, templates, config.collapseNewlines) + const docs: string[] = [] + for (const { id, contents } of renderedSite) { + await this.call('fileManager', 'setFile', id, contents) + docs.push(id) + } + this.eventEmitter.emit('docsGenerated', docs) + } + async generateDocs() { console.log('docgen client generateDocs') + this.docgen([this.build]) } async publishDocs() { diff --git a/apps/remixdocgen/src/app/plugins/docgen/common/helpers.ts b/apps/remixdocgen/src/app/docgen/common/helpers.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/common/helpers.ts rename to apps/remixdocgen/src/app/docgen/common/helpers.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/common/properties.ts b/apps/remixdocgen/src/app/docgen/common/properties.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/common/properties.ts rename to apps/remixdocgen/src/app/docgen/common/properties.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/config.ts b/apps/remixdocgen/src/app/docgen/config.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/config.ts rename to apps/remixdocgen/src/app/docgen/config.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/doc-item.ts b/apps/remixdocgen/src/app/docgen/doc-item.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/doc-item.ts rename to apps/remixdocgen/src/app/docgen/doc-item.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/render.ts b/apps/remixdocgen/src/app/docgen/render.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/render.ts rename to apps/remixdocgen/src/app/docgen/render.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/site.ts b/apps/remixdocgen/src/app/docgen/site.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/site.ts rename to apps/remixdocgen/src/app/docgen/site.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/templates.ts b/apps/remixdocgen/src/app/docgen/templates.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/templates.ts rename to apps/remixdocgen/src/app/docgen/templates.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/common.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/common.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/common.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/common.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/contract.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/contract.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/contract.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/contract.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/enum.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/enum.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/enum.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/enum.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/error.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/error.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/error.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/error.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/event.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/event.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/event.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/event.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/function.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/function.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/function.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/function.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/helpers.ts b/apps/remixdocgen/src/app/docgen/themes/markdown/helpers.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/helpers.ts rename to apps/remixdocgen/src/app/docgen/themes/markdown/helpers.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/modifier.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/modifier.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/modifier.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/modifier.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/page.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/page.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/page.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/page.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/struct.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/struct.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/struct.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/struct.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/user-defined-value-type.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/user-defined-value-type.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/user-defined-value-type.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/user-defined-value-type.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/themes/markdown/variable.hbs b/apps/remixdocgen/src/app/docgen/themes/markdown/variable.hbs similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/themes/markdown/variable.hbs rename to apps/remixdocgen/src/app/docgen/themes/markdown/variable.hbs diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/ItemError.ts b/apps/remixdocgen/src/app/docgen/utils/ItemError.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/ItemError.ts rename to apps/remixdocgen/src/app/docgen/utils/ItemError.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/arrays-equal.ts b/apps/remixdocgen/src/app/docgen/utils/arrays-equal.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/arrays-equal.ts rename to apps/remixdocgen/src/app/docgen/utils/arrays-equal.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/assert-equal-types.ts b/apps/remixdocgen/src/app/docgen/utils/assert-equal-types.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/assert-equal-types.ts rename to apps/remixdocgen/src/app/docgen/utils/assert-equal-types.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/clone.ts b/apps/remixdocgen/src/app/docgen/utils/clone.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/clone.ts rename to apps/remixdocgen/src/app/docgen/utils/clone.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/ensure-array.ts b/apps/remixdocgen/src/app/docgen/utils/ensure-array.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/ensure-array.ts rename to apps/remixdocgen/src/app/docgen/utils/ensure-array.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/execall.ts b/apps/remixdocgen/src/app/docgen/utils/execall.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/execall.ts rename to apps/remixdocgen/src/app/docgen/utils/execall.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/is-child.ts b/apps/remixdocgen/src/app/docgen/utils/is-child.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/is-child.ts rename to apps/remixdocgen/src/app/docgen/utils/is-child.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/item-type.ts b/apps/remixdocgen/src/app/docgen/utils/item-type.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/item-type.ts rename to apps/remixdocgen/src/app/docgen/utils/item-type.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/map-keys.ts b/apps/remixdocgen/src/app/docgen/utils/map-keys.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/map-keys.ts rename to apps/remixdocgen/src/app/docgen/utils/map-keys.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/map-values.ts b/apps/remixdocgen/src/app/docgen/utils/map-values.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/map-values.ts rename to apps/remixdocgen/src/app/docgen/utils/map-values.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/memoized-getter.ts b/apps/remixdocgen/src/app/docgen/utils/memoized-getter.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/memoized-getter.ts rename to apps/remixdocgen/src/app/docgen/utils/memoized-getter.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/natspec.ts b/apps/remixdocgen/src/app/docgen/utils/natspec.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/natspec.ts rename to apps/remixdocgen/src/app/docgen/utils/natspec.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/read-item-docs.ts b/apps/remixdocgen/src/app/docgen/utils/read-item-docs.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/read-item-docs.ts rename to apps/remixdocgen/src/app/docgen/utils/read-item-docs.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen/utils/scope.ts b/apps/remixdocgen/src/app/docgen/utils/scope.ts similarity index 100% rename from apps/remixdocgen/src/app/plugins/docgen/utils/scope.ts rename to apps/remixdocgen/src/app/docgen/utils/scope.ts diff --git a/apps/remixdocgen/src/app/plugins/docgen.ts b/apps/remixdocgen/src/app/plugins/docgen.ts deleted file mode 100644 index 3c366cfcbc..0000000000 --- a/apps/remixdocgen/src/app/plugins/docgen.ts +++ /dev/null @@ -1,44 +0,0 @@ -'use strict' -import { Plugin } from '@remixproject/engine' -import { Build, buildSite } from './docgen/site'; -import { render } from './docgen/render'; -import { Config, defaults } from './docgen/config'; -import { loadTemplates } from './docgen/templates'; -import { SolcInput, SolcOutput } from 'solidity-ast/solc'; - -const profile = { - name: 'docgen', - desciption: 'solidity doc gen plugin for Remix', - methods: ['docgen'], - events: [''], - version: '0.0.1' -} - - - -export class DocGen extends Plugin { - - constructor() { - super(profile) - } - - onActivation(): void { - //this.docgen([{ output: example, input: inp }]) - this.on('solidity', 'compilationFinished', (file, source, languageVersion, data, input, version) => { - this.docgen([{ output: data, input: JSON.parse(input) }]) - }) - } - - async docgen(builds: Build[], userConfig?: Config): Promise { - const config = { ...defaults, ...userConfig } - const templates = await loadTemplates(config.theme, config.root, config.templates) - const site = buildSite(builds, config, templates.properties ?? {}) - const renderedSite = render(site, templates, config.collapseNewlines) - console.log({ renderedSite }) - for (const { id, contents } of renderedSite) { - await this.call('fileManager', 'setFile', id, contents) - } - } - - -} \ No newline at end of file