"""Offline Swift 5/6 semantic verification lab for Chapter 03 (environment)."""
import json
import re
import shutil
VERSION = "CPython 3.12.14 offline Swift 5/6 semantic verifier (swiftc absent)"
def case_cli_version_and_type_error_diagnostics():
version_output = (
"swift-driver version: 1.148.6 Apple Swift version 6.3.3 "
"(swiftlang-6.3.3.1.3 clang-2100.1.1.101)\n"
"Target: arm64-apple-macosx26.0"
)
m_ver = re.search(r"Apple Swift version (\d+\.\d+\.\d+)", version_output)
m_tgt = re.search(r"Target:\s*(\S+)", version_output)
diag = "bad.swift:1:14: error: cannot convert value of type 'String' to specified type 'Int'"
m_err = re.match(r"^([^:]+):(\d+):(\d+):\s*error:\s*(.+)$", diag)
expected = {
"swift_version": "6.3.3",
"target": "arm64-apple-macosx26.0",
"error_file": "bad.swift",
"error_line": 1,
"error_col": 14,
"local_swiftc_present": False,
}
actual = {
"swift_version": m_ver.group(1) if m_ver else "",
"target": m_tgt.group(1) if m_tgt else "",
"error_file": m_err.group(1) if m_err else "",
"error_line": int(m_err.group(2)) if m_err else 0,
"error_col": int(m_err.group(3)) if m_err else 0,
"local_swiftc_present": shutil.which("swiftc") is not None,
}
return {
"name": "cli_version_and_type_error_diagnostics",
"summary": "解析 swift --version 输出格式与 swiftc 行列诊断信息,并探测本机未安装 swiftc",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_xcode_swiftui_and_playground_layout():
project_opts = {
"Product Name": "SkyDemo",
"Organization Identifier": "org.bluesky",
"Interface": "SwiftUI",
"Language": "Swift",
}
bundle_id = f"{project_opts['Organization Identifier']}.{project_opts['Product Name']}"
playground_dirs = {
"Sources": {"requires_public": True},
"Resources": {"reader": "Bundle.main"},
}
# Playground auto-displays expressions; CLI script requires explicit print()
script_prints = ["Hello, playground", "Hello, BlueSky!"]
expected = {
"bundle_id": "org.bluesky.SkyDemo",
"generated_files": ["SkyDemoApp.swift", "ContentView.swift"],
"sources_requires_public": True,
"resources_reader": "Bundle.main",
"cli_output_lines": 2,
}
actual = {
"bundle_id": bundle_id,
"generated_files": [f"{project_opts['Product Name']}App.swift", "ContentView.swift"],
"sources_requires_public": playground_dirs["Sources"]["requires_public"],
"resources_reader": playground_dirs["Resources"]["reader"],
"cli_output_lines": len(script_prints),
}
return {
"name": "xcode_swiftui_and_playground_layout",
"summary": "校验 Xcode SwiftUI 项目配置、Bundle ID 拼装规则及 Playground Sources/Resources 目录约定",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_swiftpm_package_manifest_and_main():
manifest = (
"// swift-tools-version: 6.3\n"
"import PackageDescription\n"
'let package = Package(\n'
' name: "demo",\n'
" targets: [\n"
' .executableTarget(name: "demo"),\n'
' .testTarget(name: "demoTests", dependencies: ["demo"]),\n'
" ],\n"
" swiftLanguageModes: [.v6]\n"
")\n"
)
entry_src = '@main\nstruct demo {\n static func main() {\n print("Hello, BlueSky!")\n }\n}\n'
tools_ver = re.search(r"swift-tools-version:\s*(\d+\.\d+)", manifest).group(1)
pkg_name = re.search(r'name:\s*"([^"]+)"', manifest).group(1)
has_v6 = ".v6" in manifest
has_at_main = "@main" in entry_src and "static func main()" in entry_src
expected = {
"tools_version": "6.3",
"package_name": "demo",
"swift6_mode": True,
"has_at_main_entry": True,
"required_dirs": ["Package.swift", "Sources/demo/demo.swift", "Tests/demoTests/demoTests.swift"],
}
actual = {
"tools_version": tools_ver,
"package_name": pkg_name,
"swift6_mode": has_v6,
"has_at_main_entry": has_at_main,
"required_dirs": ["Package.swift", f"Sources/{pkg_name}/{pkg_name}.swift", f"Tests/{pkg_name}Tests/{pkg_name}Tests.swift"],
}
return {
"name": "swiftpm_package_manifest_and_main",
"summary": "解析 SwiftPM Package.swift 工具版本、目标结构、Swift 6 语言模式与 @main 入口约定",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_cross_platform_framework_availability():
matrix = {
"macOS": {"SwiftPM": True, "Foundation": True, "SwiftUI": True, "UIKit": False},
"iOS": {"SwiftPM": True, "Foundation": True, "SwiftUI": True, "UIKit": True},
"Linux": {"SwiftPM": True, "Foundation": True, "SwiftUI": False, "UIKit": False},
"Windows": {"SwiftPM": True, "Foundation": True, "SwiftUI": False, "UIKit": False},
}
expected = {
"all_have_swiftpm_and_foundation": True,
"linux_windows_no_apple_ui": True,
"cli_core_commands": ["swift build", "swift run", "swift test"],
}
actual = {
"all_have_swiftpm_and_foundation": all(v["SwiftPM"] and v["Foundation"] for v in matrix.values()),
"linux_windows_no_apple_ui": all(not matrix[p]["SwiftUI"] and not matrix[p]["UIKit"] for p in ("Linux", "Windows")),
"cli_core_commands": ["swift build", "swift run", "swift test"],
}
return {
"name": "cross_platform_framework_availability",
"summary": "核对 macOS、iOS、Linux 与 Windows 跨平台框架支持边界及 SwiftPM 三大核心命令",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_cli_version_and_type_error_diagnostics(),
case_xcode_swiftui_and_playground_layout(),
case_swiftpm_package_manifest_and_main(),
case_cross_platform_framework_availability(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Swift 5/6 语义验证器(本机未安装 swiftc/Xcode)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Swift 5/6 语义验证器(本机未安装 swiftc/Xcode)。
入口文件:projects/swift-environment-lab/explore-environment.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-environment.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Swift 5/6 语义验证器(本机未安装 swiftc/Xcode)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联47章) · 含当前章节或实例 · mbskycom202612a2087.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2099.zip