NYAOSをアップデートするスクリプト(NYAOS 3.1.4_0版)

最新版がNYAOSの更新をするコマンドを修正しました - メモ:wantoraにあります。
2011-01-04 cmd.exeに渡すコマンドの「&&」を「&」に変更。*1

NYAOS 3.1.4_0にてluaからNYAOSのパスとバージョンを取得できるようになったので、それを利用しNYAOSをアップデートするスクリプトを作った - メモ:wantoraを改良しました。

また、_nyaファイルを上書きするか確認するように変更しました。

つかいかた

  1. NYAOSupdate_nyaos とタイプすると新しいバージョンを確認し、あれば更新します。
  2. 実行には wgetunzip が必要なので、無い場合はダウンロードしてPATHを通して下さい。
-- MIT License (http://d.hatena.ne.jp/wantora/20101212/1292141801)

local function wget(url, output)
	return nyaos.eval('wget -nc --user-agent="" -O "'..output..'" "'..url..'" 2>&-')
end

local function confirm(message, chars)
	local key
	
	io.write(message)
	repeat key = nyaos.getkey() until chars:find(key) ~= nil
	io.write(key.."\n")
	return key
end

local function fileread(filename)
	local f = io.open(filename, "rb")
	local body = f:read("*a")
	f:close()
	return body
end

local function check_mod(nyapath, zippath)
	local s, old = pcall(fileread, nyapath.."\\_nya")
	if s == false then return false end
	local new = nyaos.eval('unzip -cp "'..zippath..'" _nya')
	return (old:gsub("%s+$", "") ~= new:gsub("%s+$", ""))
end

local function setupdate(nyapath, zippath)
	local opt = ""
	if check_mod(nyapath, zippath) then
		if confirm("_nyaファイルを上書きしますか?(y/n)", "yn") == "n" then
			opt = "-x _nya"
		end
	end
	nyaos.goodbye.update_nyaos = function()
		nyaos.exec([[%COMSPEC% /c start %COMSPEC% /c "ping localhost -n 2 > nul & unzip -o "]]..zippath..[[" -d "]]..nyapath..[[" ]]..opt..[[ & pause"]])
	end
end

function nyaos.command.update_nyaos()
	local nyapath = nyaos.argv[0]:gsub("\\[^\\]+$", "")
	local html = wget("http://www.nyaos.org/index.cgi?p=NYAOS+3000", "-")
	local url, name, version = html:match(
		'<a href="(/index%.cgi%?p=NYAOS%+3000;f=(nyaos%-(.-)%-win.zip))"')
	
	if html == "" or url == nil then
		print("update_nyaos: ページの取得に失敗しました")
		return
	end
	
	if version:gsub("[._]", "") > nyaos.version:gsub("[._]", "") then
		if confirm(nyaos.version.." から "..version.." に更新しますか?(y/n)", "yn") == "y" then
			local zippath = nyapath.."\\"..name
			wget("http://www.nyaos.org"..url, zippath)
			setupdate(nyapath, zippath)
			print("NYAOS終了時に更新します")
		end
	else
		print("NYAOSは最新版です")
	end
end

*1:「&&」だと途中でコマンドが失敗した場合にその先へ進まなくなってしまう。でも実際にはそんな事はあまり無いと思う。