44
55// eslint-disable-next-line unicorn/prefer-node-protocol
66import { strict as assert } from 'assert' ;
7+ import path from 'node:path' ;
78
89import {
910 type DebugGranularity ,
@@ -28,9 +29,11 @@ import {
2829 type RuntimeParameterProvider ,
2930 type WrapperFactoryProvider ,
3031 createJayveeServices ,
32+ initializeWorkspace ,
3133 internalValueToString ,
3234} from '@jvalue/jayvee-language-server' ;
3335import chalk from 'chalk' ;
36+ import { type WorkspaceFolder } from 'langium' ;
3437import { NodeFileSystem } from 'langium/node' ;
3538
3639import { LoggerFactory } from './logging' ;
@@ -80,7 +83,7 @@ export interface JayveeInterpreter {
8083 * Parses a model without executing it.
8184 * Also sets up the environment so that the model can be properly executed.
8285 *
83- * @param extractAstNodeFn method that extracts the AST node; should also initialize the workspace correctly.
86+ * @param extractAstNodeFn method that extracts the AST node
8487 * @returns the parsed Jayvee model, or undefined on failure.
8588 */
8689 parseModel (
@@ -94,6 +97,8 @@ export interface JayveeInterpreter {
9497export class DefaultJayveeInterpreter implements JayveeInterpreter {
9598 private readonly services : JayveeServices ;
9699 private readonly loggerFactory : LoggerFactory ;
100+ private readonly workspaces : WorkspaceFolder [ ] = [ ] ;
101+ private isWorkspaceInitialized = false ;
97102
98103 constructor ( private readonly options : InterpreterOptions ) {
99104 this . services = createJayveeServices ( NodeFileSystem ) . Jayvee ;
@@ -102,7 +107,18 @@ export class DefaultJayveeInterpreter implements JayveeInterpreter {
102107 this . loggerFactory = new LoggerFactory ( options . debug ) ;
103108 }
104109
110+ addWorkspace ( uri : string ) : DefaultJayveeInterpreter {
111+ this . isWorkspaceInitialized = false ;
112+ this . workspaces . push ( {
113+ name : 'projectRoot' ,
114+ uri : path . resolve ( uri ) ,
115+ } ) ;
116+ return this ;
117+ }
118+
105119 async interpretModel ( model : JayveeModel ) : Promise < ExitCode > {
120+ await this . prepareInterpretation ( ) ;
121+
106122 const interpretationExitCode = await this . interpretJayveeModel (
107123 model ,
108124 new StdExecExtension ( ) ,
@@ -112,6 +128,8 @@ export class DefaultJayveeInterpreter implements JayveeInterpreter {
112128 }
113129
114130 async interpretFile ( filePath : string ) : Promise < ExitCode > {
131+ await this . prepareInterpretation ( ) ;
132+
115133 const extractAstNodeFn = async (
116134 services : JayveeServices ,
117135 loggerFactory : LoggerFactory ,
@@ -131,6 +149,8 @@ export class DefaultJayveeInterpreter implements JayveeInterpreter {
131149 }
132150
133151 async interpretString ( modelString : string ) : Promise < ExitCode > {
152+ await this . prepareInterpretation ( ) ;
153+
134154 const extractAstNodeFn = async (
135155 services : JayveeServices ,
136156 loggerFactory : LoggerFactory ,
@@ -155,6 +175,8 @@ export class DefaultJayveeInterpreter implements JayveeInterpreter {
155175 loggerFactory : LoggerFactory ,
156176 ) => Promise < JayveeModel > ,
157177 ) : Promise < JayveeModel | undefined > {
178+ await this . prepareInterpretation ( ) ;
179+
158180 try {
159181 const model = await extractAstNodeFn ( this . services , this . loggerFactory ) ;
160182 return model ;
@@ -274,6 +296,13 @@ export class DefaultJayveeInterpreter implements JayveeInterpreter {
274296 logExecutionDuration ( startTime , executionContext . logger ) ;
275297 return ExitCode . SUCCESS ;
276298 }
299+
300+ private async prepareInterpretation ( ) : Promise < void > {
301+ if ( ! this . isWorkspaceInitialized ) {
302+ await initializeWorkspace ( this . services , this . workspaces ) ;
303+ this . isWorkspaceInitialized = true ;
304+ }
305+ }
277306}
278307
279308export function logPipelineOverview (
0 commit comments