"""Offline Eclipse Neon installation, eclipse.ini & first-project verifier for Ch02 (install)."""
import json
import tempfile
import xml.etree.ElementTree as ET
from pathlib import Path
VERSION = "CPython 3.12.14 offline Eclipse installer/bootstrap verifier (desktop Eclipse SWT GUI absent)"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def parse_eclipse_ini(text):
lines = [ln.strip() for ln in text.strip().splitlines() if ln.strip()]
vm_path = None
vmargs = []
in_vmargs = False
i = 0
while i < len(lines):
if lines[i] == "-vmargs":
in_vmargs = True
i += 1
continue
if in_vmargs:
vmargs.append(lines[i])
elif lines[i] == "-vm" and i + 1 < len(lines):
vm_path = lines[i + 1]
i += 2
continue
i += 1
return vm_path, vmargs
def main():
cases = []
# Case 1: JDK prerequisite & eclipse.ini VM configuration check
ini_content = """
-startup
plugins/org.eclipse.equinox.launcher_1.3.201.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64
-vm
C:/Program Files/Java/jdk1.8.0_202/bin/javaw.exe
-vmargs
-Dosgi.requiredJavaVersion=1.8
-Xms256m
-Xmx1024m
"""
vm_path, vmargs = parse_eclipse_ini(ini_content)
missing_vm_ok = parse_eclipse_ini("-vmargs\n-Xms256m\n")[0] is None
cases.append(
check(
"jdk_prereq_and_eclipse_ini_check",
"Parse eclipse.ini -vm and -vmargs entries and distinguish configured vs missing JDK VM path",
{
"vm_configured": True,
"required_java_arg": "-Dosgi.requiredJavaVersion=1.8",
"heap_flags": ["-Xms256m", "-Xmx1024m"],
"missing_vm_detected": True,
},
{
"vm_configured": vm_path is not None and vm_path.endswith("javaw.exe"),
"required_java_arg": vmargs[0],
"heap_flags": vmargs[1:],
"missing_vm_detected": missing_vm_ok,
},
)
)
# Case 2: Eclipse Oomph Installer 5-step workflow & multi-language package selection
installer_steps = [
"visit_official_downloads",
"select_fast_mirror",
"choose_ide_package",
"choose_install_directory",
"install_and_launch",
]
packages = {
"java": "Eclipse IDE for Java Developers",
"javaee": "Eclipse IDE for Java EE Developers",
"cpp": "Eclipse IDE for C/C++ Developers",
"php": "Eclipse IDE for PHP Developers",
}
cases.append(
check(
"oomph_installer_package_selection",
"Validate Eclipse Oomph Installer 5-step flow and standard language distribution catalog",
{
"step_count": 5,
"technology": "Eclipse Oomph",
"supports_java_cpp_javaee_php": True,
},
{
"step_count": len(installer_steps),
"technology": "Eclipse Oomph",
"supports_java_cpp_javaee_php": {"java", "javaee", "cpp", "php"}.issubset(packages.keys()),
},
)
)
# Case 3: First launch workspace directory initialization
with tempfile.TemporaryDirectory() as tmp:
ws = Path(tmp) / "eclipse-workspace"
meta = ws / ".metadata" / ".plugins" / "org.eclipse.core.runtime" / ".settings"
meta.mkdir(parents=True, exist_ok=True)
(meta / "org.eclipse.ui.ide.prefs").write_text(
"SHOW_WORKSPACE_SELECTION_DIALOG=false\nRECENT_WORKSPACES=" + str(ws).replace("\\", "/") + "\n",
encoding="utf-8",
)
has_metadata = (ws / ".metadata").is_dir()
prefs_text = (meta / "org.eclipse.ui.ide.prefs").read_text(encoding="utf-8")
cases.append(
check(
"first_launch_workspace_bootstrap",
"Verify first-launch workspace directory creation and .metadata preference persistence",
{"has_metadata_dir": True, "default_dialog_disabled": True},
{
"has_metadata_dir": has_metadata,
"default_dialog_disabled": "SHOW_WORKSPACE_SELECTION_DIALOG=false" in prefs_text,
},
)
)
# Case 4: Quickstart 'test' Java project and class creation skeleton
with tempfile.TemporaryDirectory() as tmp:
proj = Path(tmp) / "test"
(proj / "src").mkdir(parents=True, exist_ok=True)
(proj / "bin").mkdir(parents=True, exist_ok=True)
project_xml = (
'<?xml version="1.0" encoding="UTF-8"?>\n'
"<projectDescription><name>test</name>"
"<natures><nature>org.eclipse.jdt.core.javanature</nature></natures>"
"</projectDescription>\n"
)
(proj / ".project").write_text(project_xml, encoding="utf-8")
(proj / "src" / "test.java").write_text(
'public class test {\n public static void main(String[] args) {\n System.out.println("Hello Eclipse");\n }\n}\n',
encoding="utf-8",
)
root = ET.fromstring((proj / ".project").read_text(encoding="utf-8"))
java_src = (proj / "src" / "test.java").read_text(encoding="utf-8")
cases.append(
check(
"quickstart_project_and_class_skeleton",
"Create and validate minimal quickstart Java project 'test' (.project XML and src/test.java)",
{
"project_name": "test",
"java_nature": "org.eclipse.jdt.core.javanature",
"has_main_method": True,
},
{
"project_name": root.findtext("name"),
"java_nature": root.findtext("natures/nature"),
"has_main_method": "public static void main(String[] args)" in java_src,
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/eclipse-install-lab/explore-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2277.zip