Skip to content

Instantly share code, notes, and snippets.

@freitzzz
Created October 21, 2020 15:02
Show Gist options
  • Save freitzzz/a6c353f6557c63881f56acb2dc96c3b6 to your computer and use it in GitHub Desktop.
Save freitzzz/a6c353f6557c63881f56acb2dc96c3b6 to your computer and use it in GitHub Desktop.
Get Flutter project external dependencies and print them as Markdown tables
#!/usr/bin/env python
import os
import subprocess
import glob
import yaml
import collections
# 0. Script preparation, changing to repository root directory
path_to_repo_root = "{0}".format(
os.path.dirname(os.path.abspath(__file__)))
os.chdir(path_to_repo_root)
# 1. Get existing pubspec.yaml filepath
print("Getting existing Flutter packages...")
all_repo_file_entities = os.walk(path_to_repo_root)
pubspec_filepaths = []
for root, dirs, files in all_repo_file_entities:
for filepath in files:
if(filepath.endswith("pubspec.yaml")):
pubspec_filepaths.append(os.path.join(root, filepath))
# 2. Run flutter pub get
print("Getting dependencies versions...")
pub_deps = []
for filepath in pubspec_filepaths:
with open(filepath, "r") as stream:
try:
pubspec_document = yaml.safe_load(stream)
project_name = pubspec_document.get("name")
dependencies = pubspec_document.get("dependencies")
for dependency, version in dependencies.items():
if(isinstance(version, str)):
pub_deps.append(
{"dependency": dependency, "version": version,
"project_name": project_name}
)
except yaml.YAMLError as exc:
print(exc)
# 3. Print dependency tables
if(pub_deps.__len__() > 0):
same_dependency_shared_projects = {}
same_dependency_number_shared_projects = {}
print("# Dependencies Table\n")
print("|Dependency|Version|Project Name|")
print("|----------|-------|------------|")
for pub_dep in pub_deps:
dependency = pub_dep.get("dependency")
version = pub_dep.get("version")
project_name = pub_dep.get("project_name")
dependency_with_version = "{0}-{1}".format(dependency, version)
same_dependency_shared_projects[dependency] = same_dependency_shared_projects.get(
dependency, []
) + [project_name]
same_dependency_number_shared_projects[dependency_with_version] = same_dependency_number_shared_projects.get(
dependency_with_version, []
) + [project_name]
print("{0}|{1}|{2}|".format(dependency, version, project_name))
print("\n\n## Projects that share the same dependencies\n")
print("|Dependency|Projects That Share Same Dependency|")
print("|----------|-----------------------------------|")
for dependency, project_names in collections.OrderedDict(sorted(same_dependency_shared_projects.items())).items():
if(project_names.__len__() > 1):
print("{0}|{1}|".format(dependency, project_names))
print("\n\n## Projects that share the same dependencies versions\n")
print("|Dependency|Version|Projects That Share Same Dependency Number|")
print("|----------|-------|------------------------------------------|")
for dependency_version, project_names in collections.OrderedDict(sorted(same_dependency_number_shared_projects.items())).items():
if(project_names.__len__() > 1):
(dependency, version) = dependency_version.split("-")
print("{0}|{1}|{2}|".format(dependency, version, project_names))
else:
print("No external dependencies found.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment