space-architect 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +284 -0
  4. data/exe/architect +13 -0
  5. data/exe/space +13 -0
  6. data/lib/space_architect/architect_mission.rb +436 -0
  7. data/lib/space_architect/atomic_write.rb +21 -0
  8. data/lib/space_architect/cli/architect.rb +388 -0
  9. data/lib/space_architect/cli/config.rb +61 -0
  10. data/lib/space_architect/cli/current.rb +22 -0
  11. data/lib/space_architect/cli/helpers.rb +117 -0
  12. data/lib/space_architect/cli/init.rb +35 -0
  13. data/lib/space_architect/cli/list.rb +30 -0
  14. data/lib/space_architect/cli/new.rb +43 -0
  15. data/lib/space_architect/cli/options.rb +12 -0
  16. data/lib/space_architect/cli/path.rb +22 -0
  17. data/lib/space_architect/cli/repo.rb +88 -0
  18. data/lib/space_architect/cli/shell.rb +137 -0
  19. data/lib/space_architect/cli/show.rb +27 -0
  20. data/lib/space_architect/cli/space.rb +35 -0
  21. data/lib/space_architect/cli/src.rb +32 -0
  22. data/lib/space_architect/cli/status.rb +39 -0
  23. data/lib/space_architect/cli/use.rb +23 -0
  24. data/lib/space_architect/cli.rb +102 -0
  25. data/lib/space_architect/config.rb +152 -0
  26. data/lib/space_architect/dispatcher.rb +21 -0
  27. data/lib/space_architect/errors.rb +14 -0
  28. data/lib/space_architect/git_client.rb +49 -0
  29. data/lib/space_architect/harness.rb +168 -0
  30. data/lib/space_architect/mise_client.rb +37 -0
  31. data/lib/space_architect/repo_reference.rb +19 -0
  32. data/lib/space_architect/repo_resolver.rb +167 -0
  33. data/lib/space_architect/shell_integration.rb +438 -0
  34. data/lib/space_architect/slugger.rb +16 -0
  35. data/lib/space_architect/space.rb +110 -0
  36. data/lib/space_architect/space_store.rb +319 -0
  37. data/lib/space_architect/state.rb +86 -0
  38. data/lib/space_architect/templates/architect.md.erb +48 -0
  39. data/lib/space_architect/templates/iteration.md.erb +66 -0
  40. data/lib/space_architect/terminal.rb +163 -0
  41. data/lib/space_architect/version.rb +5 -0
  42. data/lib/space_architect/warnings.rb +13 -0
  43. data/lib/space_architect/xdg.rb +33 -0
  44. data/lib/space_architect.rb +26 -0
  45. data/vendor/repo-tender/lib/space_architect/pristine/cli/clone.rb +55 -0
  46. data/vendor/repo-tender/lib/space_architect/pristine/cli/config.rb +66 -0
  47. data/vendor/repo-tender/lib/space_architect/pristine/cli/daemon.rb +347 -0
  48. data/vendor/repo-tender/lib/space_architect/pristine/cli/options.rb +21 -0
  49. data/vendor/repo-tender/lib/space_architect/pristine/cli/org.rb +200 -0
  50. data/vendor/repo-tender/lib/space_architect/pristine/cli/repo.rb +170 -0
  51. data/vendor/repo-tender/lib/space_architect/pristine/cli/status.rb +76 -0
  52. data/vendor/repo-tender/lib/space_architect/pristine/cli/sync.rb +149 -0
  53. data/vendor/repo-tender/lib/space_architect/pristine/cli.rb +137 -0
  54. data/vendor/repo-tender/lib/space_architect/pristine/cloner.rb +75 -0
  55. data/vendor/repo-tender/lib/space_architect/pristine/config/contract.rb +54 -0
  56. data/vendor/repo-tender/lib/space_architect/pristine/config/duration.rb +79 -0
  57. data/vendor/repo-tender/lib/space_architect/pristine/config/model.rb +49 -0
  58. data/vendor/repo-tender/lib/space_architect/pristine/config/store.rb +156 -0
  59. data/vendor/repo-tender/lib/space_architect/pristine/forge/client.rb +31 -0
  60. data/vendor/repo-tender/lib/space_architect/pristine/forge/github.rb +98 -0
  61. data/vendor/repo-tender/lib/space_architect/pristine/launchd/agent.rb +195 -0
  62. data/vendor/repo-tender/lib/space_architect/pristine/launchd/plist.rb +129 -0
  63. data/vendor/repo-tender/lib/space_architect/pristine/log_rotator.rb +46 -0
  64. data/vendor/repo-tender/lib/space_architect/pristine/paths.rb +72 -0
  65. data/vendor/repo-tender/lib/space_architect/pristine/scm/client.rb +87 -0
  66. data/vendor/repo-tender/lib/space_architect/pristine/scm/git.rb +232 -0
  67. data/vendor/repo-tender/lib/space_architect/pristine/scm/status.rb +24 -0
  68. data/vendor/repo-tender/lib/space_architect/pristine/shell.rb +90 -0
  69. data/vendor/repo-tender/lib/space_architect/pristine/state/lock.rb +59 -0
  70. data/vendor/repo-tender/lib/space_architect/pristine/state/store.rb +140 -0
  71. data/vendor/repo-tender/lib/space_architect/pristine/sync/engine.rb +464 -0
  72. data/vendor/repo-tender/lib/space_architect/pristine/sync/repo_plan.rb +215 -0
  73. data/vendor/repo-tender/lib/space_architect/pristine/ui/interactive_reporter.rb +280 -0
  74. data/vendor/repo-tender/lib/space_architect/pristine/ui/json_reporter.rb +39 -0
  75. data/vendor/repo-tender/lib/space_architect/pristine/ui/mode.rb +68 -0
  76. data/vendor/repo-tender/lib/space_architect/pristine/ui/plain_reporter.rb +53 -0
  77. data/vendor/repo-tender/lib/space_architect/pristine/ui/reporter.rb +48 -0
  78. data/vendor/repo-tender/lib/space_architect/pristine/version.rb +7 -0
  79. data/vendor/repo-tender/lib/space_architect/pristine.rb +37 -0
  80. metadata +307 -0
@@ -0,0 +1,438 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ module SpaceArchitect
6
+ module ShellIntegration
7
+ FISH_TEMPLATE = <<~'FISH'
8
+ # Generated by space-architect. Do not edit by hand.
9
+
10
+ function __space_architect_command
11
+ set -l __ps_index 1
12
+
13
+ while test $__ps_index -le (count $argv)
14
+ set -l __ps_arg $argv[$__ps_index]
15
+
16
+ switch "$__ps_arg"
17
+ case "--"
18
+ set __ps_index (math $__ps_index + 1)
19
+ if test $__ps_index -le (count $argv)
20
+ echo $argv[$__ps_index]
21
+ end
22
+ return 0
23
+ case "--color" "--colors"
24
+ set __ps_index (math $__ps_index + 2)
25
+ continue
26
+ case "--color=*" "--colors=*"
27
+ set __ps_index (math $__ps_index + 1)
28
+ continue
29
+ case "-*"
30
+ return 0
31
+ case "*"
32
+ echo $__ps_arg
33
+ return 0
34
+ end
35
+ end
36
+ end
37
+
38
+ function __space_architect_has_color_option
39
+ set -l __ps_index 1
40
+
41
+ while test $__ps_index -le (count $argv)
42
+ set -l __ps_arg $argv[$__ps_index]
43
+
44
+ switch "$__ps_arg"
45
+ case "--color" "--colors" "--color=*" "--colors=*"
46
+ return 0
47
+ case "--"
48
+ return 1
49
+ end
50
+
51
+ set __ps_index (math $__ps_index + 1)
52
+ end
53
+
54
+ return 1
55
+ end
56
+
57
+ function space --wraps space --description "Create and manage project spaces"
58
+ if not set -q __space_architect_compat_checked
59
+ set -g __space_architect_compat_checked 1
60
+ set -l __space_architect_installed_version __SPACE_ARCHITECT_VERSION__
61
+ set -l __space_architect_binary_version (command space --version 2>/dev/null)
62
+ if test "$__space_architect_binary_version" != "$__space_architect_installed_version"
63
+ echo "space-architect: shell integration version $__space_architect_installed_version does not match binary version $__space_architect_binary_version; re-run 'space shell fish install'" >&2
64
+ end
65
+ end
66
+
67
+ set -l __space_command (__space_architect_command $argv)
68
+ set -l __space_args $argv
69
+
70
+ if test -t 1; and not __space_architect_has_color_option $argv
71
+ set __space_args --color=always $__space_args
72
+ end
73
+
74
+ switch "$__space_command"
75
+ case new use
76
+ set -l __space_output (command space $__space_args)
77
+ set -l __space_status $status
78
+
79
+ if test (count $__space_output) -gt 0
80
+ printf "%s\n" $__space_output
81
+ end
82
+
83
+ if test $__space_status -eq 0
84
+ set -l __space_target $__space_output[-1]
85
+ set __space_target (string replace -r "^~(?=/|\$)" $HOME -- $__space_target)
86
+ if test -d "$__space_target"
87
+ cd "$__space_target"
88
+ end
89
+ end
90
+
91
+ return $__space_status
92
+ case "*"
93
+ command space $__space_args
94
+ return $status
95
+ end
96
+ end
97
+ FISH
98
+
99
+ FISH_COMPLETIONS = <<~'FISH'
100
+ # Generated by space-architect. Do not edit by hand.
101
+
102
+ function __space_architect_complete_command
103
+ set -l tokens (commandline -opc)
104
+ set -l index 2
105
+
106
+ while test $index -le (count $tokens)
107
+ set -l token $tokens[$index]
108
+
109
+ switch "$token"
110
+ case "--"
111
+ set index (math $index + 1)
112
+ if test $index -le (count $tokens)
113
+ echo $tokens[$index]
114
+ end
115
+ return 0
116
+ case "--color" "--colors"
117
+ set index (math $index + 2)
118
+ continue
119
+ case "--color=*" "--colors=*"
120
+ set index (math $index + 1)
121
+ continue
122
+ case "-*"
123
+ set index (math $index + 1)
124
+ continue
125
+ case "*"
126
+ echo $token
127
+ return 0
128
+ end
129
+ end
130
+ end
131
+
132
+ function __space_architect_complete_needs_command
133
+ test -z "$(__space_architect_complete_command)"
134
+ end
135
+
136
+ function __space_architect_complete_using_command
137
+ contains -- (__space_architect_complete_command) $argv
138
+ end
139
+
140
+ function __space_architect_complete_first_argument_after
141
+ set -l commands $argv
142
+ set -l tokens (commandline -opc)
143
+ set -l index 2
144
+ set -l matched_command 0
145
+
146
+ while test $index -le (count $tokens)
147
+ set -l token $tokens[$index]
148
+
149
+ switch "$token"
150
+ case "--color" "--colors"
151
+ set index (math $index + 2)
152
+ continue
153
+ case "--color=*" "--colors=*" "-*"
154
+ set index (math $index + 1)
155
+ continue
156
+ end
157
+
158
+ if test $matched_command -eq 0
159
+ if contains -- "$token" $commands
160
+ set matched_command 1
161
+ else
162
+ return 1
163
+ end
164
+ else
165
+ echo $token
166
+ return 0
167
+ end
168
+
169
+ set index (math $index + 1)
170
+ end
171
+
172
+ return 1
173
+ end
174
+
175
+ function __space_architect_complete_has_first_argument_after
176
+ set -q argv[1]; or return 1
177
+ set -l first_argument (__space_architect_complete_first_argument_after $argv)
178
+ test -n "$first_argument"
179
+ end
180
+
181
+ function __space_architect_complete_second_argument_after
182
+ set -q argv[1]; or return 1
183
+ set -l command $argv[1]
184
+ set -l tokens (commandline -opc)
185
+ set -l index 2
186
+ set -l matched_command 0
187
+ set -l matched_first_argument 0
188
+
189
+ while test $index -le (count $tokens)
190
+ set -l token $tokens[$index]
191
+
192
+ switch "$token"
193
+ case "--color" "--colors"
194
+ set index (math $index + 2)
195
+ continue
196
+ case "--color=*" "--colors=*" "-*"
197
+ set index (math $index + 1)
198
+ continue
199
+ end
200
+
201
+ if test $matched_command -eq 0
202
+ test "$token" = "$command"; or return 1
203
+ set matched_command 1
204
+ else if test $matched_first_argument -eq 0
205
+ set matched_first_argument 1
206
+ else
207
+ echo $token
208
+ return 0
209
+ end
210
+
211
+ set index (math $index + 1)
212
+ end
213
+
214
+ return 1
215
+ end
216
+
217
+ function __space_architect_complete_has_second_argument_after
218
+ set -q argv[1]; or return 1
219
+ set -l second_argument (__space_architect_complete_second_argument_after $argv)
220
+ test -n "$second_argument"
221
+ end
222
+
223
+ function __space_architect_complete_first_argument_is
224
+ set -q argv[1]; or return 1
225
+ set -l expected $argv[1]
226
+ set -e argv[1]
227
+ test "$(__space_architect_complete_first_argument_after $argv)" = "$expected"
228
+ end
229
+
230
+ function __space_architect_complete_spaces
231
+ command space shell complete spaces 2>/dev/null
232
+ end
233
+
234
+ function __space_architect_complete_statuses
235
+ command space shell complete statuses 2>/dev/null
236
+ end
237
+
238
+ function __space_architect_complete_config_keys
239
+ command space shell complete config-keys 2>/dev/null
240
+ end
241
+
242
+ function __space_architect_complete_config_set_key
243
+ set -l tokens (commandline -opc)
244
+ set -l index 2
245
+ set -l matched_config 0
246
+ set -l matched_set 0
247
+
248
+ while test $index -le (count $tokens)
249
+ set -l token $tokens[$index]
250
+
251
+ switch "$token"
252
+ case "--color" "--colors"
253
+ set index (math $index + 2)
254
+ continue
255
+ case "--color=*" "--colors=*" "-*"
256
+ set index (math $index + 1)
257
+ continue
258
+ end
259
+
260
+ if test $matched_config -eq 0
261
+ test "$token" = "config"; or return 1
262
+ set matched_config 1
263
+ else if test $matched_set -eq 0
264
+ test "$token" = "set"; or return 1
265
+ set matched_set 1
266
+ else
267
+ echo $token
268
+ return 0
269
+ end
270
+
271
+ set index (math $index + 1)
272
+ end
273
+
274
+ return 1
275
+ end
276
+
277
+ function __space_architect_complete_config_set_has_key
278
+ test -n "$(__space_architect_complete_config_set_key)"
279
+ end
280
+
281
+ function __space_architect_complete_config_set_key_is
282
+ test "$(__space_architect_complete_config_set_key)" = "$argv[1]"
283
+ end
284
+
285
+ complete -c space -f -l color -x -a "auto always never" -d "Color output"
286
+ complete -c space -f -l colors -x -a "auto always never" -d "Color output"
287
+ complete -c space -f -n "__space_architect_complete_using_command init shell" -l force -d "Overwrite existing files"
288
+ complete -c space -f -n "__space_architect_complete_using_command new" -s r -l repo -x -d "Clone a repo into the new space"
289
+
290
+ complete -c space -f -n "__space_architect_complete_needs_command" -a init -d "Create default XDG config and state files"
291
+ complete -c space -f -n "__space_architect_complete_needs_command" -a new -d "Create a new project space"
292
+ complete -c space -f -n "__space_architect_complete_needs_command" -a list -d "List spaces"
293
+ complete -c space -f -n "__space_architect_complete_needs_command" -a ls -d "List spaces"
294
+ complete -c space -f -n "__space_architect_complete_needs_command" -a show -d "Show space metadata"
295
+ complete -c space -f -n "__space_architect_complete_needs_command" -a path -d "Print a space path"
296
+ complete -c space -f -n "__space_architect_complete_needs_command" -a use -d "Select and cd to a space with fish integration"
297
+ complete -c space -f -n "__space_architect_complete_needs_command" -a current -d "Show the current space"
298
+ complete -c space -f -n "__space_architect_complete_needs_command" -a status -d "Set a space status"
299
+ complete -c space -f -n "__space_architect_complete_needs_command" -a config -d "Show or update config"
300
+ complete -c space -f -n "__space_architect_complete_needs_command" -a repo -d "Manage repos in the current space"
301
+ complete -c space -f -n "__space_architect_complete_needs_command" -a repos -d "Manage repos in the current space"
302
+ complete -c space -f -n "__space_architect_complete_needs_command" -a shell -d "Manage shell integration"
303
+
304
+ complete -c space -f -n "__space_architect_complete_using_command show path use" -a "(__space_architect_complete_spaces)" -d "Space"
305
+ complete -c space -f -n "__space_architect_complete_using_command status" -a "(__space_architect_complete_spaces)" -d "Space"
306
+ complete -c space -f -n "__space_architect_complete_using_command status" -a "(__space_architect_complete_statuses)" -d "Status"
307
+ complete -c space -f -n "__space_architect_complete_first_argument_is init shell; and not __space_architect_complete_has_second_argument_after shell" -a fish -d "Fish shell"
308
+
309
+ complete -c space -f -n "__space_architect_complete_using_command repo repos; and not __space_architect_complete_has_first_argument_after repo repos" -a add -d "Clone repos into the current space"
310
+ complete -c space -f -n "__space_architect_complete_using_command repo repos; and not __space_architect_complete_has_first_argument_after repo repos" -a list -d "List repos in the current space"
311
+ complete -c space -f -n "__space_architect_complete_using_command repo repos; and not __space_architect_complete_has_first_argument_after repo repos" -a ls -d "List repos in the current space"
312
+ complete -c space -f -n "__space_architect_complete_using_command repo repos; and not __space_architect_complete_has_first_argument_after repo repos" -a resolve -d "Resolve repo names without cloning"
313
+
314
+ complete -c space -f -n "__space_architect_complete_using_command config; and not __space_architect_complete_has_first_argument_after config" -a show -d "Show config"
315
+ complete -c space -f -n "__space_architect_complete_using_command config; and not __space_architect_complete_has_first_argument_after config" -a path -d "Print config path"
316
+ complete -c space -f -n "__space_architect_complete_using_command config; and not __space_architect_complete_has_first_argument_after config" -a set -d "Set a config value"
317
+ complete -c space -f -n "__space_architect_complete_first_argument_is set config; and not __space_architect_complete_config_set_has_key" -a "(__space_architect_complete_config_keys)" -d "Config key"
318
+ complete -c space -f -n "__space_architect_complete_config_set_key_is git_clone_protocol" -a "ssh https" -d "Clone protocol"
319
+ complete -c space -f -n "__space_architect_complete_config_set_key_is default_provider" -a "github.com gitlab.com" -d "Git provider"
320
+
321
+ complete -c space -f -n "__space_architect_complete_using_command shell; and not __space_architect_complete_has_first_argument_after shell" -a init -d "Print shell integration"
322
+ complete -c space -f -n "__space_architect_complete_using_command shell; and not __space_architect_complete_has_first_argument_after shell" -a fish -d "Manage fish integration and completions"
323
+ complete -c space -f -n "__space_architect_complete_using_command shell; and not __space_architect_complete_has_first_argument_after shell" -a complete -d "Print completion candidates"
324
+ complete -c space -f -n "__space_architect_complete_first_argument_is fish shell; and not __space_architect_complete_has_second_argument_after shell" -a install -d "Install fish integration and completions"
325
+ complete -c space -f -n "__space_architect_complete_first_argument_is fish shell; and not __space_architect_complete_has_second_argument_after shell" -a uninstall -d "Remove fish integration and completions"
326
+ complete -c space -f -n "__space_architect_complete_first_argument_is fish shell; and not __space_architect_complete_has_second_argument_after shell" -a path -d "Print fish integration paths"
327
+ complete -c space -f -n "__space_architect_complete_first_argument_is complete shell; and not __space_architect_complete_has_second_argument_after shell" -a "spaces statuses config-keys config-values shells color-modes repo-subcommands config-subcommands fish-subcommands" -d "Completion kind"
328
+ FISH
329
+
330
+ def self.for(shell)
331
+ case shell.to_s
332
+ when "fish"
333
+ FISH_TEMPLATE.gsub("__SPACE_ARCHITECT_VERSION__", VERSION)
334
+ else
335
+ raise Error, "Unsupported shell '#{shell}'. Expected: fish"
336
+ end
337
+ end
338
+
339
+ def self.completions_for(shell)
340
+ case shell.to_s
341
+ when "fish"
342
+ FISH_COMPLETIONS
343
+ else
344
+ raise Error, "Unsupported shell '#{shell}'. Expected: fish"
345
+ end
346
+ end
347
+
348
+ def self.path_for(shell, env: ENV)
349
+ case shell.to_s
350
+ when "fish"
351
+ XDG.config_home(env: env).join("fish", "functions", "space.fish")
352
+ else
353
+ raise Error, "Unsupported shell '#{shell}'. Expected: fish"
354
+ end
355
+ end
356
+
357
+ def self.completions_path_for(shell, env: ENV)
358
+ case shell.to_s
359
+ when "fish"
360
+ XDG.config_home(env: env).join("fish", "completions", "space.fish")
361
+ else
362
+ raise Error, "Unsupported shell '#{shell}'. Expected: fish"
363
+ end
364
+ end
365
+
366
+ def self.install(shell, env: ENV, force: false)
367
+ function_result = write_managed_file(
368
+ path: path_for(shell, env:),
369
+ content: self.for(shell),
370
+ force: force,
371
+ description: "fish function"
372
+ )
373
+ completions_result = write_managed_file(
374
+ path: completions_path_for(shell, env:),
375
+ content: completions_for(shell),
376
+ force: force,
377
+ description: "fish completions"
378
+ )
379
+
380
+ function_result.merge(
381
+ completions_action: completions_result.fetch(:action),
382
+ completions_path: completions_result.fetch(:path)
383
+ )
384
+ end
385
+
386
+ def self.uninstall(shell, env: ENV, force: false)
387
+ function_result = remove_managed_file(
388
+ path: path_for(shell, env:),
389
+ content: self.for(shell),
390
+ force: force,
391
+ description: "fish function"
392
+ )
393
+ completions_result = remove_managed_file(
394
+ path: completions_path_for(shell, env:),
395
+ content: completions_for(shell),
396
+ force: force,
397
+ description: "fish completions"
398
+ )
399
+
400
+ function_result.merge(
401
+ completions_action: completions_result.fetch(:action),
402
+ completions_path: completions_result.fetch(:path)
403
+ )
404
+ end
405
+
406
+ def self.managed_fish?(content)
407
+ content.include?("Generated by space-architect") ||
408
+ (content.include?("function __space_architect_command") && content.include?("function space --wraps space"))
409
+ end
410
+
411
+ def self.write_managed_file(path:, content:, force:, description:)
412
+ existing = path.read if path.exist?
413
+
414
+ if existing && existing != content && !force && !managed_fish?(existing)
415
+ raise Error, "Refusing to overwrite existing #{description} at #{path}. Re-run with --force."
416
+ end
417
+
418
+ if existing == content
419
+ { action: :unchanged, path: path }
420
+ else
421
+ AtomicWrite.write(path, content)
422
+ { action: existing ? :updated : :installed, path: path }
423
+ end
424
+ end
425
+
426
+ def self.remove_managed_file(path:, content:, force:, description:)
427
+ return { action: :missing, path: path } unless path.exist?
428
+
429
+ existing = path.read
430
+ if existing != content && !force && !managed_fish?(existing)
431
+ raise Error, "Refusing to remove existing #{description} at #{path}. Re-run with --force."
432
+ end
433
+
434
+ FileUtils.rm_f(path)
435
+ { action: :removed, path: path }
436
+ end
437
+ end
438
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SpaceArchitect
4
+ module Slugger
5
+ module_function
6
+
7
+ def slug(value)
8
+ slug = value.to_s.downcase.strip
9
+ .gsub(/[^a-z0-9]+/, "-")
10
+ .gsub(/\A-+|-+\z/, "")
11
+ .gsub(/-+/, "-")
12
+
13
+ slug.empty? ? "space" : slug
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "pathname"
5
+ require "time"
6
+
7
+ module SpaceArchitect
8
+ class Space
9
+ METADATA_FILE = "space.yaml"
10
+ VALID_STATUSES = %w[active paused done archived].freeze
11
+
12
+ attr_reader :path, :data
13
+
14
+ def self.load(path)
15
+ metadata_path = Pathname.new(path).join(METADATA_FILE)
16
+ raise NotFoundError, "No space metadata found at #{metadata_path}" unless metadata_path.exist?
17
+
18
+ parsed = YAML.safe_load(metadata_path.read, aliases: false) || {}
19
+ raise Error, "Space metadata must contain a YAML mapping: #{metadata_path}" unless parsed.is_a?(Hash)
20
+
21
+ new(Pathname.new(path), stringify_keys(parsed))
22
+ end
23
+
24
+ def self.stringify_keys(hash)
25
+ hash.each_with_object({}) { |(key, value), result| result[key.to_s] = value }
26
+ end
27
+
28
+ def initialize(path, data)
29
+ @path = Pathname.new(path)
30
+ @data = data
31
+ end
32
+
33
+ def id
34
+ data.fetch("id")
35
+ end
36
+
37
+ def title
38
+ data.fetch("title")
39
+ end
40
+
41
+ def status
42
+ data.fetch("status", "active")
43
+ end
44
+
45
+ def repos
46
+ Array(data["repos"]).map do |repo|
47
+ repo.is_a?(Hash) ? self.class.stringify_keys(repo) : { "name" => repo.to_s }
48
+ end
49
+ end
50
+
51
+ def architect
52
+ data["architect"]
53
+ end
54
+
55
+ def architect=(val)
56
+ data["architect"] = val
57
+ end
58
+
59
+ def metadata_path
60
+ path.join(METADATA_FILE)
61
+ end
62
+
63
+ def save
64
+ AtomicWrite.write(metadata_path, YAML.dump(data))
65
+ self
66
+ end
67
+
68
+ def update_status(status, now: Time.now)
69
+ normalized = status.to_s.downcase
70
+ unless VALID_STATUSES.include?(normalized)
71
+ raise InvalidStatusError, "Invalid status '#{status}'. Expected one of: #{VALID_STATUSES.join(', ')}"
72
+ end
73
+
74
+ data["status"] = normalized
75
+ data["updated_at"] = now.iso8601
76
+ save
77
+ end
78
+
79
+ def add_repo(reference, relative_path:, now: Time.now)
80
+ repo_data = repo_data_for(reference, relative_path:, now:)
81
+ existing = repos.find do |repo|
82
+ repo["full_name"] == repo_data["full_name"] ||
83
+ repo["path"] == repo_data["path"] ||
84
+ repo["name"] == repo_data["name"]
85
+ end
86
+ if existing
87
+ raise RepoExistsError, "Repo '#{repo_data['full_name']}' already exists in #{id}"
88
+ end
89
+
90
+ data["repos"] = repos + [repo_data]
91
+ data["updated_at"] = now.iso8601
92
+ save
93
+ repo_data
94
+ end
95
+
96
+ private
97
+
98
+ def repo_data_for(reference, relative_path:, now:)
99
+ {
100
+ "provider" => reference.provider,
101
+ "organization" => reference.owner,
102
+ "name" => reference.name,
103
+ "full_name" => reference.full_name,
104
+ "clone_url" => reference.clone_url,
105
+ "path" => relative_path.to_s,
106
+ "added_at" => now.iso8601
107
+ }
108
+ end
109
+ end
110
+ end