Search in sources :

Example 31 with PageComponent

use of com.twinsoft.convertigo.beans.ngx.components.PageComponent in project convertigo by convertigo.

the class NgxBuilder method writeFunctionTempTsFile.

@Override
public void writeFunctionTempTsFile(final IUIComponent uiComponent, String functionMarker) throws EngineException {
    UIComponent uic = (UIComponent) uiComponent;
    try {
        IScriptComponent main = uic.getMainScriptComponent();
        if (main != null) {
            String tempTsFileName = null, tsContent = null;
            File tempTsDir = null;
            UIActionStack sharedAction = null;
            if (main instanceof ApplicationComponent) {
                sharedAction = uic.getSharedAction();
                tempTsDir = sharedAction == null ? appDir : servicesDir;
                tempTsFileName = sharedAction == null ? "app.component.function.temp.ts" : "actionbeans.service.function.temp.ts";
                File appTsFile = sharedAction == null ? new File(appDir, "app.component.ts") : new File(servicesDir, "actionbeans.service.ts");
                synchronized (writtenFiles) {
                    if (writtenFiles.contains(appTsFile)) {
                        File appTsFileTmp = toTmpFile(appTsFile);
                        if (appTsFileTmp.exists()) {
                            appTsFile = appTsFileTmp;
                        }
                    }
                }
                tsContent = FileUtils.readFileToString(appTsFile, "UTF-8");
            }
            if (main instanceof PageComponent) {
                PageComponent page = (PageComponent) main;
                String pageName = page.getName();
                tempTsDir = pageDir(page);
                tempTsFileName = pageName.toLowerCase() + ".function.temp.ts";
                if (page.isEnabled()) {
                    File pageTsFile = new File(tempTsDir, pageName.toLowerCase() + ".ts");
                    synchronized (writtenFiles) {
                        if (writtenFiles.contains(pageTsFile)) {
                            File pageTsFileTmp = toTmpFile(pageTsFile);
                            if (pageTsFileTmp.exists()) {
                                pageTsFile = pageTsFileTmp;
                            }
                        }
                    }
                    tsContent = FileUtils.readFileToString(pageTsFile, "UTF-8");
                } else {
                    tsContent = getPageTsContent(page);
                }
            }
            if (main instanceof UISharedComponent) {
                UISharedComponent comp = (UISharedComponent) main;
                String compName = comp.getName();
                tempTsDir = compDir(comp);
                tempTsFileName = compName.toLowerCase() + ".function.temp.ts";
                boolean isEnabled = true;
                if (isEnabled) {
                    File compTsFile = new File(tempTsDir, compName.toLowerCase() + ".ts");
                    synchronized (writtenFiles) {
                        if (writtenFiles.contains(compTsFile)) {
                            File compTsFileTmp = toTmpFile(compTsFile);
                            if (compTsFileTmp.exists()) {
                                compTsFile = compTsFileTmp;
                            }
                        }
                    }
                    tsContent = FileUtils.readFileToString(compTsFile, "UTF-8");
                } else {
                    tsContent = getCompTsContent(comp);
                }
            }
            // Replace all Begin_c8o_XXX, End_c8o_XXX except for functionMarker
            Pattern pattern = Pattern.compile("/\\*Begin_c8o_(.+)\\*/");
            Matcher matcher = pattern.matcher(tsContent);
            while (matcher.find()) {
                String markerId = matcher.group(1);
                if (!markerId.equals(functionMarker)) {
                    String beginMarker = "/*Begin_c8o_" + markerId + "*/";
                    String endMarker = "/*End_c8o_" + markerId + "*/";
                    tsContent = tsContent.replace(beginMarker, "//---" + markerId + "---");
                    tsContent = tsContent.replace(endMarker, "//---" + markerId + "---");
                }
            }
            // CustomAction : reduce code lines (action's function only)
            if (tempTsDir != null && tempTsFileName != null) {
                if (uiComponent instanceof UICustomAction) {
                    UICustomAction uica = (UICustomAction) uic;
                    tempTsFileName = "CTS" + uica.priority + ".temp.ts";
                    int index = tsContent.indexOf("export class ");
                    if (index != -1) {
                        String classType = uica.getMainClassType();
                        String sImport = "";
                        if (main instanceof ApplicationComponent) {
                            if (uic.getSharedAction() == null) {
                                sImport = "import { " + classType + " } from './app.component';";
                            }
                        } else if (main instanceof PageComponent) {
                            sImport = "import { " + classType + " } from './" + classType.toLowerCase() + "';";
                        } else if (main instanceof UISharedComponent) {
                            sImport = "import { " + classType + " } from './" + classType.toLowerCase() + "';";
                        }
                        int i = tsContent.indexOf("{", index);
                        tsContent = tsContent.substring(0, i + 1) + System.lineSeparator() + uica.getActionCode() + System.lineSeparator() + "}" + System.lineSeparator();
                        tsContent = tsContent.replace("@Component", sImport + System.lineSeparator() + "@Component");
                        tsContent = tsContent.replace("export class " + classType + " ", "export class _Type_ ");
                    }
                }
            }
            // Write file (do not need delay)
            tsContent = LsPattern.matcher(tsContent).replaceAll(System.lineSeparator());
            File tempTsFile = new File(tempTsDir, tempTsFileName);
            FileUtils.write(tempTsFile, tsContent, "UTF-8");
        }
    } catch (Exception e) {
        throw new EngineException("Unable to write function temp ts file", e);
    }
}
Also used : Pattern(java.util.regex.Pattern) IScriptComponent(com.twinsoft.convertigo.beans.ngx.components.IScriptComponent) Matcher(java.util.regex.Matcher) ApplicationComponent(com.twinsoft.convertigo.beans.ngx.components.ApplicationComponent) IApplicationComponent(com.twinsoft.convertigo.beans.core.IApplicationComponent) IUIComponent(com.twinsoft.convertigo.beans.core.IUIComponent) UIComponent(com.twinsoft.convertigo.beans.ngx.components.UIComponent) EngineException(com.twinsoft.convertigo.engine.EngineException) UISharedComponent(com.twinsoft.convertigo.beans.ngx.components.UISharedComponent) IPageComponent(com.twinsoft.convertigo.beans.core.IPageComponent) PageComponent(com.twinsoft.convertigo.beans.ngx.components.PageComponent) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) UIActionStack(com.twinsoft.convertigo.beans.ngx.components.UIActionStack) UICustomAction(com.twinsoft.convertigo.beans.ngx.components.UICustomAction) File(java.io.File)

Example 32 with PageComponent

use of com.twinsoft.convertigo.beans.ngx.components.PageComponent in project convertigo by convertigo.

the class NgxBuilder method writeAppBuildSettings.

private void writeAppBuildSettings(ApplicationComponent app) throws EngineException {
    try {
        if (app != null) {
            Set<String> build_assets = new HashSet<String>();
            Set<String> build_scripts = new HashSet<String>();
            Set<String> build_styles = new HashSet<String>();
            // Menus contributors
            for (Contributor contributor : app.getContributors()) {
                build_assets.addAll(contributor.getBuildAssets());
                build_scripts.addAll(contributor.getBuildScripts());
                build_styles.addAll(contributor.getBuildStyles());
            }
            // Pages contributors
            List<PageComponent> pages = forceEnable ? app.getPageComponentList() : getEnabledPages(app);
            for (PageComponent page : pages) {
                synchronized (page) {
                    List<Contributor> contributors = page.getContributors();
                    for (Contributor contributor : contributors) {
                        build_assets.addAll(contributor.getBuildAssets());
                        build_scripts.addAll(contributor.getBuildScripts());
                        build_styles.addAll(contributor.getBuildStyles());
                    }
                }
            }
            boolean hasSettings = !build_assets.isEmpty() || !build_scripts.isEmpty() || !build_styles.isEmpty();
            if (hasSettings) {
                File tplAngularJson = new File(ionicTplDir, "angular.json");
                if (tplAngularJson.exists()) {
                    String content = FileUtils.readFileToString(tplAngularJson, "UTF-8");
                    JSONObject jsonObject = new JSONObject(content);
                    JSONObject jsonOptions = jsonObject.getJSONObject("projects").getJSONObject("app").getJSONObject("architect").getJSONObject("build").getJSONObject("options");
                    JSONArray jsonArray = null;
                    try {
                        // Assets
                        jsonArray = jsonOptions.getJSONArray("assets");
                        for (String asset : build_assets) {
                            if (jsonArrayContains(jsonArray, asset)) {
                                continue;
                            }
                            try {
                                JSONObject jsonAsset = new JSONObject(asset);
                                jsonArray.put(jsonAsset);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        // Scripts
                        jsonArray = jsonOptions.getJSONArray("scripts");
                        for (String script : build_scripts) {
                            if (jsonArrayContains(jsonArray, script)) {
                                continue;
                            }
                            try {
                                jsonArray.put(script);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        // Styles
                        jsonArray = jsonOptions.getJSONArray("styles");
                        for (String style : build_styles) {
                            if (jsonArrayContains(jsonArray, style)) {
                                continue;
                            }
                            try {
                                jsonArray.put(style);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    setNeedPkgUpdate(true);
                    File angularJson = new File(ionicWorkDir, "angular.json");
                    String aContent = jsonObject.toString(1);
                    writeFile(angularJson, aContent, "UTF-8");
                }
            }
            if (initDone) {
                Engine.logEngine.trace("(MobileBuilder) App angular json file generated");
            }
        }
    } catch (Exception e) {
        throw new EngineException("Unable to write angular json file", e);
    }
}
Also used : JSONArray(org.codehaus.jettison.json.JSONArray) EngineException(com.twinsoft.convertigo.engine.EngineException) Contributor(com.twinsoft.convertigo.beans.ngx.components.Contributor) IPageComponent(com.twinsoft.convertigo.beans.core.IPageComponent) PageComponent(com.twinsoft.convertigo.beans.ngx.components.PageComponent) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) JSONObject(org.codehaus.jettison.json.JSONObject) File(java.io.File) HashSet(java.util.HashSet)

Example 33 with PageComponent

use of com.twinsoft.convertigo.beans.ngx.components.PageComponent in project convertigo by convertigo.

the class NgxBuilder method writeAppServiceTs.

private void writeAppServiceTs(ApplicationComponent app) throws EngineException {
    try {
        if (app != null) {
            Map<String, String> action_ts_imports = new HashMap<>();
            Map<String, String> action_ts_functions = new HashMap<>();
            // App contributors
            for (Contributor contributor : app.getContributors()) {
                contributor.forContainer(app, () -> {
                    action_ts_imports.putAll(contributor.getActionTsImports());
                    action_ts_functions.putAll(contributor.getActionTsFunctions());
                });
            }
            // Shared components
            for (UISharedComponent comp : app.getSharedComponentList()) {
                if (comp.isRegular()) {
                    List<Contributor> contributors = comp.getContributors();
                    for (Contributor contributor : contributors) {
                        contributor.forContainer(app, () -> {
                            action_ts_imports.putAll(contributor.getActionTsImports());
                            action_ts_functions.putAll(contributor.getActionTsFunctions());
                        });
                    }
                }
            }
            // Pages contributors
            List<PageComponent> pages = forceEnable ? app.getPageComponentList() : getEnabledPages(app);
            for (PageComponent page : pages) {
                synchronized (page) {
                    List<Contributor> contributors = page.getContributors();
                    for (Contributor contributor : contributors) {
                        contributor.forContainer(app, () -> {
                            action_ts_imports.putAll(contributor.getActionTsImports());
                            action_ts_functions.putAll(contributor.getActionTsFunctions());
                        });
                    }
                }
            }
            String c8o_ActionTsImports = "";
            for (String comp : action_ts_imports.keySet()) {
                if (!getTplServiceActionTsImports().containsKey(comp)) {
                    if (comp.indexOf(" as ") == -1) {
                        String comPath = action_ts_imports.get(comp).replace("./pages", "../pages");
                        c8o_ActionTsImports += "import { " + comp + " } from '" + comPath + "';" + System.lineSeparator();
                    } else {
                        c8o_ActionTsImports += "import " + comp + " from '" + action_ts_imports.get(comp) + "';" + System.lineSeparator();
                    }
                }
            }
            String c8o_ActionTsFunctions = System.lineSeparator();
            for (String function : action_ts_functions.values()) {
                c8o_ActionTsFunctions += function + System.lineSeparator();
            }
            File appServiceTpl = new File(ionicTplDir, "src/app/services/actionbeans.service.ts");
            String mContent = FileUtils.readFileToString(appServiceTpl, "UTF-8");
            mContent = mContent.replaceAll("/\\*\\=c8o_ActionTsImports\\*/", Matcher.quoteReplacement(c8o_ActionTsImports));
            mContent = mContent.replaceAll("/\\*\\=c8o_ActionTsFunctions\\*/", Matcher.quoteReplacement(c8o_ActionTsFunctions));
            File appServiceTsFile = new File(servicesDir, "actionbeans.service.ts");
            writeFile(appServiceTsFile, mContent, "UTF-8");
            if (initDone) {
                Engine.logEngine.trace("(MobileBuilder) Ionic service ts file generated for 'app'");
            }
        }
    } catch (Exception e) {
        throw new EngineException("Unable to write ionic app service ts file", e);
    }
}
Also used : HashMap(java.util.HashMap) EngineException(com.twinsoft.convertigo.engine.EngineException) Contributor(com.twinsoft.convertigo.beans.ngx.components.Contributor) UISharedComponent(com.twinsoft.convertigo.beans.ngx.components.UISharedComponent) File(java.io.File) IPageComponent(com.twinsoft.convertigo.beans.core.IPageComponent) PageComponent(com.twinsoft.convertigo.beans.ngx.components.PageComponent) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 34 with PageComponent

use of com.twinsoft.convertigo.beans.ngx.components.PageComponent in project convertigo by convertigo.

the class NgxBuilder method writeAppPackageJson.

private void writeAppPackageJson(ApplicationComponent app) throws EngineException {
    try {
        if (app != null) {
            Map<String, String> pkg_dependencies = new HashMap<>();
            // Menus contributors
            for (Contributor contributor : app.getContributors()) {
                pkg_dependencies.putAll(contributor.getPackageDependencies());
            }
            // Pages contributors
            List<PageComponent> pages = forceEnable ? app.getPageComponentList() : getEnabledPages(app);
            for (PageComponent page : pages) {
                synchronized (page) {
                    List<Contributor> contributors = page.getContributors();
                    for (Contributor contributor : contributors) {
                        pkg_dependencies.putAll(contributor.getPackageDependencies());
                    }
                }
            }
            // Shared components contributors
            for (UISharedComponent comp : app.getSharedComponentList()) {
                if (comp.isRegular()) {
                    List<Contributor> contributors = comp.getContributors();
                    for (Contributor contributor : contributors) {
                        pkg_dependencies.putAll(contributor.getPackageDependencies());
                    }
                }
            }
            File appPkgJsonTpl = new File(ionicTplDir, "package.json");
            String mContent = FileUtils.readFileToString(appPkgJsonTpl, "UTF-8");
            mContent = mContent.replaceAll("\\.\\./DisplayObjects", "../../DisplayObjects");
            mContent = mContent.replaceAll("\\{\\{c8o_project\\}\\}", app.getProject().getName());
            JSONObject jsonPackage = new JSONObject(mContent);
            JSONObject jsonDependencies = jsonPackage.getJSONObject("dependencies");
            for (String pkg : pkg_dependencies.keySet()) {
                jsonDependencies.put(pkg, pkg_dependencies.get(pkg));
                if (!existPackage(pkg)) {
                    setNeedPkgUpdate(true);
                }
            }
            boolean addNode = !jsonDependencies.has("@types/node");
            if (addNode) {
                try {
                    String version = new JSONObject(FileUtils.readFileToString(new File(ionicTplDir, "version.json"), "utf-8")).getString("version");
                    addNode = version.matches("7\\.[0-7]\\..*");
                } catch (Exception e) {
                }
                if (addNode) {
                    jsonDependencies.put("@types/node", "12.0.10");
                }
            }
            File appPkgJson = new File(ionicWorkDir, "package.json");
            writeFile(appPkgJson, jsonPackage.toString(2), "UTF-8");
            if (initDone) {
                Engine.logEngine.trace("(MobileBuilder) Ionic package json file generated");
            }
        }
    } catch (Exception e) {
        throw new EngineException("Unable to write ionic package json file", e);
    }
}
Also used : HashMap(java.util.HashMap) EngineException(com.twinsoft.convertigo.engine.EngineException) Contributor(com.twinsoft.convertigo.beans.ngx.components.Contributor) UISharedComponent(com.twinsoft.convertigo.beans.ngx.components.UISharedComponent) IPageComponent(com.twinsoft.convertigo.beans.core.IPageComponent) PageComponent(com.twinsoft.convertigo.beans.ngx.components.PageComponent) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) JSONObject(org.codehaus.jettison.json.JSONObject) File(java.io.File)

Example 35 with PageComponent

use of com.twinsoft.convertigo.beans.ngx.components.PageComponent in project convertigo by convertigo.

the class NgxBuilder method updateSourceFiles.

private void updateSourceFiles() throws EngineException {
    try {
        MobileApplication mobileApplication = project.getMobileApplication();
        if (mobileApplication != null) {
            ApplicationComponent application = (ApplicationComponent) mobileApplication.getApplicationComponent();
            if (application != null) {
                String appTplVersion = application.requiredTplVersion();
                if (compareVersions(tplVersion, appTplVersion) >= 0) {
                    for (UISharedComponent comp : application.getSharedComponentList()) {
                        if (comp.isRegular()) {
                            writeCompSourceFiles(comp);
                        }
                    }
                    for (PageComponent page : getEnabledPages(application)) {
                        writePageSourceFiles(page);
                    }
                    writeAppSourceFiles(application);
                    removeUselessPages(application);
                    Engine.logEngine.trace("(MobileBuilder) Application source files updated for ionic project '" + project.getName() + "'");
                } else {
                    cleanDirectories();
                    throw new EngineException("Template project minimum " + appTplVersion + " is required for this project.\n" + "You can change template by configuring the 'Template project' property of your project's 'Application' object.\n" + "Then, be sure to update the project node modules packages (Application Right Click->Update packages and execute) \n");
                }
            }
        }
    } catch (EngineException e) {
        throw e;
    } catch (Exception e) {
        throw new EngineException("Unable to update application source files for ionic project '" + project.getName() + "'", e);
    }
}
Also used : MobileApplication(com.twinsoft.convertigo.beans.core.MobileApplication) ApplicationComponent(com.twinsoft.convertigo.beans.ngx.components.ApplicationComponent) IApplicationComponent(com.twinsoft.convertigo.beans.core.IApplicationComponent) EngineException(com.twinsoft.convertigo.engine.EngineException) UISharedComponent(com.twinsoft.convertigo.beans.ngx.components.UISharedComponent) IPageComponent(com.twinsoft.convertigo.beans.core.IPageComponent) PageComponent(com.twinsoft.convertigo.beans.ngx.components.PageComponent) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Aggregations

PageComponent (com.twinsoft.convertigo.beans.ngx.components.PageComponent)40 IPageComponent (com.twinsoft.convertigo.beans.core.IPageComponent)22 EngineException (com.twinsoft.convertigo.engine.EngineException)16 ApplicationComponent (com.twinsoft.convertigo.beans.ngx.components.ApplicationComponent)14 File (java.io.File)14 UISharedComponent (com.twinsoft.convertigo.beans.ngx.components.UISharedComponent)11 IOException (java.io.IOException)11 UIComponent (com.twinsoft.convertigo.beans.ngx.components.UIComponent)9 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)8 UIUseShared (com.twinsoft.convertigo.beans.ngx.components.UIUseShared)7 IApplicationComponent (com.twinsoft.convertigo.beans.core.IApplicationComponent)6 ProjectExplorerView (com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView)6 Cursor (org.eclipse.swt.graphics.Cursor)6 Display (org.eclipse.swt.widgets.Display)6 Shell (org.eclipse.swt.widgets.Shell)6 Contributor (com.twinsoft.convertigo.beans.ngx.components.Contributor)5 UIActionStack (com.twinsoft.convertigo.beans.ngx.components.UIActionStack)5 ArrayList (java.util.ArrayList)5 JSONObject (org.codehaus.jettison.json.JSONObject)5 MobileApplication (com.twinsoft.convertigo.beans.core.MobileApplication)4