@@ -95,7 +95,7 @@ export interface TestDirective extends BaseDirective {
95
95
96
96
export const IncludeMacros = [ "macros/openrecon/neurodocker.yaml" ] as const ;
97
97
98
- export type IncludeMacro = typeof IncludeMacros [ number ] ;
98
+ export type IncludeMacro = ( typeof IncludeMacros ) [ number ] ;
99
99
100
100
export interface IncludeDirective extends BaseDirective {
101
101
include : IncludeMacro ;
@@ -136,4 +136,81 @@ export interface ContainerRecipe {
136
136
files ?: FileInfo [ ] ;
137
137
deploy ?: DeployInfo ;
138
138
tests ?: TestInfo [ ] ;
139
+ }
140
+
141
+ export function migrateLegacyRecipe (
142
+ recipe : ContainerRecipe
143
+ ) : ContainerRecipe {
144
+ const directives : Directive [ ] = [ ] ;
145
+
146
+ // Migrate files to file directives
147
+ if ( recipe . files ?. length ) {
148
+ directives . push (
149
+ ...recipe . files . map (
150
+ ( file ) : FileDirective => ( {
151
+ file,
152
+ } )
153
+ )
154
+ ) ;
155
+ }
156
+
157
+ // Migrate deploy to deploy directive
158
+ if ( recipe . deploy ) {
159
+ directives . push ( {
160
+ deploy : recipe . deploy ,
161
+ } ) ;
162
+ }
163
+
164
+ // Migrate tests to test directives
165
+ if ( recipe . tests ?. length ) {
166
+ directives . push (
167
+ ...recipe . tests . map (
168
+ ( test ) : TestDirective => ( {
169
+ test,
170
+ } )
171
+ )
172
+ ) ;
173
+ }
174
+
175
+ const ret = {
176
+ ...recipe ,
177
+ build : {
178
+ ...recipe . build ,
179
+ directives : [ ...directives , ...recipe . build . directives ] ,
180
+ } ,
181
+ } ;
182
+ delete ret . files ;
183
+ delete ret . deploy ;
184
+ delete ret . tests ;
185
+ return ret ;
186
+ }
187
+
188
+ export async function mergeAdditionalFilesIntoRecipe ( recipe : ContainerRecipe , fetchFile : ( filename : string ) => Promise < string > ) : Promise < ContainerRecipe > {
189
+ // Look for file directives in the recipe. Make sure to handle group directives.
190
+ // For each file directive if they have a filename then download it and replace the filename with the contents.
191
+ const processDirective = async ( directive : Directive ) : Promise < Directive > => {
192
+ if ( 'file' in directive ) {
193
+ const file = directive . file ;
194
+ if ( file . filename ) {
195
+ try {
196
+ file . contents = await fetchFile ( file . filename ) ;
197
+ delete file . filename ; // Remove filename after fetching
198
+ } catch ( error ) {
199
+ console . error ( `Failed to fetch file ${ file . filename } :` , error ) ;
200
+ }
201
+ }
202
+ } else if ( 'group' in directive ) {
203
+ directive . group = await Promise . all ( directive . group . map ( processDirective ) ) ;
204
+ }
205
+ return directive ;
206
+ }
207
+
208
+ const directives = await Promise . all ( recipe . build . directives . map ( processDirective ) ) ;
209
+ return {
210
+ ...recipe ,
211
+ build : {
212
+ ...recipe . build ,
213
+ directives : directives ,
214
+ } ,
215
+ } ;
139
216
}
0 commit comments