/
romost
/
flange
Обзор
Документация
Войти
/
romost
/
flange
Код
Запросы
0
Пакеты
0
Релизы
1
Аналитика
Безопасность
master
src/update_version.lua
97 строк
3 KB
romost
add type02, 03, 04
25 фев 2025, 17:48
25 фев 2025, 17:48
5830f3e
Код
Авторство
О чём код?
local M = {} local CONFIG = { version_file = "src/parameters.json", backup = true, version_pattern = '"version":%%s*"%%d+.%%d+.%%d+"' } function M.parse_args() local args = {} for i = 1, #arg do args[i] = arg[i] end local action = "patch" local custom_version for i, a in ipairs(args) do if a == "--set" then custom_version = args[i+1] return "set", custom_version elseif ({major = true, minor = true, patch = true})[a] then action = a end end return action, nil end function M.create_backup() if CONFIG.backup then local backup_file = CONFIG.version_file .. ".bak" local cmd = string.format("copy %s %s > nul", CONFIG.version_file, backup_file) os.execute(cmd) end end function M.read_file() local file = io.open(CONFIG.version_file, "r") if not file then error("File not found: "..CONFIG.version_file) end local content = file:read("*a") file:close() return content end function M.find_version(content) local pattern = string.format('"metadata":%%s*{.-'..CONFIG.version_pattern, '') local version_str = content:match(pattern) if not version_str then error("Version not found") end local major, minor, patch = version_str:match("(%d+)%.(%d+)%.(%d+)") return tonumber(major), tonumber(minor), tonumber(patch) end function M.update_version(action, major, minor, patch, custom_version) if custom_version then local new_major, new_minor, new_patch = custom_version:match("^(%d+)%.(%d+)%.(%d+)$") if not new_major then error("Incorrect version format") end return tonumber(new_major), tonumber(new_minor), tonumber(new_patch) end if action == "major" then return major + 1, 0, 0 elseif action == "minor" then return major, minor + 1, 0 else return major, minor, patch + 1 end end function M.write_new_version(new_version) local content = M.read_file() local pattern = string.format(CONFIG.version_pattern, '') local new_content = content:gsub(pattern, string.format('"version": "%s"', new_version), 1) local file = io.open(CONFIG.version_file, "w") if not file then error("Recording error") end file:write(new_content) file:close() end function M.main() local action, custom_version = M.parse_args() M.create_backup() local major, minor, patch = M.find_version(M.read_file()) local new_major, new_minor, new_patch = M.update_version(action, major, minor, patch, custom_version) local new_version = string.format("%d.%d.%d", new_major, new_minor, new_patch) M.write_new_version(new_version) print(string.format("The RST version has been updated: %s -> %s", string.format("%d.%d.%d", major, minor, patch), new_version)) end local ok, err = pcall(M.main) if not ok then print("Error: "..err) os.exit(1) end return M