From 1d1f8dc0e28dda4e51f20db2922687e03076d678 Mon Sep 17 00:00:00 2001 From: Georg Schwarz Date: Wed, 19 Jun 2024 17:38:00 +0200 Subject: [PATCH] Add CLI flag to select pipelines in a file --- apps/interpreter/src/index.ts | 5 ++ libs/interpreter-lib/src/interpreter.ts | 68 +++++++++++++++++++++---- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/apps/interpreter/src/index.ts b/apps/interpreter/src/index.ts index f7d473370..c0ca634ef 100644 --- a/apps/interpreter/src/index.ts +++ b/apps/interpreter/src/index.ts @@ -62,6 +62,11 @@ program 'Only parses the model without running it. Exits with 0 if the model is valid, with 1 otherwise.', false, ) + .option( + '-p, --pipeline ', + 'Only runs the matching pipelines (matching by name) and ignores other pipelines in the file.', + '.*', + ) .description('Run a Jayvee file') .action(runAction); diff --git a/libs/interpreter-lib/src/interpreter.ts b/libs/interpreter-lib/src/interpreter.ts index 58d76f454..64fc13436 100644 --- a/libs/interpreter-lib/src/interpreter.ts +++ b/libs/interpreter-lib/src/interpreter.ts @@ -38,12 +38,14 @@ import { ExitCode, extractAstNodeFromString } from './parsing-util'; import { validateRuntimeParameterLiteral } from './validation-checks'; interface InterpreterOptions { + pipelineMatcher: (pipelineDefinition: PipelineDefinition) => boolean; debugGranularity: R.DebugGranularity; debugTargets: R.DebugTargets; debug: boolean; } export interface RunOptions { + pipeline: string; env: Map; debug: boolean; debugGranularity: string; @@ -126,7 +128,18 @@ export async function interpretModel( options, ); - if (model == null || services == null) { + const interpreterLogger = loggerFactory.createLogger('Interpreter'); + + const pipelineMatcherRegexp = parsePipelineMatcher( + options.pipeline, + interpreterLogger, + ); + + if ( + model == null || + services == null || + pipelineMatcherRegexp === undefined + ) { return ExitCode.FAILURE; } @@ -138,7 +151,10 @@ export async function interpretModel( new DefaultConstraintExtension(), services, loggerFactory, + interpreterLogger, { + pipelineMatcher: (pipelineDefinition) => + pipelineMatcherRegexp.test(pipelineDefinition.name), debug: options.debug, // type of options.debugGranularity is asserted in parseModel debugGranularity: options.debugGranularity as DebugGranularity, @@ -148,6 +164,22 @@ export async function interpretModel( return interpretationExitCode; } +function parsePipelineMatcher( + matcherString: string, + logger: Logger, +): RegExp | undefined { + try { + return new RegExp(matcherString); + } catch (e: unknown) { + logger.logErr( + `Given pipeline matcher argument is not valid: "${matcherString}" is no valid regular expression${ + e instanceof SyntaxError ? `: ${e.message}` : '' + }`, + ); + } + return undefined; +} + function setupJayveeServices( services: JayveeServices, rawRuntimeParameters: ReadonlyMap, @@ -179,18 +211,32 @@ async function interpretJayveeModel( constraintExtension: JayveeConstraintExtension, jayveeServices: JayveeServices, loggerFactory: LoggerFactory, + interpreterLogger: Logger, runOptions: InterpreterOptions, ): Promise { - const pipelineRuns: Promise[] = model.pipelines.map((pipeline) => { - return runPipeline( - pipeline, - executionExtension, - constraintExtension, - jayveeServices, - loggerFactory, - runOptions, - ); - }); + const selectedPipelines = model.pipelines.filter((pipeline) => + runOptions.pipelineMatcher(pipeline), + ); + interpreterLogger.logInfo( + `Found ${selectedPipelines.length} pipelines to execute${ + selectedPipelines.length > 0 + ? ': ' + selectedPipelines.map((p) => p.name).join(', ') + : '' + }`, + ); + + const pipelineRuns: Promise[] = selectedPipelines.map( + (pipeline) => { + return runPipeline( + pipeline, + executionExtension, + constraintExtension, + jayveeServices, + loggerFactory, + runOptions, + ); + }, + ); const exitCodes = await Promise.all(pipelineRuns); if (exitCodes.includes(ExitCode.FAILURE)) {