|
27 | 27 | import com.redhat.ceylon.cmr.api.Logger;
|
28 | 28 | import com.redhat.ceylon.cmr.api.RepositoryManager;
|
29 | 29 | import com.redhat.ceylon.cmr.api.RepositoryManagerBuilder;
|
| 30 | +import com.redhat.ceylon.common.ModuleDescriptorReader.NoSuchModuleException; |
30 | 31 | import com.redhat.ceylon.compiler.typechecker.analyzer.ModuleManager;
|
31 | 32 | import com.redhat.ceylon.compiler.typechecker.context.Context;
|
32 | 33 | import com.redhat.ceylon.compiler.typechecker.context.PhasedUnit;
|
33 | 34 | import com.redhat.ceylon.compiler.typechecker.context.PhasedUnits;
|
34 | 35 | import com.redhat.ceylon.compiler.typechecker.io.VFS;
|
| 36 | +import com.redhat.ceylon.compiler.typechecker.io.VirtualFile; |
35 | 37 | import com.redhat.ceylon.compiler.typechecker.model.Annotation;
|
36 | 38 | import com.redhat.ceylon.compiler.typechecker.model.Module;
|
37 | 39 |
|
@@ -61,21 +63,69 @@ public void debug(String str) {
|
61 | 63 |
|
62 | 64 | private final Module moduleDescriptor;
|
63 | 65 |
|
64 |
| - public ModuleDescriptorReader(String moduleName, File srcDir) { |
| 66 | + public ModuleDescriptorReader(String moduleName, File srcDir) throws NoSuchModuleException { |
65 | 67 | RepositoryManagerBuilder builder = new RepositoryManagerBuilder(new NullLogger(), false);
|
66 | 68 | RepositoryManager repoManager = builder.buildRepository();
|
67 | 69 | VFS vfs = new VFS();
|
68 | 70 | Context context = new Context(repoManager, vfs);
|
69 | 71 | PhasedUnits pus = new PhasedUnits(context);
|
70 |
| - pus.parseUnit(vfs.getFromFile(srcDir)); |
| 72 | + List<String> name = ModuleManager.splitModuleName(moduleName); |
| 73 | + ModuleManager moduleManager = pus.getModuleManager(); |
| 74 | + if(Module.DEFAULT_MODULE_NAME.equals(moduleName)){ |
| 75 | + // visit every folder and skip modules |
| 76 | + boolean exists = findDefaultModuleSource(srcDir); |
| 77 | + if(!exists) |
| 78 | + throw new NoSuchModuleException("No source found for default module"); |
| 79 | + }else{ |
| 80 | + visitModule(vfs, pus, name, srcDir, vfs.getFromFile(srcDir), moduleManager); |
| 81 | + } |
71 | 82 | for (PhasedUnit pu : pus.getPhasedUnits()) {
|
72 | 83 | pu.visitSrcModulePhase();
|
73 | 84 | }
|
74 |
| - ModuleManager moduleManager = pus.getModuleManager(); |
75 |
| - List<String> name = ModuleManager.splitModuleName(moduleName); |
76 | 85 | this.moduleDescriptor = moduleManager.getOrCreateModule(name, null);
|
77 | 86 | }
|
78 | 87 |
|
| 88 | + private void visitModule(VFS vfs, PhasedUnits pus, List<String> name, File srcDir, VirtualFile virtualSourceDirectory, ModuleManager moduleManager) throws NoSuchModuleException { |
| 89 | + for(String part : name){ |
| 90 | + File child = new File(srcDir, part); |
| 91 | + if(child.exists() && child.isDirectory()){ |
| 92 | + moduleManager.push(part); |
| 93 | + srcDir = child; |
| 94 | + }else{ |
| 95 | + throw new NoSuchModuleException("Failed to find module name part "+part+" in "+srcDir); |
| 96 | + } |
| 97 | + } |
| 98 | + File moduleFile = new File(srcDir, ModuleManager.MODULE_FILE); |
| 99 | + if(moduleFile.exists()){ |
| 100 | + moduleManager.visitModuleFile(); |
| 101 | + pus.parseUnit(vfs.getFromFile(moduleFile), virtualSourceDirectory); |
| 102 | + }else{ |
| 103 | + throw new NoSuchModuleException("No module file in "+srcDir); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private boolean findDefaultModuleSource(File sourceFile) { |
| 108 | + if(sourceFile.isDirectory()){ |
| 109 | + File moduleFile = new File(sourceFile, ModuleManager.MODULE_FILE); |
| 110 | + // skip modules entirely |
| 111 | + if(moduleFile.exists()) |
| 112 | + return false; |
| 113 | + // recurse down normal folders |
| 114 | + for(File f : sourceFile.listFiles()){ |
| 115 | + boolean found = findDefaultModuleSource(f); |
| 116 | + if(found) |
| 117 | + return true; |
| 118 | + } |
| 119 | + return false; |
| 120 | + }else{ |
| 121 | + String name = sourceFile.getName().toLowerCase(); |
| 122 | + // did we find a source file? |
| 123 | + return name.endsWith(".ceylon") |
| 124 | + || name.endsWith(".java") |
| 125 | + || name.endsWith(".js"); |
| 126 | + } |
| 127 | + } |
| 128 | + |
79 | 129 | /**
|
80 | 130 | * Gets the module version
|
81 | 131 | * @return The module version, or null if no version could be found
|
|
0 commit comments