home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 March / PCpro_2006_03.ISO / files / freeware / greasemonkey-0.6.4-fx.xpi / chrome / chromeFiles / content / versioning.js < prev    next >
Encoding:
JavaScript  |  2005-11-30  |  4.9 KB  |  179 lines

  1.  
  2.  
  3. /**
  4.  * Checks whether the version has changed since the last run and performs 
  5.  * any necessary upgrades.
  6.  */
  7. function GM_updateVersion() {
  8.   log("> GM_updateVersion");
  9.  
  10.   // this is the last version which has been run at least once
  11.   var initialized = GM_prefRoot.getValue("version", "0.0");
  12.   
  13.   if (!GM_versionIsGreaterOrEqual(initialized, "0.3")) {
  14.     GM_pointThreeMigrate();
  15.   }
  16.   
  17.   if (!GM_versionIsGreaterOrEqual(initialized, "0.4.2")) {
  18.     GM_pointFourMigrate();
  19.   }
  20.  
  21.   // update the currently initialized version so we don't do this work again.
  22.   GM_prefRoot.setValue("version", "0.6.4");
  23.  
  24.   log("< GM_updateVersion");
  25. }
  26.  
  27. /**
  28.  * Copies the entire scripts directory to the new location, if it exists.
  29.  */
  30. function GM_pointFourMigrate() {
  31.   log("> GM_pointFourMigrate");
  32.  
  33.   var oldDir = getOldScriptDir();
  34.   var newDir = getNewScriptDir();
  35.  
  36.   if (!oldDir.exists() && !newDir.exists()) {
  37.     newDir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
  38.  
  39.     var defaultConfigFile = getContentDir();
  40.     defaultConfigFile.append("default-config.xml");
  41.   
  42.     defaultConfigFile.copyTo(newDir, "config.xml");
  43.     defaultConfigFile.permissions = 0644;
  44.   }
  45.  
  46.   log("< GM_pointFourMigrate");
  47. }
  48.  
  49. /**
  50.  * Migrates the configuration directory from the old format to the new one
  51.  */
  52. function GM_pointThreeMigrate() {
  53.   log("> GM_pointThreeMigrate");
  54.  
  55.   // check to see whether there's any config to migrate
  56.   var configExists = GM_getPointThreeScriptFile("config.xml").exists();
  57.  
  58.   log("config file exists: " + configExists);
  59.   if (!configExists) {
  60.     return;
  61.   }
  62.   
  63.   // back up the config directory
  64.   // if an error happens, report it and exit
  65.   try {
  66.     var scriptDir = GM_getPointThreeScriptDir();
  67.     var tempDir = getTempFile();
  68.  
  69.     log("script dir: " + scriptDir.path);
  70.     log("temp dir: " + tempDir.path);
  71.  
  72.     scriptDir.copyTo(tempDir.parent, tempDir.leafName);
  73.   
  74.     // update the format of the config.xml file and move each file
  75.     var script = null;
  76.     var scriptFile = null;
  77.     var doc = document.implementation.createDocument("", "", null);
  78.     var configFile = GM_getPointThreeScriptFile("config.xml");
  79.     
  80.     var configURI = Components.classes["@mozilla.org/network/io-service;1"]
  81.                               .getService(Components.interfaces.nsIIOService)
  82.                               .newFileURI(configFile);
  83.  
  84.     // first, load config.xml raw and add the new required filename attribute
  85.     doc.async = false;
  86.     doc.load(configURI.spec);
  87.   
  88.     log("loaded existing config...");
  89.  
  90.     var nodes = document.evaluate("/UserScriptConfig/Script", doc, null,
  91.         XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  92.     var node;
  93.  
  94.     for (var i = 0; (node = nodes.snapshotItem(i)); i++) {
  95.       if (node.hasAttribute("id")) {
  96.         node.setAttribute("filename", node.getAttribute("id"));
  97.       }
  98.     }
  99.   
  100.     // save the config file
  101.     var configStream = getWriteStream(configFile);
  102.     new XMLSerializer().serializeToStream(doc, configStream, "utf-8");
  103.     configStream.close();
  104.  
  105.     log("config saved.")
  106.   
  107.     // now, load config normally and reinitialize all scripts's filenames
  108.     var config = new Config(GM_getPointThreeScriptFile("config.xml"));
  109.     config.load();
  110.   
  111.     log("config reloaded, moving files.");
  112.  
  113.     for (var i = 0; (script = config.scripts[i]); i++) {  
  114.       if (script.filename.match(/^\d+$/)) {
  115.         scriptFile = GM_getPointThreeScriptFile(script.filename);
  116.         config.initFilename(script);
  117.         log("renaming script " + scriptFile.leafName + " to " + script.filename);
  118.         scriptFile.moveTo(scriptFile.parent, script.filename);
  119.       }
  120.     }
  121.   
  122.     log("moving complete. saving configuration.")
  123.   
  124.     // save the config file
  125.     config.save();
  126.   
  127.     log("0.3 migration completed successfully!")
  128.   } catch (e) {
  129.     alert("Could not complete Greasemonkey 0.3 migration. Some changes may " + 
  130.           "have been made to your scripts directory. See JS Console for " + 
  131.           "error details.\n\nA backup of your old scripts directory is at: " + 
  132.           tempDir.path);
  133.     throw e;
  134.   } finally {
  135.     log("< GM_pointThreeMigrate");
  136.   }
  137. }
  138.  
  139. function GM_versionIsGreaterOrEqual(v1, v2) {
  140.   v1 = v1.split(".");
  141.   v2 = v2.split(".");
  142.  
  143.   if (v1[0] == "") v1[0] = "0";
  144.   if (v2[0] == "") v2[0] = "0";
  145.  
  146.   while (v1.length < v2.length) {
  147.     v1.push("0");
  148.   }
  149.   
  150.   while (v2.length < v1.length) {
  151.     v2.push("0");
  152.   }
  153.   
  154.   var diff;
  155.   for (var i = 0; i < v1.length; i++) {
  156.     diff = parseInt(v1[i]) - parseInt(v2[i]);
  157.     
  158.     if (diff != 0) {
  159.       return diff > 0;
  160.     } else {
  161.       continue;
  162.     }
  163.   }
  164.   
  165.   return 0;
  166. }
  167.  
  168. function GM_getPointThreeScriptDir() {
  169.   var file = getContentDir();
  170.   file.append("scripts");
  171.   return file;
  172. }
  173.  
  174. function GM_getPointThreeScriptFile(fileName) {
  175.   var file = GM_getPointThreeScriptDir();
  176.   file.append(fileName);
  177.   return file;
  178. }
  179.