feat(包管理): 添加创建新包的功能并更新文档

添加pkg init命令用于创建新包,包括生成package.json和主代码文件模板
新增storage命令及相关帮助文档
更新安装器版本号并改进GUI事件处理
This commit is contained in:
2025-09-03 15:11:27 +08:00
parent c91c989b57
commit 0ffb590516
6 changed files with 262 additions and 3 deletions

View File

@@ -592,10 +592,14 @@ end
function GUIManager:handleEvents() function GUIManager:handleEvents()
while self.running do while self.running do
-- Safely get the pullEvent function -- Safely get the pullEvent function with fallback to rc.pullEvent
local pullEventFunc = os.pullEvent local pullEventFunc = os.pullEvent
if type(pullEventFunc) ~= "function" then if type(pullEventFunc) ~= "function" then
error("os.pullEvent is not a function") -- Try using rc.pullEvent as fallback
pullEventFunc = rc and rc.pullEvent
if type(pullEventFunc) ~= "function" then
error("Neither os.pullEvent nor rc.pullEvent is available")
end
end end
-- Get the next event -- Get the next event

View File

@@ -0,0 +1,83 @@
=== LeonOS Package Manager ===
The `pkg` command is LeonOS's lightweight package manager, allowing you to install, update, remove, and manage software packages.
== Basic Usage ==
>>color yellow
pkg <command> [options]
>>color white
== Available Commands ==
- **install <package>**: Install a package
- **update <package>**: Update a package (leave empty to update all)
- **remove <package>**: Remove a package
- **list**: List all installed packages
- **search <query>**: Search for packages
- **info <package>**: Show package information
- **init <package>**: Create a new package
- **help**: Show help message
== Command Options ==
The following options are available for most commands:
- `-f`, `--force`: Force operation (install, update)
- `-l`, `--local`: Install from local file
- `-v`, `--verbose`: Show detailed output
- `-h`, `--help`: Show help information
== Usage Examples ==
1. Install a package:
>>color yellow
pkg install example-pkg
>>color white
2. Force install a package:
>>color yellow
pkg install --force example-pkg
>>color white
3. Update all packages:
>>color yellow
pkg update
>>color white
4. Remove a package:
>>color yellow
pkg remove example-pkg
>>color white
5. List installed packages:
>>color yellow
pkg list
>>color white
6. Search for packages:
>>color yellow
pkg search editor
>>color white
7. Show package information:
>>color yellow
pkg info example-pkg
>>color white
8. Create a new package:
>>color yellow
pkg init my-new-pkg
>>color white
== Package Creation ==
When creating a new package with `pkg init <package>`, the following structure is created:
/packages/
<package>/
1.0.0/
package.json - Package metadata
<package>.lua - Main package file
You can edit these files to customize your package before installing it.

View File

@@ -0,0 +1,23 @@
=== Storage Command ===
Description:
Displays storage information for the computer, including total space, used space,
free space, and storage devices.
Usage:
storage [options]
Options:
--help, -h Show this help message
--verbose, -v Show detailed storage information
Examples:
storage
Display basic storage information
storage --verbose
Display detailed storage information including all drives
Tips:
- Use the 'delete' command to free up space on your computer.
- Regularly check storage usage to prevent running out of space.

View File

@@ -31,6 +31,86 @@ local pkg_config = {
cache_dir = "/packages/cache" -- 缓存目录 cache_dir = "/packages/cache" -- 缓存目录
} }
-- 创建新包
local function create_package(pkg_name)
print("Creating new package: " .. pkg_name)
-- 确保包目录存在
local pkg_version = "1.0.0"
local pkg_dir = fs.combine(pkg_config.local_pkg_dir, pkg_name)
local version_dir = fs.combine(pkg_dir, pkg_version)
if not fs.exists(pkg_dir) then
fs.makeDir(pkg_dir)
end
if not fs.exists(version_dir) then
fs.makeDir(version_dir)
end
-- 创建package.json文件
local package_json = {
name = pkg_name,
version = pkg_version,
description = "A new package for LeonOS",
author = "LeonOS User",
license = "MIT",
dependencies = {},
files = {
pkg_name .. ".lua"
}
}
local json_file = io.open(fs.combine(version_dir, "package.json"), "w")
if json_file then
json_file:write(textutils.serializeJSON(package_json, true))
json_file:close()
print("Created package.json")
else
print("Error: Failed to create package.json")
return false
end
-- 创建主代码文件
local main_file_path = fs.combine(version_dir, pkg_name .. ".lua")
local main_file = io.open(main_file_path, "w")
if main_file then
local main_file_content = [[-- ]] .. pkg_name .. [[ Package
local colors = require('colors')
local term = require('term')
function drawTopBar()
local w, h = term.getSize()
term.setBackgroundColor(colors.cyan)
term.setTextColor(colors.white)
term.setCursorPos(1, 1)
term.clearLine()
local title = "=== ]] .. pkg_name .. [[ v]] .. pkg_version .. [[ ==="
local pos = math.floor((w - #title) / 2) + 1
term.setCursorPos(pos, 1)
term.write(title)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.setCursorPos(1, 3)
end
drawTopBar()
print("\nThis is the ]] .. pkg_name .. [[ package for LeonOS.")
print("\nUsage:")
print(" pkg install ]] .. pkg_name .. [[ - Install this package")
print(" pkg remove ]] .. pkg_name .. [[ - Uninstall this package")
print(" pkg list - List installed packages")
]]
main_file:write(main_file_content)
main_file:close()
print("Created ]] .. pkg_name .. [[.lua")
else
print("Error: Failed to create ]] .. pkg_name .. [[.lua")
return false
end
print("Package created successfully at: " .. version_dir)
print("You can now edit the package files and install it with 'pkg install ]] .. pkg_name .. [['")
return true
end
-- 确保必要的目录存在 -- 确保必要的目录存在
local function ensure_dirs() local function ensure_dirs()
if not fs.exists(pkg_config.local_pkg_dir) then if not fs.exists(pkg_config.local_pkg_dir) then
@@ -80,6 +160,7 @@ local function show_help()
print(" list List all installed packages") print(" list List all installed packages")
print(" search <query> Search for packages") print(" search <query> Search for packages")
print(" info <package> Show package information") print(" info <package> Show package information")
print(" init <package> Create a new package")
print(" help Show this help message") print(" help Show this help message")
print("") print("")
print("Options:") print("Options:")
@@ -378,6 +459,13 @@ local function main(args)
else else
show_package_info(pkg_args[1]) show_package_info(pkg_args[1])
end end
elseif command == "init" then
if #pkg_args < 1 then
print("Error: Missing package name.")
show_help()
else
create_package(pkg_args[1])
end
elseif command == "help" then elseif command == "help" then
show_help() show_help()
else else

View File

@@ -0,0 +1,61 @@
-- LeonOS storage command
local rc = require("rc")
local textutils = require("textutils")
local term = require("term")
local colors = require("colors")
local fs = require("fs")
-- 保存当前颜色设置
local old_fg = term.getTextColor()
local old_bg = term.getBackgroundColor()
-- 设置名称栏颜色并显示
term.setTextColor(colors.white)
term.setBackgroundColor(colors.cyan)
term.at(1, 1).clearLine()
term.at(1, 1).write("=== Storage Information ===")
-- 恢复颜色设置
term.setTextColor(old_fg)
term.setBackgroundColor(old_bg)
term.at(1, 2)
-- 获取存储信息
local total_space = fs.getSize("/")
local free_space = fs.getFreeSpace("/")
local used_space = total_space - free_space
-- 格式化存储容量转换为MB
local function formatSize(bytes)
return string.format("%.2f MB", bytes / 1024 / 1024)
end
-- 显示存储信息
textutils.coloredPrint(colors.yellow, "Total Space:", colors.white, formatSize(total_space))
textutils.coloredPrint(colors.yellow, "Used Space:", colors.white, formatSize(used_space))
textutils.coloredPrint(colors.yellow, "Free Space:", colors.white, formatSize(free_space))
-- 显示存储空间使用百分比
local usage_percent = (used_space / total_space) * 100
textutils.coloredPrint(colors.yellow, "Usage:", colors.white, string.format("%.1f%%", usage_percent))
-- 显示存储设备信息(如果可用)
local function getDriveInfo()
local drives = fs.list("/")
if drives and #drives > 0 then
textutils.coloredPrint(colors.yellow, "Storage Devices:", colors.white)
for _, drive in ipairs(drives) do
if fs.isDir("/" .. drive) and drive ~= "rom" and drive ~= "tmp" then
local drive_size = fs.getSize("/" .. drive)
print(string.format(" - %s: %s", drive, formatSize(drive_size)))
end
end
end
end
getDriveInfo()
-- 提示信息
term.setTextColor(colors.green)
print("\nTip: Use 'delete' command to free up space.")
term.setTextColor(colors.white)

View File

@@ -1,5 +1,5 @@
-- LeonOS installer -- LeonOS installer
local INSTALLER_VERSION = "0.3.8 Beta 4" local INSTALLER_VERSION = "0.3.8 Beta 4 Alpha 1"
local DEFAULT_ROM_DIR = "/leonos" local DEFAULT_ROM_DIR = "/leonos"
print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...") print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")