silentstudio 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- silentstudio/__init__.py +3 -0
- silentstudio/__main__.py +6 -0
- silentstudio/cli.py +149 -0
- silentstudio/config.py +98 -0
- silentstudio/installer.py +164 -0
- silentstudio/list_packages.py +111 -0
- silentstudio/project_new.py +157 -0
- silentstudio/utils.py +88 -0
- silentstudio/window.py +218 -0
- silentstudio-0.1.0.dist-info/METADATA +449 -0
- silentstudio-0.1.0.dist-info/RECORD +14 -0
- silentstudio-0.1.0.dist-info/WHEEL +5 -0
- silentstudio-0.1.0.dist-info/entry_points.txt +2 -0
- silentstudio-0.1.0.dist-info/top_level.txt +1 -0
silentstudio/__init__.py
ADDED
silentstudio/__main__.py
ADDED
silentstudio/cli.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
import argparse
|
|
3
|
+
import sys
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
from . import __version__
|
|
7
|
+
from .config import load_config
|
|
8
|
+
from .utils import get_system_language
|
|
9
|
+
from .installer import handle_install
|
|
10
|
+
from .list_packages import handle_list_packages
|
|
11
|
+
from .project_new import handle_new
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def show_help(lang: str) -> None:
|
|
15
|
+
"""显示帮助信息"""
|
|
16
|
+
if lang == "zh-CN":
|
|
17
|
+
print(f"""
|
|
18
|
+
SilentStudio CLI v{__version__} - 让开发者体验更好一点
|
|
19
|
+
主页: https://github.com/Silent-Studio-CN/SilentStudioCLI
|
|
20
|
+
|
|
21
|
+
用法:
|
|
22
|
+
silentstudio 显示此帮助信息
|
|
23
|
+
silentstudio -v, --version 显示版本号
|
|
24
|
+
silentstudio -w, --window 启动 GUI 界面
|
|
25
|
+
silentstudio new <name> -l <lang> 创建新项目
|
|
26
|
+
silentstudio new <name> --lang <lang>创建新项目
|
|
27
|
+
silentstudio <pkg_name> 查询并安装生态库
|
|
28
|
+
silentstudio install list 列出已安装的库
|
|
29
|
+
|
|
30
|
+
示例:
|
|
31
|
+
silentstudio new my-app -l java
|
|
32
|
+
silentstudio slimemold
|
|
33
|
+
silentstudio install list
|
|
34
|
+
silentstudio -w
|
|
35
|
+
|
|
36
|
+
更多信息: https://github.com/Silent-Studio-CN/SilentStudioCLI
|
|
37
|
+
""")
|
|
38
|
+
else:
|
|
39
|
+
print(f"""
|
|
40
|
+
SilentStudio CLI v{__version__} - Making developer experience better
|
|
41
|
+
Home: https://github.com/Silent-Studio-CN/SilentStudioCLI
|
|
42
|
+
|
|
43
|
+
Usage:
|
|
44
|
+
silentstudio Show this help
|
|
45
|
+
silentstudio -v, --version Show version
|
|
46
|
+
silentstudio -w, --window Launch GUI
|
|
47
|
+
silentstudio new <name> -l <lang> Create new project
|
|
48
|
+
silentstudio new <name> --lang <lang> Create new project
|
|
49
|
+
silentstudio <pkg_name> Query and install package
|
|
50
|
+
silentstudio install list List installed packages
|
|
51
|
+
|
|
52
|
+
Examples:
|
|
53
|
+
silentstudio new my-app -l java
|
|
54
|
+
silentstudio slimemold
|
|
55
|
+
silentstudio install list
|
|
56
|
+
silentstudio -w
|
|
57
|
+
|
|
58
|
+
More info: https://github.com/Silent-Studio-CN/SilentStudioCLI
|
|
59
|
+
""")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def main():
|
|
63
|
+
"""主入口"""
|
|
64
|
+
lang = get_system_language()
|
|
65
|
+
|
|
66
|
+
# 检查是否有参数
|
|
67
|
+
if len(sys.argv) == 1:
|
|
68
|
+
show_help(lang)
|
|
69
|
+
return 0
|
|
70
|
+
|
|
71
|
+
# 解析参数
|
|
72
|
+
parser = argparse.ArgumentParser(
|
|
73
|
+
prog="silentstudio",
|
|
74
|
+
description="SilentStudio CLI - 让开发者体验更好一点" if lang == "zh-CN" else "SilentStudio CLI - Making developer experience better",
|
|
75
|
+
add_help=False # 自定义帮助
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
# 全局选项
|
|
79
|
+
parser.add_argument("-v", "--version", action="store_true", help="显示版本号")
|
|
80
|
+
parser.add_argument("-w", "--window", action="store_true", help="启动 GUI 界面")
|
|
81
|
+
parser.add_argument("-h", "--help", action="store_true", help="显示帮助信息")
|
|
82
|
+
|
|
83
|
+
# 子命令
|
|
84
|
+
subparsers = parser.add_subparsers(dest="command", help="子命令")
|
|
85
|
+
|
|
86
|
+
# new 子命令
|
|
87
|
+
new_parser = subparsers.add_parser("new", help="创建新项目", add_help=False)
|
|
88
|
+
new_parser.add_argument("name", help="项目名称")
|
|
89
|
+
new_parser.add_argument("-l", "--lang", dest="language", required=True, help="编程语言 (java/python/go)")
|
|
90
|
+
|
|
91
|
+
# install list 子命令
|
|
92
|
+
install_parser = subparsers.add_parser("install", help="安装相关", add_help=False)
|
|
93
|
+
install_subparsers = install_parser.add_subparsers(dest="install_cmd", help="安装子命令")
|
|
94
|
+
list_parser = install_subparsers.add_parser("list", help="列出已安装的库", add_help=False)
|
|
95
|
+
|
|
96
|
+
# 解析已知参数,忽略未知(处理 silentstudio <pkg_name>)
|
|
97
|
+
args, unknown = parser.parse_known_args()
|
|
98
|
+
|
|
99
|
+
# 处理 -h/--help
|
|
100
|
+
if args.help or (len(sys.argv) >= 2 and sys.argv[1] in ("-h", "--help")):
|
|
101
|
+
show_help(lang)
|
|
102
|
+
return 0
|
|
103
|
+
|
|
104
|
+
# 处理 -v/--version
|
|
105
|
+
if args.version:
|
|
106
|
+
print(f"SilentStudio CLI v{__version__}")
|
|
107
|
+
return 0
|
|
108
|
+
|
|
109
|
+
# 处理 -w/--window
|
|
110
|
+
if args.window:
|
|
111
|
+
try:
|
|
112
|
+
from .window import main as gui_main
|
|
113
|
+
gui_main()
|
|
114
|
+
return 0
|
|
115
|
+
except ImportError:
|
|
116
|
+
if lang == "zh-CN":
|
|
117
|
+
print("❌ GUI 组件未安装,请执行: pip install silentstudio[window]")
|
|
118
|
+
else:
|
|
119
|
+
print("❌ GUI component not installed, please run: pip install silentstudio[window]")
|
|
120
|
+
return 1
|
|
121
|
+
|
|
122
|
+
# 处理子命令
|
|
123
|
+
if args.command == "new":
|
|
124
|
+
return handle_new(args.name, args.language)
|
|
125
|
+
|
|
126
|
+
if args.command == "install":
|
|
127
|
+
if args.install_cmd == "list":
|
|
128
|
+
return handle_list_packages()
|
|
129
|
+
else:
|
|
130
|
+
if lang == "zh-CN":
|
|
131
|
+
print("❌ 未知的 install 子命令,请使用: silentstudio install list")
|
|
132
|
+
else:
|
|
133
|
+
print("❌ Unknown install subcommand, use: silentstudio install list")
|
|
134
|
+
return 1
|
|
135
|
+
|
|
136
|
+
# 处理 silentstudio <pkg_name> (安装)
|
|
137
|
+
if len(sys.argv) >= 2:
|
|
138
|
+
pkg_name = sys.argv[1]
|
|
139
|
+
# 排除命令本身
|
|
140
|
+
if pkg_name not in ("new", "install", "-v", "--version", "-w", "--window", "-h", "--help"):
|
|
141
|
+
return handle_install(pkg_name, lang)
|
|
142
|
+
|
|
143
|
+
# 默认显示帮助
|
|
144
|
+
show_help(lang)
|
|
145
|
+
return 0
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
if __name__ == "__main__":
|
|
149
|
+
sys.exit(main())
|
silentstudio/config.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Dict, Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def get_config_dir() -> Path:
|
|
8
|
+
"""获取 SilentStudio 配置目录"""
|
|
9
|
+
home = Path.home()
|
|
10
|
+
config_dir = home / ".silentstudio"
|
|
11
|
+
config_dir.mkdir(exist_ok=True)
|
|
12
|
+
return config_dir
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_config_file() -> Path:
|
|
16
|
+
return get_config_dir() / "config.json"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def get_installed_file() -> Path:
|
|
20
|
+
return get_config_dir() / "installed.json"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def get_cache_file() -> Path:
|
|
24
|
+
return get_config_dir() / "index.cache.json"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
DEFAULT_CONFIG = {
|
|
28
|
+
"language": "auto", # auto / zh-CN / en-US
|
|
29
|
+
"pypi_mirror": "https://pypi.org/simple/",
|
|
30
|
+
"index_url": "https://raw.githubusercontent.com/Silent-Studio-CN/index/main/index.jsonl",
|
|
31
|
+
"install_scope": "user", # user / system
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def load_config() -> Dict[str, Any]:
|
|
36
|
+
"""加载用户配置"""
|
|
37
|
+
config_file = get_config_file()
|
|
38
|
+
if not config_file.exists():
|
|
39
|
+
save_config(DEFAULT_CONFIG)
|
|
40
|
+
return DEFAULT_CONFIG.copy()
|
|
41
|
+
|
|
42
|
+
try:
|
|
43
|
+
with open(config_file, "r", encoding="utf-8") as f:
|
|
44
|
+
config = json.load(f)
|
|
45
|
+
# 合并默认配置(补全缺失的键)
|
|
46
|
+
for key, value in DEFAULT_CONFIG.items():
|
|
47
|
+
if key not in config:
|
|
48
|
+
config[key] = value
|
|
49
|
+
return config
|
|
50
|
+
except Exception:
|
|
51
|
+
return DEFAULT_CONFIG.copy()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def save_config(config: Dict[str, Any]) -> None:
|
|
55
|
+
"""保存用户配置"""
|
|
56
|
+
config_file = get_config_file()
|
|
57
|
+
with open(config_file, "w", encoding="utf-8") as f:
|
|
58
|
+
json.dump(config, f, ensure_ascii=False, indent=2)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def load_installed() -> Dict[str, Any]:
|
|
62
|
+
"""加载安装记录"""
|
|
63
|
+
installed_file = get_installed_file()
|
|
64
|
+
if not installed_file.exists():
|
|
65
|
+
return {"installed": []}
|
|
66
|
+
|
|
67
|
+
try:
|
|
68
|
+
with open(installed_file, "r", encoding="utf-8") as f:
|
|
69
|
+
return json.load(f)
|
|
70
|
+
except Exception:
|
|
71
|
+
return {"installed": []}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def save_installed(data: Dict[str, Any]) -> None:
|
|
75
|
+
"""保存安装记录"""
|
|
76
|
+
installed_file = get_installed_file()
|
|
77
|
+
with open(installed_file, "w", encoding="utf-8") as f:
|
|
78
|
+
json.dump(data, f, ensure_ascii=False, indent=2)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def load_cache() -> Dict[str, Any]:
|
|
82
|
+
"""加载索引缓存"""
|
|
83
|
+
cache_file = get_cache_file()
|
|
84
|
+
if not cache_file.exists():
|
|
85
|
+
return {}
|
|
86
|
+
|
|
87
|
+
try:
|
|
88
|
+
with open(cache_file, "r", encoding="utf-8") as f:
|
|
89
|
+
return json.load(f)
|
|
90
|
+
except Exception:
|
|
91
|
+
return {}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def save_cache(data: Dict[str, Any]) -> None:
|
|
95
|
+
"""保存索引缓存"""
|
|
96
|
+
cache_file = get_cache_file()
|
|
97
|
+
with open(cache_file, "w", encoding="utf-8") as f:
|
|
98
|
+
json.dump(data, f, ensure_ascii=False, indent=2)
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
from typing import Optional, Dict, Any, Tuple
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
|
|
7
|
+
from .config import load_config, load_installed, save_installed
|
|
8
|
+
from .utils import (
|
|
9
|
+
get_system_language,
|
|
10
|
+
run_pip_command,
|
|
11
|
+
check_package_installed,
|
|
12
|
+
get_package_version,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def fetch_index() -> list:
|
|
17
|
+
"""从服务器拉取索引"""
|
|
18
|
+
config = load_config()
|
|
19
|
+
index_url = config.get("index_url", "")
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
resp = requests.get(index_url, timeout=10)
|
|
23
|
+
if resp.status_code == 200:
|
|
24
|
+
entries = []
|
|
25
|
+
for line in resp.text.splitlines():
|
|
26
|
+
line = line.strip()
|
|
27
|
+
if line:
|
|
28
|
+
try:
|
|
29
|
+
entries.append(json.loads(line))
|
|
30
|
+
except json.JSONDecodeError:
|
|
31
|
+
continue
|
|
32
|
+
return entries
|
|
33
|
+
return []
|
|
34
|
+
except Exception:
|
|
35
|
+
return []
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def check_pypi(pkg_name: str) -> Tuple[bool, Optional[str]]:
|
|
39
|
+
"""检查 PyPI 上是否存在该包"""
|
|
40
|
+
try:
|
|
41
|
+
url = f"https://pypi.org/pypi/{pkg_name}/json"
|
|
42
|
+
resp = requests.get(url, timeout=10)
|
|
43
|
+
if resp.status_code == 200:
|
|
44
|
+
data = resp.json()
|
|
45
|
+
version = data.get("info", {}).get("version")
|
|
46
|
+
return True, version
|
|
47
|
+
return False, None
|
|
48
|
+
except Exception:
|
|
49
|
+
return False, None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def install_package(pkg_name: str, channel: str = "pypi") -> Tuple[bool, str]:
|
|
53
|
+
"""安装包"""
|
|
54
|
+
config = load_config()
|
|
55
|
+
scope = config.get("install_scope", "user")
|
|
56
|
+
|
|
57
|
+
if channel == "pypi":
|
|
58
|
+
success, stdout, stderr = run_pip_command(
|
|
59
|
+
["install", pkg_name],
|
|
60
|
+
scope=scope
|
|
61
|
+
)
|
|
62
|
+
if success:
|
|
63
|
+
# 记录安装
|
|
64
|
+
version = get_package_version(pkg_name)
|
|
65
|
+
if version:
|
|
66
|
+
record_installation(pkg_name, version, "pypi")
|
|
67
|
+
return True, f"✅ 成功安装 {pkg_name} (版本: {version or '未知'})"
|
|
68
|
+
else:
|
|
69
|
+
return False, f"❌ 安装失败: {stderr or stdout}"
|
|
70
|
+
|
|
71
|
+
return False, f"❌ 不支持的渠道: {channel}"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def record_installation(name: str, version: str, channel: str) -> None:
|
|
75
|
+
"""记录安装到 installed.json"""
|
|
76
|
+
data = load_installed()
|
|
77
|
+
|
|
78
|
+
# 检查是否已存在
|
|
79
|
+
for pkg in data["installed"]:
|
|
80
|
+
if pkg["name"] == name:
|
|
81
|
+
pkg["version"] = version
|
|
82
|
+
pkg["channel"] = channel
|
|
83
|
+
pkg["installed_at"] = __import__("datetime").datetime.now().isoformat()
|
|
84
|
+
save_installed(data)
|
|
85
|
+
return
|
|
86
|
+
|
|
87
|
+
# 新增
|
|
88
|
+
data["installed"].append({
|
|
89
|
+
"name": name,
|
|
90
|
+
"version": version,
|
|
91
|
+
"channel": channel,
|
|
92
|
+
"installed_at": __import__("datetime").datetime.now().isoformat()
|
|
93
|
+
})
|
|
94
|
+
save_installed(data)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def handle_install(pkg_name: str, lang: str) -> int:
|
|
98
|
+
"""处理安装逻辑"""
|
|
99
|
+
# 检查是否已安装
|
|
100
|
+
if check_package_installed(pkg_name):
|
|
101
|
+
version = get_package_version(pkg_name)
|
|
102
|
+
msg_zh = f"✅ {pkg_name} 已安装 (版本: {version})"
|
|
103
|
+
msg_en = f"✅ {pkg_name} is already installed (version: {version})"
|
|
104
|
+
print(msg_zh if lang == "zh-CN" else msg_en)
|
|
105
|
+
return 0
|
|
106
|
+
|
|
107
|
+
# 1. 拉取 SilentStudio 索引
|
|
108
|
+
print("📡 正在查询 SilentStudio 索引..." if lang == "zh-CN" else "📡 Querying SilentStudio index...")
|
|
109
|
+
index = fetch_index()
|
|
110
|
+
|
|
111
|
+
# 查找匹配
|
|
112
|
+
matched = None
|
|
113
|
+
for entry in index:
|
|
114
|
+
if entry.get("name", "").lower() == pkg_name.lower():
|
|
115
|
+
matched = entry
|
|
116
|
+
break
|
|
117
|
+
# 也通过 pkg_name 匹配
|
|
118
|
+
if entry.get("pkg_name", "").lower() == pkg_name.lower():
|
|
119
|
+
matched = entry
|
|
120
|
+
break
|
|
121
|
+
|
|
122
|
+
if matched:
|
|
123
|
+
channel = matched.get("channel", "pypi")
|
|
124
|
+
pkg_name_actual = matched.get("pkg_name", pkg_name)
|
|
125
|
+
|
|
126
|
+
if channel == "pypi":
|
|
127
|
+
print(f"📦 从 PyPI 安装 {pkg_name_actual}..." if lang == "zh-CN" else f"📦 Installing {pkg_name_actual} from PyPI...")
|
|
128
|
+
success, msg = install_package(pkg_name_actual, "pypi")
|
|
129
|
+
print(msg)
|
|
130
|
+
return 0 if success else 1
|
|
131
|
+
|
|
132
|
+
# 2. 未在索引中找到,回退到 PyPI
|
|
133
|
+
msg_zh_not_found = f"⚠️ 您搜索的 '{pkg_name}' 并未在 SilentStudio 项目列表中查询到"
|
|
134
|
+
msg_en_not_found = f"⚠️ '{pkg_name}' was not found in the SilentStudio project list"
|
|
135
|
+
print(msg_zh_not_found if lang == "zh-CN" else msg_en_not_found)
|
|
136
|
+
|
|
137
|
+
exists, version = check_pypi(pkg_name)
|
|
138
|
+
if exists:
|
|
139
|
+
msg_zh_pypi = f"📦 但在官方 PyPI 上找到了同名库 (最新版本: {version})"
|
|
140
|
+
msg_en_pypi = f"📦 But found the same-named package on PyPI (latest version: {version})"
|
|
141
|
+
print(msg_zh_pypi if lang == "zh-CN" else msg_en_pypi)
|
|
142
|
+
|
|
143
|
+
prompt_zh = "是否安装?[y/N]: "
|
|
144
|
+
prompt_en = "Install? [y/N]: "
|
|
145
|
+
prompt = prompt_zh if lang == "zh-CN" else prompt_en
|
|
146
|
+
|
|
147
|
+
try:
|
|
148
|
+
answer = input(prompt).strip().lower()
|
|
149
|
+
except KeyboardInterrupt:
|
|
150
|
+
print("\n❌ 已取消")
|
|
151
|
+
return 1
|
|
152
|
+
|
|
153
|
+
if answer in ("y", "yes", "是"):
|
|
154
|
+
success, msg = install_package(pkg_name, "pypi")
|
|
155
|
+
print(msg)
|
|
156
|
+
return 0 if success else 1
|
|
157
|
+
else:
|
|
158
|
+
print("❌ 已取消安装" if lang == "zh-CN" else "❌ Installation cancelled")
|
|
159
|
+
return 0
|
|
160
|
+
else:
|
|
161
|
+
msg_zh_not_found_all = f"❌ 未在任何源中找到 '{pkg_name}'"
|
|
162
|
+
msg_en_not_found_all = f"❌ '{pkg_name}' not found in any source"
|
|
163
|
+
print(msg_zh_not_found_all if lang == "zh-CN" else msg_en_not_found_all)
|
|
164
|
+
return 1
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
from typing import Dict, Any
|
|
2
|
+
|
|
3
|
+
from .config import load_installed
|
|
4
|
+
from .utils import get_pip_packages
|
|
5
|
+
from .utils import get_system_language
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def format_package_list() -> str:
|
|
9
|
+
"""格式化包列表"""
|
|
10
|
+
lang = get_system_language()
|
|
11
|
+
|
|
12
|
+
# 获取所有包
|
|
13
|
+
all_pkgs = get_pip_packages()
|
|
14
|
+
|
|
15
|
+
# 获取 SilentStudio 安装的记录
|
|
16
|
+
installed_data = load_installed()
|
|
17
|
+
silent_names = {pkg["name"] for pkg in installed_data.get("installed", [])}
|
|
18
|
+
|
|
19
|
+
# 分类
|
|
20
|
+
silent_pkgs = []
|
|
21
|
+
other_pkgs = []
|
|
22
|
+
for pkg in all_pkgs:
|
|
23
|
+
name = pkg.get("name", "")
|
|
24
|
+
if name in silent_names:
|
|
25
|
+
silent_pkgs.append(pkg)
|
|
26
|
+
else:
|
|
27
|
+
other_pkgs.append(pkg)
|
|
28
|
+
|
|
29
|
+
# 排序
|
|
30
|
+
silent_pkgs.sort(key=lambda x: x.get("name", ""))
|
|
31
|
+
other_pkgs.sort(key=lambda x: x.get("name", ""))
|
|
32
|
+
|
|
33
|
+
# 构建输出
|
|
34
|
+
lines = []
|
|
35
|
+
width = 60
|
|
36
|
+
|
|
37
|
+
if lang == "zh-CN":
|
|
38
|
+
lines.append("╔" + "═" * width + "╗")
|
|
39
|
+
lines.append("║" + " 📦 已安装 Python 库".ljust(width) + "║")
|
|
40
|
+
lines.append("╠" + "═" * width + "╣")
|
|
41
|
+
|
|
42
|
+
if silent_pkgs:
|
|
43
|
+
lines.append(f"║ ✦ SilentStudio 安装 ({len(silent_pkgs)} 个):".ljust(width) + "║")
|
|
44
|
+
for i, pkg in enumerate(silent_pkgs):
|
|
45
|
+
name = pkg.get("name", "")
|
|
46
|
+
version = pkg.get("version", "未知")
|
|
47
|
+
prefix = " ├─ " if i < len(silent_pkgs) - 1 else " └─ "
|
|
48
|
+
lines.append(f"║ {prefix}{name}".ljust(width - 20) + f"v{version}".rjust(20) + "║")
|
|
49
|
+
|
|
50
|
+
if silent_pkgs and other_pkgs:
|
|
51
|
+
lines.append("╠" + "═" * width + "╣")
|
|
52
|
+
|
|
53
|
+
if other_pkgs:
|
|
54
|
+
lines.append(f"║ 📚 其他库 ({len(other_pkgs)} 个):".ljust(width) + "║")
|
|
55
|
+
# 只显示前 20 个,避免太长
|
|
56
|
+
display = other_pkgs[:20]
|
|
57
|
+
for i, pkg in enumerate(display):
|
|
58
|
+
name = pkg.get("name", "")
|
|
59
|
+
version = pkg.get("version", "未知")
|
|
60
|
+
prefix = " ├─ " if i < len(display) - 1 else " └─ "
|
|
61
|
+
lines.append(f"║ {prefix}{name}".ljust(width - 20) + f"v{version}".rjust(20) + "║")
|
|
62
|
+
|
|
63
|
+
if len(other_pkgs) > 20:
|
|
64
|
+
lines.append(f"║ ... 还有 {len(other_pkgs) - 20} 个".ljust(width) + "║")
|
|
65
|
+
|
|
66
|
+
lines.append("╚" + "═" * width + "╝")
|
|
67
|
+
|
|
68
|
+
else:
|
|
69
|
+
lines.append("╔" + "═" * width + "╗")
|
|
70
|
+
lines.append("║" + " 📦 Installed Python Packages".ljust(width) + "║")
|
|
71
|
+
lines.append("╠" + "═" * width + "╣")
|
|
72
|
+
|
|
73
|
+
if silent_pkgs:
|
|
74
|
+
lines.append(f"║ ✦ Installed via SilentStudio ({len(silent_pkgs)}):".ljust(width) + "║")
|
|
75
|
+
for i, pkg in enumerate(silent_pkgs):
|
|
76
|
+
name = pkg.get("name", "")
|
|
77
|
+
version = pkg.get("version", "unknown")
|
|
78
|
+
prefix = " ├─ " if i < len(silent_pkgs) - 1 else " └─ "
|
|
79
|
+
lines.append(f"║ {prefix}{name}".ljust(width - 20) + f"v{version}".rjust(20) + "║")
|
|
80
|
+
|
|
81
|
+
if silent_pkgs and other_pkgs:
|
|
82
|
+
lines.append("╠" + "═" * width + "╣")
|
|
83
|
+
|
|
84
|
+
if other_pkgs:
|
|
85
|
+
lines.append(f"║ 📚 Other packages ({len(other_pkgs)}):".ljust(width) + "║")
|
|
86
|
+
display = other_pkgs[:20]
|
|
87
|
+
for i, pkg in enumerate(display):
|
|
88
|
+
name = pkg.get("name", "")
|
|
89
|
+
version = pkg.get("version", "unknown")
|
|
90
|
+
prefix = " ├─ " if i < len(display) - 1 else " └─ "
|
|
91
|
+
lines.append(f"║ {prefix}{name}".ljust(width - 20) + f"v{version}".rjust(20) + "║")
|
|
92
|
+
|
|
93
|
+
if len(other_pkgs) > 20:
|
|
94
|
+
lines.append(f"║ ... and {len(other_pkgs) - 20} more".ljust(width) + "║")
|
|
95
|
+
|
|
96
|
+
lines.append("╚" + "═" * width + "╝")
|
|
97
|
+
|
|
98
|
+
return "\n".join(lines)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def handle_list_packages() -> int:
|
|
102
|
+
"""处理 list 命令"""
|
|
103
|
+
try:
|
|
104
|
+
print(format_package_list())
|
|
105
|
+
return 0
|
|
106
|
+
except Exception as e:
|
|
107
|
+
lang = get_system_language()
|
|
108
|
+
msg_zh = f"❌ 获取包列表失败: {e}"
|
|
109
|
+
msg_en = f"❌ Failed to get package list: {e}"
|
|
110
|
+
print(msg_zh if lang == "zh-CN" else msg_en)
|
|
111
|
+
return 1
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Dict, Any
|
|
4
|
+
|
|
5
|
+
from .utils import get_system_language
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# 项目模板
|
|
9
|
+
TEMPLATES: Dict[str, Dict[str, Any]] = {
|
|
10
|
+
"java": {
|
|
11
|
+
"description": "Java 项目",
|
|
12
|
+
"dirs": ["src/main/java", "src/main/resources", "src/test/java"],
|
|
13
|
+
"files": {
|
|
14
|
+
"pom.xml": """<?xml version="1.0" encoding="UTF-8"?>
|
|
15
|
+
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
16
|
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
17
|
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
|
18
|
+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
19
|
+
<modelVersion>4.0.0</modelVersion>
|
|
20
|
+
<groupId>com.example</groupId>
|
|
21
|
+
<artifactId>{project_name}</artifactId>
|
|
22
|
+
<version>1.0-SNAPSHOT</version>
|
|
23
|
+
<properties>
|
|
24
|
+
<maven.compiler.source>11</maven.compiler.source>
|
|
25
|
+
<maven.compiler.target>11</maven.compiler.target>
|
|
26
|
+
</properties>
|
|
27
|
+
</project>""",
|
|
28
|
+
".gitignore": """# Java
|
|
29
|
+
*.class
|
|
30
|
+
*.jar
|
|
31
|
+
target/
|
|
32
|
+
!/.mvn/wrapper/maven-wrapper.jar
|
|
33
|
+
""",
|
|
34
|
+
"README.md": "# {project_name}\n\nJava project created with SilentStudio."
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"python": {
|
|
38
|
+
"description": "Python 项目",
|
|
39
|
+
"dirs": ["src", "tests"],
|
|
40
|
+
"files": {
|
|
41
|
+
"setup.py": """from setuptools import setup, find_packages
|
|
42
|
+
|
|
43
|
+
setup(
|
|
44
|
+
name="{project_name}",
|
|
45
|
+
version="0.1.0",
|
|
46
|
+
packages=find_packages(where="src"),
|
|
47
|
+
package_dir={"": "src"},
|
|
48
|
+
python_requires=">=3.8",
|
|
49
|
+
)
|
|
50
|
+
""",
|
|
51
|
+
"README.md": "# {project_name}\n\nPython project created with SilentStudio.",
|
|
52
|
+
"requirements.txt": "",
|
|
53
|
+
".gitignore": """# Python
|
|
54
|
+
__pycache__/
|
|
55
|
+
*.pyc
|
|
56
|
+
*.pyo
|
|
57
|
+
*.pyd
|
|
58
|
+
.Python
|
|
59
|
+
env/
|
|
60
|
+
venv/
|
|
61
|
+
.venv/
|
|
62
|
+
*.egg-info/
|
|
63
|
+
dist/
|
|
64
|
+
build/
|
|
65
|
+
"""
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"go": {
|
|
69
|
+
"description": "Go 项目",
|
|
70
|
+
"dirs": ["cmd", "internal", "pkg"],
|
|
71
|
+
"files": {
|
|
72
|
+
"go.mod": "module {project_name}\n\ngo 1.21",
|
|
73
|
+
"main.go": """package main
|
|
74
|
+
|
|
75
|
+
import "fmt"
|
|
76
|
+
|
|
77
|
+
func main() {
|
|
78
|
+
fmt.Println("Hello from {project_name}!")
|
|
79
|
+
}
|
|
80
|
+
""",
|
|
81
|
+
"README.md": "# {project_name}\n\nGo project created with SilentStudio.",
|
|
82
|
+
".gitignore": """# Go
|
|
83
|
+
*.exe
|
|
84
|
+
*.test
|
|
85
|
+
*.out
|
|
86
|
+
/bin/
|
|
87
|
+
/dist/
|
|
88
|
+
/vendor/
|
|
89
|
+
"""
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def create_project(project_name: str, language: str) -> bool:
|
|
96
|
+
"""创建项目"""
|
|
97
|
+
lang = get_system_language()
|
|
98
|
+
|
|
99
|
+
# 检查语言是否支持
|
|
100
|
+
if language not in TEMPLATES:
|
|
101
|
+
msg_zh = f"❌ 不支持的编程语言: {language}"
|
|
102
|
+
msg_en = f"❌ Unsupported programming language: {language}"
|
|
103
|
+
print(msg_zh if lang == "zh-CN" else msg_en)
|
|
104
|
+
print(f" 支持的语言: {', '.join(TEMPLATES.keys())}")
|
|
105
|
+
return False
|
|
106
|
+
|
|
107
|
+
template = TEMPLATES[language]
|
|
108
|
+
project_path = Path.cwd() / project_name
|
|
109
|
+
|
|
110
|
+
# 检查目录是否已存在
|
|
111
|
+
if project_path.exists():
|
|
112
|
+
msg_zh = f"❌ 目录已存在: {project_path}"
|
|
113
|
+
msg_en = f"❌ Directory already exists: {project_path}"
|
|
114
|
+
print(msg_zh if lang == "zh-CN" else msg_en)
|
|
115
|
+
return False
|
|
116
|
+
|
|
117
|
+
# 创建目录
|
|
118
|
+
project_path.mkdir(parents=True)
|
|
119
|
+
|
|
120
|
+
# 创建子目录
|
|
121
|
+
for subdir in template["dirs"]:
|
|
122
|
+
(project_path / subdir).mkdir(parents=True, exist_ok=True)
|
|
123
|
+
|
|
124
|
+
# 创建文件
|
|
125
|
+
for filename, content in template["files"].items():
|
|
126
|
+
filepath = project_path / filename
|
|
127
|
+
# 替换模板变量
|
|
128
|
+
content = content.format(project_name=project_name)
|
|
129
|
+
with open(filepath, "w", encoding="utf-8") as f:
|
|
130
|
+
f.write(content)
|
|
131
|
+
|
|
132
|
+
msg_zh = f"✅ 项目 '{project_name}' 已创建 (语言: {language})"
|
|
133
|
+
msg_en = f"✅ Project '{project_name}' created (language: {language})"
|
|
134
|
+
print(msg_zh if lang == "zh-CN" else msg_en)
|
|
135
|
+
print(f"📁 位置: {project_path}")
|
|
136
|
+
|
|
137
|
+
return True
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def handle_new(project_name: str, language: str) -> int:
|
|
141
|
+
"""处理 new 命令"""
|
|
142
|
+
if not project_name:
|
|
143
|
+
lang = get_system_language()
|
|
144
|
+
msg_zh = "❌ 请指定项目名称"
|
|
145
|
+
msg_en = "❌ Please specify a project name"
|
|
146
|
+
print(msg_zh if lang == "zh-CN" else msg_en)
|
|
147
|
+
return 1
|
|
148
|
+
|
|
149
|
+
if not language:
|
|
150
|
+
lang = get_system_language()
|
|
151
|
+
msg_zh = "❌ 请指定编程语言 (使用 -l 或 --lang)"
|
|
152
|
+
msg_en = "❌ Please specify a programming language (use -l or --lang)"
|
|
153
|
+
print(msg_zh if lang == "zh-CN" else msg_en)
|
|
154
|
+
return 1
|
|
155
|
+
|
|
156
|
+
success = create_project(project_name, language)
|
|
157
|
+
return 0 if success else 1
|