Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Package Manager for CC Tweaked
- local packages = "/disk/packages/packages.json" -- Path to store installed packages
- local packageDir = "/disk/packages/" -- Path to store downloaded programs
- -- Ensure the package directory exists
- if not fs.exists(packageDir) then
- fs.makeDir(packageDir)
- end
- -- Helper to load package data
- local function loadPackages()
- if fs.exists(packages) then
- local file = fs.open(packages, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data or {}
- end
- return {}
- end
- -- Helper to save package data
- local function savePackages(data)
- local file = fs.open(packages, "w")
- file.write(textutils.serialize(data))
- file.close()
- end
- -- Install a package by Pastebin ID
- local function installPackage(pastebinId, name)
- local programPath = packageDir .. name
- if fs.exists(programPath) then
- print("Package already installed!")
- return
- end
- -- Download from Pastebin
- shell.run("pastebin get " .. pastebinId .. " " .. programPath)
- if fs.exists(programPath) then
- print("Package installed:", name)
- -- Update package metadata
- local pkgData = loadPackages()
- pkgData[name] = { id = pastebinId, version = "1.0" } -- You can add versioning later
- savePackages(pkgData)
- else
- print("Failed to download the package.")
- end
- end
- -- Remove a package
- local function removePackage(name)
- local programPath = packageDir .. name
- if fs.exists(programPath) then
- fs.delete(programPath)
- -- Update package metadata
- local pkgData = loadPackages()
- pkgData[name] = nil
- savePackages(pkgData)
- print("Package removed:", name)
- else
- print("Package not found.")
- end
- end
- -- List installed packages
- local function listPackages()
- local pkgData = loadPackages()
- if next(pkgData) == nil then
- print("No packages installed.")
- return
- end
- for name, data in pairs(pkgData) do
- print(name .. " - Version: " .. data.version .. " - Pastebin ID: " .. data.id)
- end
- end
- -- Show a basic progress bar
- local function progressBar(current, total)
- local percent = math.floor((current / total) * 100)
- local bar = "["
- for i = 1, 20 do
- if i <= (percent / 5) then
- bar = bar .. "="
- else
- bar = bar .. " "
- end
- end
- bar = bar .. "] " .. percent .. "%"
- term.clearLine()
- term.write(bar)
- end
- -- Update all packages with a nice screen
- local function updatePackages()
- local pkgData = loadPackages()
- if next(pkgData) == nil then
- print("No packages to update.")
- return
- end
- local total = #pkgData
- local count = 0
- print("Starting update of all packages...\n")
- -- Remove all installed packages
- print("Step 1: Removing old packages\n")
- for name, data in pairs(pkgData) do
- count = count + 1
- print("Removing package: " .. name)
- removePackage(name)
- sleep(0.2) -- Simulate process time
- progressBar(count, total)
- end
- print("\n\nAll packages removed.\n")
- -- Reinstall all packages from Pastebin
- print("Step 2: Reinstalling packages\n")
- count = 0
- for name, data in pairs(pkgData) do
- count = count + 1
- print("Reinstalling package: " .. name)
- installPackage(data.id, name)
- sleep(0.2) -- Simulate process time
- progressBar(count, total)
- end
- print("\n\nUpdate completed! All packages are reinstalled.")
- end
- -- Command handler
- local args = {...}
- if #args < 1 then
- print("Usage: pkg <command> [args]")
- print("Commands: install <pastebin_id> <name>, remove <name>, list, update")
- return
- end
- local command = args[1]
- if command == "install" and #args == 3 then
- installPackage(args[2], args[3])
- elseif command == "remove" and #args == 2 then
- removePackage(args[2])
- elseif command == "list" then
- listPackages()
- elseif command == "update" then
- updatePackages()
- else
- print("Invalid command or arguments.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement