Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Interpreter (to an Interface + Implementation Class) #594

Merged
merged 13 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions apps/interpreter/src/examples-smoke-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import nock from 'nock';
import { type MockInstance, vi } from 'vitest';

import { runAction } from './run-action';
import { type RunOptions } from './run-options';

// Mock global imports
vi.mock('pg', () => {
Expand Down Expand Up @@ -46,11 +47,13 @@ vi.mock('sqlite3', () => {
describe('jv example smoke tests', () => {
const baseDir = path.resolve(__dirname, '../../../example/');

const defaultOptions = {
const defaultOptions: RunOptions = {
pipeline: '.*',
env: new Map<string, string>(),
debug: false,
debugGranularity: 'minimal',
debugTarget: undefined,
debugTarget: 'all',
parseOnly: false,
};

let exitSpy: MockInstance;
Expand Down
7 changes: 5 additions & 2 deletions apps/interpreter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//
// SPDX-License-Identifier: AGPL-3.0-only

import { DebugGranularityValues } from '@jvalue/jayvee-execution';
import {
DebugGranularityValues,
DefaultDebugTargetsValue,
} from '@jvalue/jayvee-execution';
import { Command } from 'commander';

import { version as packageJsonVersion } from '../package.json';
Expand Down Expand Up @@ -55,7 +58,7 @@ program
.option(
'-dt, --debug-target <block name>',
`Sets the target blocks of the of block debug logging, separated by comma. If not given, all blocks are targeted.`,
undefined,
DefaultDebugTargetsValue,
)
.option(
'-po, --parse-only',
Expand Down
34 changes: 15 additions & 19 deletions apps/interpreter/src/parse-only.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@ import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';

import {
type RunOptions,
interpretModel,
interpretString,
} from '@jvalue/jayvee-interpreter-lib';
import { vi } from 'vitest';
import { type JayveeInterpreter } from '@jvalue/jayvee-interpreter-lib';

import { runAction } from './run-action';
import { type RunOptions } from './run-options';

vi.mock('@jvalue/jayvee-interpreter-lib', async () => {
const original: object = await vi.importActual(
'@jvalue/jayvee-interpreter-lib',
);
return {
...original,
interpretModel: vi.fn(),
interpretString: vi.fn(),
};
});
const interpreterMock: JayveeInterpreter = {
interpretModel: vi.fn(),
interpretFile: vi.fn(),
interpretString: vi.fn(),
parseModel: vi.fn(),
};

vi.stubGlobal('DefaultJayveeInterpreter', interpreterMock);

describe('Parse Only', () => {
const pathToValidModel = path.resolve(__dirname, '../../../example/cars.jv');
Expand All @@ -34,16 +28,18 @@ describe('Parse Only', () => {
);

const defaultOptions: RunOptions = {
pipeline: '.*',
env: new Map<string, string>(),
debug: false,
debugGranularity: 'minimal',
debugTarget: undefined,
debugTarget: 'all',
parseOnly: false,
};

afterEach(() => {
// Assert that model is not executed
expect(interpretString).not.toBeCalled();
expect(interpretModel).not.toBeCalled();
expect(interpreterMock.interpretString).not.toBeCalled();
expect(interpreterMock.interpretModel).not.toBeCalled();
});

beforeEach(() => {
Expand Down
66 changes: 47 additions & 19 deletions apps/interpreter/src/run-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,63 @@
import process from 'node:process';

import {
type LoggerFactory,
type RunOptions,
DefaultJayveeInterpreter,
ExitCode,
type JayveeInterpreter,
LoggerFactory,
extractAstNodeFromFile,
interpretModel,
parseModel,
} from '@jvalue/jayvee-interpreter-lib';
import {
type JayveeModel,
type JayveeServices,
} from '@jvalue/jayvee-language-server';

import { parsePipelineMatcherRegExp, parseRunOptions } from './run-options';

export async function runAction(
fileName: string,
options: RunOptions,
filePath: string,
optionsRaw: unknown,
): Promise<void> {
const extractAstNodeFn = async (
services: JayveeServices,
loggerFactory: LoggerFactory,
) =>
await extractAstNodeFromFile<JayveeModel>(
fileName,
services,
loggerFactory.createLogger(),
);
const logger = new LoggerFactory(true).createLogger('Arguments');
const options = parseRunOptions(optionsRaw, logger);
if (options === undefined) {
return process.exit(ExitCode.FAILURE);
}

const pipelineRegExp = parsePipelineMatcherRegExp(options.pipeline, logger);
if (pipelineRegExp === undefined) {
return process.exit(ExitCode.FAILURE);
}

const interpreter = new DefaultJayveeInterpreter({
pipelineMatcher: (pipelineDefinition) =>
pipelineRegExp.test(pipelineDefinition.name),
env: options.env,
debug: options.debug,
debugGranularity: options.debugGranularity,
debugTarget: options.debugTarget,
});

if (options.parseOnly === true) {
const { model, services } = await parseModel(extractAstNodeFn, options);
const exitCode = model != null && services != null ? 0 : 1;
process.exit(exitCode);
return await runParseOnly(filePath, interpreter);
}
const exitCode = await interpretModel(extractAstNodeFn, options);

const exitCode = await interpreter.interpretFile(filePath);
process.exit(exitCode);
}

async function runParseOnly(
filePath: string,
interpreter: JayveeInterpreter,
): Promise<void> {
const model = await interpreter.parseModel(
async (services: JayveeServices, loggerFactory: LoggerFactory) =>
await extractAstNodeFromFile<JayveeModel>(
filePath,
services,
loggerFactory.createLogger(),
),
);
const exitCode = model === undefined ? ExitCode.FAILURE : ExitCode.SUCCESS;
process.exit(exitCode);
}
192 changes: 192 additions & 0 deletions apps/interpreter/src/run-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg
georg-schwarz marked this conversation as resolved.
Show resolved Hide resolved
//
// SPDX-License-Identifier: AGPL-3.0-only

import {
type DebugGranularity,
DebugGranularityValues,
type DebugTargets,
DefaultDebugTargetsValue,
type Logger,
isDebugGranularity,
} from '@jvalue/jayvee-execution';

export interface RunOptions {
pipeline: string;
env: Map<string, string>;
debug: boolean;
debugGranularity: DebugGranularity;
debugTarget: DebugTargets;
parseOnly: boolean;
}

export function parseRunOptions(
optionsRaw: unknown,
logger: Logger,
): RunOptions | undefined {
if (typeof optionsRaw !== 'object' || optionsRaw == null) {
logger.logErr(
"Error in the interpreter: didn't receive a valid RunOptions object.",
);
return undefined;
}

const requiredFields = [
'pipeline',
'env',
'debug',
'debugGranularity',
'debugTarget',
'parseOnly',
];
if (requiredFields.some((f) => !(f in optionsRaw))) {
logger.logErr(
`Error in the interpreter: didn't receive a valid RunOptions object. Must have the fields ${requiredFields
.map((f) => `"${f}"`)
.join(', ')} but got object ${JSON.stringify(optionsRaw, null, 2)}`,
);
return undefined;
}

const options = optionsRaw as Record<keyof RunOptions, unknown>;

if (
!isPipelineArgument(options.pipeline, logger) ||
!isEnvArgument(options.env, logger) ||
!isDebugArgument(options.debug, logger) ||
!isDebugGranularityArgument(options.debugGranularity, logger) ||
!isDebugTargetArgument(options.debugTarget, logger) ||
!isParseOnlyArgument(options.parseOnly, logger)
) {
return undefined;
}

return {
pipeline: options.pipeline,
env: options.env,
debug: options.debug === true || options.debug === 'true',
debugGranularity: options.debugGranularity as DebugGranularity,
debugTarget: getDebugTargets(options.debugTarget),
parseOnly: options.parseOnly === true || options.parseOnly === 'true',
};
}

function getDebugTargets(debugTargetsString: string): DebugTargets {
const areAllBlocksTargeted = debugTargetsString === DefaultDebugTargetsValue;
if (areAllBlocksTargeted) {
return DefaultDebugTargetsValue;
}

return debugTargetsString.split(',').map((target) => target.trim());
}

function isPipelineArgument(arg: unknown, logger: Logger): arg is string {
if (typeof arg !== 'string') {
logger.logErr(
`Invalid value "${JSON.stringify(
arg,
)}" for pipeline selection option: -p --pipeline.\n` +
'Must be a string value.',
);
return false;
}
return true;
}

function isDebugGranularityArgument(
arg: unknown,
logger: Logger,
): arg is DebugGranularity {
if (!isDebugGranularity(arg)) {
logger.logErr(
`Invalid value "${JSON.stringify(
arg,
)}" for debug granularity option: -dg --debug-granularity.\n` +
`Must be one of the following values: ${DebugGranularityValues.join(
', ',
)}.`,
);
return false;
}
return true;
}

function isDebugTargetArgument(arg: unknown, logger: Logger): arg is string {
// options.debugTarget
if (typeof arg !== 'string') {
logger.logErr(
`Invalid value "${JSON.stringify(
arg,
)}" for debug target option: -dt --debug-target.\n` +
'Must be a string value.',
);
return false;
}
return true;
}

function isDebugArgument(
arg: unknown,
logger: Logger,
): arg is boolean | 'true' | 'false' {
if (typeof arg !== 'boolean' && arg !== 'true' && arg !== 'false') {
logger.logErr(
`Invalid value "${JSON.stringify(arg)}" for debug option: -d --debug.\n` +
'Must be true or false.',
);
return false;
}
return true;
}

function isParseOnlyArgument(
arg: unknown,
logger: Logger,
): arg is boolean | 'true' | 'false' {
if (typeof arg !== 'boolean' && arg !== 'true' && arg !== 'false') {
logger.logErr(
`Invalid value "${JSON.stringify(
arg,
)}" for parse-only option: -po --parse-only.\n` +
'Must be true or false.',
);
return false;
}
return true;
}

function isEnvArgument(
arg: unknown,
logger: Logger,
): arg is Map<string, string> {
if (
!(
arg instanceof Map &&
[...arg.entries()].every(
([key, value]) => typeof key === 'string' && typeof value === 'string',
)
)
) {
logger.logErr(
`Invalid value "${JSON.stringify(arg)}" for env option: -e --env.\n` +
'Must be map from string keys to string values.',
);
return false;
}
return true;
}

export function parsePipelineMatcherRegExp(
matcher: string,
logger: Logger,
): RegExp | undefined {
try {
return new RegExp(matcher);
} catch (e: unknown) {
logger.logErr(
`Invalid value "${matcher}" for pipeline selection option: -p --pipeline.\n` +
'Must be a valid regular expression.',
);
return undefined;
}
}
Loading
Loading