import click
import os

from popper.cli import pass_context, log

ci_files = {
    # ##########################################################3
    "travis": {
        "./.travis.yml": """
---
dist: xenial
language: python
python: 3.7
services: docker
install:
- git clone https://github.com/systemslab/popper /tmp/popper
- export PYTHONUNBUFFERED=1
- pip install /tmp/popper/cli
script: popper run -f {}
"""
    },
    # ##########################################################3
    "circle": {
        "./.circleci/config.yml": """
---
version: 2
jobs:
  build:
    machine: true
    steps:
    - checkout
    - run:
        command: |
        git clone https://github.com/systemslab/popper /tmp/popper
        export PYTHONUNBUFFERED=1
        pip install /tmp/popper/cli
        popper run -f {}
"""
    },
    # ##########################################################3
    "jenkins": {
        "./Jenkinsfile": """
---
stage ('Popper') {{ node {{
  sh "git clone https://github.com/systemslab/popper /tmp/popper"
  sh "export PYTHONUNBUFFERED=1"
  sh "pip install /tmp/popper/cli"
  sh "popper run -f {}"
}}}}
"""
    },
    # ##########################################################3
    "gitlab": {
        ".gitlab-ci.yml": """
---
image: docker:stable

variables:
  DOCKER_DRIVER: overlay

services:
- docker:dind

before_script:
- docker info
- apk update
- apk upgrade
- apk add python python-dev py-pip build-base git bash
- pip install virtualenv
- git clone https://github.com/systemslab/popper /tmp/popper
- pip install /tmp/popper/cli
popper:
  script: popper run -f {}
"""
    },
    # ##########################################################3
    "brigade": {
        "brigade.js": """
const { events, Job } = require("brigadier")
events.on("push", () => {
    var popper = new Job("popper", "python:3.7-slim-stretch")
    popper.tasks = [
        "apt-get update",
        "apt-get install -y git",
        "git clone https://github.com/systemslab/popper /tmp/popper",
        "export PYTHONUNBUFFERED=1",
        "pip install /tmp/popper/cli",
        "popper run -f {}"
    ]
    popper.run()
})
"""
    },
}


@click.command("ci", short_help="Generate CI service configuration files.")
@click.argument(
    "service",
    type=click.Choice(["travis", "circle", "jenkins", "gitlab", "brigade"]),
    required=True,
)
@click.option("-f", "--file", help="Specify workflow to run in CI.", required=True)
@pass_context
def cli(ctx, service, file):
    """Generates configuration files for distinct CI services. This command
    needs to be executed on the root of your Git repository folder.
    """
    if not os.path.exists(".git"):
        log.fail(
            "This command needs to be executed on the root of your "
            "Git project folder (where the .git/ folder is located)."
        )

    for ci_file, ci_file_content in ci_files[service].items():
        ci_file = os.path.join(os.getcwd(), ci_file)
        os.makedirs(os.path.dirname(ci_file), exist_ok=True)
        with open(ci_file, "w") as f:
            f.write(ci_file_content.format(file))

    log.info(f"Wrote {service} configuration successfully.")

@@ -62,2373 +62,548 @@
 log%0A
-%0Aci_files = %7B%0A    # ##########################################################3%0A    %22travis%22: %7B%0A        %22./.travis.yml%22: %22%22%22%0A---%0Adist: xenial%0Alanguage: python%0Apython: 3.7%0Aservices: docker%0Ainstall:%0A- git clone https://github.com/systemslab/popper /tmp/popper%0A- export PYTHONUNBUFFERED=1%0A- pip install /tmp/popper/cli%0Ascript: popper run -f %7B%7D%0A%22%22%22%0A    %7D,%0A    # ##########################################################3%0A    %22circle%22: %7B%0A        %22./.circleci/config.yml%22: %22%22%22%0A---%0Aversion: 2%0Ajobs:%0A  build:%0A    machine: true%0A    steps:%0A    - checkout%0A    - run:%0A        command: %7C%0A        git clone https://github.com/systemslab/popper /tmp/popper%0A        export PYTHONUNBUFFERED=1%0A        pip install /tmp/popper/cli%0A        popper run -f %7B%7D%0A%22%22%22%0A    %7D,%0A    # ##########################################################3%0A    %22jenkins%22: %7B%0A        %22./Jenkinsfile%22: %22%22%22%0A---%0Astage ('Popper') %7B%7B node %7B%7B%0A  sh %22git clone https://github.com/systemslab/popper /tmp/popper%22%0A  sh %22export PYTHONUNBUFFERED=1%22%0A  sh %22pip install /tmp/popper/cli%22%0A  sh %22popper run -f %7B%7D%22%0A%7D%7D%7D%7D%0A%22%22%22%0A    %7D,%0A    # ##########################################################3%0A    %22gitlab%22: %7B%0A        %22.gitlab-ci.yml%22: %22%22%22%0A---%0Aimage: docker:stable%0A%0Avariables:%0A  DOCKER_DRIVER: overlay%0A%0Aservices:%0A- docker:dind%0A%0Abefore_script:%0A- docker info%0A- apk update%0A- apk upgrade%0A- apk add python python-dev py-pip build-base git bash%0A- pip install virtualenv%0A- git clone https://github.com/systemslab/popper /tmp/popper%0A- pip install /tmp/popper/cli%0Apopper:%0A  script: popper run -f %7B%7D%0A%22%22%22%0A    %7D,%0A    # ##########################################################3%0A    %22brigade%22: %7B%0A        %22brigade.js%22: %22%22%22%0Aconst %7B events, Job %7D = require(%22brigadier%22)%0Aevents.on(%22push%22, () =%3E %7B%0A    var popper = new Job(%22popper%22, %22python:3.7-slim-stretch%22)%0A    popper.tasks = %5B%0A        %22apt-get update%22,%0A        %22apt-get install -y git%22,%0A        %22git clone https://github.com/systemslab/popper /tmp/popper%22,%0A        %22export PYTHONUNBUFFERED=1%22,%0A        %22pip install /tmp/popper/cli%22,%0A        %22popper run -f %7B%7D%22%0A    %5D%0A    popper.run()%0A%7D)%0A%22%22%22%0A    %7D,%0A%7D%0A%0A%0A@click.command(%22ci%22, short_help=%22Generate CI service configuration files.%22)%0A@click.argument(%0A    %22service%22,%0A    type=click.Choice(%5B%22travis%22, %22circle%22, %22jenkins%22, %22gitlab%22, %22brigade%22%5D),%0A    required=True,%0A)%0A@click.option(%22-f%22, %22--file%22, help=%22Specify workflow to run in CI.%22, required
+from popper.exporter import WorkflowExporter%0A%0A%0A@click.command(%22ci%22, short_help=%22Generate CI service configuration files.%22)%0A@click.argument(%0A    %22service%22, type=click.Choice(%5B%22travis%22%5D), required=True,%0A)%0A@click.option(%0A    %22-f%22,%0A    %22--file%22,%0A    help=%22Workflow file to be executed by CI service job.%22,%0A    required=False,%0A    default=%22.popper.yml%22,%0A)%0A@click.option(%0A    %22-s%22,%0A    %22--substitution%22,%0A    help=%22A key-value pair defining a substitution. Can be given multiple times.%22,%0A    required=False,%0A    default=list(),%0A    multiple
 =True
+,%0A
 )%0A@p
@@ -640,16 +640,30 @@
 ce, file
+, substitution
 ):%0A    %22
@@ -1015,274 +1015,101 @@
   )%0A
-%0A
     
-for ci_file, ci_file_content in ci_files%5Bservice%5D.items():%0A        ci_file = os.path.join(os.getcwd(), ci_file)%0A        os.makedirs(os.path.dirname(ci_file), exist_ok=True)%0A        with open(ci_file, %22w%22) as f:%0A            f.write(ci_file_content.format(file))%0A
+exporter = WorkflowExporter.get_exporter(service)%0A    exporter.export(file, substitution)
 %0A   

use exporter for 'popper ci' command