Skip to content

Conversation

@jsoverson
Copy link
Contributor

This PR adds the concept of "plugins" to apex.

Goal: To simplify configuration and reduce the amount someone needs to know or change before getting started.

Secondary goal: Support dynamic configurations.

Implementation:

Apex plugins are TypeScript files that export a single default function that has the signature (doc: apex.Document, config: Configuration) -> Configuration. They take in a parsed apex document with an existing configuration and return a new configuration.

For example, given an apex configuration that looks like this:

spec: sample.axdl
plugins:
  - https://deno.land/x/sample_plugin@v.0.0.0/mod.ts

And where sample_plugin@v.0.0.0/mod.ts looks like this:

import { Configuration } from "https://deno.land/x/apex_cli@v0.0.6/src/config.ts";
import * as apex from "https://deno.land/x/apex_core@v0.1.0/mod.ts";

export default function (doc: apex.ast.Document, config: Configuration): Configuration {
  config.generates ||= {};

  const interfaces = doc.definitions.filter(
    (def) => def.getKind() === apex.ast.Kind.InterfaceDefinition
  ) as apex.ast.InterfaceDefinition[];

  for (const iface of interfaces) {
    config.generates[`./src/${iface.name.value}.ts`] = {
      module: "./some/generator/module.ts",
    };
  }

  return config;
}

Running generate would result in configuration that looks like this before getting passed to the generators:

spec: sample.axdl
plugins:
  - ./sample-plugin.ts
generates:
  ./src/Test.ts: 
    module: ./some/generator/module.ts

Note: The new configuration doesn't need to be dynamic. Plugins are also suitable for turning blobs of configuration into a single plugin definition.

@jsoverson jsoverson requested a review from pkedy December 13, 2022 19:17
@jsoverson jsoverson changed the title Added concept of plugins to simply and generate configuration Added concept of plugins to simplify and generate configuration Dec 13, 2022
src/process.ts Outdated
return JSON.parse(output) as Output[];
}

async function processPlugins(config: Configuration): Promise<Configuration> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of duplicating the logic in process.ts and generate.ts, could the plugins be added / iterated in the existing code path?

src/generate.ts Outdated
2,
)
}`,
`Generating source for '${file}' with generator from ${url} with config\n${JSON.stringify(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using deno fmt src to format all the code instead of using prettier. It looks like the formatter you are using is making a change here. Could you pls run deno fmt src?

@jsoverson jsoverson marked this pull request as ready for review December 16, 2022 18:39
@jsoverson
Copy link
Contributor Author

PR is ready for review @pkedy.

@pkedy pkedy merged commit 0b59ab3 into main Dec 18, 2022
@pkedy pkedy deleted the config-plugins branch December 18, 2022 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants