-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.py
More file actions
135 lines (109 loc) · 4.14 KB
/
install.py
File metadata and controls
135 lines (109 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import pathlib
import re
import urllib.request
def make_parser():
"""Create a command-line argument parser."""
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("platform", choices=["astro", "hugo", "jekyll"])
# Mutually exclusive group for version selection
version_group = parser.add_mutually_exclusive_group()
version_group.add_argument("--tag", help="Use a specific tag")
version_group.add_argument("--commit", help="Use a specific commit hash")
version_group.add_argument("--branch", help="Use a specific branch (default: main)")
parser.add_argument("--site-dir", default=".")
parser.add_argument(
"--output-dir",
default=".github/workflows",
help="Directory to save workflow files",
)
parser.add_argument("--repo", default="omsf/static-site-tools")
return parser
def download_file(url: str, destination: os.PathLike):
"""Download a file from a URL to a local destination."""
print(f"Downloading {url} to {destination}...")
destination = pathlib.Path(destination)
destination.parent.mkdir(parents=True, exist_ok=True)
urllib.request.urlretrieve(url, destination)
print("Download complete.")
def install(
platform: str,
ref_type: str = "branch",
ref_value: str = "main",
output_dir: str = ".github/workflows",
site_dir: str = ".",
repo: str = "omsf/static-site-tools",
):
workflow_dir = output_dir
url_base = f"https://raw.githubusercontent.com/{repo}/"
if ref_type == "tag":
url_base += f"refs/tags/{ref_value}/"
elif ref_type == "commit":
url_base += f"{ref_value}/"
else: # branch
url_base += f"refs/heads/{ref_value}/"
url_base += ".github/workflows/"
url_base += f"example-{platform}-"
files = [
"build-pr.yaml",
"build-push.yaml",
"cleanup-cloudflare.yaml",
"prod-cloudflare.yaml",
"stage-cloudflare.yaml",
]
for file in files:
url = url_base + file
destination = pathlib.Path(workflow_dir) / file
download_file(url, destination)
# Edit the downloaded file to replace the source directory
edit_source_directory(destination, platform, site_dir)
update_reusable_workflow_references(destination, repo, ref_value)
def edit_source_directory(file_path: os.PathLike, platform: str, site_dir: str):
"""Edit the downloaded workflow file to replace the source directory."""
file_path = pathlib.Path(file_path)
# Read the file content
with open(file_path, "r") as f:
content = f.read()
# Replace the platform source directory
old_value = f'"example-{platform}"'
new_value = f'"{site_dir}"'
original_content = content
content = content.replace(old_value, new_value)
# Write the modified content back
with open(file_path, "w") as f:
f.write(content)
def update_reusable_workflow_references(
file_path: os.PathLike, repo: str, ref_for_uses: str
):
"""Pin reusable workflow references to the selected ref."""
file_path = pathlib.Path(file_path)
repos_to_update = {repo, "omsf/static-site-tools"}
original_content = file_path.read_text()
updated_content = original_content
total_replacements = 0
for target_repo in repos_to_update:
pattern = re.compile(rf"(uses:\s+{re.escape(target_repo)}/[^\s@]+@)([^\s]+)")
updated_content, replacements = pattern.subn(
lambda match: match.group(1) + ref_for_uses, updated_content
)
total_replacements += replacements
if total_replacements:
file_path.write_text(updated_content)
if __name__ == "__main__":
parser = make_parser()
args = parser.parse_args()
print(f"Selected platform: {args.platform}")
# Determine which reference type was specified
if args.tag:
ref_type = "tag"
ref_value = args.tag
elif args.commit:
ref_type = "commit"
ref_value = args.commit
else:
ref_type = "branch"
ref_value = args.branch if args.branch else "main"
install(
args.platform, ref_type, ref_value, args.output_dir, args.site_dir, args.repo
)