Search in sources :

Example 1 with TSSource

use of io.crnk.gen.typescript.model.TSSource in project crnk-framework by crnk-project.

the class TSMetaDataObjectTransformation method setupParent.

private static void setupParent(TSMetaTransformationContext context, TSInterfaceType interfaceType, MetaDataObject metaDataObject) {
    TSContainerElement parent = null;
    // move links and meta information to the resource itself
    boolean isMeta = TypescriptUtils.isInstance(metaDataObject.getImplementationClass(), "io.crnk.core.resource.meta.MetaInformation");
    boolean isLinks = TypescriptUtils.isInstance(metaDataObject.getImplementationClass(), "io.crnk.core.resource.links.LinksInformation");
    if ((isMeta || isLinks) && metaDataObject.getImplementationClass().getEnclosingClass() != null) {
        MetaElement enclosingMeta = context.getMeta(metaDataObject.getImplementationClass().getEnclosingClass());
        if (enclosingMeta instanceof MetaResource) {
            TSType enclosingType = (TSType) context.transform(enclosingMeta, TSMetaTransformationOptions.EMPTY);
            TSModule module = TypescriptUtils.getNestedTypeContainer(enclosingType, true);
            interfaceType.setName(isLinks ? "Links" : "Meta");
            parent = module;
        }
    }
    if (parent == null) {
        TSSource source = new TSSource();
        source.setName(TypescriptUtils.toFileName(metaDataObject.getName()));
        source.setNpmPackage(context.getNpmPackage(metaDataObject));
        source.setDirectory(context.getDirectory(metaDataObject));
        context.addSource(source);
        parent = source;
    }
    parent.getElements().add(interfaceType);
    interfaceType.setParent(parent);
}
Also used : TSContainerElement(io.crnk.gen.typescript.model.TSContainerElement) TSModule(io.crnk.gen.typescript.model.TSModule) MetaElement(io.crnk.meta.model.MetaElement) MetaResource(io.crnk.meta.model.resource.MetaResource) TSSource(io.crnk.gen.typescript.model.TSSource) TSType(io.crnk.gen.typescript.model.TSType)

Example 2 with TSSource

use of io.crnk.gen.typescript.model.TSSource in project crnk-framework by crnk-project.

the class TSImportProcessor method process.

@Override
public Set<TSSource> process(Set<TSSource> sources) {
    for (TSSource source : sources) {
        transform(source);
        sortImports(source);
    }
    return sources;
}
Also used : TSSource(io.crnk.gen.typescript.model.TSSource)

Example 3 with TSSource

use of io.crnk.gen.typescript.model.TSSource in project crnk-framework by crnk-project.

the class TSMetaEnumTypeTransformation method transform.

@Override
public TSElement transform(MetaElement elementObj, TSMetaTransformationContext context, TSMetaTransformationOptions options) {
    MetaEnumType element = (MetaEnumType) elementObj;
    TSSource source = new TSSource();
    source.setName(TypescriptUtils.toFileName(element.getName()));
    source.setNpmPackage(context.getNpmPackage(element));
    source.setDirectory(context.getDirectory(element));
    Class<?> implementationClass = element.getImplementationClass();
    TSEnumType enumType = new TSEnumType();
    enumType.setExported(true);
    enumType.setParent(source);
    enumType.setName(element.getName());
    for (Object literal : implementationClass.getEnumConstants()) {
        enumType.getLiterals().add(new TSEnumLiteral(literal.toString()));
    }
    source.getElements().add(enumType);
    context.putMapping(element, enumType);
    context.addSource(source);
    return enumType;
}
Also used : TSEnumLiteral(io.crnk.gen.typescript.model.TSEnumLiteral) TSSource(io.crnk.gen.typescript.model.TSSource) MetaEnumType(io.crnk.meta.model.MetaEnumType) TSEnumType(io.crnk.gen.typescript.model.TSEnumType)

Example 4 with TSSource

use of io.crnk.gen.typescript.model.TSSource in project crnk-framework by crnk-project.

the class TSIndexFileProcessor method process.

@Override
public Set<TSSource> process(Set<TSSource> sources) {
    Set<TSSource> newSources = new HashSet<>(sources);
    Map<String, List<TSSource>> directoryIndex = new HashMap<>();
    for (TSSource fileSource : sources) {
        String dir = fileSource.getDirectory();
        if (!directoryIndex.containsKey(dir)) {
            directoryIndex.put(dir, new ArrayList<TSSource>());
        }
        directoryIndex.get(dir).add(fileSource);
    }
    List<String> keys = new ArrayList<>(directoryIndex.keySet());
    Collections.sort(keys);
    for (String key : keys) {
        List<TSSource> sourceFiles = new ArrayList<>(directoryIndex.get(key));
        Collections.sort(sourceFiles, new Comparator<TSSource>() {

            @Override
            public int compare(TSSource o1, TSSource o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        TSSource indexSource = new TSSource();
        indexSource.setName("index");
        indexSource.setNpmPackage(sourceFiles.get(0).getNpmPackage());
        indexSource.setDirectory(key);
        for (TSSource sourceFile : sourceFiles) {
            TSExport exportElement = new TSExport();
            exportElement.setAny(true);
            exportElement.setPath("./" + sourceFile.getName());
            indexSource.getExports().add(exportElement);
        }
        newSources.add(indexSource);
    }
    return newSources;
}
Also used : HashMap(java.util.HashMap) TSExport(io.crnk.gen.typescript.model.TSExport) TSSource(io.crnk.gen.typescript.model.TSSource) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 5 with TSSource

use of io.crnk.gen.typescript.model.TSSource in project crnk-framework by crnk-project.

the class TSGenerator method writeSources.

protected void writeSources() throws IOException {
    for (TSSource fileSource : sources) {
        TSWriter writer = new TSWriter(config.getCodeStyle());
        fileSource.accept(writer);
        File file = getFile(fileSource);
        String source = writer.toString();
        write(file, source);
    }
}
Also used : TSSource(io.crnk.gen.typescript.model.TSSource) TSWriter(io.crnk.gen.typescript.writer.TSWriter) File(java.io.File)

Aggregations

TSSource (io.crnk.gen.typescript.model.TSSource)6 TSModule (io.crnk.gen.typescript.model.TSModule)2 TSContainerElement (io.crnk.gen.typescript.model.TSContainerElement)1 TSEnumLiteral (io.crnk.gen.typescript.model.TSEnumLiteral)1 TSEnumType (io.crnk.gen.typescript.model.TSEnumType)1 TSExport (io.crnk.gen.typescript.model.TSExport)1 TSNamedElement (io.crnk.gen.typescript.model.TSNamedElement)1 TSPrimitiveType (io.crnk.gen.typescript.model.TSPrimitiveType)1 TSType (io.crnk.gen.typescript.model.TSType)1 TSWriter (io.crnk.gen.typescript.writer.TSWriter)1 MetaElement (io.crnk.meta.model.MetaElement)1 MetaEnumType (io.crnk.meta.model.MetaEnumType)1 MetaResource (io.crnk.meta.model.resource.MetaResource)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1