Search in sources :

Example 6 with TypeLoader

use of net.morimekta.providence.reflect.TypeLoader in project providence by morimekta.

the class ConvertOptions method getProgramRegistry.

public ProgramRegistry getProgramRegistry() throws IOException {
    Map<String, File> includeMap = FormatUtils.getIncludeMap(getRc(), includes);
    if (type.isEmpty()) {
        throw new ArgumentException("Missing input type name");
    }
    Set<File> rootSet = new TreeSet<File>();
    for (File file : includeMap.values()) {
        rootSet.add(file.getParentFile());
    }
    String programName = type.substring(0, type.lastIndexOf("."));
    programName = programName.replaceAll("[-.]", "_");
    TypeLoader loader = new TypeLoader(rootSet);
    try {
        if (!includeMap.containsKey(programName)) {
            throw new ArgumentException("No program " + programName + " found in include path.\n" + "Found: " + Strings.join(", ", new TreeSet<Object>(includeMap.keySet())));
        }
        loader.load(includeMap.get(programName));
    } catch (IOException e) {
        throw new ArgumentException(e.getLocalizedMessage());
    }
    return loader.getProgramRegistry();
}
Also used : TreeSet(java.util.TreeSet) TypeLoader(net.morimekta.providence.reflect.TypeLoader) ArgumentException(net.morimekta.console.args.ArgumentException) IOException(java.io.IOException) File(java.io.File)

Example 7 with TypeLoader

use of net.morimekta.providence.reflect.TypeLoader in project providence by morimekta.

the class GeneratorMain method run.

public void run(String... args) {
    try {
        ArgumentParser cli = options.getArgumentParser("pvdgen", "Providence generator");
        cli.parse(args);
        if (options.isHelp()) {
            System.out.println("Providence generator - " + Utils.getVersionString());
            System.out.println("Usage: pvdgen [-I dir] [-o dir] -g generator[:opt[,opt]*] file...");
            System.out.println();
            if (options.help.factory != null) {
                System.out.format("%s : %s%n", options.help.factory.generatorName(), options.help.factory.generatorDescription());
                System.out.println();
                System.out.println("Available options");
                System.out.println();
                options.help.factory.printGeneratorOptionsHelp(System.out);
            } else {
                System.out.println("Example code to run:");
                System.out.println("$ pvdgen -I thrift/ --out target/ --gen java:android thrift/the-one.thrift");
                System.out.println();
                cli.printUsage(System.out);
                System.out.println();
                System.out.println("Available generators:");
                Map<String, GeneratorFactory> factories = options.getFactories();
                for (GeneratorFactory lang : factories.values()) {
                    System.out.format(" - %-10s : %s%n", lang.generatorName(), lang.generatorDescription());
                }
            }
            return;
        } else if (options.version) {
            System.out.println("Providence generator - " + Utils.getVersionString());
            return;
        }
        cli.validate();
        ProgramParser parser = options.getParser();
        List<File> includes = options.getIncludes();
        List<File> input = options.getInputFiles();
        TypeLoader loader = new TypeLoader(includes, parser);
        Generator generator = options.getGenerator(loader);
        for (File f : input) {
            ProgramTypeRegistry registry = loader.load(f);
            if (options.skipIfMissingNamespace && registry.getProgram().getNamespaceForLanguage(options.gen.factory.generatorName()) == null) {
                System.out.println("Skipping (no " + options.gen.factory.generatorName() + " namespace) " + f.getCanonicalFile().getAbsolutePath());
            }
            generator.generate(loader.load(f));
        }
        generator.generateGlobal(loader.getProgramRegistry(), input);
        return;
    } catch (ArgumentException e) {
        System.err.println("Usage: pvdgen [-I dir] [-o dir] -g generator[:opt[,opt]*] file...");
        System.err.println(e.getLocalizedMessage());
        System.err.println();
        System.err.println("Run $ pvdgen --help # for available options.");
        if (options.verbose) {
            e.printStackTrace();
        }
    } catch (ParseException e) {
        System.err.println(e.asString());
        if (options.verbose) {
            e.printStackTrace();
        }
    } catch (IllegalArgumentException e) {
        System.err.println(e.getMessage());
        if (options.verbose) {
            e.printStackTrace();
        }
    } catch (GeneratorException e) {
        System.err.println("Generator error: " + e.getMessage());
        if (options.verbose) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        System.err.println("I/O error: " + e.getMessage());
        if (options.verbose) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    exit(1);
}
Also used : ProgramParser(net.morimekta.providence.reflect.parser.ProgramParser) TypeLoader(net.morimekta.providence.reflect.TypeLoader) IOException(java.io.IOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) GeneratorException(net.morimekta.providence.generator.GeneratorException) IOException(java.io.IOException) ParseException(net.morimekta.providence.reflect.parser.ParseException) ArgumentException(net.morimekta.console.args.ArgumentException) GeneratorFactory(net.morimekta.providence.generator.GeneratorFactory) ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) ArgumentException(net.morimekta.console.args.ArgumentException) ParseException(net.morimekta.providence.reflect.parser.ParseException) GeneratorException(net.morimekta.providence.generator.GeneratorException) File(java.io.File) Generator(net.morimekta.providence.generator.Generator)

Aggregations

File (java.io.File)7 TypeLoader (net.morimekta.providence.reflect.TypeLoader)7 IOException (java.io.IOException)4 TreeSet (java.util.TreeSet)3 ArgumentException (net.morimekta.console.args.ArgumentException)3 FileManager (net.morimekta.providence.generator.util.FileManager)3 ProgramParser (net.morimekta.providence.reflect.parser.ProgramParser)3 ThriftProgramParser (net.morimekta.providence.reflect.parser.ThriftProgramParser)3 Generator (net.morimekta.providence.generator.Generator)2 GeneratorException (net.morimekta.providence.generator.GeneratorException)2 GeneratorFactory (net.morimekta.providence.generator.GeneratorFactory)2 GeneratorOptions (net.morimekta.providence.generator.GeneratorOptions)2 Before (org.junit.Before)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1