Skip to content

Commit

Permalink
Merge pull request #592 from jvalue/interpreter-pipeline-flag
Browse files Browse the repository at this point in the history
Add CLI flag to select pipelines to execute
  • Loading branch information
georg-schwarz committed Jun 20, 2024
2 parents d4c8559 + 1d1f8dc commit 1f80d25
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
5 changes: 5 additions & 0 deletions apps/interpreter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <regular expression to match with pipeline name>',
'Only runs the matching pipelines (matching by name) and ignores other pipelines in the file.',
'.*',
)
.description('Run a Jayvee file')
.action(runAction);

Expand Down
68 changes: 57 additions & 11 deletions libs/interpreter-lib/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
debug: boolean;
debugGranularity: string;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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,
Expand All @@ -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<string, string>,
Expand Down Expand Up @@ -179,18 +211,32 @@ async function interpretJayveeModel(
constraintExtension: JayveeConstraintExtension,
jayveeServices: JayveeServices,
loggerFactory: LoggerFactory,
interpreterLogger: Logger,
runOptions: InterpreterOptions,
): Promise<ExitCode> {
const pipelineRuns: Promise<ExitCode>[] = 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<ExitCode>[] = selectedPipelines.map(
(pipeline) => {
return runPipeline(
pipeline,
executionExtension,
constraintExtension,
jayveeServices,
loggerFactory,
runOptions,
);
},
);
const exitCodes = await Promise.all(pipelineRuns);

if (exitCodes.includes(ExitCode.FAILURE)) {
Expand Down

0 comments on commit 1f80d25

Please sign in to comment.