Search in sources :

Example 1 with ProvidenceTools

use of net.morimekta.providence.tools.common.ProvidenceTools in project providence by morimekta.

the class FormatUtils method collectConfigIncludes.

public static void collectConfigIncludes(File rc, Map<String, File> includes) throws IOException {
    if (!rc.exists()) {
        return;
    }
    rc = rc.getCanonicalFile();
    if (!rc.isFile()) {
        throw new ProvidenceConfigException("Rc file is not a file " + rc.getPath());
    }
    try {
        SimpleTypeRegistry registry = new SimpleTypeRegistry();
        registry.registerRecursively(ProvidenceTools.kDescriptor);
        ProvidenceConfig loader = new ProvidenceConfig(registry);
        ProvidenceTools config = loader.getConfig(rc);
        if (config.hasIncludes()) {
            File basePath = rc.getParentFile();
            if (config.hasIncludesBasePath()) {
                String base = config.getIncludesBasePath();
                if (base.charAt(0) == '~') {
                    base = System.getenv("HOME") + base.substring(1);
                }
                basePath = new File(base);
                if (!basePath.exists() || !basePath.isDirectory()) {
                    throw new ProvidenceConfigException("Includes Base path in " + rc.getPath() + " is not a directory: " + basePath);
                }
            }
            for (String path : config.getIncludes()) {
                File include = new File(basePath, path);
                collectIncludes(include, includes);
            }
        }
    } catch (SerializerException e) {
        System.err.println("Config error: " + e.getMessage());
        System.err.println(e.asString());
        System.err.println();
        throw new ArgumentException(e, "Exception when parsing " + rc.getCanonicalFile());
    }
}
Also used : SimpleTypeRegistry(net.morimekta.providence.util.SimpleTypeRegistry) ProvidenceTools(net.morimekta.providence.tools.common.ProvidenceTools) ArgumentException(net.morimekta.console.args.ArgumentException) ProvidenceConfig(net.morimekta.providence.config.ProvidenceConfig) File(java.io.File) SerializerException(net.morimekta.providence.serializer.SerializerException) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException)

Example 2 with ProvidenceTools

use of net.morimekta.providence.tools.common.ProvidenceTools in project providence by morimekta.

the class GeneratorOptions method currentJarDirectory.

public File currentJarDirectory() {
    try {
        ProvidenceTools config = getConfig();
        List<File> paths = new ArrayList<>();
        URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
        if ("file".equals(url.getProtocol())) {
            String path = url.getPath();
            if (path.endsWith(".jar")) {
                return new File(path).getParentFile();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ProvidenceTools(net.morimekta.providence.tools.common.ProvidenceTools) ArrayList(java.util.ArrayList) File(java.io.File) URL(java.net.URL) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) GeneratorException(net.morimekta.providence.generator.GeneratorException) IOException(java.io.IOException) ArgumentException(net.morimekta.console.args.ArgumentException) UncheckedProvidenceConfigException(net.morimekta.providence.config.UncheckedProvidenceConfigException)

Example 3 with ProvidenceTools

use of net.morimekta.providence.tools.common.ProvidenceTools in project providence by morimekta.

the class GeneratorOptions method getFactories.

public Map<String, GeneratorFactory> getFactories() {
    try {
        FactoryLoader<GeneratorFactory> loader = new FactoryLoader<>(GeneratorFactory.MANIFEST_PROPERTY);
        Map<String, GeneratorFactory> factories = new TreeMap<>();
        factories.put("json", new JsonGeneratorFactory());
        File currentDir = currentJarDirectory();
        if (currentDir != null) {
            File generators = new File(currentDir, "generator");
            if (generators.isDirectory()) {
                addToMap(factories, loader.getFactories(generators));
            }
        }
        {
            ProvidenceTools config = getConfig();
            if (config.hasGeneratorPaths()) {
                for (String path : config.getGeneratorPaths()) {
                    addToMap(factories, loader.getFactories(new File(path)));
                }
            }
        }
        for (File extra : extraGenerators) {
            GeneratorFactory factory = loader.getFactory(extra);
            factories.put(factory.generatorName(), factory);
        }
        return factories;
    } catch (ProvidenceConfigException e) {
        throw new UncheckedProvidenceConfigException(e);
    }
}
Also used : GeneratorFactory(net.morimekta.providence.generator.GeneratorFactory) JsonGeneratorFactory(net.morimekta.providence.generator.format.json.JsonGeneratorFactory) FactoryLoader(net.morimekta.providence.generator.util.FactoryLoader) ProvidenceTools(net.morimekta.providence.tools.common.ProvidenceTools) JsonGeneratorFactory(net.morimekta.providence.generator.format.json.JsonGeneratorFactory) TreeMap(java.util.TreeMap) UncheckedProvidenceConfigException(net.morimekta.providence.config.UncheckedProvidenceConfigException) File(java.io.File) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) UncheckedProvidenceConfigException(net.morimekta.providence.config.UncheckedProvidenceConfigException)

Aggregations

File (java.io.File)3 ProvidenceConfigException (net.morimekta.providence.config.ProvidenceConfigException)3 ProvidenceTools (net.morimekta.providence.tools.common.ProvidenceTools)3 ArgumentException (net.morimekta.console.args.ArgumentException)2 UncheckedProvidenceConfigException (net.morimekta.providence.config.UncheckedProvidenceConfigException)2 IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 ProvidenceConfig (net.morimekta.providence.config.ProvidenceConfig)1 GeneratorException (net.morimekta.providence.generator.GeneratorException)1 GeneratorFactory (net.morimekta.providence.generator.GeneratorFactory)1 JsonGeneratorFactory (net.morimekta.providence.generator.format.json.JsonGeneratorFactory)1 FactoryLoader (net.morimekta.providence.generator.util.FactoryLoader)1 SerializerException (net.morimekta.providence.serializer.SerializerException)1 SimpleTypeRegistry (net.morimekta.providence.util.SimpleTypeRegistry)1