Skip to content

Commit

Permalink
Update WireLib.GetVersion (#3032)
Browse files Browse the repository at this point in the history
Add WIREVERSION and WIREVESRION_STR E2 constants
  • Loading branch information
Denneisk authored Apr 10, 2024
1 parent 4178d2e commit bc74667
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- name: Embed version
run: |
echo -e "\ncachedversion = \"Canary ${GITHUB_SHA:0:7}\"" >> lua/wire/server/wirelib.lua
echo -e "\nversion = $(date +%y%m%d)\nversion_string = \"Canary $(date +%Y.%m.%d) (${GITHUB_SHA:0:7})\"" >> lua/wire/server/wirelib.lua
- uses: wiremod/gmod-upload@master
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

- name: Embed version
run: |
echo -e "\ncachedversion = \"Workshop ${GITHUB_SHA:0:7}\"" >> lua/wire/server/wirelib.lua
echo -e "\nversion = $(date +%y%m%d)\nversion_string = \"Workshop $(date +%Y.%m.%d) (${GITHUB_SHA:0:7})\"" >> lua/wire/server/wirelib.lua
- uses: wiremod/gmod-upload@master
with:
Expand Down
7 changes: 6 additions & 1 deletion lua/entities/gmod_wire_expression2/core/selfaware.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

local isOwner = E2Lib.isOwner

do
local v1, v2 = WireLib.GetVersion()
E2Lib.registerConstant("WIREVERSION", v1)
E2Lib.registerConstant("WIREVERSION_STR", v2)
end

__e2setcost(1) -- temporary

[nodiscard]
Expand Down Expand Up @@ -244,7 +250,6 @@ e2function number getExtensionStatus(string extension)
return getExtensionStatus(extension) and 1 or 0
end


--[[******************************************************************************]]--

registerCallback("construct", function(self)
Expand Down
67 changes: 45 additions & 22 deletions lua/wire/server/wirelib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1202,17 +1202,19 @@ function WireLib.CalcElasticConsts(Ent1, Ent2)
end


-- Returns a string like "Git f3a4ac3" or "SVN 2703" or "Workshop" or "Extracted"
-- The partial git hash can be plugged into https://github.com/wiremod/wire/commit/f3a4ac3 to show the actual commit
local cachedversion
local version
local version_string
--- Returns the current Wiremod version
---@return number version The version as a number formatted YYMMDD
---@return string version_string A verbose version for printing
function WireLib.GetVersion()
-- If we've already found our version just return that again
if cachedversion then return cachedversion end
if version then return version, version_string end

-- Find what our legacy folder is called
local wirefolder = "addons/wire"
-- Brute force find the wire folder if it's not named wire
if not file.Exists(wirefolder, "GAME") then
for k, folder in pairs(({file.Find("addons/*", "GAME")})[2]) do
for _, folder in pairs(({file.Find("addons/*", "GAME")})[2]) do
if folder:find("wire") and not folder:find("extra") then
wirefolder = "addons/"..folder
break
Expand All @@ -1221,28 +1223,49 @@ function WireLib.GetVersion()
end

if file.Exists(wirefolder, "GAME") then
if file.Exists(wirefolder.."/.git", "GAME") then
cachedversion = "Git "..(file.Read(wirefolder.."/.git/refs/heads/master", "GAME") or "Unknown"):sub(1,7)
elseif file.Exists(wirefolder.."/.svn", "GAME") then
-- Note: This method will likely only detect TortoiseSVN installs
local wcdb = file.Read(wirefolder.."/.svn/wc.db", "GAME") or ""
local start = wcdb:find("/wiremod/wire/!svn/ver/%d+/branches%)")
if start then
cachedversion = "SVN "..wcdb:sub(start+23, start+26)
else
cachedversion = "SVN Unknown"
wirefolder = wirefolder .. "/.git"
if file.Exists(wirefolder, "GAME") then
-- Find where git HEAD is
local head = file.Open(wirefolder .. "/HEAD", "r", "GAME")
if head then
local ref
while not head:EndOfFile() do
local line = head:ReadLine()
if line:StartsWith("ref: ") then
ref = line:sub(6, -2)
end
end
head:Close()
if ref then
-- Generate version string
local path = wirefolder .. "/" .. ref
local name = ref:StartsWith("refs/heads/") and ref:sub(12) or ref
local time = -1
local time_str = "Unknown"
local hash = ""
if file.Exists(path, "GAME") then
local t = file.Time(path, "GAME")
time = tonumber(os.date("%y%m%d", t))
time_str = os.date("%Y.%m.%d", t)
hash = file.Read(path, "GAME"):sub(1, 7)
end

version_string = string.format("Local %s (%s:%s)", time_str, name, hash)
version = time
end
end
else
cachedversion = "Extracted"
end
end

if not cachedversion then cachedversion = "Unknown" end
if not version then
version = -1
version_string = "Unknown"
end

return cachedversion
return version, version_string
end
concommand.Add("wireversion", function(ply,cmd,args)
local text = "Wiremod's version: '"..WireLib.GetVersion().."'"
concommand.Add("wireversion", function(ply)
local text = "Wiremod version: " .. select(2, WireLib.GetVersion())
if IsValid(ply) then
ply:ChatPrint(text)
else
Expand Down

0 comments on commit bc74667

Please sign in to comment.