2323import java .util .Collection ;
2424import java .util .Collections ;
2525import java .util .HashMap ;
26+ import java .util .IdentityHashMap ;
2627import java .util .List ;
2728import java .util .Map ;
2829import java .util .Set ;
2930
3031import groovy .lang .GroovyClassLoader ;
3132import groovy .lang .GroovyCodeSource ;
3233import com .google .common .hash .Hashing ;
34+ import nextflow .script .ast .WorkflowNode ;
35+ import nextflow .script .control .CallSiteCollector ;
3336import nextflow .script .control .Compiler ;
3437import nextflow .script .control .ModuleResolver ;
3538import nextflow .script .control .OpCriteriaVisitor ;
3639import nextflow .script .control .PathCompareVisitor ;
40+ import nextflow .script .control .ProcessNameResolver ;
3741import nextflow .script .control .ResolveIncludeVisitor ;
3842import nextflow .script .control .ScriptResolveVisitor ;
3943import nextflow .script .control .ScriptToGroovyVisitor ;
4246import org .codehaus .groovy .ast .ASTNode ;
4347import org .codehaus .groovy .ast .ClassHelper ;
4448import org .codehaus .groovy .ast .ClassNode ;
49+ import org .codehaus .groovy .ast .MethodNode ;
4550import org .codehaus .groovy .control .CompilationFailedException ;
4651import org .codehaus .groovy .control .CompilationUnit ;
4752import org .codehaus .groovy .control .CompilerConfiguration ;
@@ -162,24 +167,31 @@ private CompileResult compile0(GroovyCodeSource codeSource) throws IOException {
162167 .findFirst ()
163168 .get ();
164169
170+ var modules = collectModules (unit , classes );
171+ var processNames = new ProcessNameResolver (unit .getCallSites ()).resolve (su );
172+ return new CompileResult (main , modules , processNames );
173+ }
174+
175+ private Map <Path ,Class > collectModules (ScriptCompilationUnit unit , List <Class > classes ) {
165176 // match each module script class to the source path
166177 // using the class name
167- var modules = new HashMap <Path ,Class >();
178+ var result = new HashMap <Path ,Class >();
168179 for ( var c : classes ) {
169180 for ( var source : unit .getModules () ) {
170181 if ( source .getName ().equals (c .getSimpleName ()) ) {
171182 var path = Path .of (source .getSource ().getURI ());
172- modules .put (path , c );
183+ result .put (path , c );
173184 break ;
174185 }
175186 }
176187 }
177- return new CompileResult ( main , modules ) ;
188+ return result ;
178189 }
179190
180191 public static record CompileResult (
181192 Class main ,
182- Map <Path ,Class > modules
193+ Map <Path ,Class > modules ,
194+ Set <String > processNames
183195 ) {}
184196
185197 private static class ScriptClassLoader extends GroovyClassLoader {
@@ -207,6 +219,8 @@ private static List<ClassNode> defaultImports() {
207219
208220 private Set <SourceUnit > modules ;
209221
222+ private Map <WorkflowNode , Map <String , MethodNode >> callSites = new IdentityHashMap <>();
223+
210224 ScriptCompilationUnit (CompilerConfiguration configuration , GroovyClassLoader loader ) {
211225 super (configuration , null , loader );
212226 super .addPhaseOperation (source -> analyze (source ), Phases .CONVERSION );
@@ -216,6 +230,10 @@ Set<SourceUnit> getModules() {
216230 return modules ;
217231 }
218232
233+ Map <WorkflowNode , Map <String , MethodNode >> getCallSites () {
234+ return callSites ;
235+ }
236+
219237 @ Override
220238 public void addPhaseOperation (ISourceUnitOperation op , int phase ) {
221239 super .addPhaseOperation ((source ) -> {
@@ -264,6 +282,9 @@ private void analyze(SourceUnit source) {
264282 if ( source .getErrorCollector ().hasErrors () )
265283 return ;
266284
285+ // collect call sites for each workflow in the script
286+ callSites .putAll (new CallSiteCollector ().apply (source ));
287+
267288 // convert to Groovy
268289 new ScriptToGroovyVisitor (source ).visit ();
269290 new PathCompareVisitor (source ).visitClass (cn );
0 commit comments