-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_flake.py
80 lines (67 loc) · 4.31 KB
/
update_flake.py
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
import os
import json
import urllib.request
import subprocess
def fetch_latest_release(repo):
url = f"https://api.github.com/repos/kcl-lang/{repo}/releases/latest"
with urllib.request.urlopen(url) as response:
data = json.loads(response.read().decode())
return data['tag_name'].lstrip('v')
def calculate_hash(url):
try:
print(f"Calculating hash for {url}")
result = subprocess.run(['nix-prefetch-url', url], capture_output=True, text=True, check=True)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Error calculating hash for {url}")
print(f"stderr: {e.stderr}")
raise
def generate_flake():
with open('flake.nix.tpl', 'r') as f:
template = f.read()
kcl_version = fetch_latest_release("kcl")
cli_version = fetch_latest_release("cli")
kubectl_kcl_version = fetch_latest_release("kubectl-kcl")
substitutions = {
'kcl_version': kcl_version,
'cli_version': cli_version,
'kubectl_kcl_version': kubectl_kcl_version,
'cli_hash_x86_64_linux': calculate_hash(f"https://github.com/kcl-lang/cli/releases/download/v{cli_version}/kcl-v{cli_version}-linux-amd64.tar.gz"),
'cli_hash_aarch64_linux': calculate_hash(f"https://github.com/kcl-lang/cli/releases/download/v{cli_version}/kcl-v{cli_version}-linux-arm64.tar.gz"),
'cli_hash_x86_64_darwin': calculate_hash(f"https://github.com/kcl-lang/cli/releases/download/v{cli_version}/kcl-v{cli_version}-darwin-amd64.tar.gz"),
'cli_hash_aarch64_darwin': calculate_hash(f"https://github.com/kcl-lang/cli/releases/download/v{cli_version}/kcl-v{cli_version}-darwin-arm64.tar.gz"),
'language_server_hash_x86_64_linux': calculate_hash(f"https://github.com/kcl-lang/kcl/releases/download/v{kcl_version}/kclvm-v{kcl_version}-linux-amd64.tar.gz"),
'language_server_hash_aarch64_linux': calculate_hash(f"https://github.com/kcl-lang/kcl/releases/download/v{kcl_version}/kclvm-v{kcl_version}-linux-arm64.tar.gz"),
'language_server_hash_x86_64_darwin': calculate_hash(f"https://github.com/kcl-lang/kcl/releases/download/v{kcl_version}/kclvm-v{kcl_version}-darwin-amd64.tar.gz"),
'language_server_hash_aarch64_darwin': calculate_hash(f"https://github.com/kcl-lang/kcl/releases/download/v{kcl_version}/kclvm-v{kcl_version}-darwin-arm64.tar.gz"),
'kubectl_kcl_hash_x86_64_linux': calculate_hash(f"https://github.com/kcl-lang/kubectl-kcl/releases/download/v{kubectl_kcl_version}/kubectl-kcl-linux-amd64.tgz"),
'kubectl_kcl_hash_aarch64_linux': calculate_hash(f"https://github.com/kcl-lang/kubectl-kcl/releases/download/v{kubectl_kcl_version}/kubectl-kcl-linux-arm64.tgz"),
'kubectl_kcl_hash_x86_64_darwin': calculate_hash(f"https://github.com/kcl-lang/kubectl-kcl/releases/download/v{kubectl_kcl_version}/kubectl-kcl-macos-amd64.tgz"),
'kubectl_kcl_hash_aarch64_darwin': calculate_hash(f"https://github.com/kcl-lang/kubectl-kcl/releases/download/v{kubectl_kcl_version}/kubectl-kcl-macos-arm64.tgz"),
}
for key, value in substitutions.items():
template = template.replace(f'{{{{ {key} }}}}', value)
print(f"substitutions: {substitutions}")
warning_comment = "# WARNING: This file is automatically generated. Do not edit directly.\n"
warning_comment += "# Instead, modify the flake.nix.tpl file and run the update script.\n\n"
new_content = warning_comment + template
content_changed = True
if os.path.exists('flake.nix'):
with open('flake.nix', 'r') as f:
old_content = f.read()
if old_content == new_content:
content_changed = False
with open('flake.nix', 'w') as f:
f.write(new_content)
print(f"flake.nix has been generated successfully for KCL version {kcl_version}, CLI version {cli_version}, and kubectl-kcl version {kubectl_kcl_version}.")
# Write outputs to GITHUB_OUTPUT environment file
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write(f"KCL_VERSION={kcl_version}\n")
f.write(f"CLI_VERSION={cli_version}\n")
f.write(f"KUBECTL_KCL_VERSION={kubectl_kcl_version}\n")
f.write(f"CONTENT_CHANGED={str(content_changed).lower()}\n")
f.write("FLAKE_CONTENT<<EOF\n")
f.write(new_content)
f.write("\nEOF\n")
if __name__ == "__main__":
generate_flake()