self_update.lua

152 lines ยท 4.0 KB

Open raw

eHydra Self-Update System

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/eHydra_wip/self_update.lua
1-- eHydra Self-Update System
2-- Auto-updates all eHydra programs from GitHub repository
3
4local baseUrl = "https://raw.githubusercontent.com/perlytiara/CC-Tweaked-TurtsAndComputers/refs/heads/main/programs/perlytiara/eHydra/"
5
6-- eHydra programs to auto-update
7local eHydraPrograms = {
8 "autoupdater.lua",
9 "batch_updater.lua",
10 "init.lua",
11 "turtle_deployer.lua",
12 "startup.lua",
13 "self_update.lua", -- Self-update capability
14 "README.md"
15}
16
17local function backupFile(filepath)
18 if fs.exists(filepath) then
19 local backupPath = filepath .. ".backup"
20 if fs.exists(backupPath) then
21 fs.delete(backupPath)
22 end
23 fs.copy(filepath, backupPath)
24 print(" ๐Ÿ“ฆ Backed up: " .. filepath)
25 return true
26 end
27 return false
28end
29
30local function restoreFile(filepath)
31 local backupPath = filepath .. ".backup"
32 if fs.exists(backupPath) then
33 if fs.exists(filepath) then
34 fs.delete(filepath)
35 end
36 fs.copy(backupPath, filepath)
37 fs.delete(backupPath)
38 print(" ๐Ÿ”„ Restored: " .. filepath)
39 return true
40 end
41 return false
42end
43
44local function cleanupBackups()
45 print("Cleaning up backup files...")
46 for _, filename in ipairs(eHydraPrograms) do
47 local backupPath = filename .. ".backup"
48 if fs.exists(backupPath) then
49 fs.delete(backupPath)
50 print(" ๐Ÿ—‘๏ธ Removed backup: " .. backupPath)
51 end
52 end
53end
54
55print("eHydra Self-Update System v1.0")
56print("==============================")
57print("This will update all eHydra programs from GitHub")
58print()
59
60write("Proceed with update? (y/n) [y]: ")
61local confirm = string.lower(read())
62if confirm == "n" then
63 print("Update cancelled.")
64 return
65end
66
67print()
68print("Starting eHydra self-update...")
69print("Repository: " .. baseUrl)
70print("Files to update: " .. #eHydraPrograms)
71print()
72
73local success = 0
74local failed = 0
75local backups = {}
76
77-- Create backups first
78print("๐Ÿ“ฆ Creating backups...")
79for _, filename in ipairs(eHydraPrograms) do
80 if backupFile(filename) then
81 table.insert(backups, filename)
82 end
83end
84
85print()
86print("๐Ÿ“ฅ Downloading updates...")
87
88for i, filename in ipairs(eHydraPrograms) do
89 print("[" .. i .. "/" .. #eHydraPrograms .. "] Updating " .. filename .. "...")
90
91 local url = baseUrl .. filename
92 local response = http.get(url)
93
94 if response then
95 -- Delete existing file
96 if fs.exists(filename) then
97 fs.delete(filename)
98 end
99
100 -- Write new content
101 local file = fs.open(filename, "w")
102 if file then
103 file.write(response.readAll())
104 file.close()
105 response.close()
106 print(" โœ… " .. filename .. " updated successfully")
107 success = success + 1
108 else
109 print(" โŒ " .. filename .. " - failed to write file")
110 -- Restore from backup
111 restoreFile(filename)
112 failed = failed + 1
113 end
114 else
115 print(" โŒ " .. filename .. " - download failed")
116 -- Restore from backup
117 restoreFile(filename)
118 failed = failed + 1
119 end
120end
121
122print()
123print("๐Ÿ“Š Update Summary")
124print("================")
125print("โœ… Successfully updated: " .. success .. " files")
126print("โŒ Failed updates: " .. failed .. " files")
127
128if failed == 0 then
129 print("๐ŸŽ‰ All updates completed successfully!")
130 cleanupBackups()
131else
132 print("โš ๏ธ Some updates failed - backups preserved")
133 print(" To restore all backups, run: restore_backups")
134end
135
136print()
137if success > 0 then
138 print("๐Ÿ”„ eHydra system has been updated!")
139 print(" Restart any running eHydra programs to use new versions")
140
141 -- Offer to restart
142 print()
143 write("Restart eHydra startup menu? (y/n) [n]: ")
144 local restart = string.lower(read())
145 if restart == "y" then
146 print("Restarting eHydra...")
147 shell.run("startup")
148 end
149else
150 print("No files were updated.")
151end
152