/** * Module where all the interactions with the storage system are done. * * Here the storage system is P4, we interact with it with the P4JsApi. * We also use the LocalStorage to store values. */ define([ "config", "underscore", ], function(config){ /*********************************************/ /***************** P4JsApi *******************/ /*********************************************/ /** * Return the version of the system storage. * * @return - the version number */ var getVersion = function(){ return P4JsApi.getApiVersion(); }; /** * Return the current user. * * @return - the name of the user. */ var getUser = function(){ return P4JsApi.getUser(); }; /** * Return the email of the current user. * * @return - the user's email */ var getEmail = function(){ var result = this.executeCommand("user -o"); return result[0].Email; }; /** * Get all the groups of current user. * * @return - an array containing the name of all the groups of the current user. */ var getGroups = function(){ var user = this.getUser(); var groups = this.executeCommand("groups " + user); //we have an array of object, //and we want an array with "Group" attribute (name of the group) of each object. //See underscore documentation for pluck method, for further informations. return _.pluck(groups, "Group"); }; /** * Return the current CL. * * @return - an object containing the current CL. */ var getCurrentCL = function(){ var clNumber = P4JsApi.getSubmitChange(); var command = ""; if(clNumber == "default") { command = "change -o"; } else if(clNumber && parseInt(clNumber) > 0) { command = "change -o " + clNumber; } var currentCL = this.executeCommand(command); return currentCL[0]; }; /** * Return the files of the CL. * * @return - an array containing objects for each file. */ var getFiles = function(clNum, workspace){ if(! clNum){ clNum = P4JsApi.getSubmitChange(); } if(! workspace){ workspace = P4JsApi.getClient(); } var command = "fstat -Rco -e "; if(clNum == "new"){ command += "default "; } else { command += clNum + " "; } command += "//" + workspace + "/..."; var files = this.executeCommand(command); //p4ERROR is set when CL is empty. if(files[0].p4ERROR){ files = []; } return files; }; /** * Get the official maps. * * @return - the content of the text file as a string. */ var getOfficialMaps = function(){ var req = this.executeCommand("print -q \"" + config.path_maplist + "\""); //for local line ending characters (workspace configuration) var official_map_txt = req[0].text; if(! official_map_txt){ //for shared line ending characters var bin = req[0].binary; official_map_txt = ""; for(var i = 0, length = bin.length ; i < length ; i++){ official_map_txt += String.fromCharCode(bin[i]); } } return official_map_txt; }; /** * Enable / disable debug. * * @param is_debug - boolean to enable / disable debug. */ var setDebug = function(is_debug){ P4JsApi.setWebKitDeveloperExtrasEnabled(is_debug); }; /** * Execute given command and return results. * * @param command - the comand to execute. * * @return - return array containing all data * @throw - error if no result */ var executeCommand = function(command){ var result = P4JsApi.p4(command); if(result && result.size > 0){ return result.data; } else{ throw "Error with following command: " + command; } }; /** * Save the given CL with its description and files in the storage system. * * @param changelistModel - the changelist model. * @param filesCollection - the files collection. */ var saveCL = function(changelistModel, filesCollection){ var form = this.formatModel(changelistModel, filesCollection); var data = P4JsApi.p4("change -i \"" + form + "\""); // we are going to return the CL number if the new CL was newly created. var p4Info_message = data.data[0].p4INFO.message; var regEx = /Change\s*(\d+)\s*(?:created|saved)/gm; var match = regEx.exec(p4Info_message); if (match != null) { var changeListNumber = match[1]; return changeListNumber; } }; /** * Submit the given CL with its description and files, and applying the given options. * * @param changelistModel - the changelist model. * @param filesCollection - the files collection. * @param options - the options of the submit */ var submitCL = function(changelistModel, filesCollection, options){ var form = this.formatModel(changelistModel, filesCollection); //can't pass the form to submit command //so we have to save the changelist before var result = P4JsApi.p4("change -i \"" + form + "\""); var chgNum = result.data[0]["p4INFO"]["args"]["change"].split(" ")[1]; P4JsApi.p4("submit -c " + chgNum + " " + options); }; /** * Close the storage system. */ var closeSA = function(){ P4JsApi.refreshAll(); P4JsApi.accepted(); }; /** * Format the CL with its description and files, in order to be a valid CL for P4. * * @param changelistModel - the changelist model. * @param filesCollection - the files collection. * * @return - the formated model. */ var formatModel = function(changelistModel, filesCollection){ var formated_description = changelistModel.getFormatedDescription(); var formated_files = filesCollection.getFormatedFiles(); var formated_model = "Change: " + changelistModel.get("Change") + "\nClient: " + changelistModel.get("Client") + "\nUser: " + changelistModel.get("User") + "\nStatus: " + changelistModel.get("Status") + "\nDescription: \n" + formated_description + "\nFiles: \n" + formated_files + "\nJobs: \n" + ""; //removed from the submit assistant return formated_model; }; /** * Open the given URL in the default browser. * * @param url - the url to open. */ var openURL = function(url){ P4JsApi.openUrlInBrowser(url); }; /*********************************************/ /************** LocalStorage *****************/ /*********************************************/ /** * Save the given value with the given key in the localStorage. * * @param key - the key of the value to save. * @param value - the value to save. */ var localSave = function(key, value){ localStorage[key] = value; }; /** * Return the value of the given key from localStorage. * * @param key - the key of the value to return. * * @return - the value of the key or empty string if undefined * (localStorage only support strings). */ var localLoad = function(key){ var value = localStorage[key]; if(value){ return value; } else{ return ""; } }; return { getVersion: getVersion, getUser: getUser, getEmail: getEmail, getGroups: getGroups, getCurrentCL: getCurrentCL, getFiles: getFiles, getOfficialMaps: getOfficialMaps, setDebug: setDebug, executeCommand: executeCommand, saveCL: saveCL, submitCL: submitCL, closeSA: closeSA, formatModel: formatModel, openURL: openURL, localSave: localSave, localLoad: localLoad, }; } );