Search in sources :

Example 1 with EmitterExtension

use of cz.habarta.typescript.generator.emitter.EmitterExtension in project typescript-generator by vojtechhabarta.

the class Settings method loadExtensions.

public void loadExtensions(ClassLoader classLoader, List<String> extensions, List<Settings.ConfiguredExtension> extensionsWithConfiguration) {
    this.extensions = new ArrayList<>();
    if (extensions != null) {
        this.extensions.addAll(loadInstances(classLoader, extensions, EmitterExtension.class));
    }
    if (extensionsWithConfiguration != null) {
        for (ConfiguredExtension configuredExtension : extensionsWithConfiguration) {
            final EmitterExtension emitterExtension = loadInstance(classLoader, configuredExtension.className, EmitterExtension.class);
            if (emitterExtension instanceof Extension) {
                final Extension extension = (Extension) emitterExtension;
                extension.setConfiguration(Utils.mapFromNullable(configuredExtension.configuration));
            }
            this.extensions.add(emitterExtension);
        }
    }
}
Also used : EmitterExtension(cz.habarta.typescript.generator.emitter.EmitterExtension) EmitterExtension(cz.habarta.typescript.generator.emitter.EmitterExtension)

Example 2 with EmitterExtension

use of cz.habarta.typescript.generator.emitter.EmitterExtension in project typescript-generator by vojtechhabarta.

the class Settings method validate.

public void validate() {
    if (outputKind == null) {
        throw new RuntimeException("Required 'outputKind' parameter is not configured. " + seeLink());
    }
    if (outputKind == TypeScriptOutputKind.ambientModule && outputFileType == TypeScriptFileType.implementationFile) {
        throw new RuntimeException("Ambient modules are not supported in implementation files. " + seeLink());
    }
    if (outputKind == TypeScriptOutputKind.ambientModule && module == null) {
        throw new RuntimeException("'module' parameter must be specified for ambient module. " + seeLink());
    }
    if (outputKind != TypeScriptOutputKind.ambientModule && module != null) {
        throw new RuntimeException("'module' parameter is only applicable to ambient modules. " + seeLink());
    }
    if (outputKind != TypeScriptOutputKind.module && umdNamespace != null) {
        throw new RuntimeException("'umdNamespace' parameter is only applicable to modules. " + seeLink());
    }
    if (outputFileType == TypeScriptFileType.implementationFile && umdNamespace != null) {
        throw new RuntimeException("'umdNamespace' parameter is not applicable to implementation files. " + seeLink());
    }
    if (umdNamespace != null && !Emitter.isValidIdentifierName(umdNamespace)) {
        throw new RuntimeException("Value of 'umdNamespace' parameter is not valid identifier: " + umdNamespace + ". " + seeLink());
    }
    if (jsonLibrary == null) {
        throw new RuntimeException("Required 'jsonLibrary' parameter is not configured.");
    }
    for (EmitterExtension extension : extensions) {
        final String extensionName = extension.getClass().getSimpleName();
        final DeprecationText deprecation = extension.getClass().getAnnotation(DeprecationText.class);
        if (deprecation != null) {
            System.out.println(String.format("Warning: Extension '%s' is deprecated: %s", extensionName, deprecation.value()));
        }
        final EmitterExtensionFeatures features = extension.getFeatures();
        if (features.generatesRuntimeCode && outputFileType != TypeScriptFileType.implementationFile) {
            throw new RuntimeException(String.format("Extension '%s' generates runtime code but 'outputFileType' parameter is not set to 'implementationFile'.", extensionName));
        }
        if (features.generatesModuleCode && outputKind != TypeScriptOutputKind.module) {
            throw new RuntimeException(String.format("Extension '%s' generates code as module but 'outputKind' parameter is not set to 'module'.", extensionName));
        }
        if (!features.worksWithPackagesMappedToNamespaces && mapPackagesToNamespaces) {
            throw new RuntimeException(String.format("Extension '%s' doesn't work with 'mapPackagesToNamespaces' parameter.", extensionName));
        }
        if (features.generatesJaxrsApplicationClient) {
            reportConfigurationChange(extensionName, "generateJaxrsApplicationClient", "true");
            generateJaxrsApplicationClient = true;
        }
        if (features.restResponseType != null) {
            reportConfigurationChange(extensionName, "restResponseType", features.restResponseType);
            restResponseType = features.restResponseType;
        }
        if (features.restOptionsType != null) {
            reportConfigurationChange(extensionName, "restOptionsType", features.restOptionsType);
            setRestOptionsType(features.restOptionsType);
        }
        if (features.npmPackageDependencies != null) {
            npmPackageDependencies.putAll(features.npmPackageDependencies);
        }
        if (features.overridesStringEnums) {
            defaultStringEnumsOverriddenByExtension = true;
        }
    }
    if (nonConstEnums && outputFileType != TypeScriptFileType.implementationFile) {
        throw new RuntimeException("Non-const enums can only be used in implementation files but 'outputFileType' parameter is not set to 'implementationFile'.");
    }
    if (mapClasses == ClassMapping.asClasses && outputFileType != TypeScriptFileType.implementationFile) {
        throw new RuntimeException("'mapClasses' parameter is set to 'asClasses' which generates runtime code but 'outputFileType' parameter is not set to 'implementationFile'.");
    }
    if (mapClassesAsClassesPatterns != null && mapClasses != ClassMapping.asClasses) {
        throw new RuntimeException("'mapClassesAsClassesPatterns' parameter can only be used when 'mapClasses' parameter is set to 'asClasses'.");
    }
    if (generateJaxrsApplicationClient && outputFileType != TypeScriptFileType.implementationFile) {
        throw new RuntimeException("'generateJaxrsApplicationClient' can only be used when generating implementation file ('outputFileType' parameter is 'implementationFile').");
    }
    final boolean generateJaxrs = generateJaxrsApplicationClient || generateJaxrsApplicationInterface;
    if (jaxrsNamespacing != null && !generateJaxrs) {
        throw new RuntimeException("'jaxrsNamespacing' parameter can only be used when generating JAX-RS client or interface.");
    }
    if (jaxrsNamespacingAnnotation != null && jaxrsNamespacing != JaxrsNamespacing.byAnnotation) {
        throw new RuntimeException("'jaxrsNamespacingAnnotation' parameter can only be used when 'jaxrsNamespacing' parameter is set to 'byAnnotation'.");
    }
    if (jaxrsNamespacingAnnotation == null && jaxrsNamespacing == JaxrsNamespacing.byAnnotation) {
        throw new RuntimeException("'jaxrsNamespacingAnnotation' must be specified when 'jaxrsNamespacing' parameter is set to 'byAnnotation'.");
    }
    if (restResponseType != null && !generateJaxrs) {
        throw new RuntimeException("'restResponseType' parameter can only be used when generating JAX-RS client or interface.");
    }
    if (restOptionsType != null && !generateJaxrs) {
        throw new RuntimeException("'restOptionsType' parameter can only be used when generating JAX-RS client or interface.");
    }
    if (generateNpmPackageJson && outputKind != TypeScriptOutputKind.module) {
        throw new RuntimeException("'generateNpmPackageJson' can only be used when generating proper module ('outputKind' parameter is 'module').");
    }
    if (generateNpmPackageJson) {
        if (npmName == null || npmVersion == null) {
            throw new RuntimeException("'npmName' and 'npmVersion' must be specified when generating NPM 'package.json'.");
        }
    }
    if (!generateNpmPackageJson) {
        if (npmName != null || npmVersion != null) {
            throw new RuntimeException("'npmName' and 'npmVersion' is only applicable when generating NPM 'package.json'.");
        }
    }
    if (declarePropertiesAsOptional) {
        System.out.println("Warning: Parameter 'declarePropertiesAsOptional' is deprecated. Use 'optionalProperties' parameter.");
        if (optionalProperties == null) {
            optionalProperties = OptionalProperties.all;
        }
    }
    if (disableJackson2ModuleDiscovery) {
        System.out.println("Warning: Parameter 'disableJackson2ModuleDiscovery' was removed. See 'jackson2ModuleDiscovery' and 'jackson2Modules' parameters.");
    }
}
Also used : EmitterExtension(cz.habarta.typescript.generator.emitter.EmitterExtension) EmitterExtensionFeatures(cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures)

Aggregations

EmitterExtension (cz.habarta.typescript.generator.emitter.EmitterExtension)2 EmitterExtensionFeatures (cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures)1