蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Flutter 系统要求矩阵、跨平台 PATH 解析与 flutter doctor 诊断离线校验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-install.py
explore-install.py159 行
"""Offline Flutter SDK installation, PATH, and doctor verifier for Ch02 (install)."""
import json
import re

VERSION = "CPython 3.12.14 offline Flutter/Dart architecture & runtime verifier (Flutter SDK absent)"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def check_system_requirements(platform_name, os_ver, free_gb, tools):
    min_disk = 2.8
    required_tools = {
        "Windows": {"git"},
        "macOS": {"git"},
        "Linux": {"git", "curl", "unzip"},
    }[platform_name]
    min_os = {"Windows": (10, 0), "macOS": (10, 14), "Linux": (18, 4)}[platform_name]
    missing = sorted(required_tools - set(tools))
    return {
        "os_ok": os_ver >= min_os,
        "disk_ok": free_gb >= min_disk,
        "missing_tools": missing,
        "ready": os_ver >= min_os and free_gb >= min_disk and not missing,
    }


def resolve_path_lookup(path_entries, target_bin):
    for entry in path_entries:
        normalized = entry.replace("\\", "/").rstrip("/")
        if normalized.endswith("/flutter/bin"):
            return f"{normalized}/{target_bin}"
    return None


def parse_doctor_summary(text):
    statuses = {}
    for line in text.splitlines():
        m = re.match(r"^\[([✓!✗])\]\s+(.+)$", line.strip())
        if m:
            mark, label = m.groups()
            statuses[label.split(" (")[0]] = {"✓": "ok", "!": "warning", "✗": "error"}[mark]
    return statuses


def main():
    cases = []

    # Case 1: Cross-platform system requirement validation
    win_ok = check_system_requirements("Windows", (11, 0), 12.5, ["git"])
    mac_ok = check_system_requirements("macOS", (14, 2), 8.0, ["git"])
    linux_missing = check_system_requirements("Linux", (22, 4), 2.5, ["git", "curl"])
    cases.append(
        check(
            "system_requirements_matrix",
            "Verify Windows/macOS/Linux minimum OS version, 2.8 GB disk floor, and prerequisite CLI tools",
            {"win_ready": True, "mac_ready": True, "linux_ready": False, "linux_missing": ["unzip"], "linux_disk_ok": False},
            {
                "win_ready": win_ok["ready"],
                "mac_ready": mac_ok["ready"],
                "linux_ready": linux_missing["ready"],
                "linux_missing": linux_missing["missing_tools"],
                "linux_disk_ok": linux_missing["disk_ok"],
            },
        )
    )

    # Case 2: Windows, macOS (.zshrc), Linux (.bashrc) PATH resolution
    win_path = [r"C:\Windows\System32", r"C:\flutter\bin"]
    mac_path = ["/usr/local/bin", "/Users/dev/flutter/bin"]
    linux_path = ["/usr/bin", "/home/dev/flutter/bin"]
    no_flutter_path = ["/usr/bin", "/bin"]
    cases.append(
        check(
            "path_env_resolution",
            "Verify flutter/bin lookup across Windows, macOS (~/.zshrc), Linux (~/.bashrc), and missing-PATH diagnosis",
            {
                "windows": "C:/flutter/bin/flutter.bat",
                "macos": "/Users/dev/flutter/bin/flutter",
                "linux": "/home/dev/flutter/bin/flutter",
                "missing": None,
            },
            {
                "windows": resolve_path_lookup(win_path, "flutter.bat"),
                "macos": resolve_path_lookup(mac_path, "flutter"),
                "linux": resolve_path_lookup(linux_path, "flutter"),
                "missing": resolve_path_lookup(no_flutter_path, "flutter"),
            },
        )
    )

    # Case 3: Shell command templates (git clone -b stable, apt-get, xcode-select)
    commands = {
        "mac_git_check": "git --version",
        "mac_clt": "xcode-select --install",
        "clone_stable": "git clone https://github.com/flutter/flutter.git -b stable",
        "linux_deps": "sudo apt-get install -y curl git unzip xz-utils zip libglu1-mesa",
        "mac_rc": "~/.zshrc",
        "linux_rc": "~/.bashrc",
    }
    cases.append(
        check(
            "install_cli_commands_and_channels",
            "Verify git clone stable channel flag, Linux apt package list, and shell profile targets",
            {
                "branch": "stable",
                "linux_pkg_count": 6,
                "shell_profiles": ["~/.zshrc", "~/.bashrc"],
            },
            {
                "branch": commands["clone_stable"].split("-b ")[1],
                "linux_pkg_count": len(commands["linux_deps"].split("-y ")[1].split()),
                "shell_profiles": [commands["mac_rc"], commands["linux_rc"]],
            },
        )
    )

    # Case 4: flutter doctor output parser and troubleshooting diagnosis
    sample_doctor = """Doctor summary (v3.24.0):
[✓] Flutter (Channel stable, 3.24.0)
[!] Android toolchain (Android Studio not installed)
[✓] Chrome (for web development)
[✓] Windows toolchain
[✓] Web server
"""
    parsed = parse_doctor_summary(sample_doctor)
    can_build_web_without_android = parsed["Flutter"] == "ok" and parsed["Chrome"] == "ok"
    cases.append(
        check(
            "flutter_doctor_diagnosis",
            "Verify flutter doctor status parsing and that Android Studio warning does not block Web/Windows targets",
            {
                "flutter": "ok",
                "android": "warning",
                "chrome": "ok",
                "web_ready_despite_android_warning": True,
            },
            {
                "flutter": parsed["Flutter"],
                "android": parsed["Android toolchain"],
                "chrome": parsed["Chrome"],
                "web_ready_despite_android_warning": can_build_web_without_android,
            },
        )
    )

    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/flutter-install-lab/explore-install.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-install.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Flutter

课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a1988.zip

下载课程关联工程包:Flutter

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1998.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习