Firefoxでクリックしたファイルをpatchあてる

そのへんにぺろっと貼ってあるgreasemonkeyスクリプトにインストールボタンを付けるgreasemonkeyスクリプトなかんじにローカルファイルにパッチをあてたい。めんどくさいもんよー。


nsIProcessを使ってプロセスを実行できる。
のはいいけど実行する以外何もできなくてstdin/stdoutとかとれない。

リダイレクトとかもできないのでシェルスクリプトを作って、その中に好きにスクリプト書いて、実行するのは/bin/shにして、その引数に書いたシェルスクリプトを渡すとよい。Windowsでもたぶん.batにいろいろ書いてそれをcmd.exeの引数として渡して実行すればいけるんじゃないかな。コマンドラインでちゃったりするのかな。あのへんよくわかんない。


patchといえば、DOSの改行のやつ(tomblooのことなんだけど)をOSXで編集してdiffをとるとdiffファイルの改行がLFだけになってて、それをpatchでパッチあてようとすると元のDOS改行のファイルにdiffのUNIX改行になってるやつのパッチ元の行が見つかんなくなっちゃってパッチあてられなくなる。改行コードあわせればいける(nkf --overwrite -Lw)んだけどめんどくさー!

#!/bin/sh

cd $1
/usr/bin/patch < $1/ippatch.tmp > $1/ippatch.log 2>&1

jsactionsでpatchのファイルをクリックして実行。diffはfirefoxのプロファイルディレクトリからのパスで作っておく。


function log(){

	Firebug.Console.log(_jsaCScript);

	if(typeof (FirebugConsole) != 'undefined' && typeof (FirebugContext) != 'undefined') {
	var console = new FirebugConsole(FirebugContext, content);
		console.log.apply(console, arguments);
	} else if ( Firebug && Firebug.Console ) {
		// Firebug 1.2~
		Firebug.Console.logFormatted.call(Firebug.Console, Array.slice(arguments), FirebugContext, 'log');
	} else if (Application && Application.Console) {
		Application.Console.log.apply(null, arguments);
		return false;
	}
	return true;
}
var hybridListener = {
	data: "",
	onStartRequest: function ( request, context ) {
		this.data = "";
	},
	onDataAvailable: function ( request, context ,  inputStream ,  offset ,  count)  {
		var bs = Cc["@mozilla.org/binaryinputstream;1"]
					.createInstance(Ci.nsIBinaryInputStream);
		bs.setInputStream(inputStream);
		var n =  bs.available();
		var bytes = bs.readBytes(n);
		this.data += bytes;
		this.os.write(bytes, n);
		bs.close();
	},
	onStopRequest: function ( request, context ,  statusCode ) {
		this.os.close();
		this.callback.apply(this);
	}
};

function save( uri, localfile, onComplete ) {
	var ios = Cc["@mozilla.org/network/io-service;1"]
					.getService(Components.interfaces.nsIIOService);
	var dm = Cc["@mozilla.org/download-manager;1"]
				.getService(Components.interfaces.nsIDownloadManager);
	var sourceUri = ios.newURI(uri, null, null);

	var os = Components.classes["@mozilla.org/network/file-output-stream;1"]
		.createInstance(Components.interfaces.nsIFileOutputStream);
	os.init(localfile, 0x04 | 0x08 | 0x20, 0664, 0); // write, create, truncate
	hybridListener.os = os;
	hybridListener.callback = onComplete;

	var sourceUri = ios.newURI(uri, null, null);
	var channel = ios.newChannelFromURI( sourceUri );
	channel.asyncOpen(hybridListener, null);
}

function run (patchUri) {
	var ios = Components.classes["@mozilla.org/network/io-service;1"]
								.getService(Components.interfaces.nsIIOService);

	var file = Components.classes["@mozilla.org/file/directory_service;1"]
							.getService(Components.interfaces.nsIProperties)
							.get("ProfD", Components.interfaces.nsILocalFile);
	file.append( 'ippatch.tmp' );

	save(patchUri, file, function () {
		try {
			//log(this.data);
			var profileDir = Components.classes["@mozilla.org/file/directory_service;1"]
								.getService(Components.interfaces.nsIProperties)
								.get("ProfD", Components.interfaces.nsILocalFile);

			var filesToPatch = this.data.match( /^--- (\S+)/mg ).map( function (filename) {
				return( [profileDir.path, filename.replace(/^--- /, '') ].join("/") );
			} );
			log("files to be patched", filesToPatch);
			
			var sh = Components.classes["@mozilla.org/file/local;1"]
				.createInstance(Components.interfaces.nsILocalFile);
			sh.initWithPath("/bin/sh");
			var process = Components.classes["@mozilla.org/process/util;1"]
				.createInstance(Components.interfaces.nsIProcess);
			process.init(sh);

			var args = [
				"/home/kuma/patch.sh",
				profileDir.path
			];
			var t = process.run(false, args, args.length);

			// print log.
			var filename = profileDir.path + "/ippatch.log";
			var output = getContents( filename );
			log(output);
			
		}catch(e) {
			log(e);
		}
		
	});
}

function getContents(filename) {
	var ios = Cc["@mozilla.org/network/io-service;1"]
					.getService(Components.interfaces.nsIIOService);
	 var ss = Components
	     .classes["@mozilla.org/scriptableinputstream;1"]
		     .getService(Components.interfaces.nsIScriptableInputStream);

	var localfile = Components.classes["@mozilla.org/file/local;1"] .createInstance(Components.interfaces.nsILocalFile);
	localfile.initWithPath( filename );
	var uri = ios.newFileURI(localfile);
	var channel = ios.newChannelFromURI(uri);
	var input=channel.open();
	ss.init(input);
	var str = ss.read(input.available());
	ss.close();
	input.close();
	return (str)
}

//getContents('/home/kuma/Desktop/tombloo-firebug-0.1.2.patch');


var u = (gContextMenu.link.href);
run(u)