Created
March 21, 2012 11:00
-
-
Save evaisse/2146195 to your computer and use it in GitHub Desktop.
[KOMODO] backup this file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var filename = ko.views.manager.currentView.koDoc.baseName, | |
path = ko.views.manager.currentView.koDoc.displayPath; | |
/* | |
Set this to the path of your local directory where you want to backup your files | |
*/ | |
var backupLocation = "/Users/evaisse/Library/Application Support/" | |
+ "KomodoEdit/autobackup"; | |
String.prototype.hashCode = function () { | |
var hash = 0, c; | |
if (this.length == 0) return hash; | |
for (i = 0; i < this.length; i++) { | |
c = this.charCodeAt(i); | |
hash = ((hash << 5) - hash) + c; | |
hash = hash & hash; // Convert to 32bit integer | |
} | |
return hash; | |
} | |
function ISODateString(d){ | |
function pad(n){ | |
return n < 10 ? '0' + n : n | |
} | |
return d.getUTCFullYear()+'' | |
+ pad(d.getUTCMonth()+1)+'' | |
+ pad(d.getUTCDate())+'T' | |
+ pad(d.getUTCHours())+'' | |
+ pad(d.getUTCMinutes())+'' | |
+ pad(d.getUTCSeconds())+'Z' | |
} | |
StatusBar_AddMessage('Start backup', "debugger", 5000, true); | |
function backupThisFile() { | |
var hash = path.hashCode(); | |
if (hash < 0) { | |
hash = "a" + (hash + "").substr(1); | |
} else { | |
hash = "b" + hash; | |
} | |
var fileSvc = Components | |
.classes["@activestate.com/koFileService;1"] | |
.getService(Components.interfaces.koIFileService); | |
var koTmpfile = fileSvc.makeTempFile("test.txt", "w+"); | |
koTmpfile.puts(komodo.editor.text); | |
koTmpfile.flush(); | |
koTmpfile.close(); | |
var cmd = 'cp "' + koTmpfile.path + '" "' + backupLocation + '/' | |
+ filename + '.' + hash + '.' + ISODateString(new Date()) + '.bak"'; | |
var runSvc = Components.classes["@activestate.com/koRunService;1"] | |
.createInstance(Components.interfaces.koIRunService); | |
var process = runSvc.RunAndNotify(cmd /* command */, | |
"" /* cwd */, | |
"" /* environment settings */, | |
"" /* stdin input */); | |
// cleanupDir | |
runSvc.RunAndNotify( | |
'find "' + backupLocation + '" -type f -name *Z.bak -ctime -15 -exec rm {} \;' /* command */, | |
"" /* cwd */, | |
"" /* environment settings */, | |
"" /* stdin input */ | |
); | |
} | |
backupThisFile(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment