<commit_before>/*
 * This file is part of Arduino
 *
 * Arduino is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As a special exception, you may use this file as part of a free software
 * library without restriction.  Specifically, if other files instantiate
 * templates or use macros or inline functions from this file, or you compile
 * this file and link it with other files to produce an executable, this
 * file does not by itself cause the resulting executable to be covered by
 * the GNU General Public License.  This exception does not however
 * invalidate any other reasons why the executable file might be covered by
 * the GNU General Public License.
 *
 * Copyright 2015 Arduino Srl (http://www.arduino.org/)
 *
 * authors: arduino.org team - <EMAIL>
 * 
 */

define(function (require, exports, module) {
    "use strict";

    var FileUtils           = brackets.getModule("file/FileUtils"),
        FileSystem          = brackets.getModule("filesystem/FileSystem"),
        WorkspaceManager    = brackets.getModule("view/WorkspaceManager"),
        Dialogs             = brackets.getModule("widgets/Dialogs"),
        DefaultDialogs      = brackets.getModule("widgets/DefaultDialogs");

    var optionsDirName         = null,
        optionsDir             = null,
        optionsPrefix          = "[arduino ide - options]";

    var optionBoardSelectMessage        = "Select your board...",
        optionProgrammerSelectMessage   = "Select your programmer...",
        optionPortSelectMessage         = "Select your port...",

        listBoardsDetectedHTML          = "<option disabled selected>" + optionBoardSelectMessage + "</option>",
        listProgrammersDetectedHTML     = "<option disabled selected>" + optionProgrammerSelectMessage + "</option>",
        listPortsDetectedHTML           = "<option disabled selected>" + optionPortSelectMessage + "</option>";

    var portsDialog, boardsDialog, programmersDialog;

    /**
     * This module read all files of the core
     * boards, platform and preferences.
     * @param {String} hardwareDirectoryPath absolute path to the harwdare/arduino directory
     */
    function Options (hardwareDirectoryPath) {
        brackets.arduino.options.archs  = {};
        optionsDirName = hardwareDirectoryPath;

        //get the arch/platforms directory name
        optionsDir = FileSystem.getDirectoryForPath( optionsDirName );
        optionsDir.getContents(function(err, contents, stats){              //get the directories of the arch/platform under harwdare/arduino
            if(err)
                console.error(optionsPrefix + " Error in loading options files: " + err);
            else
                contents.forEach(function (element, index, array) {         //element is the directory of the arch/platform
                    //element is a 'Diretory' object
                    if (element.isDirectory) {

                        brackets.arduino.options.archs[element.name] = {};
                        brackets.arduino.options.archs[element.name].boards = [];
                        brackets.arduino.options.archs[element.name].programmers = [];

                        loadOptionsInfo(element.fullPath, function(err, result){
                            if(err)
                                console.error(optionsPrefix + " - " + err);
                            else
                                brackets.arduino.options.archs[element.name].info = result;
                        });

                        loadOptions(element.fullPath, "boards", function(err, result){
                            if(err)
                                console.error(optionsPrefix + " - " + err);
                            else
                                brackets.arduino.options.archs[element.name].boards.push(result);
                        });

                        loadOptions(element.fullPath, "programmers", function(err, result){
                            if(err)
                                console.error(optionsPrefix + " - " + err);
                            else
                                brackets.arduino.options.archs[element.name].programmers.push(result);
                        });
                    }
            });
        });


        brackets.arduino.dispatcher.on("arduino-event-menu-tool-ports", portListEvent);
        brackets.arduino.dispatcher.on("arduino-event-menu-tool-boards", boardListEvent);
        brackets.arduino.dispatcher.on("arduino-event-menu-tool-programmers", programmerListEvent);

    };


    var loadOptionsFile = function(file, callback){
        file.read(function(err, data, stat){
            callback(err, data, stat);
        });
    };

    /**
     *
     * @param path is the absolute path to the arch/platform directory
     * @param info is the options to load ( boards, platform or programmers)
     * @param callback
     */
    var loadOptions = function(path, info ,callback) {

        if (info == "boards" || info == "platform" || info == "programmers") {
            var boardDir = FileSystem.getDirectoryForPath(path + "/opt/" + info);   //get the data dir that contains the boards, platform and programmers info
            if (boardDir) {
                boardDir.getContents(function(err, contents, stats) {
                    if(err)
                        callback(err);
                    else
                        contents.forEach( function (file, index, array) {    // get the files

                            if (file.isFile) {
                                loadOptionsFile(file, function (err, data, stat){ //read the contents of each file.
                                    if(!err){
                                        callback(null, JSON.parse(data));
                                    }
                                    else{
                                        callback(err);
                                    }
                                });
                            }
                        });
                });
            }
            else {
                callback(err);
            }
        }
    }
    /**
     *
     * @param path is the absolute path to the arch/platform directory
     * @param callback
     */
    var loadOptionsInfo = function(path, callback) {
        var infoFile = FileSystem.getFileForPath(path + "/opt/info.json");
        loadOptionsFile(infoFile, function(err, data, stat){<commit_msg>read only json files<commit_after>/*
 * This file is part of Arduino
 *
 * Arduino is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As a special exception, you may use this file as part of a free software
 * library without restriction.  Specifically, if other files instantiate
 * templates or use macros or inline functions from this file, or you compile
 * this file and link it with other files to produce an executable, this
 * file does not by itself cause the resulting executable to be covered by
 * the GNU General Public License.  This exception does not however
 * invalidate any other reasons why the executable file might be covered by
 * the GNU General Public License.
 *
 * Copyright 2015 Arduino Srl (http://www.arduino.org/)
 *
 * authors: arduino.org team - <EMAIL>
 * 
 */

define(function (require, exports, module) {
    "use strict";

    var FileUtils           = brackets.getModule("file/FileUtils"),
        FileSystem          = brackets.getModule("filesystem/FileSystem"),
        WorkspaceManager    = brackets.getModule("view/WorkspaceManager"),
        Dialogs             = brackets.getModule("widgets/Dialogs"),
        DefaultDialogs      = brackets.getModule("widgets/DefaultDialogs");

    var optionsDirName         = null,
        optionsDir             = null,
        optionsPrefix          = "[arduino ide - options]";

    var optionBoardSelectMessage        = "Select your board...",
        optionProgrammerSelectMessage   = "Select your programmer...",
        optionPortSelectMessage         = "Select your port...",

        listBoardsDetectedHTML          = "<option disabled selected>" + optionBoardSelectMessage + "</option>",
        listProgrammersDetectedHTML     = "<option disabled selected>" + optionProgrammerSelectMessage + "</option>",
        listPortsDetectedHTML           = "<option disabled selected>" + optionPortSelectMessage + "</option>";

    var portsDialog, boardsDialog, programmersDialog;

    /**
     * This module read all files of the core
     * boards, platform and preferences.
     * @param {String} hardwareDirectoryPath absolute path to the harwdare/arduino directory
     */
    function Options (hardwareDirectoryPath) {
        brackets.arduino.options.archs  = {};
        optionsDirName = hardwareDirectoryPath;

        //get the arch/platforms directory name
        optionsDir = FileSystem.getDirectoryForPath( optionsDirName );
        optionsDir.getContents(function(err, contents, stats){              //get the directories of the arch/platform under harwdare/arduino
            if(err)
                console.error(optionsPrefix + " Error in loading options files: " + err);
            else
                contents.forEach(function (element, index, array) {         //element is the directory of the arch/platform
                    //element is a 'Diretory' object
                    if (element.isDirectory) {

                        brackets.arduino.options.archs[element.name] = {};
                        brackets.arduino.options.archs[element.name].boards = [];
                        brackets.arduino.options.archs[element.name].programmers = [];

                        loadOptionsInfo(element.fullPath, function(err, result){
                            if(err)
                                console.error(optionsPrefix + " - " + err);
                            else
                                brackets.arduino.options.archs[element.name].info = result;
                        });

                        loadOptions(element.fullPath, "boards", function(err, result){
                            if(err)
                                console.error(optionsPrefix + " - " + err);
                            else
                                brackets.arduino.options.archs[element.name].boards.push(result);
                        });

                        loadOptions(element.fullPath, "programmers", function(err, result){
                            if(err)
                                console.error(optionsPrefix + " - " + err);
                            else
                                brackets.arduino.options.archs[element.name].programmers.push(result);
                        });
                    }
            });
        });


        brackets.arduino.dispatcher.on("arduino-event-menu-tool-ports", portListEvent);
        brackets.arduino.dispatcher.on("arduino-event-menu-tool-boards", boardListEvent);
        brackets.arduino.dispatcher.on("arduino-event-menu-tool-programmers", programmerListEvent);

    };


    var loadOptionsFile = function(file, callback){
        file.read(function(err, data, stat){
            callback(err, data, stat);
        });
    };

    /**
     *
     * @param path is the absolute path to the arch/platform directory
     * @param info is the options to load ( boards, platform or programmers)
     * @param callback
     */
    var loadOptions = function(path, info ,callback) {

        if (info == "boards" || info == "platform" || info == "programmers") {
            var boardDir = FileSystem.getDirectoryForPath(path + "/opt/" + info);   //get the data dir that contains the boards, platform and programmers info
            if (boardDir) {
                boardDir.getContents(function(err, contents, stats) {
                    if(err)
                        callback(err);
                    else
                        contents.forEach( function (file, index, array) {    // get the files

                            if (file.isFile && FileUtils.getFileExtension(file.fullPath) == "json") {
                                loadOptionsFile(file, function (err, data, stat){ //read the contents of each file.
                                    if(!err){
                                        callback(null, JSON.parse(data));
                                    }
                                    else{
                                        callback(err);
                                    }
                                });
                            }
                        });
                });
            }
            else {
                callback(err);
            }
        }
    }
    /**
     *
     * @param path is the absolute path to the arch/platform directory
     * @param callback
     */
    var loadOptionsInfo = function(path, callback) {
        var infoFile = FileSystem.getFileForPath(path + "/opt/info.json");
        loadOptionsFile(infoFile, function(err, data, stat){
{'fraction_non_alphanumeric': 0.0897902097902098, 'fraction_numerical': 0.0022377622377622378, 'mean_word_length': 1.7365097588978187, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4010254', 'n_tokens_mistral': 3518, 'n_tokens_neox': 3303, 'n_words': 1022}