space-cadet 9.0.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 (56) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +32 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +279 -0
  5. data/exe/space +13 -0
  6. data/lib/space_core/atomic_write.rb +21 -0
  7. data/lib/space_core/cli/base_command.rb +19 -0
  8. data/lib/space_core/cli/build.rb +27 -0
  9. data/lib/space_core/cli/config.rb +49 -0
  10. data/lib/space_core/cli/current.rb +16 -0
  11. data/lib/space_core/cli/help.rb +195 -0
  12. data/lib/space_core/cli/helpers.rb +115 -0
  13. data/lib/space_core/cli/init.rb +29 -0
  14. data/lib/space_core/cli/list.rb +24 -0
  15. data/lib/space_core/cli/loop_status.rb +47 -0
  16. data/lib/space_core/cli/new.rb +38 -0
  17. data/lib/space_core/cli/pack.rb +29 -0
  18. data/lib/space_core/cli/path.rb +16 -0
  19. data/lib/space_core/cli/repeatable_options.rb +84 -0
  20. data/lib/space_core/cli/repo.rb +76 -0
  21. data/lib/space_core/cli/run.rb +44 -0
  22. data/lib/space_core/cli/shell.rb +125 -0
  23. data/lib/space_core/cli/show.rb +21 -0
  24. data/lib/space_core/cli/status.rb +74 -0
  25. data/lib/space_core/cli/use.rb +17 -0
  26. data/lib/space_core/cli.rb +177 -0
  27. data/lib/space_core/cloner.rb +77 -0
  28. data/lib/space_core/commands.rb +51 -0
  29. data/lib/space_core/config.rb +152 -0
  30. data/lib/space_core/errors.rb +14 -0
  31. data/lib/space_core/git_client.rb +49 -0
  32. data/lib/space_core/mise_client.rb +37 -0
  33. data/lib/space_core/oci_builder.rb +56 -0
  34. data/lib/space_core/oci_packer.rb +103 -0
  35. data/lib/space_core/oci_runner.rb +89 -0
  36. data/lib/space_core/paths.rb +58 -0
  37. data/lib/space_core/repo_reference.rb +19 -0
  38. data/lib/space_core/repo_resolver.rb +167 -0
  39. data/lib/space_core/scm/client.rb +87 -0
  40. data/lib/space_core/scm/git.rb +232 -0
  41. data/lib/space_core/scm/status.rb +24 -0
  42. data/lib/space_core/shell.rb +90 -0
  43. data/lib/space_core/shell_integration.rb +438 -0
  44. data/lib/space_core/slugger.rb +16 -0
  45. data/lib/space_core/space.rb +174 -0
  46. data/lib/space_core/space_store.rb +319 -0
  47. data/lib/space_core/state.rb +86 -0
  48. data/lib/space_core/templates/oci/dockerfile.erb +73 -0
  49. data/lib/space_core/templates/oci/dockerignore.erb +17 -0
  50. data/lib/space_core/templates/oci/entrypoint.sh.erb +16 -0
  51. data/lib/space_core/terminal.rb +161 -0
  52. data/lib/space_core/version.rb +7 -0
  53. data/lib/space_core/warnings.rb +13 -0
  54. data/lib/space_core/xdg.rb +33 -0
  55. data/lib/space_core.rb +26 -0
  56. metadata +199 -0
@@ -0,0 +1,438 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ module Space::Core
6
+ module ShellIntegration
7
+ FISH_TEMPLATE = <<~'FISH'
8
+ # Generated by space-cadet. Do not edit by hand.
9
+
10
+ function __space_core_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_core_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_core_compat_checked
59
+ set -g __space_core_compat_checked 1
60
+ set -l __space_core_installed_version __SPACE_CORE_VERSION__
61
+ set -l __space_core_binary_version (command space --version 2>/dev/null)
62
+ if test "$__space_core_binary_version" != "$__space_core_installed_version"
63
+ echo "space-cadet: shell integration version $__space_core_installed_version does not match binary version $__space_core_binary_version; re-run 'space shell fish install'" >&2
64
+ end
65
+ end
66
+
67
+ set -l __space_command (__space_core_command $argv)
68
+ set -l __space_args $argv
69
+
70
+ if test -t 1; and not __space_core_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-cadet. Do not edit by hand.
101
+
102
+ function __space_core_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_core_complete_needs_command
133
+ test -z "$(__space_core_complete_command)"
134
+ end
135
+
136
+ function __space_core_complete_using_command
137
+ contains -- (__space_core_complete_command) $argv
138
+ end
139
+
140
+ function __space_core_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_core_complete_has_first_argument_after
176
+ set -q argv[1]; or return 1
177
+ set -l first_argument (__space_core_complete_first_argument_after $argv)
178
+ test -n "$first_argument"
179
+ end
180
+
181
+ function __space_core_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_core_complete_has_second_argument_after
218
+ set -q argv[1]; or return 1
219
+ set -l second_argument (__space_core_complete_second_argument_after $argv)
220
+ test -n "$second_argument"
221
+ end
222
+
223
+ function __space_core_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_core_complete_first_argument_after $argv)" = "$expected"
228
+ end
229
+
230
+ function __space_core_complete_spaces
231
+ command space shell complete spaces 2>/dev/null
232
+ end
233
+
234
+ function __space_core_complete_statuses
235
+ command space shell complete statuses 2>/dev/null
236
+ end
237
+
238
+ function __space_core_complete_config_keys
239
+ command space shell complete config-keys 2>/dev/null
240
+ end
241
+
242
+ function __space_core_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_core_complete_config_set_has_key
278
+ test -n "$(__space_core_complete_config_set_key)"
279
+ end
280
+
281
+ function __space_core_complete_config_set_key_is
282
+ test "$(__space_core_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_core_complete_using_command init shell" -l force -d "Overwrite existing files"
288
+ complete -c space -f -n "__space_core_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_core_complete_needs_command" -a init -d "Create default XDG config and state files"
291
+ complete -c space -f -n "__space_core_complete_needs_command" -a new -d "Create a new project space"
292
+ complete -c space -f -n "__space_core_complete_needs_command" -a list -d "List spaces"
293
+ complete -c space -f -n "__space_core_complete_needs_command" -a ls -d "List spaces"
294
+ complete -c space -f -n "__space_core_complete_needs_command" -a show -d "Show space metadata"
295
+ complete -c space -f -n "__space_core_complete_needs_command" -a path -d "Print a space path"
296
+ complete -c space -f -n "__space_core_complete_needs_command" -a use -d "Select and cd to a space with fish integration"
297
+ complete -c space -f -n "__space_core_complete_needs_command" -a current -d "Show the current space"
298
+ complete -c space -f -n "__space_core_complete_needs_command" -a status -d "Report or set a space status"
299
+ complete -c space -f -n "__space_core_complete_needs_command" -a config -d "Show or update config"
300
+ complete -c space -f -n "__space_core_complete_needs_command" -a repo -d "Manage repos in the current space"
301
+ complete -c space -f -n "__space_core_complete_needs_command" -a repos -d "Manage repos in the current space"
302
+ complete -c space -f -n "__space_core_complete_needs_command" -a shell -d "Manage shell integration"
303
+
304
+ complete -c space -f -n "__space_core_complete_using_command show path use" -a "(__space_core_complete_spaces)" -d "Space"
305
+ complete -c space -f -n "__space_core_complete_using_command status" -a "(__space_core_complete_spaces)" -d "Space"
306
+ complete -c space -f -n "__space_core_complete_using_command status" -a "(__space_core_complete_statuses)" -d "Status"
307
+ complete -c space -f -n "__space_core_complete_first_argument_is init shell; and not __space_core_complete_has_second_argument_after shell" -a fish -d "Fish shell"
308
+
309
+ complete -c space -f -n "__space_core_complete_using_command repo repos; and not __space_core_complete_has_first_argument_after repo repos" -a add -d "Clone repos into the current space"
310
+ complete -c space -f -n "__space_core_complete_using_command repo repos; and not __space_core_complete_has_first_argument_after repo repos" -a list -d "List repos in the current space"
311
+ complete -c space -f -n "__space_core_complete_using_command repo repos; and not __space_core_complete_has_first_argument_after repo repos" -a ls -d "List repos in the current space"
312
+ complete -c space -f -n "__space_core_complete_using_command repo repos; and not __space_core_complete_has_first_argument_after repo repos" -a resolve -d "Resolve repo names without cloning"
313
+
314
+ complete -c space -f -n "__space_core_complete_using_command config; and not __space_core_complete_has_first_argument_after config" -a show -d "Show config"
315
+ complete -c space -f -n "__space_core_complete_using_command config; and not __space_core_complete_has_first_argument_after config" -a path -d "Print config path"
316
+ complete -c space -f -n "__space_core_complete_using_command config; and not __space_core_complete_has_first_argument_after config" -a set -d "Set a config value"
317
+ complete -c space -f -n "__space_core_complete_first_argument_is set config; and not __space_core_complete_config_set_has_key" -a "(__space_core_complete_config_keys)" -d "Config key"
318
+ complete -c space -f -n "__space_core_complete_config_set_key_is git_clone_protocol" -a "ssh https" -d "Clone protocol"
319
+ complete -c space -f -n "__space_core_complete_config_set_key_is default_provider" -a "github.com gitlab.com" -d "Git provider"
320
+
321
+ complete -c space -f -n "__space_core_complete_using_command shell; and not __space_core_complete_has_first_argument_after shell" -a init -d "Print shell integration"
322
+ complete -c space -f -n "__space_core_complete_using_command shell; and not __space_core_complete_has_first_argument_after shell" -a fish -d "Manage fish integration and completions"
323
+ complete -c space -f -n "__space_core_complete_using_command shell; and not __space_core_complete_has_first_argument_after shell" -a complete -d "Print completion candidates"
324
+ complete -c space -f -n "__space_core_complete_first_argument_is fish shell; and not __space_core_complete_has_second_argument_after shell" -a install -d "Install fish integration and completions"
325
+ complete -c space -f -n "__space_core_complete_first_argument_is fish shell; and not __space_core_complete_has_second_argument_after shell" -a uninstall -d "Remove fish integration and completions"
326
+ complete -c space -f -n "__space_core_complete_first_argument_is fish shell; and not __space_core_complete_has_second_argument_after shell" -a path -d "Print fish integration paths"
327
+ complete -c space -f -n "__space_core_complete_first_argument_is complete shell; and not __space_core_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_CORE_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-cadet") ||
408
+ (content.include?("function __space_core_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 Space::Core
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,174 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "pathname"
5
+ require "time"
6
+
7
+ module Space::Core
8
+ class Space
9
+ METADATA_FILE = "space.yaml"
10
+ VALID_STATUSES = %w[active paused done archived].freeze
11
+
12
+ # Canonical space.yaml shape: registry under `project:` (renamed from the
13
+ # pre-2.0 `architect:` key). Bumped from 1 in the release that did the
14
+ # rename, since that release shipped no read-side alias for it.
15
+ SCHEMA_VERSION = 2
16
+
17
+ attr_reader :path, :data
18
+
19
+ def self.load(path)
20
+ metadata_path = Pathname.new(path).join(METADATA_FILE)
21
+ raise NotFoundError, "No space metadata found at #{metadata_path}" unless metadata_path.exist?
22
+
23
+ parsed = YAML.safe_load(metadata_path.read, aliases: false) || {}
24
+ raise Error, "Space metadata must contain a YAML mapping: #{metadata_path}" unless parsed.is_a?(Hash)
25
+
26
+ data = stringify_keys(parsed)
27
+ normalize_schema!(data, metadata_path)
28
+ new(Pathname.new(path), data)
29
+ end
30
+
31
+ def self.stringify_keys(hash)
32
+ hash.each_with_object({}) { |(key, value), result| result[key.to_s] = value }
33
+ end
34
+
35
+ # Normalizes a parsed space.yaml hash to canonical schema v2, in place:
36
+ # - future version (> SCHEMA_VERSION) → raise, refuse to misread it.
37
+ # - `architect:` only (v1a) → becomes `project:`.
38
+ # - `project:` only (v1b or v2) → left as-is.
39
+ # - both present, `project:` empty and `architect:` non-empty → the
40
+ # corruption from the old silent-default read path; take `architect:`.
41
+ # - both present and non-empty: identical → keep; differing → raise
42
+ # rather than silently pick one and lose data.
43
+ # Idempotent, so a canonical v2 space is a no-op through this method.
44
+ def self.normalize_schema!(data, metadata_path)
45
+ version = data["version"]
46
+ if version.is_a?(Integer) && version > SCHEMA_VERSION
47
+ raise Error,
48
+ "space.yaml schema version #{version} is newer than this gem supports " \
49
+ "(#{SCHEMA_VERSION}); upgrade space-cadet: #{metadata_path}"
50
+ end
51
+
52
+ legacy = data.delete("architect")
53
+ current = data["project"]
54
+
55
+ data["project"] =
56
+ if legacy.nil?
57
+ current
58
+ elsif current.nil? || empty_project_block?(current)
59
+ legacy
60
+ elsif legacy == current
61
+ current
62
+ else
63
+ raise Error,
64
+ "#{metadata_path} has both 'architect:' and 'project:' blocks with " \
65
+ "conflicting content; resolve manually.\narchitect: #{legacy.inspect}\n" \
66
+ "project: #{current.inspect}"
67
+ end
68
+
69
+ data["version"] = SCHEMA_VERSION
70
+ end
71
+ private_class_method :normalize_schema!
72
+
73
+ def self.empty_project_block?(block)
74
+ block.is_a?(Hash) && Array(block["iterations"]).empty? && block["current_iteration"].nil?
75
+ end
76
+ private_class_method :empty_project_block?
77
+
78
+ def initialize(path, data)
79
+ @path = Pathname.new(path)
80
+ @data = data
81
+ end
82
+
83
+ def id
84
+ data.fetch("id")
85
+ end
86
+
87
+ def title
88
+ data.fetch("title")
89
+ end
90
+
91
+ def status
92
+ data.fetch("status", "active")
93
+ end
94
+
95
+ def repos
96
+ Array(data["repos"]).map do |repo|
97
+ repo.is_a?(Hash) ? self.class.stringify_keys(repo) : { "name" => repo.to_s }
98
+ end
99
+ end
100
+
101
+ def provision_scripts
102
+ Array(data.dig("pack", "provision")).map(&:to_s)
103
+ end
104
+
105
+ def persist_paths
106
+ Array(data.dig("pack", "persist")).map(&:to_s)
107
+ end
108
+
109
+ # Host env vars to forward into the container at `space run` (bare passthrough).
110
+ # Declared per-space so payloads needing credentials run without a wall of flags.
111
+ def run_env
112
+ Array(data.dig("run", "env")).map(&:to_s)
113
+ end
114
+
115
+ def architect
116
+ data["project"]
117
+ end
118
+
119
+ def architect=(val)
120
+ data["project"] = val
121
+ end
122
+
123
+ def metadata_path
124
+ path.join(METADATA_FILE)
125
+ end
126
+
127
+ def save
128
+ AtomicWrite.write(metadata_path, YAML.dump(data))
129
+ self
130
+ end
131
+
132
+ def update_status(status, now: Time.now)
133
+ normalized = status.to_s.downcase
134
+ unless VALID_STATUSES.include?(normalized)
135
+ raise InvalidStatusError, "Invalid status '#{status}'. Expected one of: #{VALID_STATUSES.join(', ')}"
136
+ end
137
+
138
+ data["status"] = normalized
139
+ data["updated_at"] = now.iso8601
140
+ save
141
+ end
142
+
143
+ def add_repo(reference, relative_path:, now: Time.now)
144
+ repo_data = repo_data_for(reference, relative_path:, now:)
145
+ existing = repos.find do |repo|
146
+ repo["full_name"] == repo_data["full_name"] ||
147
+ repo["path"] == repo_data["path"] ||
148
+ repo["name"] == repo_data["name"]
149
+ end
150
+ if existing
151
+ raise RepoExistsError, "Repo '#{repo_data['full_name']}' already exists in #{id}"
152
+ end
153
+
154
+ data["repos"] = repos + [repo_data]
155
+ data["updated_at"] = now.iso8601
156
+ save
157
+ repo_data
158
+ end
159
+
160
+ private
161
+
162
+ def repo_data_for(reference, relative_path:, now:)
163
+ {
164
+ "provider" => reference.provider,
165
+ "organization" => reference.owner,
166
+ "name" => reference.name,
167
+ "full_name" => reference.full_name,
168
+ "clone_url" => reference.clone_url,
169
+ "path" => relative_path.to_s,
170
+ "added_at" => now.iso8601
171
+ }
172
+ end
173
+ end
174
+ end