#!/usr/bin/env python3
# Python script to parse flutter pubspec.lock file and extract library names and versions
import yaml
import csv
import sys


def parse_pubspec(file_path, sdk):
    with open(file_path, 'r') as stream:
        try:
            data = yaml.safe_load(stream)
            packages = data.get('packages', {})
            with open('packages.csv', 'w', newline='') as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(['name', 'version', 'sdk'])
                for package_name, package_data in packages.items():
                    version = package_data.get('version', None)
                    description = package_data.get('description', {})
                    if isinstance(description, str):
                        continue
                    else:
                        url = description.get('url', None)

                    if "castbox" in url:
                        continue

                    print(package_name, version, url)

                    writer.writerow([package_name, version, sdk])
        except yaml.YAMLError as exc:
            print(exc)


if len(sys.argv) < 2:
    print("Please provide sdk parameter")
    sys.exit(1)

sdk = sys.argv[1]

# 调用函数并传入pubspec.lock的文件路径
parse_pubspec('pubspec.lock', sdk)
