Skip to content

Commit

Permalink
Adding trust swift macros command
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaal111 committed Feb 14, 2024
1 parent 04849fd commit 5e8505f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 42 deletions.
9 changes: 9 additions & 0 deletions src/xctools_kamaalio/actions/trust_swift_macros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import click

from xctools_kamaalio.xctools import XcTools


@click.command(context_settings={"ignore_unknown_options": True})
@click.option("--trust-file", required=True)
def trust_swift_macros(trust_file):
XcTools.trust_swift_macros(trust_file_path=trust_file)
2 changes: 1 addition & 1 deletion src/xctools_kamaalio/actions/trust_swift_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
@click.command(context_settings={"ignore_unknown_options": True})
@click.option("--trust-file", required=True)
def trust_swift_plugins(trust_file):
XcTools.trust_swift_version(trust_file_path=trust_file)
XcTools.trust_swift_plugins(trust_file_path=trust_file)
4 changes: 4 additions & 0 deletions src/xctools_kamaalio/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from xctools_kamaalio.actions.bump_version import bump_version
from xctools_kamaalio.actions.export_archive import export_archive
from xctools_kamaalio.actions.trust_swift_plugins import trust_swift_plugins
from xctools_kamaalio.actions.trust_swift_macros import trust_swift_macros
from xctools_kamaalio.actions.test import test
from xctools_kamaalio.actions.build import build

Expand All @@ -18,6 +19,7 @@
"bump-version",
"export-archive",
"trust-swift-plugins",
"trust-swift-macros",
"test",
"build",
"acknowledgments",
Expand All @@ -42,6 +44,8 @@ def cli():
export_archive()
if action == "trust-swift-plugins":
trust_swift_plugins()
if action == "trust-swift-macros":
trust_swift_macros()
if action == "test":
test()
if action == "build":
Expand Down
100 changes: 59 additions & 41 deletions src/xctools_kamaalio/xctools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import subprocess
from enum import Enum
from pathlib import Path
from typing import Literal

Expand Down Expand Up @@ -60,49 +61,16 @@ def bump_version(build_number: int | None, version_number: str | None):
updater.bump_version(build_number=build_number, version_number=version_number)

@classmethod
def trust_swift_version(cls, trust_file_path: str):
# TODO: DO THIS IN PYTHON INSTEAD
cls.__run_command(
command=["zsh", "-c", "mkdir -p ~/Library/org.swift.swiftpm/security/"],
command_type="create security folder",
)
# TODO: DO THIS IN PYTHON INSTEAD
cls.__run_command(
command=[
"zsh",
"-c",
"rm -f ~/Library/org.swift.swiftpm/security/plugins.json",
],
command_type="remove plugins file",
def trust_swift_macros(cls, trust_file_path: str):
cls.__trust_swift_package(
trust_file_path=trust_file_path, file_type=TrustModuleTypes.MACROS
)
# TODO: DO THIS IN PYTHON INSTEAD
cls.__run_command(
command=[
"zsh",
"-c",
"touch ~/Library/org.swift.swiftpm/security/plugins.json",
],
command_type="create plugins file",
)
trust_file = Path(trust_file_path)
trust_file_content = json.loads(trust_file.read_text())
assert isinstance(trust_file_content, list)
trusted_plugins = []
for trust_config in trust_file_content:
for i in range(0, len(trusted_plugins)):
trusted_plugin = trusted_plugins[i]
if trusted_plugin["packageIdentity"] == trust_config["packageIdentity"]:
trusted_plugins[i] = trust_config
break
else:
trusted_plugins.append(trust_config)
for item in Path.home().glob("Library/org.swift.swiftpm"):
trust_output_file = item / "security/plugins.json"
break
else:
raise XcToolsException("Output file not found")

trust_output_file.write_text(json.dumps(trusted_plugins, indent=2))
@classmethod
def trust_swift_plugins(cls, trust_file_path: str):
cls.__trust_swift_package(
trust_file_path=trust_file_path, file_type=TrustModuleTypes.PLUGINS
)

@classmethod
def test(
Expand Down Expand Up @@ -152,6 +120,51 @@ def build(

cls.__run_command(command, "build")

@classmethod
def __trust_swift_package(cls, trust_file_path: str, file_type: "TrustModuleTypes"):
# TODO: DO THIS IN PYTHON INSTEAD
cls.__run_command(
command=["zsh", "-c", "mkdir -p ~/Library/org.swift.swiftpm/security/"],
command_type="create security folder",
)
# TODO: DO THIS IN PYTHON INSTEAD
cls.__run_command(
command=[
"zsh",
"-c",
f"rm -f ~/Library/org.swift.swiftpm/security/{file_type.name}.json",
],
command_type="remove plugins file",
)
# TODO: DO THIS IN PYTHON INSTEAD
cls.__run_command(
command=[
"zsh",
"-c",
f"touch ~/Library/org.swift.swiftpm/security/{file_type.name}.json",
],
command_type="create plugins file",
)
trust_file = Path(trust_file_path)
trust_file_content = json.loads(trust_file.read_text())
assert isinstance(trust_file_content, list)
trusted_modules = []
for trust_config in trust_file_content:
for i in range(0, len(trusted_modules)):
trusted_plugin = trusted_modules[i]
if trusted_plugin["packageIdentity"] == trust_config["packageIdentity"]:
trusted_modules[i] = trust_config
break
else:
trusted_modules.append(trust_config)
for item in Path.home().glob("Library/org.swift.swiftpm"):
trust_output_file = item / f"security/{file_type.name}.json"
break
else:
raise XcToolsException("Output file not found")

trust_output_file.write_text(json.dumps(trusted_modules, indent=2))

@staticmethod
def __run_command(command: list[str], command_type: str):
process = subprocess.Popen(command)
Expand All @@ -162,6 +175,11 @@ def __run_command(command: list[str], command_type: str):
)


class TrustModuleTypes(Enum):
MACROS = "macros"
PLUGINS = "plugins"


class XcToolsException(Exception):
def __init__(self, message: str) -> None:
super().__init__(message)

0 comments on commit 5e8505f

Please sign in to comment.