Created
April 4, 2015 08:33
-
-
Save formula1/95c1e2ad32a891ebff83 to your computer and use it in GitHub Desktop.
Lazy Path Registration
This file contains hidden or 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 c = require("child_process"); | |
var _ = require("lazy.js"); | |
var fs = require("fs"); | |
var matches = /^\s*(LIB|LIBPATH|PATH|PlatformToolset|sdkdir|TARGET_PLATFORM|VS\d{3}COMNTOOLS|WindowsSDKDir)=/; | |
var HOME = require("osenv").home(); | |
if(process.argv[2] && process.argv[2].toString("utf8") === "uninstall"){ | |
uninstall(); | |
}else{ | |
install(); | |
} | |
function uninstall(next){ | |
var stored = JSON.parse(fs.readFileSync(HOME+"\\CodeSmith\\uninstall\\addedPath.json","utf8")); | |
console.log("iterating"); | |
_(c.execSync("SET").toString("utf8")) | |
.split("\r\n") | |
.filter(function(str){ | |
return matches.test(str); | |
}).each(function(val){ | |
val = val.split("="); | |
if(typeof stored[val[0]] === "undefined") return; | |
ori = stored[val[0]]; | |
val[1] = _(val[1]) | |
.split(";") | |
.without(ori.concat(["","\n","\r\n","\r"])) | |
.toArray(); | |
console.log(val[1]); | |
console.log(ori); | |
c.execSync( | |
"SetX "+ | |
val[0]+" \""+val[1]+"\"" | |
); | |
}); | |
fs.unlink(HOME+"\\CodeSmith\\uninstall\\addedPath.json",function(err){ | |
if(err) throw err; | |
fs.rmdirSync(HOME+"\\CodeSmith\\uninstall"); | |
fs.rmdirSync(HOME+"\\CodeSmith"); | |
console.log("uninstalled"); | |
next(); | |
}); | |
} | |
function install(){ | |
var stats; | |
var new_ws = false; | |
try{ | |
stats = fs.statSync(HOME+"\\CodeSmith\\uninstall\\addedPath.json"); | |
}catch(e){ | |
//If the file doesn't exist, we can do this fresh. | |
} | |
if(stats && stats.size > 0){ | |
return uninstall(install); | |
} | |
try{ | |
stats = fs.statSync(HOME+"\\CodeSmith"); | |
if(!stats.isDirectory()){ | |
fs.unlinkSync(HOME+"\\CodeSmith"); | |
throw new Error("was not directory"); | |
} | |
}catch(e){ | |
fs.mkdir(HOME+"\\CodeSmith"); | |
} | |
try{ | |
stats = fs.statSync(HOME+"\\CodeSmith\\uninstall"); | |
if(!stats.isDirectory()){ | |
fs.unlinkSync(HOME+"\\CodeSmith\\uninstall"); | |
throw new Error("was not a directory"); | |
} | |
}catch(e){ | |
fs.mkdir(HOME+"\\CodeSmith\\uninstall"); | |
} | |
new_ws = fs.createWriteStream(HOME+"\\CodeSmith\\uninstall\\addedPath.json"); | |
new_ws.started = true; | |
new_ws.write("{\n"); | |
var cmd = 'C:\\Program" "Files\\Microsoft" "SDKs\\Windows\\v7.1\\Bin\\SetEnv.cmd'; | |
var orig = _( | |
c.execSync("SET").toString("utf8") | |
).split("\r\n").filter(function(str){ | |
return matches.test(str); | |
}).map(function(val){ | |
val = val.split("="); | |
val[1] = val[1].split(";"); | |
return val; | |
}).toObject(); | |
_( | |
c.execSync(cmd+" & SET").toString("utf8") | |
).split("\r\n").filter(function(str){ | |
return matches.test(str); | |
}).each(function(val){ | |
val = val.split("="); | |
var ori = orig[val[0]]||[]; | |
val[1] = _(val[1]) | |
.split(";") | |
.without(ori.concat(["","\n","\r\n","\r"])) | |
.toArray(); | |
console.log(val[1]); | |
console.log(ori); | |
if(new_ws.started){ | |
new_ws.started = false | |
}else{ | |
new_ws.write(",\r\n"); | |
} | |
new_ws.write("\""+val[0]+"\":"+JSON.stringify(val[1])); | |
c.execSync("SetX "+val[0]+" \""+ori.concat(val[1]).join(";")+"\""); | |
}); | |
new_ws.end("\n}"); | |
console.log("installed"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment