<commit_before>/*
 * Copyright (c) 2012-2015 S-Core Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * webida - run configuration manager
 *
 * Src:
 *   plugins/webida.ide.project-management.run/run-configuration-manager.js
 */
define([
    'webida-lib/app',
    'webida-lib/util/path',
    'dojo/topic',
    'webida-lib/plugins/workspace/plugin',
    'external/async/dist/async.min',
    'external/lodash/lodash.min',
    'plugins/webida.notification/notification-message',
    'webida-lib/util/logger/logger-client'
], function (
    ide,
    pathutil,
    topic,
    workspace,
    async,
    _,
    toastr,
    Logger
) {
    'use strict';

    var logger = new Logger();
    //logger.setConfig('level', Logger.LEVELS.log);
    logger.off();

    var PATH_WORKSPACE = ide.getPath();
    var WORKSPACE_INFO_DIR_NAME = '.workspace';
    var RUN_CONFIG_FILE_NAME = 'workspace.json';
    var PATH_RUN_CONFIG = PATH_WORKSPACE + '/' + WORKSPACE_INFO_DIR_NAME + '/' + RUN_CONFIG_FILE_NAME;

    //var topics = {
    //    RUN_AS_DISABLED: 'toolbar.runas.disable',
    //    RUN_AS_ENABLED: 'toolbar.runas.enable'
    //};

    var MODE = {
        RUN_MODE: 'run',
        DEBUG_MODE: 'debug'
    };

    var fsMount = ide.getFSCache();

    var runConfigurationFileCache;
    var runConfigurations = {};

    var isFlushing = false;

    /**
     * load run configurations of all project from workspace.json file
     */
    function loadRunConfigurations(callback) {
        if (!isFlushing) {
            async.waterfall([
                function (next) {
                    fsMount.exists(PATH_RUN_CONFIG, function (err, exist) {
                        if (err) {
                            next(err);
                        } else if (!exist) {
                            next(PATH_RUN_CONFIG + ' is not exists');
                        } else {
                            next();
                        }
                    });
                },
                function (next) {
                    fsMount.readFile(PATH_RUN_CONFIG, function (err, content) {
                        var workspaceObj;
                        if (err) {
                            next(err);
                        } else {
                            runConfigurationFileCache = content || '{}';
                            workspaceObj = JSON.parse(content);
                            if (workspaceObj.run && Object.getOwnPropertyNames(workspaceObj.run).length > 0) {
                                runConfigurations = workspaceObj.run;
                                next();
                            } else {
                                next(PATH_RUN_CONFIG + ' hasn\'t run configuration info');
                            }
                        }
                    });
                }
            ], function (err) {
                if (err) {
                    logger.error('loadRunConfigurations', err);
                }
                if (callback) {
                    callback(err);
                }
            });
        }
    }

    /**
     * flush updated run configurations to workspace.json file
     */
    function flushRunConfigurations(callback) {
        var originalRun = JSON.parse(runConfigurationFileCache).run;
        isFlushing = true;

        _.each(runConfigurations, function (runConf) {
            if (runConf._dirty) {
                if (originalRun[runConf.originalName]) {
                    runConfigurations[runConf.originalName] = originalRun[runConf.originalName];
                } else {
                    runConf._deleted = true;
                }
            }
            delete runConf._dirty;
            delete runConf.originalName;
        });

        runConfigurations = _.omit(runConfigurations, function (runConf) {
            return runConf._deleted;
        });

        var unsyncedItems = [];
        _.map(runConfigurations, function (runConf, name) {
            if (runConf.name !== name) {
                unsyncedItems.push({
                    oldName: name,
                    newName: runConf.name
                });
            }
        });

        _.each(unsyncedItems, function (item) {
            runConfigurations[item.newName] = runConfigurations[item.oldName];
            delete runConfigurations[item.oldName];
        });


        runConfigurationFileCache = JSON.stringify({ run: runConfigurations });
        fsMount.writeFile(PATH_RUN_CONFIG, runConfigurationFileCache, function (err) {
            isFlushing = false;
            if (err) {
                toastr.error(err);
                logger.error('flushRunConfigurations:writeFile', PATH_RUN_CONFIG);
            }<commit_msg>[TASK] fix configuration order [DESC.] Modified to be sorted in ascending order when a toolbar and configuration list to show.<commit_after>/*
 * Copyright (c) 2012-2015 S-Core Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * webida - run configuration manager
 *
 * Src:
 *   plugins/webida.ide.project-management.run/run-configuration-manager.js
 */
define([
    'webida-lib/app',
    'webida-lib/util/path',
    'dojo/topic',
    'webida-lib/plugins/workspace/plugin',
    'external/async/dist/async.min',
    'external/lodash/lodash.min',
    'plugins/webida.notification/notification-message',
    'webida-lib/util/logger/logger-client'
], function (
    ide,
    pathutil,
    topic,
    workspace,
    async,
    _,
    toastr,
    Logger
) {
    'use strict';

    var logger = new Logger();
    //logger.setConfig('level', Logger.LEVELS.log);
    logger.off();

    var PATH_WORKSPACE = ide.getPath();
    var WORKSPACE_INFO_DIR_NAME = '.workspace';
    var RUN_CONFIG_FILE_NAME = 'workspace.json';
    var PATH_RUN_CONFIG = PATH_WORKSPACE + '/' + WORKSPACE_INFO_DIR_NAME + '/' + RUN_CONFIG_FILE_NAME;

    //var topics = {
    //    RUN_AS_DISABLED: 'toolbar.runas.disable',
    //    RUN_AS_ENABLED: 'toolbar.runas.enable'
    //};

    var MODE = {
        RUN_MODE: 'run',
        DEBUG_MODE: 'debug'
    };

    var fsMount = ide.getFSCache();

    var runConfigurationFileCache;
    var runConfigurations = {};

    var isFlushing = false;

    /**
     * Create sorted object in ascending order by keys
     */
    function sortKeysBy(obj) {
        var keys = _.sortBy(_.keys(obj));
        return _.object(keys, _.map(keys, function (key) {
            return obj[key];
        }));
    }

    /**
     * load run configurations of all project from workspace.json file
     */
    function loadRunConfigurations(callback) {
        if (!isFlushing) {
            async.waterfall([
                function (next) {
                    fsMount.exists(PATH_RUN_CONFIG, function (err, exist) {
                        if (err) {
                            next(err);
                        } else if (!exist) {
                            next(PATH_RUN_CONFIG + ' is not exists');
                        } else {
                            next();
                        }
                    });
                },
                function (next) {
                    fsMount.readFile(PATH_RUN_CONFIG, function (err, content) {
                        var workspaceObj;
                        if (err) {
                            next(err);
                        } else {
                            runConfigurationFileCache = content || '{}';
                            workspaceObj = JSON.parse(content);
                            if (workspaceObj.run && Object.getOwnPropertyNames(workspaceObj.run).length > 0) {
                                runConfigurations = sortKeysBy(workspaceObj.run);
                                next();
                            } else {
                                next(PATH_RUN_CONFIG + ' hasn\'t run configuration info');
                            }
                        }
                    });
                }
            ], function (err) {
                if (err) {
                    logger.error('loadRunConfigurations', err);
                }
                if (callback) {
                    callback(err);
                }
            });
        }
    }

    /**
     * flush updated run configurations to workspace.json file
     */
    function flushRunConfigurations(callback) {
        var originalRun = JSON.parse(runConfigurationFileCache).run;
        isFlushing = true;

        _.each(runConfigurations, function (runConf) {
            if (runConf._dirty) {
                if (originalRun[runConf.originalName]) {
                    runConfigurations[runConf.originalName] = originalRun[runConf.originalName];
                } else {
                    runConf._deleted = true;
                }
            }
            delete runConf._dirty;
            delete runConf.originalName;
        });

        runConfigurations = _.omit(runConfigurations, function (runConf) {
            return runConf._deleted;
        });

        var unsyncedItems = [];
        _.map(runConfigurations, function (runConf, name) {
            if (runConf.name !== name) {
                unsyncedItems.push({
                    oldName: name,
                    newName: runConf.name
                });
            }
        });

        _.each(unsyncedItems, function (item) {
            runConfigurations[item.newName] = runConfigurations[item.oldName];
            delete runConfigurations[item.oldName];
        });

        runConfigurationFileCache = JSON.stringify({ run: sortKeysBy(runConfigurations) });
        fsMount.writeFile(PATH_RUN_CONFIG, runConfigurationFileCache, function (err) {
            isFlushing = false;
            if (err) {
                toastr.error(err);
                logger.error('flushRunConfigurations:writeFile', PATH_RUN_CONFIG);
            }
{'fraction_non_alphanumeric': 0.10211981566820276, 'fraction_numerical': 0.0023963133640552995, 'mean_word_length': 1.5555817239755063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4630966', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2779, 'n_words': 629}