<filename>conductor/execs/workspaces/update.py<gh_stars>1-10
import json
import os
from pathlib import Path
from typing import Any, Dict, List

import yaml

from conductor.utils.colors import modulo_pastel
from conductor.utils.strings import decapitalize, title_case


def parse_workspaces(
    workspaces: Dict[str, List[str]], organizations: List[str], output: str
) -> None:
    Path(output).mkdir(parents=True, exist_ok=True)

    for workspace, projects in workspaces.items():
        with open(os.path.join(output, f"{workspace}.code-workspace"), "w") as stream:
            stream.write(
                json.dumps(
                    workspace_configuration(
                        workspace=workspace,
                        projects=projects,
                        is_organization=title_case(workspace) in organizations,
                    )
                )
            )


def update_settings(
    workspaces: Dict[str, List[str]], organizations: List[str], output: str
) -> None:
    settings_path = os.path.expanduser(
        "~/Library/Application Support/Code/User/settings.json"
    )

    project_data = [
        group_project_data(
            {k: v for k, v in workspaces.items() if title_case(k) not in organizations},
            "projects",
            output,
        ),
        group_project_data(
            {k: v for k, v in workspaces.items() if title_case(k) in organizations},
            "organizations",
            output,
        ),
    ]

    with open(settings_path, "r") as stream:
        settings = json.load(stream)

    with open(settings_path, "w") as stream:
        settings["dashboard.projectData"] = project_data
        stream.write(json.dumps(settings))


def workspace_configuration(
    workspace: str, projects: List[str], is_organization: bool
) -> Dict[str, Any]:
    root = os.path.expanduser(
        os.path.join("~", title_case(workspace) if is_organization else "Workspace")
    )

    return {
        "folders": [
            {
                "name": project,
                "path": os.path.normpath(
                    os.path.join(
                        *(
                            [root, "..", workspace.capitalize(), project]
                            if is_organization
                            else [root, project]
                        )
                    )
                ),
            }
            for project in projects
        ]
    }


def group_project_data(
    workspaces: Dict[str, Any], group_name: str, output: str
) -> Dict[str, Any]:
    return {
        "collapsed": False,
        "id": decapitalize(group_name),
        "groupName": group_name.capitalize(),
        "projects": [
            {
                "color": "#" + modulo_pastel(i),
                "id": workspace.lower(),
                "isGitRepo": False,
                "name": workspace.capitalize(),
                "path": os.path.abspath(
                    os.path.join(output, f"{workspace}.code-workspace")
                ),
            }
            for i, workspace in enumerate(workspaces)
        ],
    }


def update(path: str) -> None:
    organizations = ["Mint", "Papergym"]
    output = os.path.join(os.path.dirname(path), "dist")

    with open(path, "r") as stream:
        workspaces = yaml.safe_load(stream)

    parse_workspaces(workspaces, organizations, output)
    update_settings(workspaces, organizations, output)

<filename>conductor/execs/workspaces/update.py<gh_stars>1-10
import json
import os
from pathlib import Path
from typing import Any, Dict, List

import yaml

from conductor.utils.colors import modulo_pastel
from conductor.utils.strings import decapitalize, title_case


def parse_workspaces(
    workspaces: Dict[str, List[str]], organizations: List[str], output: str
) -> None:
    Path(output).mkdir(parents=True, exist_ok=True)

    for workspace, projects in workspaces.items():
        with open(os.path.join(output, f"{workspace}.code-workspace"), "w") as stream:
            stream.write(
                json.dumps(
                    workspace_configuration(
                        workspace=workspace,
                        projects=projects,
                        is_organization=title_case(workspace) in organizations,
                    )
                )
            )


def update_settings(
    workspaces: Dict[str, List[str]], organizations: List[str], output: str
) -> None:
    settings_path = os.path.expanduser(
        "~/Library/Application Support/Code/User/settings.json"
    )

    project_data = [
        group_project_data(
            {k: v for k, v in workspaces.items() if title_case(k) not in organizations},
            "projects",
            output,
        ),
        group_project_data(
            {k: v for k, v in workspaces.items() if title_case(k) in organizations},
            "organizations",
            output,
        ),
    ]

    with open(settings_path, "r") as stream:
        settings = json.load(stream)

    with open(settings_path, "w") as stream:
        settings["dashboard.projectData"] = project_data
        stream.write(json.dumps(settings))


def workspace_configuration(
    workspace: str, projects: List[str], is_organization: bool
) -> Dict[str, Any]:
    root = os.path.expanduser(
        os.path.join("~", title_case(workspace) if is_organization else "Workspace")
    )

    return {
        "folders": [
            {
                "name": project,
                "path": os.path.normpath(
                    os.path.join(
                        *(
                            [root, "..", workspace.capitalize(), project]
                            if is_organization
                            else [root, project]
                        )
                    )
                ),
            }
            for project in projects
        ]
    }


def group_project_data(
    workspaces: Dict[str, Any], group_name: str, output: str
) -> Dict[str, Any]:
    return {
        "collapsed": False,
        "id": decapitalize(group_name),
        "groupName": group_name.capitalize(),
        "projects": [
            {
                "color": "#" + modulo_pastel(i),
                "id": workspace.lower(),
                "isGitRepo": False,
                "name": workspace.capitalize(),
                "path": os.path.abspath(
                    os.path.join(output, f"{workspace}.code-workspace")
                ),
            }
            for i, workspace in enumerate(workspaces)
        ],
    }


def update(path: str) -> None:
    organizations = ["Mint", "Papergym"]
    output = os.path.join(os.path.dirname(path), "dist")

    with open(path, "r") as stream:
        workspaces = yaml.safe_load(stream)

    parse_workspaces(workspaces, organizations, output)
    update_settings(workspaces, organizations, output)
