codbex / codbex-kronos

SAP HANA XS Classic and ABAP Compatibility Platform
https://codbex.com
Eclipse Public License 2.0
5 stars 3 forks source link

[IDE] Extract Project Info #442

Open ThuF opened 1 year ago

ThuF commented 1 year ago

Overview

Extract project info with names and number of artefacts:

_Note: The complete list of artefacts, can be found here._

The info should be extracted, based on the published artefacts, or the artefacts in the workspace. Ideally it should be possible to determine how much of the "desired" artefacts are actually "published", so that we can provide migration status/percent.

Solution

The following sample code can be used as a starting point:

import { registry } from "@dirigible/platform";
import { response } from "@dirigible/http";

class XSArtifactsReporter {

    projectName;

    constructor(projectName) {
        this.projectName = projectName;
    }

    getHDBProcedures(schemaName) {
        const files = registry.find(this.projectName, '*.hdbprocedure');
        const procedures = [];
        for (let filePath of files) {
            const text = registry.getText(filePath);
            const token = `"${schemaName}"`
            const startIndexOf = text.indexOf(token)
            const endIndexOf = text.indexOf('"', startIndexOf + token.length + '."'.length) + 1;
            const name = text.substring(startIndexOf, endIndexOf).replace("\\", "");
            procedures.push(name);
        }
        return procedures.sort();
    };

    getHDBTableFunctions() {
        const files = registry.find(this.projectName, '*.hdbtablefunction');
        const tableFunctions = [];
        for (let filePath of files) {
            const text = registry.getText(filePath);
            const token = `"`
            const startIndexOf = text.indexOf(token)
            const endIndexOf = text.indexOf('"', startIndexOf + token.length + '."'.length) + 1;
            const name = text.substring(startIndexOf, endIndexOf).replace("\\", "");
            tableFunctions.push(name);
        }
        return tableFunctions.sort();
    };

    getHDBScalarFunctions() {
        const files = registry.find(this.projectName, '*.hdbscalarfunction');
        const scalarFunctions = [];
        for (let filePath of files) {
            const text = registry.getText(filePath);
            const token = `"`
            const startIndexOf = text.indexOf(token)
            const endIndexOf = text.indexOf('"', startIndexOf + token.length + '."'.length) + 1;
            const name = text.substring(startIndexOf, endIndexOf).replace("\\", "");
            scalarFunctions.push(name);
        }
        return scalarFunctions.sort();
    };

    getHDBSynonyms() {
        const files = registry.find(this.projectName, '*.hdbsynonym').filter(e => !e.endsWith('hdi-synonyms.hdbsynonym'));
        const synonyms = [];
        for (let filePath of files) {
            const text = registry.getText(filePath);
            const name = Object.getOwnPropertyNames(JSON.parse(text))[0];
            synonyms.push(name);
        }
        return synonyms.sort();
    };

    getHDBSchemas() {
        const files = registry.find(this.projectName, '*.hdbschema');
        const schemas = [];
        for (let filePath of files) {
            const text = registry.getText(filePath);
            const token = `"`
            const startIndexOf = text.indexOf(token)
            const endIndexOf = text.indexOf('"', startIndexOf + 1) + 1;
            const name = text.substring(startIndexOf, endIndexOf);
            schemas.push(name);
        }
        return schemas.sort();
    };

    getHdbddEntities() {
        const files = registry.find(this.projectName, '*.hdbdd');
        const entities = [];
        for (let filePath of files) {
            const text = registry.getText(filePath);
            const regExpContext = /context[\w\s]+{/gm;
            let contextMatch;
            do {
                contextMatch = regExpContext.exec(text);
                if (contextMatch) {
                    const context = contextMatch[0].replaceAll('context', '').replaceAll('{', '').trim();
                    this.__addEntities(filePath, entities, text, context);
                }
            } while (contextMatch);
        }
        return entities.sort();
    };

    getHdbddViews() {
        const files = registry.find(this.projectName, '*.hdbdd');
        const views = [];
        for (let filePath of files) {
            const text = registry.getText(filePath);
            const regExpContext = /context[\w\s]+{/gm;
            let contextMatch;
            let contextFound = false;
            do {
                contextMatch = regExpContext.exec(text);
                if (contextMatch) {
                    contextFound = true;
                    const context = contextMatch[0].replaceAll('context', '').replaceAll('{', '').trim();
                    this.__addViews(filePath, views, text, context);
                }
                if (!contextFound) {
                    this.__addViews(filePath, views, text);
                }
            } while (contextMatch);
        }
        return views.sort();
    };

    getHdbddTableTypes() {
        const files = registry.find(this.projectName, '*.hdbdd');
        const tableTypes = [];
        for (let filePath of files) {
            const text = registry.getText(filePath);
            const regExpContext = /context[\w\s]+{/gm;
            let contextMatch;
            let contextFound = false;
            do {
                contextMatch = regExpContext.exec(text);
                if (contextMatch) {
                    contextFound = true;
                    const context = contextMatch[0].replaceAll('context', '').replaceAll('{', '').trim();
                    this.__addTableTypes(filePath, tableTypes, text, context);
                }
                if (!contextFound) {
                    this.__addTableTypes(filePath, tableTypes, text);
                }
            } while (contextMatch);
        }
        return tableTypes.sort();
    };

    getXSODataEntities() {
        const files = registry.find(this.projectName, '*.xsodata');
        const xsodataEntities = {};
        for (let filePath of files) {
            const text = registry.getText(filePath);
            const regExpEntity = /[\w".:]+ as "[\w]+[;]?"/gm;
            let entityMatch;
            console.log(filePath);
            xsodataEntities[filePath] = []
            do {
                entityMatch = regExpEntity.exec(text);
                if (entityMatch) {
                    const entity = entityMatch[0].trim();
                    const tokens = entity.split(" as ");
                    const xsodataEntityName = tokens[1].replaceAll('"', '');
                    const xsodataEntity = `${filePath.substring(0, filePath.lastIndexOf('.xsodata')).replaceAll('/', '.')}.${xsodataEntityName}Type`;
                    xsodataEntities[filePath].push(xsodataEntity);
                }
            } while (entityMatch);
            xsodataEntities[filePath] = xsodataEntities[filePath].sort();
        }
        return xsodataEntities;
    };

    getXSODataSynonyms() {
        const files = registry.find(this.projectName, '*.xsodata');
        const xsodataSynonyms = {};
        for (let filePath of files) {
            const text = registry.getText(filePath);
            const regExpEntity = /[\w".:]+ as "[\w]+[;]?"/gm;
            let entityMatch;
            console.log(filePath);
            do {
                entityMatch = regExpEntity.exec(text);
                if (entityMatch) {
                    const entity = entityMatch[0].trim().replaceAll('"', '');
                    const tokens = entity.split(" as ");
                    const xsodataEntityTokens = tokens[0].split('.');
                    const schema = xsodataEntityTokens[0];
                    const resource = tokens[0].substring(schema.length + 1);
                    const xsodataEntityName = tokens[1].replaceAll('"', '');
                    xsodataSynonyms[resource] = {
                        target: {
                            object: resource,
                            schema: schema
                        }
                    }
                }
            } while (entityMatch);
        }
        return xsodataSynonyms;
    };

    __addEntities(filePath, entities, text, context = '') {
        const regExpEntity = /entity[\w\s]+{/gm;
        let entityMatch;
        do {
            entityMatch = regExpEntity.exec(text);
            if (entityMatch) {
                const entity = entityMatch[0].replaceAll('entity', '').replaceAll('{', '').trim();
                const name = `${filePath.substring(0, filePath.lastIndexOf('/')).replaceAll('/', '.')}::${context}.${entity}`;
                entities.push(name);
            }
        } while (entityMatch);
    };

    __addViews(filePath, views, text, context = '') {
        const regExpView = /view[\w\s='"/.><*]+{/gm
        let viewMatch;
        do {
            viewMatch = regExpView.exec(text);
            if (viewMatch) {
                let view = viewMatch[0].replaceAll('view', '').replaceAll('{', '').trim();
                view = view.substring(0, view.indexOf(' ')).trim();
                const name = `${filePath.substring(0, filePath.lastIndexOf('/')).replaceAll('/', '.')}::${context}.${view}`;
                views.push(name);
            }
        } while (viewMatch);
    };

    __addTableTypes(filePath, tableTypes, text, context = '') {
        const regExpTableType = /type[\w\s='"/.><*]+[{,:]/gm
        let tableTypeMatch;
        do {
            tableTypeMatch = regExpTableType.exec(text);
            if (tableTypeMatch) {
                let tableType = tableTypeMatch[0].replaceAll('type', '').replaceAll('{', '').replace(':', '').trim();
                const name = `${filePath.substring(0, filePath.lastIndexOf('/')).replaceAll('/', '.')}::${context}.${tableType}`;
                tableTypes.push(name);
            }
        } while (tableTypeMatch);
    };

}

const projectName = 'project1';
const schemaName = 'SCHEMA_NAME_1'

const xsArtifactsReporter = new XSArtifactsReporter(projectName);
const procedures = xsArtifactsReporter.getHDBProcedures(schemaName);
const tableFunctions = xsArtifactsReporter.getHDBTableFunctions(schemaName);
const scalarFunctions = xsArtifactsReporter.getHDBScalarFunctions(schemaName);
const synonyms = xsArtifactsReporter.getHDBSynonyms();
const schemas = xsArtifactsReporter.getHDBSchemas();
const entities = xsArtifactsReporter.getHdbddEntities();
const views = xsArtifactsReporter.getHdbddViews();
const tableTypes = xsArtifactsReporter.getHdbddTableTypes()
const xsodata = xsArtifactsReporter.getXSODataEntities()
const xsodataSynonyms = xsArtifactsReporter.getXSODataSynonyms()

const report = {
    procedures: procedures,
    tableFunctions: tableFunctions,
    scalarFunctions: scalarFunctions,
    synonyms: synonyms,
    schemas: schemas,
    entities: entities,
    views: views,
    tableTypes: tableTypes,
    xsodata: xsodata,
    xsodataSynonyms: xsodataSynonyms
}

response.println(JSON.stringify(report));
response.flush();
response.close();