1 package org.simantics.scl.compiler.source.repository;
4 import java.io.IOException;
6 import java.util.ArrayList;
9 import org.simantics.scl.compiler.module.ImportDeclaration;
10 import org.simantics.scl.runtime.SCLContext;
12 import gnu.trove.procedure.TObjectProcedure;
14 public class SourceRepositories {
15 public static final ModuleSourceRepository BUILTIN_SOURCE_REPOSITORY = new BuiltinModuleSourceRepository();
17 public static final ModuleSourceRepository PRELUDE_SOURCE_REPOSITORY = createPreludeSourceEnvironment();
19 private static ModuleSourceRepository createPreludeSourceEnvironment() {
21 File directory = getPreludeDirectory();
22 if(directory != null) {
23 return new FileModuleSourceRepository(directory, SourceRepositories.class.getClassLoader()) {
24 protected ImportDeclaration[] getBuiltinImports() {
25 return ImportDeclaration.ONLY_BUILTINS;
30 // Hope that ClassSourceEnvironment works
31 return new ClassModuleSourceRepository(SCLContext.class, SourceRepositories.class.getClassLoader(), "/scl") {
32 protected ImportDeclaration[] getBuiltinImports() {
33 return ImportDeclaration.ONLY_BUILTINS;
36 } catch(IOException e) {
42 private static File getPreludeDirectory() throws IOException {
43 URL url = SCLContext.class.getResource(".");
46 File file = ModuleBrowsingUtil.toFile(url);
49 File preludeDir = new File(file, "../../../../scl");
50 if(preludeDir.exists())
51 return preludeDir.getCanonicalFile();
52 preludeDir = new File(file, "../../../../../scl");
53 if(preludeDir.exists())
54 return preludeDir.getCanonicalFile();
58 public static List<String> getModuleNames(ModuleSourceRepository repository) {
59 final ArrayList<String> result = new ArrayList<String>();
60 repository.forAllModules(new TObjectProcedure<String>() {
62 public boolean execute(String moduleName) {
63 result.add(moduleName);