@@ -55,33 +55,38 @@ export function isAddonPath(directoryPath: string): boolean {
5555 isReallyModule ( directoryPath , name )
5656 ) ;
5757}
58-
59- export async function fillTemplate ( template , vars = { } ) {
60- const handler = new Function ( 'vars' , [
61- 'const tagged = ( ' + Object . keys ( vars ) . join ( ', ' ) + ' ) =>' ,
62- '`' + template + '`' ,
63- 'return tagged(...Object.values(vars))'
64- ] . join ( '\n' ) ) ;
58+ export async function fillTemplate ( template : string , vars : Record < string , string > = { } ) : Promise < string | null > {
6559 try {
66- return handler ( vars ) ;
60+ return template . replace ( / \$ \{ ( [ ^ } ] + ) \} / g, ( _ , key ) => {
61+ if ( key in vars ) {
62+ return vars [ key ] ;
63+ } else {
64+ const allowedKeys = [ 'workspaceFolder' , 'userHome' ] ;
65+ const message = `Invalid path template parameter "${ key } ". Only "${ allowedKeys . join ( '" and "' ) } " are currently supported.` ;
66+ window . showErrorMessage ( message ) ;
67+ throw new ReferenceError ( `Missing template variable: ${ key } ` ) ;
68+ }
69+ } ) ;
6770 } catch ( error ) {
68- if ( error instanceof ReferenceError ) {
69- const missingVariableMatch = error . message . match ( / ( \w + ) i s n o t d e f i n e d / ) ;
70- if ( missingVariableMatch ) {
71- const missingVariable = missingVariableMatch [ 1 ] ;
72- window . showErrorMessage ( `Invalid path template paramater "${ missingVariable } ". Only "workspaceFolder" and "userHome" are currently supported` )
73- }
74- }
75- throw error ;
71+ return null
7672 }
7773}
7874
7975export async function validateAddonPath ( addonPath ) {
8076 addonPath = addonPath . replaceAll ( "\\" , "/" ) ;
77+ const PATH_VAR_LOCAL = { ...global . PATH_VARIABLES } ;
78+ // Step 1, fill specific workspaceFolder tepmplates. e.g. ${workspaceFolder:odoo}
79+ for ( const folder of workspace . workspaceFolders ) {
80+ PATH_VAR_LOCAL [ `workspaceFolder\:${ folder . name } ` ] = folder . uri . fsPath . replaceAll ( "\\" , "/" ) ;
81+ }
82+ // Step 2, For generic workspaceFolder templates. e.g. ${workspaceFolder}
83+ // This is needed to support the case where the user has a workspace with multiple folders and wants to use ${workspaceFolder} as a generic template for all of them.
84+ // Checks for the first match with a valid addon path and returns it.
8185 for ( const folder of workspace . workspaceFolders ) {
82- const PATH_VAR_LOCAL = { ...global . PATH_VARIABLES } ;
8386 PATH_VAR_LOCAL [ "workspaceFolder" ] = folder . uri . fsPath . replaceAll ( "\\" , "/" ) ;
84- let filledPath = path . resolve ( await fillTemplate ( addonPath , PATH_VAR_LOCAL ) ) . replaceAll ( "\\" , "/" ) . trim ( ) ;
87+ let formatted = await fillTemplate ( addonPath , PATH_VAR_LOCAL ) ;
88+ if ( formatted == null ) continue ;
89+ let filledPath = path . resolve ( formatted ) . replaceAll ( "\\" , "/" ) . trim ( ) ;
8590 if ( ! filledPath ) continue ;
8691 do {
8792 if ( isAddonPath ( filledPath ) ) {
@@ -99,10 +104,16 @@ export async function evaluateOdooPath(odooPath) {
99104 }
100105 odooPath = odooPath . replaceAll ( "\\" , "/" ) ;
101106
107+ const PATH_VAR_LOCAL = { ...global . PATH_VARIABLES } ;
108+ for ( const folder of workspace . workspaceFolders ) {
109+ PATH_VAR_LOCAL [ `workspaceFolder\:${ folder . name } ` ] = folder . uri . fsPath . replaceAll ( "\\" , "/" ) ;
110+ }
102111
103112 for ( const folder of workspace . workspaceFolders ) {
104- global . PATH_VARIABLES [ "workspaceFolder" ] = folder . uri . fsPath . replaceAll ( "\\" , "/" ) ;
105- let filledOdooPath = path . resolve ( await fillTemplate ( odooPath , global . PATH_VARIABLES ) ) . replaceAll ( "\\" , "/" ) . trim ( ) ;
113+ PATH_VAR_LOCAL [ "workspaceFolder" ] = folder . uri . fsPath . replaceAll ( "\\" , "/" ) ;
114+ let formatted = await fillTemplate ( odooPath , PATH_VAR_LOCAL ) ;
115+ if ( formatted == null ) continue ;
116+ let filledOdooPath = path . resolve ( formatted ) . replaceAll ( "\\" , "/" ) . trim ( ) ;
106117 do {
107118 const version = await getOdooVersion ( filledOdooPath ) ;
108119 if ( version ) {
0 commit comments