Skip to content

Commit 860f8f2

Browse files
authored
Merge pull request #17 from mc-cc-scripts/issue-11/check-version
add version check to SCM
2 parents e9cce76 + be98481 commit 860f8f2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

scm.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ scm.config = {
1414
["infoFile"] = "files.txt", -- provides the structure of a git repo (paths to all files)
1515
["apiGithubURL"] = "https://api.github.com/orgs/",
1616
["apiGithubGetRepos"] = "/repos?type=all&per_page=100&page=1",
17+
["apiGithubGetTags"] = "https://api.github.com/repos/<USER>/<REPO>/tags",
1718
["installScript"] = "1kKZ8zTS",
1819
-- Local Settings
20+
["currentVersion"] = "0.0.0", -- will get the newest version through the github api, no need to update here
21+
["updateAvailable"] = false,
22+
["lastVersionCheck"] = "1",
1923
["programDirectory"] = "progs/",
2024
["libraryDirectory"] = "libs/",
2125
["configDirectory"] = "config/",
@@ -679,6 +683,13 @@ end
679683
function scm:updateSCM ()
680684
scm:log("Updating scm...")
681685
shell.run("pastebin", "run", self.config.installScript)
686+
local success, version = self:getNewestVersion()
687+
if success then
688+
self.config["currentVersion"] = version
689+
self.config["updateAvailable"] = false
690+
self.config["lastVersionCheck"] = os.day("utc")
691+
self:saveConfig()
692+
end
682693
end
683694

684695
---@source: https://stackoverflow.com/a/2705804/10495683
@@ -878,6 +889,36 @@ function scm:load(name)
878889
return fallbackRequire(name)
879890
end
880891

892+
function scm:getNewestVersion ()
893+
local githubAPIgetTags = self.config["apiGithubGetTags"]
894+
githubAPIgetTags = githubAPIgetTags:gsub("<USER>", self.config["user"])
895+
githubAPIgetTags = githubAPIgetTags:gsub("<REPO>", self.config["repository"])
896+
897+
local request = http.get(githubAPIgetTags)
898+
899+
if request then
900+
local content = request.readAll()
901+
request.close()
902+
local scmTags = textutils.unserializeJSON(content)
903+
return true, scmTags[1]["name"]
904+
else
905+
self:log("Request to GitHub API failed.")
906+
return false, "0.0.0"
907+
end
908+
end
909+
910+
function scm:checkVersion ()
911+
if not self.config["updateAvailable"] and self.config["lastVersionCheck"] ~= '' .. os.day("utc") then
912+
local success, newestVersion = scm:getNewestVersion()
913+
if success and newestVersion ~= self.config["currentVersion"] then
914+
self.config["updateAvailable"] = true
915+
end
916+
917+
self.config["lastVersionCheck"] = os.day("utc") .. ''
918+
self:saveConfig()
919+
end
920+
end
921+
881922
function scm:init ()
882923
-- Create directories
883924
if not fs.exists(self.config["configDirectory"]) then
@@ -889,6 +930,8 @@ function scm:init ()
889930

890931
self:loadConfig()
891932
self:loadScripts()
933+
934+
self:checkVersion()
892935
end
893936

894937
---@param resetPosition boolean | nil
@@ -923,6 +966,11 @@ function scm:cli (resetPosition, args)
923966
term.blit(" Type `scm help` to learn more. ","77777777ffffffff7777777777777777","44444444444444444444444444444444")
924967
term.setCursorPos(1, cursorY)
925968
term.scroll(1)
969+
if (self.config["updateAvailable"]) then
970+
term.blit(" Update available! ","7eeeeeeeeeeeeeeeee77777777777777","44444444444444444444444444444444")
971+
term.setCursorPos(1, cursorY)
972+
term.scroll(1)
973+
end
926974
term.blit(" ","ffffffffffffffffffffffffffffffff","44444444444444444444444444444444")
927975
term.setCursorPos(1, cursorY)
928976
term.scroll(2)

0 commit comments

Comments
 (0)