platformsh / template-builder

Utilities to manage Platform.sh project templates
https://github.com/platformsh-templates
MIT License
15 stars 31 forks source link

Expose migration/modification details #534

Open chadwcarlson opened 2 years ago

chadwcarlson commented 2 years ago

Need a list of changed files and steps that can be exposed to config builder.

Tests:

# test.py in template-builder root

import os
import dodo
# 'cleanup', 'init', 'update', 'platformify', 'branch', 'push'

generate_file = 'generate.sh'
template = 'django3'

def init_file():
    f = open(generate_file, "w")
    f.write("#!/usr/bin/env bash\n\n")
    f.close()

def append_file(header, data):
    f = open(generate_file, "a")
    f.write("\n# {0}\n".format(header))
    for item in data:
        if isinstance(item, str):
            f.write(item + "\n")
    f.close()

init_file()
append_file("Cleanup", dodo.project_factory(template).cleanup)
append_file("Init", dodo.project_factory(template).init)
append_file("Update", dodo.project_factory(template).update)
append_file("Platformify", dodo.project_factory(template).platformify)
append_file("Branch", dodo.project_factory(template).branch)
append_file("Push", dodo.project_factory(template).push)
# generate.sh output
#!/usr/bin/env bash

# Cleanup
rm -rf /Users/chadcarlson/template-builder/templates/django3/build/

# Init
git clone git@github.com:platformsh-templates/django3.git /Users/chadcarlson/template-builder/templates/django3/build/

# Update
cd /Users/chadcarlson/template-builder/templates/django3/build/ && git checkout master && git pull --prune

# Platformify
rsync -aP /Users/chadcarlson/template-builder/templates/django3/files/ /Users/chadcarlson/template-builder/templates/django3/build/

# Branch
cd /Users/chadcarlson/template-builder/templates/django3/build/ && if git rev-parse --verify --quiet updatesLocal; then git checkout master && git branch -D updatesLocal; fi;
cd /Users/chadcarlson/template-builder/templates/django3/build/ && git checkout -b updatesLocal
cd /Users/chadcarlson/template-builder/templates/django3/build/ && git add -A && git diff-index --quiet HEAD || git commit -m "Update to latest upstream."

# Push
cd /Users/chadcarlson/template-builder/templates/django3/build/ && if [ `git rev-parse updatesLocal` != `git rev-parse master` ] ; then git checkout updatesLocal && git push --force -u origin updatesLocal; fi
chadwcarlson commented 2 years ago

Better example,

migrate.py

# migrate.py in template-builder root

import os
import json
import dodo
from datetime import datetime

template = 'drupal9'

mypath = "{0}/templates/{1}/files".format(os.getcwd(), template)
template_link = 'https://raw.githubusercontent.com/platformsh/template-builder/master/'

def get_files(path, depth):
    """Retrieve files added to upstream templates."""
    depth -= 1
    with os.scandir(path) as p:
        for entry in p:
            file_link = entry.path.replace("{0}/".format(mypath),template_link)
            yield file_link
            if entry.is_dir() and depth > 0:
                yield from get_files(entry.path, depth)

def get_commands(data):
    # data = dodo.project_factory(template).update
    commands = []
    for item in data:
        if isinstance(item, str):
            if 'echo' not in item and 'git' not in item and 'rsync' not in item:
                if "&&" in item:
                    if len(item.split(" && ")) == 2:
                        new_item = item.split(" && ")[1]
                        # print(new_item)
                        if 'composer require' in new_item:
                            commands.append(new_item.split(" --")[0])
                        else:
                            commands.append(item.split(" && ")[1])
                else:
                    commands.append(item)
    return commands

def get_update_commands():
    return get_commands(dodo.project_factory(template).update)

def get_platformify_commands():
    return get_commands(dodo.project_factory(template).platformify)

migrate_files = list(get_files(mypath, 10))
update_commands = get_update_commands()
platformify_commands = get_platformify_commands()

data = {
    "template": template,
    "last_updated_on": datetime.today().strftime('%Y-%m-%d-%H:%M:%S'),
    "migration": {
        "files": migrate_files,
        "commands": [*update_commands, *platformify_commands]
    }
}

json_data=json.dumps(data, indent = 4)
with open("{0}/templates/{1}/.platform.migrate.json".format(os.getcwd(), template), "w") as outfile:
    outfile.write(json_data)

templates/drupal9/.platform.migrate.json

{
    "template": "drupal9",
    "last_updated_on": "2022-02-24-11:51:24",
    "migration": {
        "files": [
            "https://raw.githubusercontent.com/platformsh/template-builder/master/config",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/config/sync",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/config/sync/.gitkeep",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/web",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/web/sites",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/web/sites/default",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/web/sites/default/settings.php",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/web/sites/default/settings.platformsh.php",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.editorconfig",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.environment",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/README.md",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.gitignore",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.lando.upstream.yml",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform.app.yaml",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/drush",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/drush/platformsh_generate_drush_yml.php",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform/services.yaml",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform/routes.yaml"
        ],
        "commands": [
            "composer require platformsh/config-reader drush/drush drupal/redis"
        ]
    }
}

templates/laravel/.platform.migrate.json

{
    "template": "laravel",
    "last_updated_on": "2022-02-24-11:51:09",
    "migration": {
        "files": [
            "https://raw.githubusercontent.com/platformsh/template-builder/master/README.md",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.lando.upstream.yml",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform.app.yaml",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform/services.yaml",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform/routes.yaml"
        ],
        "commands": [
            "composer require platformsh/laravel-bridge"
        ]
    }
}
chadwcarlson commented 2 years ago

Things to consider:

chadwcarlson commented 2 years ago
{
    "template": "wordpress-composer",
    "last_updated_on": "2022-02-24-14:18:10",
    "migration": {
        "files": [
            "https://raw.githubusercontent.com/platformsh/template-builder/master/wp-config.php",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/wp-cli.yml",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/plugins",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/plugins/README.txt",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.editorconfig",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/README.md",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.gitignore",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.lando.upstream.yml",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform.app.yaml",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/example.wp-config-local.php",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform/services.yaml",
            "https://raw.githubusercontent.com/platformsh/template-builder/master/.platform/routes.yaml"
        ],
        "commands": [
            "composer config platform.php 7.4",
            "composer update",
            "composer require platformsh/config-reader wp-cli/wp-cli-bundle psy/psysh",
            "composer require None",
            "composer config --unset platform"
        ]
    }
}