Search in sources :

Example 1 with DiscoApiModel

use of com.google.api.codegen.config.DiscoApiModel in project toolkit by googleapis.

the class DiscoGapicGeneratorApp method getGenerators.

/**
 * From config file paths, constructs the DiscoGapicGenerators to run.
 */
@VisibleForTesting
public static List<CodeGenerator<?>> getGenerators(String discoveryDocPath, List<String> configFileNames, String packageConfig2File, String dependencyConfigFile, String languageStr, List<String> enabledArtifacts, ArtifactType artifactType) throws IOException {
    if (!new File(discoveryDocPath).exists()) {
        throw new IOException("File not found: " + discoveryDocPath);
    }
    Reader reader = new InputStreamReader(new FileInputStream(new File(discoveryDocPath)));
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(reader);
    // Read the YAML config and convert it to proto.
    if (configFileNames.size() == 0) {
        throw new IOException(String.format("--%s must be provided", GENERATOR_CONFIG_FILES.name()));
    }
    ConfigProto configProto = loadConfigFromFiles(configFileNames);
    if (configProto == null) {
        throw new IOException("Failed to load config proto.");
    }
    PackageMetadataConfig packageConfig = null;
    if (!Strings.isNullOrEmpty(packageConfig2File)) {
        ApiDefaultsConfig apiDefaultsConfig = ApiDefaultsConfig.load();
        DependenciesConfig dependenciesConfig;
        if (dependencyConfigFile != null) {
            dependenciesConfig = DependenciesConfig.loadFromURL(new File(dependencyConfigFile).toURI().toURL());
        } else {
            dependenciesConfig = DependenciesConfig.load();
        }
        PackagingConfig packagingConfig = PackagingConfig.load(packageConfig2File);
        packageConfig = PackageMetadataConfig.createFromPackaging(apiDefaultsConfig, dependenciesConfig, packagingConfig);
    }
    TargetLanguage language;
    if (!Strings.isNullOrEmpty(languageStr)) {
        language = TargetLanguage.fromString(languageStr.toUpperCase());
    } else {
        throw new IllegalArgumentException("Language not set by --language option.");
    }
    String defaultPackageName = configProto.getLanguageSettingsMap().get(languageStr).getPackageName();
    DiscoApiModel model = new DiscoApiModel(Document.from(new DiscoveryNode(root)), defaultPackageName);
    GapicProductConfig productConfig = GapicProductConfig.create(model, configProto, language);
    ArtifactFlags artifactFlags = new ArtifactFlags(enabledArtifacts, artifactType, false);
    return DiscoGapicGeneratorFactory.create(language, model, productConfig, packageConfig, artifactFlags);
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) DiscoveryNode(com.google.api.codegen.discovery.DiscoveryNode) InputStreamReader(java.io.InputStreamReader) ConfigProto(com.google.api.codegen.ConfigProto) MultiYamlReader(com.google.api.codegen.util.MultiYamlReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiDefaultsConfig(com.google.api.codegen.config.ApiDefaultsConfig) IOException(java.io.IOException) ArtifactFlags(com.google.api.codegen.gapic.ArtifactFlags) FileInputStream(java.io.FileInputStream) DiscoApiModel(com.google.api.codegen.config.DiscoApiModel) PackagingConfig(com.google.api.codegen.config.PackagingConfig) DependenciesConfig(com.google.api.codegen.config.DependenciesConfig) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) File(java.io.File) TargetLanguage(com.google.api.codegen.common.TargetLanguage) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with DiscoApiModel

use of com.google.api.codegen.config.DiscoApiModel in project toolkit by googleapis.

the class DiscoConfigGeneratorApi method generateConfig.

private Map<String, Doc> generateConfig(String outputPath) {
    Document document = setupDocument();
    ViewModel viewModel = new DiscoConfigTransformer().generateConfig(new DiscoApiModel(document, ""), outputPath);
    Doc generatedConfig = new CommonSnippetSetRunner(new CommonRenderingUtil()).generate(viewModel);
    return ImmutableMap.of(outputPath, generatedConfig);
}
Also used : DiscoApiModel(com.google.api.codegen.config.DiscoApiModel) CommonRenderingUtil(com.google.api.codegen.util.CommonRenderingUtil) CommonSnippetSetRunner(com.google.api.codegen.rendering.CommonSnippetSetRunner) Doc(com.google.api.tools.framework.snippet.Doc) ViewModel(com.google.api.codegen.viewmodel.ViewModel) DiscoConfigTransformer(com.google.api.codegen.configgen.transformer.DiscoConfigTransformer) Document(com.google.api.codegen.discovery.Document)

Example 3 with DiscoApiModel

use of com.google.api.codegen.config.DiscoApiModel in project toolkit by googleapis.

the class DiscoGapicGeneratorApi method getProviders.

/**
 * From config file paths, constructs the DiscoGapicProviders to run.
 */
@VisibleForTesting
static List<GapicProvider> getProviders(String discoveryDocPath, List<String> configFileNames, String packageConfigFile, List<String> enabledArtifacts) throws IOException {
    if (!new File(discoveryDocPath).exists()) {
        throw new IOException("File not found: " + discoveryDocPath);
    }
    Reader reader = new InputStreamReader(new FileInputStream(new File(discoveryDocPath)));
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(reader);
    // Read the YAML config and convert it to proto.
    if (configFileNames.size() == 0) {
        throw new IOException(String.format("--%s must be provided", GENERATOR_CONFIG_FILES.name()));
    }
    ConfigProto configProto = loadConfigFromFiles(configFileNames);
    if (configProto == null) {
        throw new IOException("Failed to load config proto.");
    }
    PackageMetadataConfig packageConfig = null;
    if (!Strings.isNullOrEmpty(packageConfigFile)) {
        String contents = new String(Files.readAllBytes(Paths.get(packageConfigFile)), StandardCharsets.UTF_8);
        packageConfig = PackageMetadataConfig.createFromString(contents);
    }
    GeneratorProto generator = configProto.getGenerator();
    String language = configProto.getLanguage();
    String defaultPackageName = configProto.getLanguageSettingsMap().get(language).getPackageName();
    SurfaceNamer surfaceNamer = null;
    if (language.equals(JAVA)) {
        surfaceNamer = new JavaSurfaceNamer(defaultPackageName, defaultPackageName, new JavaNameFormatter());
    }
    if (surfaceNamer == null) {
        throw new UnsupportedOperationException("DiscoGapicGeneratorApi: language \"" + language + "\" not yet supported");
    }
    DiscoApiModel model = new DiscoApiModel(Document.from(new DiscoveryNode(root)), defaultPackageName);
    GapicProductConfig productConfig = GapicProductConfig.create(model, configProto, surfaceNamer);
    String factory = generator.getFactory();
    String id = generator.getId();
    DiscoGapicProviderFactory providerFactory = createProviderFactory(factory);
    GapicGeneratorConfig generatorConfig = GapicGeneratorConfig.newBuilder().id(id).enabledArtifacts(enabledArtifacts).build();
    return providerFactory.create(model, productConfig, generatorConfig, packageConfig);
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) DiscoveryNode(com.google.api.codegen.discovery.DiscoveryNode) InputStreamReader(java.io.InputStreamReader) ConfigYamlReader(com.google.api.codegen.configgen.ConfigYamlReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) FileInputStream(java.io.FileInputStream) JavaNameFormatter(com.google.api.codegen.util.java.JavaNameFormatter) DiscoGapicProviderFactory(com.google.api.codegen.discogapic.DiscoGapicProviderFactory) DiscoApiModel(com.google.api.codegen.config.DiscoApiModel) GapicGeneratorConfig(com.google.api.codegen.gapic.GapicGeneratorConfig) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with DiscoApiModel

use of com.google.api.codegen.config.DiscoApiModel in project toolkit by googleapis.

the class DiscoConfigGeneratorApp method generateConfig.

private Map<String, Doc> generateConfig(String outputPath) {
    Document document = setupDocument(options.get(DiscoGapicGeneratorApp.DISCOVERY_DOC), getDiagCollector());
    ViewModel viewModel = new DiscoConfigTransformer().generateConfig(new DiscoApiModel(document, ""), outputPath);
    Map<String, GeneratedResult<Doc>> generatedConfig = new CommonSnippetSetRunner(new CommonRenderingUtil(), true).generate(viewModel);
    return GeneratedResult.extractBodies(generatedConfig);
}
Also used : DiscoApiModel(com.google.api.codegen.config.DiscoApiModel) CommonRenderingUtil(com.google.api.codegen.util.CommonRenderingUtil) CommonSnippetSetRunner(com.google.api.codegen.rendering.CommonSnippetSetRunner) GeneratedResult(com.google.api.codegen.common.GeneratedResult) ViewModel(com.google.api.codegen.viewmodel.ViewModel) DiscoConfigTransformer(com.google.api.codegen.configgen.transformer.DiscoConfigTransformer) Document(com.google.api.codegen.discovery.Document)

Aggregations

DiscoApiModel (com.google.api.codegen.config.DiscoApiModel)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)2 PackageMetadataConfig (com.google.api.codegen.config.PackageMetadataConfig)2 DiscoConfigTransformer (com.google.api.codegen.configgen.transformer.DiscoConfigTransformer)2 DiscoveryNode (com.google.api.codegen.discovery.DiscoveryNode)2 Document (com.google.api.codegen.discovery.Document)2 CommonSnippetSetRunner (com.google.api.codegen.rendering.CommonSnippetSetRunner)2 CommonRenderingUtil (com.google.api.codegen.util.CommonRenderingUtil)2 ViewModel (com.google.api.codegen.viewmodel.ViewModel)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 ConfigProto (com.google.api.codegen.ConfigProto)1 GeneratedResult (com.google.api.codegen.common.GeneratedResult)1 TargetLanguage (com.google.api.codegen.common.TargetLanguage)1