//////////////////////////////////////////////////////////////////////////// // Rev: 2010.1 (P4JsApi 1.0 Example) // // NOTE: This is an example only, and should be modified to work in // a production environment! No warranty is expressed or implied. // Scripts should be tested thoroughly on a test server before // using in a production environment. // //////////////////////////////////////////////////////////////////////////// // Copyright (c) 2010, Perforce Software, Inc. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. //////////////////////////////////////////////////////////////////////////// var P4 = P4 || {}; P4.changelist = P4.changelist || {}; try { /** * Loads the current changelist that the submit is for into an * object literal with the key/value pairs of the current * changelist. * * @return - object literal of current changelist data */ P4.changelist.load = function() { var changelist = {}; try { var clNumber = P4JsApi.getSubmitChange(); if (clNumber && parseInt(clNumber) > 0) { var changelistData = P4JsApi.p4("describe " + clNumber); if (changelistData) { changelist = changelistData; } } } catch (e) { changelist = {}; } return (changelist.data && changelist.data[0]) ? changelist.data[0] : {}; } /** * Gets all occurrences of an incrementing key sequence from the specified JSON data object. * This method looks for properties in the specified data object starting at key+index where * index starts at zero and increments until no property is found by key+index in data. * * * @param data - Object to index into for specified key * @param key - incrementing key sequence * * @return - non-null but possibly empty array of value found by the specified incrementing key */ P4.changelist.get = function(data, key) { var entries = []; if (data && key) { if (data[key]) { // For handling data from commands like p4 opened entries.push(data[key]); } else { // For handling data from commands like p4 describe var index = 0; while (data[key + index]) { entries.push(data[key + index]); index++; } } } return entries; } /** * Get an array of depot file paths in the specified * changelist data. * * @param changelist - object literal of changelist data * * @return - non-null but possibly empty array of depot file paths */ P4.changelist.files = function(changelist) { return this.get(changelist, "depotFile"); } /** * Get an array of job ids in the specified * changelist data. * * @param changelist - object literal of changelist data * * @return - non-null but possibly empty array of depot file paths */ P4.changelist.jobs = function(changelist) { return this.get(changelist, "job"); } /** * Convert the specified files array, jobs array, and * changelist description into a changelist form that * can be submitted or saved. * * @files - array of depot file paths * @jobs - array of job ids * @description - string changelist description * * @return - string changelist form */ P4.changelist.form = function(files, jobs, description) { try { var clFiles = ""; if (files && files.length > 0) { clFiles = "Files: "; for ( var i = 0; i < files.length; i++) { clFiles += '\n\t' + files[i]; } } var clJobs = ""; if (jobs && jobs.length > 0) { clJobs = "Jobs: "; for ( var i = 0; i < jobs.length; i++) { clJobs += '\n\t' + jobs[i]; } clJobs += '\n\n'; } var change = (P4JsApi.getSubmitChange() === "default") ? "new" : P4JsApi.getSubmitChange(); var form = 'Change: ' + change + '\n\n'; form += 'Client: ' + P4JsApi.getClient() + '\n\n'; form += 'User: ' + P4JsApi.getUser() + '\n\n'; form += 'Status: pending' + '\n\n'; form += 'Description: ' + description.replace(/\n/g, '\n\t') + '\n\n'; form += clJobs; form += clFiles; return form; } catch(e) { alert(e); return ''; } } /** * Save the current changelist with the specified files, jobs, * and description and optionally close the submit dialog. * * @param files - array of depot file paths * @param jobs - array of job ids * @param description - string changelist description * @close - true to close the submit dialog, false/null to not */ P4.changelist.save = function(files, jobs, description, close) { var form = this.form(files, jobs, description); var result = P4JsApi.p4('change -i "' + form + '"'); if (result.error) { alert("Perforce error: " + result.error); } else { P4JsApi.refreshAll(); if (close) { P4JsApi.accepted(); } } return result; } /** * Submit the current changelist with the specified files, jobs, * and description and close the submit dialog. * * This method first saves the current changelist and then submits * it by changelist number and closes the submit dialog. * * @param files - array of depot file paths * @param jobs - array of job ids * @param description - string changelist description */ P4.changelist.submit = function(files, jobs, description) { var change = P4JsApi.getSubmitChange(); // if change is default then first run save, retrieve the changelist number and then run submit // if change is not default, then no need run save. Just run submit if (change === "default") { var saveResult = this.save(files, jobs, description, false); if (saveResult && saveResult.info) { // Get the changelist number from saveResult.info change = saveResult.info.split(" ", 2)[1]; P4JsApi.p4('submit -c ' + change); P4JsApi.refreshAll(); P4JsApi.accepted(); } else { // Do nothing. "save" should have shown the error message already. } } else { P4JsApi.p4('submit -c ' + change); P4JsApi.refreshAll(); P4JsApi.accepted(); } } } catch(e) { alert(e); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 7663 | dscheirer | Rollback //public/perforce/p4jsapi to changelist 7643 | ||
#3 | 7660 | jhalbig |
As per meeting to resolve issue with sync calls made from within async calls hanging P4V (job039138) pulling example code from Public Depot until it can be re-worked and confirmed to function correctly with pending P4JsApi changes. |
||
#2 | 7643 | jhalbig |
Cleaned up any remaining discrepancies in the code. Removed all internal references and debugging code. Cleaned up formatting, added more commenting for submit dialog code. Ready for 2010.1 Beta. |
||
#1 | 7638 | jhalbig | Initial Addition of P4JsApi samples for 2010.1 Beta |