如何用Git管理TI-Nspire CAS中的Lua源码(tns二进制文件)
我正在学习TI-Nspire计算器的Lua脚本API参考指南,编写了一段用于获取用户输入两个数字并求和的代码,结果会显示在屏幕上:
-- '2.4' is for OS 3.7 -- Our OS is 3.9 so it's good. platform.apiLevel = '2.4' local state = "first_number" local first_number = "" local second_number = "" local FIRST_NUMBER_PROMPT = "First number [enter]: " local SECOND_NUMBER_PROMPT = "Second number [enter]: " function on.paint(gc) gc:drawString(FIRST_NUMBER_PROMPT .. first_number, 8, 62) if state == "second_number" or state == "result" then gc:drawString(SECOND_NUMBER_PROMPT .. second_number, 8, 82) end if state == "result" then if tonumber(first_number) and tonumber(second_number) then local result = tonumber(first_number) + tonumber(second_number) gc:drawString("Addition result: " .. result, 8, 122) else gc:drawString("Error: Invalid input", 8, 122) end end end function on.charIn(ch) if (ch >= "0" and ch <= "9") or (ch == "." and ((state == "first_number" and not string.find(first_number, "%.")) or (state == "second_number" and not string.find(second_number, "%.")))) or ((ch == "+" or ch == "-") and ((state == "first_number" and first_number == "") or (state == "second_number" and second_number == ""))) then if (state == "first_number" and first_number == "") or (state == "second_number" and second_number == "") then if ch == "." then ch = "0." end if ch == "+" or ch == "-" then ch = ch end end if state == "first_number" then first_number = first_number .. ch elseif state == "second_number" then second_number = second_number .. ch end platform.window:invalidate() end end function on.enterKey() if state == "first_number" and first_number ~= "" then state = "second_number" elseif state == "second_number" and second_number ~= "" then state = "result" end platform.window:invalidate() end function on.escapeKey() first_number = "" second_number = "" state = "first_number" platform.window:invalidate() end function on.backspaceKey() if state == "first_number" and first_number ~= "" then first_number = string.sub(first_number, 1, -2) elseif state == "second_number" and second_number ~= "" then second_number = string.sub(second_number, 1, -2) end platform.window:invalidate() end function on.deleteKey() on.backspaceKey() end
我使用TI-Nspire CAS Student Software开发并运行代码,但不知道怎么把Git版本控制系统和该软件结合使用。因为软件会把文件保存成二进制的*.tns文件,想请教如何在这种场景下管理我的Lua源码?
解决方案
由于*.tns是二进制文件,无法被Git高效追踪版本差异,核心思路是将Lua源码与二进制tns文件分离管理,具体操作如下:
单独维护Lua源码文件
不要仅依赖tns文件保存代码,每次编写或修改后,把Lua代码复制到独立的.lua文件中,将这个文件纳入Git仓库管理。所有版本迭代、提交都基于.lua文件,测试时再把代码导入到TI-Nspire软件中。用脚本自动同步源码到tns文件
利用TI-Nspire提供的命令行工具,编写简单的批处理或Shell脚本,实现将.lua文件中的代码自动导入到tns文件的脚本区域。这样可以避免手动复制粘贴的繁琐,同时保证源码与运行文件的一致性。合理处理tns文件
在Git仓库的.gitignore中添加*.tns,避免二进制文件污染仓库。如果需要保存可运行的发布版本,可以单独创建发布分支,或者使用Git LFS来管理tns大文件,但优先保证Lua源码的版本追踪。添加版本溯源标识
在Lua源码的注释中加入Git版本短哈希(可通过git rev-parse --short HEAD获取),这样即使在tns文件运行时,也能快速对应到Git仓库中的具体版本,方便调试和回溯。
内容的提问来源于stack exchange,提问作者Megidd

