Skip to content

Commit

Permalink
[automation] Add Python script to synchorize recommended extensions t…
Browse files Browse the repository at this point in the history
…o Gitpod config

Also json5 and pyyaml are added to devDependencies btw.

Signed-off-by: Andrei Jiroh Eugenio Halili <[email protected]>
  • Loading branch information
Andrei Jiroh Eugenio Halili authored and ajhalili2006 committed Dec 1, 2021
1 parent f68c752 commit c725d2b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
71 changes: 71 additions & 0 deletions .automation/syncExts2Gitpod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# !/usr/bin/env python3
"""
Automatically sync recommended extensions from .vscode/extensions.json to .gitpod.yml
"""

# pylint: disable=import-error
import json5 as json # This is needed so comments on .vscode/extensions.json are rendered fine.
import logging
import os
import re
import subprocess
import sys
from datetime import date, datetime
from shutil import copyfile
from typing import Any
import yaml

REPO_HOME = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + ".."
BASE_MAGE = "gitpod/workspace-full"

def replace_in_file(file_path, start, end, content, add_new_line=True):
# Read in the file
with open(file_path, "r", encoding="utf-8") as file:
file_content = file.read()
# Replace the target string
if add_new_line is True:
replacement = f"{start}\n{content}\n{end}"
else:
replacement = f"{start}{content}{end}"
regex = rf"{start}([\s\S]*?){end}"
file_content = re.sub(regex, replacement, file_content, re.DOTALL)
# Write the file out again
with open(file_path, "w", encoding="utf-8") as file:
file.write(file_content)
logging.info("Updated " + file.name)

def updateWSExtensions():
extConfigPath = f"{REPO_HOME}/.vscode/extensions.json"
gitpodConfigPath = f"{REPO_HOME}/.gitpod.yml"

# Load the files first as streams and the load it as Python dictionaries
with open(extConfigPath) as f:
extConfigVSC = json.load(f)
with open(gitpodConfigPath) as f:
gpConfig = yaml.load(f)

# Pull and dump to memory first
recommends = extConfigVSC["recommendations"]
gpConfig['vscode']['extensions'] = recommends

# When done, dump the changes to our Gitpod config
with open(gitpodConfigPath, 'w') as configDump:
yaml.dump(obj, configDump)

if __name__ == "__main__":
try:
logging.basicConfig(
force=True,
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
except ValueError:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)

# Generate vscode.extensions array in .gitpod.yml
updateWSExtensions()
2 changes: 2 additions & 0 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ mdx_truly_sane_lists
beautifulsoup4
giturlparse
json-schema-for-humans
pyyaml
json5

0 comments on commit c725d2b

Please sign in to comment.