-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxpak.gleam
More file actions
361 lines (339 loc) · 10.7 KB
/
Copy pathmxpak.gleam
File metadata and controls
361 lines (339 loc) · 10.7 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//// Provides the public mxpak package facade.
//// CLI: mxp install / add / marketplace / update / ...
import gleam/dict
import gleam/io
import gleam/json
import gleam/list
import gleam/option
import gleam/string
import mxpak/audit
import mxpak/cache
import mxpak/cli
import mxpak/config
import mxpak/error
import mxpak/lockfile
import mxpak/marketplace
import mxpak/output
import mxpak/resolver
import mxpak/widget
import mxpak/workspace
import mxpak/workspace/scanner
import mxpak/workspace/status
/// The `version` constant used by the mxpak capability.
pub const version = "1.0.0"
/// Runs this module's command-line entrypoint.
pub fn main() -> Nil {
case ensure_apps_started() {
Error(reason) ->
io.println_error(
"mxpak runtime initialization failed: " <> error.message(reason),
)
Ok(Nil) -> {
let args = get_arguments()
case cli.parse(args) {
cli.Install(project_root) -> run_install(project_root)
cli.Marketplace(project_root) -> run_marketplace(project_root)
cli.Add(name, version_constraint) -> run_add(name, version_constraint)
cli.Remove(name) -> run_remove(name)
cli.Update(name) -> run_update(name)
cli.Outdated(project_root) -> run_outdated(project_root)
cli.List(project_root) -> run_list(project_root)
cli.Info(name) -> run_info(name)
cli.Audit(project_root) -> run_audit(project_root)
cli.CacheClean -> run_cache_clean()
cli.Scan(path) -> run_workspace_scan(path)
cli.Status(path) -> run_workspace_status(path)
cli.Version -> io.println("mxpak v" <> version)
cli.Help -> cli.print_help()
cli.Unknown(cmd) -> {
io.println_error("알 수 없는 명령: " <> cmd)
cli.print_help()
}
}
}
}
}
// -- Install --
fn run_install(project_root: String) -> Nil {
output.log("mxpak v" <> version <> " — 위젯 설치")
case resolver.resolve_all(project_root) {
Ok(results) -> {
let entries =
list.fold(results, dict.new(), fn(acc, r) {
dict.insert(
acc,
r.name,
lockfile.LockEntry(
version: r.version,
hash: r.hash,
content_id: r.content_id,
s3_id: r.s3_id,
kind: r.kind,
),
)
})
case lockfile.write(project_root, entries) {
Error(reason) ->
output.result_error("락파일 쓰기 실패: " <> error.message(reason))
Ok(Nil) -> {
let widgets_json =
list.map(results, fn(r) {
json.object([
#("name", json.string(r.name)),
#("version", json.string(r.version)),
#("classic", json.bool(widget.is_classic(r.kind))),
])
})
|> json.array(fn(x) { x })
output.result_ok(widgets_json)
}
}
}
Error(reason) -> output.result_error(error.message(reason))
}
}
// -- Marketplace --
fn run_marketplace(project_root: String) -> Nil {
case config.get_pat(project_root) {
Ok(option.Some(pat)) ->
case marketplace.run(pat, project_root) {
Ok(Nil) -> Nil
Error(error) ->
io.println_error(
"Marketplace TUI를 시작할 수 없습니다: " <> string.inspect(error),
)
}
Ok(option.None) -> {
io.println_error("MENDIX_PAT가 필요합니다. .env에 설정하세요.")
}
Error(reason) ->
io.println_error("MENDIX_PAT 읽기 실패: " <> error.message(reason))
}
}
// -- Add --
fn run_add(name: String, version_arg: cli.VersionArg) -> Nil {
let version = case version_arg {
cli.VersionSpecified(v) -> v
cli.VersionLatest -> ""
}
case version {
"" -> io.println_error("버전을 지정하세요: mxp add " <> name <> " --version <v>")
v -> {
case config.write_widget(".", name, v, option.None, option.None) {
Ok(_) -> {
output.log(name <> " v" <> v <> " 추가됨")
run_install(".")
}
Error(reason) -> io.println_error("추가 실패: " <> error.message(reason))
}
}
}
}
// -- Remove --
fn run_remove(name: String) -> Nil {
case config.remove_widget(".", name) {
Ok(_) -> output.log(name <> " 제거됨")
Error(reason) -> io.println_error("제거 실패: " <> error.message(reason))
}
}
// -- Update --
fn run_update(name: String) -> Nil {
case name {
"" -> output.log("전체 업데이트: mxp install 실행")
_ -> output.log("업데이트: " <> name <> " (락파일 삭제 후 재설치)")
}
case lockfile.remove(".") {
Ok(Nil) -> run_install(".")
Error(reason) -> io.println_error("락파일 삭제 실패: " <> error.message(reason))
}
}
// -- Outdated --
fn run_outdated(project_root: String) -> Nil {
case config.read(project_root) {
Ok(cfg) -> {
let widgets = dict.to_list(cfg.widgets)
case widgets {
[] -> io.println("설치된 위젯 없음")
_ ->
list.each(widgets, fn(pair) {
let #(name, widget) = pair
io.println(name <> " v" <> widget.version)
})
}
}
Error(reason) -> io.println_error(error.message(reason))
}
}
// -- List --
fn run_list(project_root: String) -> Nil {
case config.read(project_root) {
Ok(cfg) -> {
let widgets = dict.to_list(cfg.widgets)
case widgets {
[] -> io.println("설치된 위젯 없음")
_ ->
list.each(widgets, fn(pair) {
let #(name, widget) = pair
let id_str = case widget.id {
option.Some(id) -> " [" <> string.inspect(id) <> "]"
option.None -> ""
}
let classic_str = case widget.kind {
widget.Classic -> " (classic)"
widget.Pluggable -> ""
}
io.println(
" " <> name <> " v" <> widget.version <> id_str <> classic_str,
)
})
}
}
Error(reason) -> io.println_error(error.message(reason))
}
}
// -- Info --
fn run_info(name: String) -> Nil {
io.println("위젯 정보: " <> name)
io.println("(API 조회는 PAT가 필요합니다)")
}
// -- Audit --
fn run_audit(project_root: String) -> Nil {
case audit.verify(project_root) {
Ok(lock) -> {
let entries = lock
case entries {
[] -> io.println("락파일에 엔트리 없음")
_ -> list.each(entries, fn(pair) { print_audit(pair) })
}
}
Error(_) -> io.println("mxpak.lock 파일이 없습니다. mxp install을 먼저 실행하세요.")
}
}
fn print_audit(entry: audit.EntryAudit) -> Nil {
let name = entry.name
case entry.status {
audit.Verified(version) ->
io.println(" " <> name <> " v" <> version <> " ✓")
audit.HashMismatch(version, expected, actual) ->
io.println(
" "
<> name
<> " v"
<> version
<> " ✗ 해시 불일치 (기대 "
<> string.slice(expected, 0, 12)
<> ", 실제 "
<> string.slice(actual, 0, 12)
<> ")",
)
audit.CacheMissing(version) ->
io.println(" " <> name <> " v" <> version <> " ✗ 캐시 없음")
audit.CacheUnreadable(version, reason) ->
io.println_error(
" " <> name <> " v" <> version <> " — 캐시 읽기 실패: " <> reason,
)
audit.HashUnavailable(version) ->
io.println(" " <> name <> " v" <> version <> " — 해시 없음 (검증 불가)")
}
}
// -- Cache Clean --
fn run_cache_clean() -> Nil {
case cache.clean() {
Ok(_) -> output.log("캐시 삭제 완료")
Error(reason) -> io.println_error("캐시 삭제 실패: " <> error.message(reason))
}
}
// -- Workspace --
fn resolve_workspace_path(path: String) -> String {
case path {
"" -> "."
p -> p
}
}
fn run_workspace_scan(path: String) -> Nil {
let root = resolve_workspace_path(path)
case ensure_workspace_root(root) {
Error(_) -> Nil
Ok(_) -> {
output.log("mxpak v" <> version <> " — 워크스페이스 스캔")
case workspace.read_config(root) {
Ok(config) ->
case scanner.scan_and_dedup(config) {
Ok(result) -> {
io.println_error(
"스캔 완료: "
<> int_to_string(result.total_files)
<> "개 파일, "
<> int_to_string(result.unique_hashes)
<> "개 고유 해시",
)
io.println_error(
"중복 그룹: "
<> int_to_string(result.duplicate_groups)
<> ", 링크: "
<> int_to_string(result.linked_files)
<> "개",
)
io.println_error("절감: " <> format_bytes(result.saved_bytes))
}
Error(reason) ->
io.println_error("스캔 실패: " <> error.message(reason))
}
Error(reason) -> io.println_error("설정 읽기 실패: " <> error.message(reason))
}
}
}
}
fn run_workspace_status(path: String) -> Nil {
let root = resolve_workspace_path(path)
case ensure_workspace_root(root) {
Error(_) -> Nil
Ok(_) ->
case workspace.read_config(root) {
Ok(config) ->
case status.check(config) {
Ok(ws_status) -> io.println(status.format(ws_status))
Error(reason) ->
io.println_error("상태 조회 실패: " <> error.message(reason))
}
Error(reason) -> io.println_error("설정 읽기 실패: " <> error.message(reason))
}
}
}
/// Validates that a workspace root was supplied.
fn ensure_workspace_root(root: String) -> Result(Nil, error.MissingValue) {
case workspace.discover_projects(root) {
Ok([]) -> {
io.println_error("Mendix 프로젝트를 찾을 수 없습니다: " <> root)
io.println_error("이 디렉토리(또는 그 하위 1단계)에 *.mpr 파일이 있는 디렉토리가 있어야 합니다.")
Error(error.MissingValue)
}
Ok(_) -> Ok(Nil)
Error(reason) -> {
io.println_error("프로젝트 검색 실패: " <> error.message(reason))
Error(error.MissingValue)
}
}
}
fn int_to_string(n: Int) -> String {
string.inspect(n)
}
fn format_bytes(bytes: Int) -> String {
case bytes {
b if b >= 1_048_576 -> {
let mb = b / 1_048_576
let remainder = { b % 1_048_576 } * 10 / 1_048_576
int_to_string(mb) <> "." <> int_to_string(remainder) <> " MB"
}
b if b >= 1024 -> {
let kb = b / 1024
int_to_string(kb) <> " KB"
}
b -> int_to_string(b) <> " B"
}
}
// -- FFI --
@external(erlang, "mxpak_ffi", "get_arguments")
fn get_arguments() -> List(String)
@external(erlang, "mxpak_ffi", "ensure_apps_started")
fn ensure_apps_started() -> Result(Nil, error.Error)