from collections import OrderedDict as odict
import itertools
import yaml

"""
Autogenerate `azure-pipelines.yml` file, because I'm too lazy to
figure out how to do it with `strategy: matrix`.
"""

yaml.add_representer(
    odict,
    lambda self, data: yaml.representer.SafeRepresenter.represent_dict(
        self, data.items()
    ),
)


def define_conda_jobs():
    # fmt: off
    ARCH = [
        #"32",
        "64",
    ]
    OPENSSL = {
        # "1_0": ">=1.0,<1.1",
        "1_1": ">=1.1,<1.2",
    }
    BUILD_TYPE = [
        # "Debug", # debug mode, disable optimization
        "RelWithDebInfo", # release mode but keep debugging symbols
    ]
    # fmt: on

    for arch, (ssl_name, ssl_version), build_type in itertools.product(
        ARCH, OPENSSL.items(), BUILD_TYPE
    ):
        yield odict(
            template="azure-pipelines/windows-template.yml",
            parameters=dict(
                arch=arch,
                openssl_version_name=ssl_name,
                openssl_version=ssl_version,
                build_type=build_type,
            ),
        )


def main():
    jobs = [
        odict(
            template="azure-pipelines/get_sources_job.yml",
        ),
    ]
    jobs.extend(define_conda_jobs())

    doc = {"jobs": jobs}

    with open("azure-pipelines.yml", "wt", encoding="utf-8") as file:
        file.write("# NOTE: autogenerated by azure-pipelines.yml.py\n\n")
        yaml.dump(doc, file, default_flow_style=False)


if __name__ == "__main__":
    main()
