fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #3286 from hey-api/chore/py-plugin

chore: add Python post processors

authored by

Lubos and committed by
GitHub
339cbe23 2b10f1ea

+163 -70
+8 -10
packages/openapi-python/src/config/init.ts
··· 1 1 import type { Logger } from '@hey-api/codegen-core'; 2 2 import { loadConfigFile } from '@hey-api/codegen-core'; 3 3 4 - // import { expandToJobs } from './expand'; 4 + import { expandToJobs } from './expand'; 5 5 // import { getProjectDependencies } from './packages'; 6 6 import type { ResolvedJob } from './resolve'; 7 - // import { resolveConfig } from './resolve'; 7 + import { resolveConfig } from './resolve'; 8 8 import type { UserConfig } from './types'; 9 - // import { validateJobs } from './validate'; 9 + import { validateJobs } from './validate'; 10 10 11 11 export type Configs = { 12 12 dependencies: Record<string, string>; ··· 38 38 const loaded = await loadConfigFile<UserConfig>({ 39 39 configFile, 40 40 logger, 41 - name: 'openapi-ts', 41 + name: 'openapi-python', 42 42 userConfig, 43 43 }); 44 44 45 45 if (!Object.keys(dependencies).length) { 46 46 // TODO: handle dependencies for multiple configs properly? 47 + // TODO: collect Python dependencies 47 48 // dependencies = getProjectDependencies( 48 49 // loaded.foundConfig ? loaded.configFile : undefined, 49 50 // ); ··· 54 55 eventLoad.timeEnd(); 55 56 56 57 const eventBuild = logger.timeEvent('build'); 57 - // const jobs = validateJobs(expandToJobs(configs)); 58 - // const resolvedJobs = jobs.map((validated) => 59 - // resolveConfig(validated, dependencies), 60 - // ); 58 + const jobs = validateJobs(expandToJobs(configs)); 59 + const resolvedJobs = jobs.map((validated) => resolveConfig(validated, dependencies)); 61 60 eventBuild.timeEnd(); 62 61 63 62 return { 64 63 dependencies, 65 - jobs: [], 66 - // jobs: resolvedJobs, 64 + jobs: resolvedJobs, 67 65 }; 68 66 }
+30 -1
packages/openapi-python/src/config/output/postprocess.ts
··· 1 1 import type { PostProcessor } from '@hey-api/shared'; 2 2 3 3 export const postProcessors = { 4 - // TODO: add common post-processors 4 + autopep8: { 5 + args: ['--in-place', '{{path}}'], 6 + command: 'autopep8', 7 + name: 'autopep8', 8 + }, 9 + black: { 10 + args: ['{{path}}'], 11 + command: 'black', 12 + name: 'Black', 13 + }, 14 + isort: { 15 + args: ['{{path}}'], 16 + command: 'isort', 17 + name: 'isort', 18 + }, 19 + 'ruff:format': { 20 + args: ['format', '{{path}}'], 21 + command: 'ruff', 22 + name: 'Ruff (Format)', 23 + }, 24 + 'ruff:lint': { 25 + args: ['check', '--fix', '{{path}}'], 26 + command: 'ruff', 27 + name: 'Ruff (Lint)', 28 + }, 29 + yapf: { 30 + args: ['-i', '{{path}}'], 31 + command: 'yapf', 32 + name: 'YAPF', 33 + }, 5 34 } as const satisfies Record<string, PostProcessor>; 6 35 7 36 export type PostProcessorPreset = keyof typeof postProcessors;
+2 -3
packages/openapi-python/src/config/output/types.ts
··· 20 20 * 21 21 * Use preset strings for common tools, or provide custom configurations. 22 22 * 23 - * @example ['biome:lint', 'prettier'] 24 - * @example [{ command: 'dprint', args: ['fmt', '{{path}}'] }] 25 - * @example ['eslint', { command: 'prettier', args: ['{{path}}', '--write'] }] 23 + * @example ['ruff:lint', 'ruff:format'] 24 + * @example [{ command: 'flake8', args: ['{{path}}'] }] 26 25 * 27 26 * @default [] 28 27 */
+2 -2
packages/openapi-python/src/createClient.ts
··· 20 20 21 21 import { postProcessors } from './config/output/postprocess'; 22 22 import type { Config } from './config/types'; 23 - // import { generateOutput } from './generate/output'; 23 + import { generateOutput } from './generate/output'; 24 24 // import { TypeScriptRenderer } from './ts-dsl'; 25 25 26 26 export async function createClient({ ··· 143 143 eventParser.timeEnd(); 144 144 145 145 const eventGenerator = logger.timeEvent('generator'); 146 - // await generateOutput({ context }); 146 + await generateOutput(context); 147 147 eventGenerator.timeEnd(); 148 148 149 149 const eventPostprocess = logger.timeEvent('postprocess');
+76
packages/openapi-python/src/generate/output.ts
··· 1 + import fs from 'node:fs'; 2 + import path from 'node:path'; 3 + 4 + import type { Context } from '@hey-api/shared'; 5 + import { IntentContext } from '@hey-api/shared'; 6 + 7 + // import { getTypedConfig } from '../config/utils'; 8 + // import { getClientPlugin } from '../plugins/@hey-api/client-core/utils'; 9 + // import { generateClientBundle } from './client'; 10 + 11 + export async function generateOutput(context: Context): Promise<void> { 12 + const outputPath = path.resolve(context.config.output.path); 13 + 14 + if (context.config.output.clean) { 15 + if (fs.existsSync(outputPath)) { 16 + fs.rmSync(outputPath, { force: true, recursive: true }); 17 + } 18 + } 19 + 20 + // const config = getTypedConfig(context); 21 + 22 + // const client = getClientPlugin(config); 23 + // if ('bundle' in client.config && client.config.bundle && !config.dryRun) { 24 + // not proud of this one 25 + // // @ts-expect-error 26 + // config._FRAGILE_CLIENT_BUNDLE_RENAMED = generateClientBundle({ 27 + // meta: { 28 + // importFileExtension: config.output.importFileExtension, 29 + // }, 30 + // outputPath, 31 + // // @ts-expect-error 32 + // plugin: client, 33 + // project: context.gen, 34 + // }); 35 + // } 36 + 37 + for (const plugin of context.registerPlugins()) { 38 + await plugin.run(); 39 + } 40 + 41 + context.gen.plan(); 42 + 43 + const ctx = new IntentContext(context.spec); 44 + for (const intent of context.intents) { 45 + await intent.run(ctx); 46 + } 47 + 48 + for (const file of context.gen.render()) { 49 + const filePath = path.resolve(outputPath, file.path); 50 + const dir = path.dirname(filePath); 51 + if (!context.config.dryRun) { 52 + fs.mkdirSync(dir, { recursive: true }); 53 + fs.writeFileSync(filePath, file.content, { encoding: 'utf8' }); 54 + } 55 + } 56 + 57 + const { source } = context.config.output; 58 + if (source.enabled) { 59 + const sourcePath = source.path === null ? undefined : path.resolve(outputPath, source.path); 60 + if (!context.config.dryRun && sourcePath && sourcePath !== outputPath) { 61 + fs.mkdirSync(sourcePath, { recursive: true }); 62 + } 63 + const serialized = await source.serialize(context.spec); 64 + // TODO: handle yaml (convert before writing) 65 + if (!context.config.dryRun && sourcePath) { 66 + fs.writeFileSync( 67 + path.resolve(sourcePath, `${source.fileName}.${source.extension}`), 68 + serialized, 69 + { encoding: 'utf8' }, 70 + ); 71 + } 72 + if (source.callback) { 73 + await source.callback(serialized); 74 + } 75 + } 76 + }
+42 -51
packages/openapi-python/src/plugins/@hey-api/sdk/config.ts
··· 19 19 // eslint-disable-next-line sort-keys-fix/sort-keys-fix 20 20 response: 'body', 21 21 }, 22 - dependencies: ['@hey-api/typescript'], 23 22 handler, 24 23 name: '@hey-api/python-sdk', 25 - resolveConfig: (plugin, context) => { 26 - if (plugin.config.client) { 27 - if (typeof plugin.config.client === 'boolean') { 28 - plugin.config.client = context.pluginByTag('client', { 29 - defaultPlugin: '@hey-api/client-httpx', 30 - }); 31 - } 32 - 33 - plugin.dependencies.add(plugin.config.client!); 34 - } else { 35 - plugin.config.client = false; 36 - } 37 - 38 - if (plugin.config.transformer) { 39 - if (typeof plugin.config.transformer === 'boolean') { 40 - plugin.config.transformer = context.pluginByTag('transformer'); 41 - } 42 - 43 - plugin.dependencies.add(plugin.config.transformer!); 44 - } else { 45 - plugin.config.transformer = false; 46 - } 47 - 48 - if (typeof plugin.config.validator !== 'object') { 49 - plugin.config.validator = { 50 - request: plugin.config.validator, 51 - response: plugin.config.validator, 52 - }; 53 - } 54 - 55 - if (plugin.config.validator.request) { 56 - if (typeof plugin.config.validator.request === 'boolean') { 57 - plugin.config.validator.request = context.pluginByTag('validator'); 58 - } 59 - 60 - plugin.dependencies.add(plugin.config.validator.request!); 61 - } else { 62 - plugin.config.validator.request = false; 63 - } 64 - 65 - if (plugin.config.validator.response) { 66 - if (typeof plugin.config.validator.response === 'boolean') { 67 - plugin.config.validator.response = context.pluginByTag('validator'); 68 - } 69 - 70 - plugin.dependencies.add(plugin.config.validator.response!); 71 - } else { 72 - plugin.config.validator.response = false; 73 - } 74 - 24 + // resolveConfig: (plugin, context) => { 25 + resolveConfig: () => { 26 + // if (plugin.config.client) { 27 + // if (typeof plugin.config.client === 'boolean') { 28 + // plugin.config.client = context.pluginByTag('client', { 29 + // defaultPlugin: '@hey-api/client-httpx', 30 + // }); 31 + // } 32 + // plugin.dependencies.add(plugin.config.client!); 33 + // } else { 34 + // plugin.config.client = false; 35 + // } 36 + // if (plugin.config.transformer) { 37 + // if (typeof plugin.config.transformer === 'boolean') { 38 + // plugin.config.transformer = context.pluginByTag('transformer'); 39 + // } 40 + // plugin.dependencies.add(plugin.config.transformer!); 41 + // } else { 42 + // plugin.config.transformer = false; 43 + // } 44 + // if (typeof plugin.config.validator !== 'object') { 45 + // plugin.config.validator = { 46 + // request: plugin.config.validator, 47 + // response: plugin.config.validator, 48 + // }; 49 + // } 50 + // if (plugin.config.validator.request) { 51 + // if (typeof plugin.config.validator.request === 'boolean') { 52 + // plugin.config.validator.request = context.pluginByTag('validator'); 53 + // } 54 + // plugin.dependencies.add(plugin.config.validator.request!); 55 + // } else { 56 + // plugin.config.validator.request = false; 57 + // } 58 + // if (plugin.config.validator.response) { 59 + // if (typeof plugin.config.validator.response === 'boolean') { 60 + // plugin.config.validator.response = context.pluginByTag('validator'); 61 + // } 62 + // plugin.dependencies.add(plugin.config.validator.response!); 63 + // } else { 64 + // plugin.config.validator.response = false; 65 + // } 75 66 // plugin.config.examples = resolveExamples(plugin.config, context); 76 67 // plugin.config.operations = resolveOperations(plugin.config, context); 77 68 },
+1 -1
packages/openapi-ts/src/createClient.ts
··· 142 142 eventParser.timeEnd(); 143 143 144 144 const eventGenerator = logger.timeEvent('generator'); 145 - await generateOutput({ context }); 145 + await generateOutput(context); 146 146 eventGenerator.timeEnd(); 147 147 148 148 const eventPostprocess = logger.timeEvent('postprocess');
+2 -2
packages/openapi-ts/src/generate/output.ts
··· 8 8 import { getClientPlugin } from '../plugins/@hey-api/client-core/utils'; 9 9 import { generateClientBundle } from './client'; 10 10 11 - export const generateOutput = async ({ context }: { context: Context }) => { 11 + export async function generateOutput(context: Context): Promise<void> { 12 12 const outputPath = path.resolve(context.config.output.path); 13 13 14 14 if (context.config.output.clean) { ··· 73 73 await source.callback(serialized); 74 74 } 75 75 } 76 - }; 76 + }