Skip to content

Instantly share code, notes, and snippets.

@znepb
Created July 6, 2020 02:24
Show Gist options
  • Save znepb/03a279207edbfd82cb821c0d3959c52f to your computer and use it in GitHub Desktop.
Save znepb/03a279207edbfd82cb821c0d3959c52f to your computer and use it in GitHub Desktop.
--[[
CCInstaller
by znepb
Version 1.0
Example configuration: https://gist.github.com/znepb/5f5be3a68faa8b8758ddaa141e253d8c
]]
local repo = "" -- The GitHub repo for your program (user/repo/branch)
local installStart = "" -- Where the installation will start at
local applicationName = "" -- The name of your application
local promptPath = false -- Prompt the user to set a path to install to
local defaultPath = "" -- The path if PromptPath is disabled
local colorSet = { -- Just the colors
backgroundPrimary = colors.white,
backgroundSecondary = colors.lightGray,
text = colors.gray
}
local installFiles = { -- A list of files and folders to be downloaded and installed.
-- Put some files here...
}
--[[ Main Installer Program ]]--
local base = ""
local page = "path"
local w, h = term.getSize()
local current = term.current()
local installStatus = window.create(term.current(), 2, 4, w - 1, h - 4, false)
if not promptPath then
base = defaultPath
end
print("Initalizing installer...")
local function get(url)
local file = http.get(url)
local content = file.readAll()
file.close()
return content
end
local function save(content, path)
local file = fs.open(fs.combine(base, path), "w")
file.write(content)
file.close()
end
local function getBaseInstallPath()
local url = "https://raw.githubusercontent.com/" .. repo .. "/" .. installStart .. "/"
if not installStart then
url = "https://raw.githubusercontent.com/" .. repo .. "/"
end
return url
end
local function startDownloading()
local function downloadTable(files, path)
for i, v in pairs(files) do
if type(v) == "table" then
downloadTable(v, fs.combine(path, i))
elseif type(v) == "string" then
term.redirect(installStatus)
print("Downloading: " .. fs.combine(path, v))
save(get(getBaseInstallPath() .. fs.combine(path, v)), fs.combine(path, v))
end
end
end
downloadTable(installFiles, "")
print("Complete, press any key to exit")
term.redirect(current)
os.pullEvent("key")
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1, 1)
sleep(0.1)
end
sleep(0.1)
local function draw()
term.setBackgroundColor(colorSet.backgroundPrimary)
term.clear()
term.setTextColor(colorSet.text)
if page == "path" then
if promptPath then
term.setCursorPos(2, 2)
term.write("Install Path")
term.setCursorPos(2, 6)
term.write("Press Enter to continue")
paintutils.drawLine(2, 4, w - 1, 4, colorSet.backgroundSecondary)
term.setCursorPos(2, 4)
base = read()
else
term.setCursorPos(2, 2)
term.write("Install " .. applicationName)
term.setCursorPos(2, 4)
term.write("Press Enter to start installation")
while true do
local _, key = os.pullEvent("key")
if key == keys.enter then
break
end
end
base = ""
end
page = "install"
draw()
elseif page == "install" then
term.setCursorPos(2, 2)
term.write("Installing...")
installStatus.setVisible(true)
startDownloading()
end
end
draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment