Skip to content

Commit

Permalink
rename variables to global and submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Mar 6, 2024
1 parent 0805bb3 commit 84ad296
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 165 deletions.
211 changes: 82 additions & 129 deletions .submodules/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,168 +13,121 @@
be removed).
"""

import json
import os
import subprocess
from pathlib import Path

from config import JsonDict, SubmoduleConfig, load_configs
from configs import GlobalSubmoduleConfig, JsonDict, JsonValue, load_global_configs
from helpers import (
CONFIG_DIR,
DOT_SUBMODULES_DIR,
GIT_PUSH_CHANGES,
git_commit_and_push,
load_jsonc,
merge_json_dicts,
merge_json_lists_of_json_objects,
merge_submodule_file,
)

GIT_PUSH_CHANGES = bool(int(os.getenv("GIT_PUSH_CHANGES", "0")))

def _merge_vscode_code_snippets(global_code_snippets: JsonDict):
def merge(
submodule_code_snippets: JsonValue,
global_code_snippets: JsonDict,
):
assert isinstance(submodule_code_snippets, dict)

def _merge_devcontainer(devcontainer: JsonDict):
with open(".devcontainer.json", "a+", encoding="utf-8") as devcontainer_file:
current_devcontainer = load_jsonc(devcontainer_file)
for key, code_snippet in global_code_snippets.items():
submodule_code_snippets[key] = code_snippet

devcontainer = merge_json_dicts(current_devcontainer, devcontainer)
return submodule_code_snippets

devcontainer_file.truncate(0)
json.dump(devcontainer, devcontainer_file, indent=2, sort_keys=True)
merge_submodule_file(
".vscode/codeforlife.code-snippets",
global_code_snippets,
merge,
)

if GIT_PUSH_CHANGES:
subprocess.run(["git", "add", ".devcontainer.json"], check=True)


def _merge_vscode_settings(settings: JsonDict):
with open(".vscode/settings.json", "a+", encoding="utf-8") as settings_file:
current_settings = load_jsonc(settings_file)
if current_settings is not None:
assert isinstance(current_settings, dict)
settings = merge_json_dicts(current_settings, settings)

settings_file.truncate(0)
def merge_global_config(global_config: GlobalSubmoduleConfig):
if global_config.devcontainer:
merge_submodule_file(
".devcontainer.json",
global_config.devcontainer,
merge_json_dicts,
)

json.dump(settings, settings_file, indent=2, sort_keys=True)
if global_config.vscode:
# Create .vscode directory if not exists.
Path(".vscode").mkdir(exist_ok=True)

if GIT_PUSH_CHANGES:
subprocess.run(["git", "add", ".vscode/settings.json"], check=True)


def _merge_vscode_tasks(tasks: JsonDict):
with open(".vscode/tasks.json", "a+", encoding="utf-8") as tasks_file:
current_tasks = load_jsonc(tasks_file)
if current_tasks is not None:
assert isinstance(current_tasks, dict)
tasks = merge_json_lists_of_json_objects(
current_tasks,
tasks,
list_names_and_obj_id_fields=[("tasks", "label")],
if global_config.vscode.settings:
merge_submodule_file(
".vscode/settings.json",
global_config.vscode.settings,
merge_json_dicts,
)

tasks_file.truncate(0)

json.dump(tasks, tasks_file, indent=2, sort_keys=True)

if GIT_PUSH_CHANGES:
subprocess.run(["git", "add", ".vscode/tasks.json"], check=True)


def _merge_vscode_launch(launch: JsonDict):
with open(".vscode/launch.json", "a+", encoding="utf-8") as launch_file:
current_launch = load_jsonc(launch_file)
if current_launch is not None:
assert isinstance(current_launch, dict)
launch = merge_json_lists_of_json_objects(
current_launch,
launch,
list_names_and_obj_id_fields=[("configurations", "name")],
if global_config.vscode.tasks:
merge_submodule_file(
".vscode/tasks.json",
global_config.vscode.tasks,
merge=lambda submodule_tasks, global_tasks: (
merge_json_lists_of_json_objects(
submodule_tasks,
global_tasks,
list_names_and_obj_id_fields=[("tasks", "label")],
)
),
)

launch_file.truncate(0)

json.dump(launch, launch_file, indent=2, sort_keys=True)

if GIT_PUSH_CHANGES:
subprocess.run(["git", "add", ".vscode/launch.json"], check=True)


def _merge_vscode_code_snippets(code_snippets: JsonDict):
with open(
".vscode/codeforlife.code-snippets", "a+", encoding="utf-8"
) as code_snippets_file:
current_code_snippets = load_jsonc(code_snippets_file)
if current_code_snippets is not None:
assert isinstance(current_code_snippets, dict)

for key, code_snippet in code_snippets.items():
current_code_snippets[key] = code_snippet
code_snippets = current_code_snippets

code_snippets_file.truncate(0)

json.dump(code_snippets, code_snippets_file, indent=2, sort_keys=True)

if GIT_PUSH_CHANGES:
subprocess.run(
["git", "add", ".vscode/codeforlife.code-snippets"],
check=True,
)


def _merge_workspace(workspace: JsonDict):
with open("codeforlife.code-workspace", "a+", encoding="utf-8") as workspace_file:
current_workspace = load_jsonc(workspace_file)
if current_workspace is not None:
assert isinstance(current_workspace, dict)
workspace = merge_json_lists_of_json_objects(
current_workspace,
workspace,
list_names_and_obj_id_fields=[("folders", "path")],
if global_config.vscode.launch:
merge_submodule_file(
".vscode/launch.json",
global_config.vscode.launch,
merge=lambda submodule_launch, global_launch: (
merge_json_lists_of_json_objects(
submodule_launch,
global_launch,
list_names_and_obj_id_fields=[("configurations", "name")],
)
),
)

workspace_file.truncate(0)

json.dump(workspace, workspace_file, indent=2, sort_keys=True)

if GIT_PUSH_CHANGES:
subprocess.run(["git", "add", "codeforlife.code-workspace"], check=True)


def merge_config(config: SubmoduleConfig):
if config.devcontainer:
_merge_devcontainer(config.devcontainer)
if config.vscode:
# Create .vscode directory if not exists.
Path(".vscode").mkdir(exist_ok=True)
if config.vscode.settings:
_merge_vscode_settings(config.vscode.settings)
if config.vscode.tasks:
_merge_vscode_tasks(config.vscode.tasks)
if config.vscode.launch:
_merge_vscode_launch(config.vscode.launch)
if config.vscode.codeSnippets:
_merge_vscode_code_snippets(config.vscode.codeSnippets)
if config.workspace:
_merge_workspace(config.workspace)
if global_config.vscode.codeSnippets:
_merge_vscode_code_snippets(global_config.vscode.codeSnippets)

if global_config.workspace:
merge_submodule_file(
"codeforlife.code-workspace",
global_config.workspace,
merge=lambda submodule_workspace, global_workspace: (
merge_json_lists_of_json_objects(
submodule_workspace,
global_workspace,
list_names_and_obj_id_fields=[("folders", "path")],
)
),
)


def main() -> None:
configs, inheritances = load_configs()
global_configs, inheritances = load_global_configs()

# Process each config.
for key, config in configs.items():
for key, global_config in global_configs.items():
# Skip config if it's not going to merged into any submodules.
if not config.submodules:
if not global_config.submodules:
continue

# Print config details.
print(f"Key: {key}")
if config.description:
print(f"Description: {config.description}")
if global_config.description:
print(f"Description: {global_config.description}")

# Print config inheritances.
if inheritances[key]:
print("Inherits:")
for inheritance in inheritances[key]:
inheritance_description = configs[inheritance].description
inheritance_description = global_configs[inheritance].description
print(
f" - {inheritance}"
+ (
Expand All @@ -186,22 +139,22 @@ def main() -> None:

# Print config submodules.
print("Submodules:")
for submodule in config.submodules:
for submodule in global_config.submodules:
print(f" - {submodule}")

# Merge inherited configs and config into submodule in order.
for submodule in config.submodules:
for submodule in global_config.submodules:
# Change directory to submodule's directory.
os.chdir(f"{CONFIG_DIR}/../{submodule}")
os.chdir(f"{DOT_SUBMODULES_DIR}/../{submodule}")

for inheritance in inheritances[key]:
merge_config(configs[inheritance])
merge_global_config(global_configs[inheritance])

merge_config(config)
merge_global_config(global_config)

if GIT_PUSH_CHANGES:
git_commit_and_push(message="Configured submodule [skip ci]")
os.chdir(f"{CONFIG_DIR}/..")
os.chdir(f"{DOT_SUBMODULES_DIR}/..")
subprocess.run(["git", "add", submodule], check=True)

print("---")
Expand Down
13 changes: 6 additions & 7 deletions .submodules/config.jsonc → .submodules/configs.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is used to define a global-config for the submodules within CFL's
// workspace. This saves us having to manually repeat common configurations
// across 2+ submodules and avoids human error when accidentally copy/pasting
// something incorrectly.
// This file is used to define global submodule-configs for the submodules
// within CFL's workspace. This saves us having to manually repeat common
// configurations across 2+ submodules and avoids human error when accidentally
// copy/pasting something incorrectly.
//
// WARNING: This file is written in JSONC format (JSON with comments). However,
// only single lines comments with no preceding text are allowed. For example:
Expand All @@ -10,9 +10,8 @@
// - "/* multi-line comment, no preceding text */" 🚫
// - "{"age": 27} /* multi-line comment, some preceding text */" 🚫
//
// This config is processed by the __main__.py file in this directory. To
// understand more about this shape of the config data and how it's processed,
// please read __main__.py.
// These configs are processed by .submodules/__main__.py. To understand more
// about the shape of this data, please read .submodules/configs.py.
{
"base": {
"description": "Base configuration to be inherited by all other configurations.",
Expand Down
Loading

0 comments on commit 84ad296

Please sign in to comment.