Search in sources :

Example 1 with CodeRepository

use of org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository in project legend-pure by finos.

the class JavaClassLoaderSourceCodeGenerator method gen.

public static void gen(long start, Path filePath, String name, MutableList<CompiledExtension> extensions) {
    RichIterable<CodeRepository> repositoriesForCompilation = Lists.fixedSize.of(CodeRepository.newPlatformCodeRepository());
    PureCodeStorage codeStorage = new PureCodeStorage(null, new ClassLoaderCodeStorage(repositoriesForCompilation));
    ClassLoaderPureGraphCache graphCache = new ClassLoaderPureGraphCache();
    PureRuntime runtime = new PureRuntimeBuilder(codeStorage).withCache(graphCache).setTransactionalByDefault(false).buildAndTryToInitializeFromCache();
    if (!runtime.isInitialized()) {
        CacheState cacheState = graphCache.getCacheState();
        if (cacheState != null) {
            String lastStackTrace = cacheState.getLastStackTrace();
            if (lastStackTrace != null) {
                System.out.println("Cache initialization failure: " + lastStackTrace);
            }
        }
        System.out.println("Initialization from caches failed - compiling from scratch");
        runtime.reset();
        runtime.loadAndCompileCore();
        runtime.loadAndCompileSystem();
    }
    System.out.format("Finished Pure initialization (%.6fs)%n", (System.nanoTime() - start) / 1_000_000_000.0);
    JavaSourceCodeGenerator javaSourceCodeGenerator = new JavaSourceCodeGenerator(runtime.getProcessorSupport(), runtime.getCodeStorage(), true, filePath, false, extensions, name, JavaPackageAndImportBuilder.externalizablePackage());
    javaSourceCodeGenerator.generateCode();
    javaSourceCodeGenerator.generatePureCoreHelperClasses(new ProcessorContext(runtime.getProcessorSupport(), false));
}
Also used : PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) ClassLoaderPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache) ClassLoaderCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage) CacheState(org.finos.legend.pure.m3.serialization.runtime.cache.CacheState) PureRuntimeBuilder(org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder) PureCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage)

Example 2 with CodeRepository

use of org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository in project legend-pure by finos.

the class PureJarMojo method resolveRepositories.

private RichIterable<CodeRepository> resolveRepositories(String[] repositories, String[] excludedRepositories, String[] extraRepositories) {
    if (extraRepositories != null) {
        RichIterable<CodeRepository> resolvedRepositories = ArrayIterate.collect(extraRepositories, r -> {
            try {
                return GenericCodeRepository.build(new FileInputStream(r));
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        });
        PureRepositoriesExternal.addRepositories(resolvedRepositories);
    }
    RichIterable<CodeRepository> selectedRepos = repositories == null ? PureRepositoriesExternal.repositories() : ArrayIterate.collect(repositories, PureRepositoriesExternal::getRepository);
    Set<String> excludedRepositoriesSet = Sets.mutable.of(excludedRepositories == null ? new String[0] : excludedRepositories);
    return selectedRepos.select(r -> !excludedRepositoriesSet.contains(r.getName()));
}
Also used : CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) GenericCodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.GenericCodeRepository) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Example 3 with CodeRepository

use of org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository in project legend-pure by finos.

the class Test_PureTestSuite method getClassLoaderExecutionSupport.

public static CompiledExecutionSupport getClassLoaderExecutionSupport() {
    MutableList<CodeRepository> codeRepos = Lists.mutable.of(CodeRepository.newPlatformCodeRepository()).withAll(CodeRepositoryProviderHelper.findCodeRepositories());
    ClassLoader classLoader = Test_PureTestSuite.class.getClassLoader();
    return new CompiledExecutionSupport(new JavaCompilerState(null, classLoader), new CompiledProcessorSupport(classLoader, MetadataLazy.fromClassLoader(classLoader), Sets.mutable.empty()), null, new PureCodeStorage(null, new ClassLoaderCodeStorage(classLoader, codeRepos)), null, null, new ConsoleCompiled(), new FunctionCache(), new ClassCache(classLoader), null, Sets.mutable.empty());
}
Also used : CompiledProcessorSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledProcessorSupport) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) JavaCompilerState(org.finos.legend.pure.runtime.java.compiled.compiler.JavaCompilerState) CompiledExecutionSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport) ClassLoaderCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage) ClassCache(org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache) ConsoleCompiled(org.finos.legend.pure.runtime.java.compiled.execution.ConsoleCompiled) PureCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage) FunctionCache(org.finos.legend.pure.runtime.java.compiled.metadata.FunctionCache)

Example 4 with CodeRepository

use of org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository in project legend-pure by finos.

the class RepositoryPackageValidator method run.

@Override
public void run(CoreInstance instance, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
    PackageableElement packageableElement = PackageableElementCoreInstanceWrapper.toPackageableElement(instance);
    SourceInformation sourceInfo = packageableElement.getSourceInformation();
    if (sourceInfo != null) {
        checkValidPackage(packageableElement._package(), sourceInfo);
        String sourceId = sourceInfo.getSourceId();
        String repoName = PureCodeStorage.getSourceRepoName(sourceId);
        if (repoName != null) {
            CodeRepository repo = ((ValidatorState) state).getCodeStorage().getRepository(repoName);
            if (repo != null) {
                if (!(packageableElement instanceof Package) && !(packageableElement instanceof ImportGroup)) {
                    Package pkg = packageableElement._package();
                    if (pkg != null) {
                        String pkgName = org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement.getUserPathForPackageableElement(pkg);
                        if (!repo.isPackageAllowed(pkgName)) {
                            throw new PureCompilationException(sourceInfo, "Package " + pkgName + " is not allowed in " + repoName + "; only packages matching " + repo.getAllowedPackagesPattern().toString() + " are allowed");
                        }
                    }
                }
            }
        }
    }
}
Also used : PackageableElement(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) Package(org.finos.legend.pure.m3.coreinstance.Package) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) ImportGroup(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportGroup) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 5 with CodeRepository

use of org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository in project legend-pure by finos.

the class Visibility method isVisibleInRepository.

/**
 * Return whether the given instance is visible in the
 * given repository.  This is true if the repository
 * the instance is defined in is visible to the given
 * repository.  It is also true if the given repository
 * is null.
 *
 * @param instance         Pure instance
 * @param repository       repository
 * @param processorSupport processor support
 * @return whether instance is visible in repository
 */
private static boolean isVisibleInRepository(CoreInstance instance, CodeRepository repository, RichIterable<CodeRepository> codeRepositories, ProcessorSupport processorSupport) {
    if (codeRepositories == null || repository == null) {
        return true;
    }
    // Packages must be handled specially since they are not defined in a source
    if (processorSupport.instance_instanceOf(instance, M3Paths.Package)) {
        String packagePath = PackageableElement.getUserPathForPackageableElement(instance, "::");
        if (M3Paths.Root.equals(packagePath)) {
            return true;
        }
        for (CodeRepository repo : PureCodeStorage.getVisibleRepositories(codeRepositories, repository)) {
            if (repo.isPackageAllowed(packagePath)) {
                return true;
            }
        }
        return false;
    }
    SourceInformation sourceInfo = instance.getSourceInformation();
    if (sourceInfo == null) {
        throw new RuntimeException("Cannot test visibility for an instance with no source information: " + instance);
    }
    String instanceRepositoryName = PureCodeStorage.getSourceRepoName(sourceInfo.getSourceId());
    if (instanceRepositoryName == null) {
        return false;
    }
    CodeRepository instanceRepository = codeRepositories.select(r -> r.getName().equals(instanceRepositoryName)).getFirst();
    return (instanceRepository != null) && repository.isVisible(instanceRepository);
}
Also used : Context(org.finos.legend.pure.m3.compiler.Context) PackageableElement(org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement) RichIterable(org.eclipse.collections.api.RichIterable) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) PureCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage) M3Properties(org.finos.legend.pure.m3.navigation.M3Properties) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ElementWithStereotypes(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.ElementWithStereotypes) M3Paths(org.finos.legend.pure.m3.navigation.M3Paths) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation)

Aggregations

CodeRepository (org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository)19 PureCodeStorage (org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage)6 GenericCodeRepository (org.finos.legend.pure.m3.serialization.filesystem.repository.GenericCodeRepository)6 ClassLoaderCodeStorage (org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage)5 FileNotFoundException (java.io.FileNotFoundException)4 Message (org.finos.legend.pure.m3.serialization.runtime.Message)4 PureRuntimeBuilder (org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder)4 FileInputStream (java.io.FileInputStream)3 RichIterable (org.eclipse.collections.api.RichIterable)3 TestCodeRepositoryWithDependencies (org.finos.legend.pure.m3.serialization.filesystem.TestCodeRepositoryWithDependencies)3 PureRuntime (org.finos.legend.pure.m3.serialization.runtime.PureRuntime)3 File (java.io.File)2 Path (java.nio.file.Path)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 SetIterable (org.eclipse.collections.api.set.SetIterable)2 Sets (org.eclipse.collections.impl.factory.Sets)2 PureRepositoriesExternal (org.finos.legend.pure.configuration.PureRepositoriesExternal)2 AppCodeRepository (org.finos.legend.pure.m3.serialization.filesystem.repository.AppCodeRepository)2 ContractsCodeRepository (org.finos.legend.pure.m3.serialization.filesystem.repository.ContractsCodeRepository)2