"""Offline Ionic 1 installation, bundle assets, Hello World DOM & CLI workflow verifier for Ch02 (install)."""
from __future__ import annotations
import json
import shlex
from lxml import html
VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"
def make_case(name: str, summary: str, expected, actual) -> dict:
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_cdn_and_archive_layout() -> dict:
cdn_snippet = """
<head>
<link href="https://cdn.example.invalid/ionic/1.3.2/css/ionic.css" rel="stylesheet">
<script src="https://cdn.example.invalid/ionic/1.3.2/js/ionic.bundle.min.js"></script>
</head>
"""
doc = html.document_fromstring(f"<html>{cdn_snippet}<body></body></html>")
css_href = doc.xpath(".//link[@rel='stylesheet']/@href")[0]
js_src = doc.xpath(".//script/@src")[0]
release_layout = {
"css/": "stylesheets (ionic.css, ionic.min.css)",
"fonts/": "Ionicons icon font files",
"js/": "JavaScript bundles (ionic.bundle.min.js)",
"version.json": "release version metadata",
}
actual = {
"css_file": css_href.rsplit("/", 1)[-1],
"js_file": js_src.rsplit("/", 1)[-1],
"archive_entries": sorted(release_layout.keys()),
"entry_count": len(release_layout),
}
expected = {
"css_file": "ionic.css",
"js_file": "ionic.bundle.min.js",
"archive_entries": ["css/", "fonts/", "js/", "version.json"],
"entry_count": 4,
}
return make_case(
"cdn_and_release_bundle_structure",
"Verify Ionic 1.3.2 CSS/JS bundle references and 4 release archive entries",
expected,
actual,
)
def case_hello_world_dom() -> dict:
markup = """
<div>
<ion-header-bar class="bar-positive">
<h1 class="title">Hello World!</h1>
</ion-header-bar>
<ion-content>
<p>我的第一个 ionic 应用。</p>
</ion-content>
</div>
"""
root = html.fragment_fromstring(markup)
header = root.xpath(".//ion-header-bar")[0]
title = header.xpath("./h1[@class='title']/text()")[0].strip()
content_p = root.xpath(".//ion-content/p/text()")[0].strip()
actual = {
"header_class": header.get("class"),
"title_text": title,
"content_text": content_p,
"has_content_directive": len(root.xpath(".//ion-content")) == 1,
}
expected = {
"header_class": "bar-positive",
"title_text": "Hello World!",
"content_text": "我的第一个 ionic 应用。",
"has_content_directive": True,
}
return make_case(
"hello_world_ionic_markup",
"Parse Hello World <ion-header-bar> and <ion-content> directives via lxml",
expected,
actual,
)
def case_npm_install_and_update_cli() -> dict:
cmds = [
"npm install -g cordova ionic",
"sudo npm install -g cordova ionic",
"npm update -g cordova ionic",
"sudo npm update -g cordova ionic",
"ionic start myApp tabs",
]
parsed = []
for raw in cmds:
tokens = shlex.split(raw)
elevated = tokens[0] == "sudo"
core = tokens[1:] if elevated else tokens
parsed.append({"elevated": elevated, "tool": core[0], "subcommand": core[1], "args": core[2:]})
actual = {
"command_count": len(parsed),
"global_packages": [p["args"][1:] for p in parsed[:4]],
"starter_app": {"name": parsed[4]["args"][0], "template": parsed[4]["args"][1]},
"modern_cli_equivalent": "@ionic/cli + npx cap sync",
}
expected = {
"command_count": 5,
"global_packages": [
["cordova", "ionic"],
["cordova", "ionic"],
["cordova", "ionic"],
["cordova", "ionic"],
],
"starter_app": {"name": "myApp", "template": "tabs"},
"modern_cli_equivalent": "@ionic/cli + npx cap sync",
}
return make_case(
"npm_and_ionic_start_cli_workflows",
"Validate global npm install/update commands and ionic start tabs scaffold syntax",
expected,
actual,
)
def case_cordova_android_ios_pipeline() -> dict:
android_steps = [
"cd myApp",
"ionic cordova platform add android",
"ionic cordova build android",
"ionic cordova emulate android",
]
ios_steps = [
"cd myApp",
"ionic cordova platform add ios",
"ionic cordova build ios",
"ionic cordova emulate ios",
"npm install -g ios-sim",
]
def extract_actions(steps: list[str], platform: str) -> list[str]:
out = []
for s in steps:
parts = shlex.split(s)
if parts[:2] == ["ionic", "cordova"] and parts[-1] == platform:
out.append(" ".join(parts[2:-1]))
return out
actual = {
"android_actions": extract_actions(android_steps, "android"),
"ios_actions": extract_actions(ios_steps, "ios"),
"ios_sim_fallback": "ios-sim" in shlex.split(ios_steps[-1]),
"ionic_lab_status": "discontinued",
"ionic_lab_features_count": 6,
}
expected = {
"android_actions": ["platform add", "build", "emulate"],
"ios_actions": ["platform add", "build", "emulate"],
"ios_sim_fallback": True,
"ionic_lab_status": "discontinued",
"ionic_lab_features_count": 6,
}
return make_case(
"cordova_android_ios_build_pipeline",
"Verify Android/iOS Cordova platform add/build/emulate command sequence and ios-sim helper",
expected,
actual,
)
def main() -> None:
cases = [
case_cdn_and_archive_layout(),
case_hello_world_dom(),
case_npm_install_and_update_cli(),
case_cordova_android_ios_pipeline(),
]
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/ionic-install-lab/explore-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2024.zip