-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathinstall_and_update.gd
More file actions
281 lines (217 loc) · 7.87 KB
/
install_and_update.gd
File metadata and controls
281 lines (217 loc) · 7.87 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@tool
extends Node
enum HttpRequestState {
IDLE,
FETCHING_RELEASE_INFO,
DOWNLOADING_BINARY,
}
const URL_GITHUB_API_LATEST_RELEASE = "https://api.github.com/repos/gdquest/GDScript-formatter/releases/latest"
signal installation_completed(binary_path: String)
signal installation_failed(error_message: String)
signal show_file_select_dialog
signal zip_file_selected(path: String)
var http_request_state := HttpRequestState.IDLE
var http_request: HTTPRequest = HTTPRequest.new()
var formatter_cache_dir: String
func _init(cache_dir: String) -> void:
formatter_cache_dir = cache_dir
func _ready() -> void:
add_child(http_request)
http_request.request_completed.connect(_on_request_completed)
zip_file_selected.connect(_process_selected_zip_file)
func install_or_update_formatter() -> void:
print("Starting GDScript formatter installation...")
http_request_state = HttpRequestState.FETCHING_RELEASE_INFO
http_request.request(URL_GITHUB_API_LATEST_RELEASE)
func manual_install_formatter() -> void:
print("Starting GDScript formatter installation...")
show_file_select_dialog.emit()
pass
func _process_selected_zip_file(path: String) -> void:
if not path:
push_error("Path is invalid")
return
var file := FileAccess.open(path, FileAccess.READ)
if not file:
push_error("Can not open the file(%s)" % path)
return
var body: PackedByteArray = file.get_buffer(file.get_length())
var asset_info := _get_platform_info()
if asset_info.is_empty():
var error_message := "Failed to determine platform information"
push_error(error_message)
installation_failed.emit(error_message)
return
print("Extracting and installing binary...")
var binary_path := _download_and_install_binary(body, asset_info)
if binary_path.is_empty():
var error_message := "Installation failed"
push_error(error_message)
installation_failed.emit(error_message)
return
print(
"\n".join(
[
"GDScript formatter installed successfully!",
"Binary location: " + binary_path,
],
),
)
installation_completed.emit(binary_path)
func _on_request_completed(
_http_result: int,
response_code: int,
_http_headers: PackedStringArray,
body: PackedByteArray,
) -> void:
if response_code != 200:
var error_message := "HTTP request failed. Response code: " + str(response_code)
push_error(error_message)
http_request_state = HttpRequestState.IDLE
installation_failed.emit(error_message)
return
match http_request_state:
HttpRequestState.FETCHING_RELEASE_INFO:
_process_response_latest_release(body)
HttpRequestState.DOWNLOADING_BINARY:
_process_response_download_file(body)
_:
var error_message := "Unexpected HTTP request state: " + str(http_request_state)
push_error(error_message)
http_request_state = HttpRequestState.IDLE
installation_failed.emit(error_message)
func _process_response_latest_release(body: PackedByteArray) -> void:
var json = JSON.parse_string(body.get_string_from_utf8())
if not json or not json.has("assets"):
var error_message := "Failed to parse release information from GitHub API"
push_error(error_message)
http_request_state = HttpRequestState.IDLE
installation_failed.emit(error_message)
return
print("GDScript Formatter release information loaded successfully")
var assets = json["assets"]
var tag = json["tag_name"]
var download_url := _find_matching_asset(assets, tag)
if download_url.is_empty():
var error_message := "No matching binary found for current platform"
push_error(error_message)
http_request_state = HttpRequestState.IDLE
installation_failed.emit(error_message)
return
print(
"Found compatible release, starting download...\n",
"Download URL: ",
download_url,
)
http_request_state = HttpRequestState.DOWNLOADING_BINARY
http_request.request(download_url)
func _process_response_download_file(body: PackedByteArray) -> void:
http_request_state = HttpRequestState.IDLE
print("Download completed successfully (", body.size(), " bytes)")
var asset_info := _get_platform_info()
if asset_info.is_empty():
var error_message := "Failed to determine platform information"
push_error(error_message)
installation_failed.emit(error_message)
return
print("Extracting and installing binary...")
var binary_path := _download_and_install_binary(body, asset_info)
if binary_path.is_empty():
var error_message := "Installation failed"
push_error(error_message)
installation_failed.emit(error_message)
return
print(
"\n".join(
[
"GDScript formatter installed successfully!",
"Binary location: " + binary_path,
],
),
)
installation_completed.emit(binary_path)
func _get_platform_info() -> Dictionary:
var os_name := OS.get_name().to_lower()
var processor_name := OS.get_processor_name().to_lower()
var architecture := "x86_64"
if processor_name.contains("aarch64") or processor_name.contains("arm64"):
architecture = "aarch64"
elif processor_name.contains("x86_64") or processor_name.contains("amd64"):
architecture = "x86_64"
var binary_name := "gdscript-formatter"
if os_name.contains("windows"):
binary_name = "gdscript-formatter.exe"
var platform_info := {
"architecture": architecture,
"binary_name": binary_name,
}
if os_name.contains("windows"):
platform_info["os"] = "windows"
elif os_name.contains("linux"):
platform_info["os"] = "linux"
elif os_name.contains("macos") or os_name.contains("osx"):
platform_info["os"] = "macos"
return platform_info
func _find_matching_asset(assets: Array, tag: String) -> String:
var platform_info := _get_platform_info()
if platform_info.is_empty():
return ""
var expected_pattern := "gdscript-formatter-%s-%s-%s" % [tag, platform_info["os"], platform_info["architecture"]]
if platform_info["os"] == "windows":
expected_pattern += ".exe"
expected_pattern += ".zip"
for asset in assets:
var asset_name: String = asset["name"]
if asset_name == expected_pattern:
return asset["browser_download_url"]
return ""
func _download_and_install_binary(zip_data: PackedByteArray, platform_info: Dictionary) -> String:
var binary_name: String = platform_info["binary_name"]
# We get the contents of the zip file as a byte array. we need to write
# it to a file to be able to read it. We use a temporary file for that.
var temp_archive_path := formatter_cache_dir.path_join("temp_formatter_archive.zip")
print("Installing to cache directory: ", formatter_cache_dir)
# Create the gdquest cache directory if it doesn't exist
if not DirAccess.dir_exists_absolute(formatter_cache_dir):
var dir_result := DirAccess.make_dir_recursive_absolute(formatter_cache_dir)
if dir_result != OK:
push_error("Failed to create cache directory: ", formatter_cache_dir)
return ""
var temp_file := FileAccess.open(temp_archive_path, FileAccess.WRITE)
if not temp_file:
push_error("Failed to create temporary archive file")
return ""
temp_file.store_buffer(zip_data)
temp_file.close()
var zip_reader := ZIPReader.new()
var err := zip_reader.open(temp_archive_path)
if err != OK:
push_error("Failed to open ZIP archive from temporary file")
zip_reader.close()
DirAccess.remove_absolute(temp_archive_path)
return ""
var files := zip_reader.get_files()
var binary_data: PackedByteArray
var found_binary := false
var actual_binary_name := ""
for file_path in files:
if not file_path.ends_with("/"):
binary_data = zip_reader.read_file(file_path)
actual_binary_name = file_path.get_file()
found_binary = true
break
zip_reader.close()
DirAccess.remove_absolute(temp_archive_path)
if not found_binary:
push_error("No executable found in ZIP archive")
return ""
var binary_path := formatter_cache_dir.path_join(binary_name)
var file := FileAccess.open(binary_path, FileAccess.WRITE)
if not file:
push_error("Failed to create binary file at: ", binary_path)
return ""
file.store_buffer(binary_data)
file.close()
if not OS.get_name().to_lower().contains("windows"):
OS.execute("chmod", ["+x", binary_path])
return binary_path