def main()

in scripts/test-examples.py [0:0]


def main(argv):
    script_name = os.path.basename(__file__)

    help_message = f"""
        {BColors.HEADER}Options{BColors.ENDC}:
            -c | version=   : Version we need to apply, if left empty it will calculate the version based
                              on Gradle's Project task.
            -p | path=      : Path where the examples should be found. Defaults to [{examples_path}].
            -g              : Clone the examples before attempting to execute them, this is done via git-clone.
                              The repositories are specified via the "repositories:" section in {config_file}.
            -k              : By default the directory defined in the path will be removed on success.
                              If this flag is set the directory will be kept.
            -v              : Verbose

        {BColors.HEADER}Example{BColors.ENDC}: {script_name} -c <project version>
        """

    projects_dir = examples_path
    p_version = ""
    git_clone_projects = False
    keep_project_dir = False
    try:
        opts, args = getopt.getopt(argv, "hvkgc:p:", ["version=", "path="])
    except getopt.GetoptError:
        Out.usage(script_name, help_message)
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            Out.usage(script_name, help_message)
            sys.exit()
        elif opt in ("-c", "--version"):
            p_version = arg
        elif opt in ("-p", "--path"):
            projects_dir = os.path.abspath(arg)
        elif opt in "-g":
            git_clone_projects = True
        elif opt in "-k":
            keep_project_dir = True
        elif opt in "-v":
            State.verbose = True

    if not p_version:
        Out.info("Version not supplied, inferring...")
        p_version = infer_version()
        if p_version:
            Out.info(f"Version resolved to {BColors.OKGREEN}{p_version}{BColors.ENDC}")
        else:
            Out.error("Unable to resolved a version!")
            exit(2)

    if git_clone_projects:
        Out.info(f"Cloning example repositories to {projects_dir}...")
        clone_repos(load_config(), projects_dir)

    if not os.path.exists(projects_dir):
        Out.error(f"Can not find projects to build, the path {projects_dir} doesn't exist!")
        exit(2)

    projects = next(os.walk(projects_dir))[1]
    if not projects:
        Out.error(f"No projects available at [{projects_dir}]!")
        exit(2)

    for project in projects:
        project_root = f"{projects_dir}/{project}"
        Out.info(f"Processing project [{project_root}]...")
        build_file = infer_build_file(project_root)
        settings_file = infer_gradle_settings_file(project_root)
        update_build(build_file, p_version)
        run_example_build(project_root, build_file=build_file, settings_file=settings_file)

    if not keep_project_dir:
        Out.info(f"Removing {projects_dir}...")
        try:
            shutil.rmtree(projects_dir)
        except Exception as error:
            Out.error(f"Failed deleting {projects_dir}.", error)
    Out.ok(f"Build successful.")