Class: SmartBox::Box
- Inherits:
-
Object
- Object
- SmartBox::Box
- Defined in:
- lib/smart_box/box.rb
Constant Summary collapse
- SMART_BOX_DIR =
".smart_box"- BOXES_DIR =
File.join(SMART_BOX_DIR, "boxes")
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
-
#workspace_path ⇒ Object
readonly
Returns the value of attribute workspace_path.
Class Method Summary collapse
-
.create(source:, id:, mode:, name: nil) ⇒ Object
— Class methods —.
- .list(source:) ⇒ Object
- .load(source:, id:) ⇒ Object
Instance Method Summary collapse
- #apply(dry_run: false, force: false) ⇒ Object
- #checkpoint(name) ⇒ Object
- #checkpoints ⇒ Object
- #diff(from: nil, to: nil) ⇒ Object
- #discard ⇒ Object
- #export_patch(output:, from: nil, to: nil) ⇒ Object
- #git_status_short ⇒ Object
-
#initialize(source_path:, id:, mode:, name: nil) ⇒ Box
constructor
A new instance of Box.
- #rollback(checkpoint_id) ⇒ Object
- #run(command, env: {}, timeout: nil, allow_dangerous: false) ⇒ Object
- #source_clean? ⇒ Boolean
-
#status_summary ⇒ Object
— Instance methods —.
Constructor Details
#initialize(source_path:, id:, mode:, name: nil) ⇒ Box
Returns a new instance of Box.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/smart_box/box.rb', line 16 def initialize(source_path:, id:, mode:, name: nil) @id = id @mode = mode.to_s @source_path = File.(source_path) @name = name @box_dir = File.join(@source_path, BOXES_DIR, @id) @workspace_path = File.join(@box_dir, "workspace") @metadata_path = File.join(@box_dir, "metadata.yml") @metadata = Metadata.new(@metadata_path) end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/smart_box/box.rb', line 11 def id @id end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
11 12 13 |
# File 'lib/smart_box/box.rb', line 11 def @metadata end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
11 12 13 |
# File 'lib/smart_box/box.rb', line 11 def mode @mode end |
#source_path ⇒ Object (readonly)
Returns the value of attribute source_path.
11 12 13 |
# File 'lib/smart_box/box.rb', line 11 def source_path @source_path end |
#workspace_path ⇒ Object (readonly)
Returns the value of attribute workspace_path.
11 12 13 |
# File 'lib/smart_box/box.rb', line 11 def workspace_path @workspace_path end |
Class Method Details
.create(source:, id:, mode:, name: nil) ⇒ Object
— Class methods —
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 |
# File 'lib/smart_box/box.rb', line 29 def self.create(source:, id:, mode:, name: nil) box = new(source_path: source, id: id, mode: mode, name: name) if Dir.exist?(box.send(:box_dir)) raise BoxAlreadyExistsError, "Box '#{id}' already exists" end FileUtils.mkdir_p(box.send(:box_dir)) FileUtils.mkdir_p(File.join(box.send(:box_dir), "logs")) FileUtils.mkdir_p(File.join(box.send(:box_dir), "patches")) FileUtils.mkdir_p(File.join(box.send(:box_dir), "checkpoints")) mode_instance = box.send(:mode_instance) mode_instance.setup # Capture initial git commit hash initial_commit = box.send(:git_latest_commit) box..id = id box..name = name box..mode = mode.to_s box..source_path = box.source_path box..workspace_path = box.workspace_path box..status = "active" box..created_at = Time.now.utc.iso8601 box..updated_at = Time.now.utc.iso8601 box..base = { "type" => mode.to_s, "source_git_commit" => box.send(:source_git_commit), "source_git_branch" => box.send(:source_git_branch) } box..add_checkpoint( id: "cp-001", name: "initial", git_commit: initial_commit, created_at: Time.now.utc.iso8601 ) box..stats = { "commands_count" => 0, "changed_files_count" => 0 } box..save! box end |
.list(source:) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/smart_box/box.rb', line 91 def self.list(source:) boxes_path = File.join(File.(source), BOXES_DIR) return [] unless Dir.exist?(boxes_path) Dir.each_child(boxes_path).filter_map do |entry| box_path = File.join(boxes_path, entry) next unless Dir.exist?(box_path) = File.join(box_path, "metadata.yml") next unless File.exist?() = YAML.safe_load_file(, permitted_classes: [Time]) next unless .is_a?(Hash) { "id" => ["id"] || entry, "mode" => ["mode"] || "unknown", "status" => ["status"] || "unknown", "updated_at" => ["updated_at"] || "unknown" } end end |
.load(source:, id:) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/smart_box/box.rb', line 75 def self.load(source:, id:) box = new(source_path: source, id: id, mode: nil) unless Dir.exist?(box.send(:box_dir)) raise BoxNotFoundError, "Box '#{id}' not found" end box..load! unless box..id raise BoxNotFoundError, "Box '#{id}' metadata is missing 'id' field" end box end |
Instance Method Details
#apply(dry_run: false, force: false) ⇒ Object
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 |
# File 'lib/smart_box/box.rb', line 233 def apply(dry_run: false, force: false) raise Error, "Workspace does not exist" unless Dir.exist?(@workspace_path) patch_content = diff(from: @metadata.checkpoints.first&.dig("git_commit")) if dry_run return { dry_run: true, patch_size: patch_content.bytesize } end # Check if source is a git repo and if it's clean if Dir.exist?(File.join(@source_path, ".git")) unless force check_source_clean! end # Create backup patch of current source state backup_patch_path = File.join(@box_dir, "patches", "backup.patch") FileUtils.mkdir_p(File.join(@box_dir, "patches")) Dir.chdir(@source_path) do backup = `git diff 2>/dev/null` File.write(backup_patch_path, backup) unless backup.empty? end # Apply the patch Dir.chdir(@source_path) do IO.popen(["git", "apply", "-v"], "w") do |io| io.write(patch_content) end unless $?.success? raise PatchApplyError, "Failed to apply patch. The source project may have conflicts." end # Show what changed result_diff = `git diff 2>/dev/null` result_diff end else # Non-git source: apply patch with patch command backup_patch_path = File.join(@box_dir, "patches", "backup.diff") FileUtils.mkdir_p(File.join(@box_dir, "patches")) File.write(backup_patch_path, "backup not available for non-git source") Dir.chdir(@source_path) do IO.popen(["patch", "-p1", "-N", "-r", "/dev/null"], "w") do |io| io.write(patch_content) end end "Patch applied to non-git source at #{@source_path}" end end |
#checkpoint(name) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/smart_box/box.rb', line 149 def checkpoint(name) raise Error, "Workspace does not exist" unless Dir.exist?(@workspace_path) Dir.chdir(@workspace_path) do system("git", "add", "-A", out: File::NULL, err: File::NULL) system("git", "commit", "--allow-empty", "-m", name, out: File::NULL, err: File::NULL) end commit = git_latest_commit cp_id = "cp-#{format('%03d', (@metadata.checkpoints.size + 1))}" @metadata.load! @metadata.add_checkpoint( id: cp_id, name: name, git_commit: commit, created_at: Time.now.utc.iso8601 ) @metadata.updated_at = Time.now.utc.iso8601 @metadata.save! { id: cp_id, name: name, commit: commit } end |
#checkpoints ⇒ Object
173 174 175 176 177 178 |
# File 'lib/smart_box/box.rb', line 173 def checkpoints @metadata.load! @metadata.checkpoints.map do |cp| { "id" => cp["id"], "name" => cp["name"] } end end |
#diff(from: nil, to: nil) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/smart_box/box.rb', line 196 def diff(from: nil, to: nil) raise Error, "Workspace does not exist" unless Dir.exist?(@workspace_path) Dir.chdir(@workspace_path) do # Stage everything so untracked files appear in diff system("git", "add", "-A", out: File::NULL, err: File::NULL) from_commit = if from resolve_checkpoint_commit(from) else init = @metadata.checkpoints.first init ? init["git_commit"] : "HEAD~1" end to_commit = to ? resolve_checkpoint_commit(to) : nil if to_commit `git diff #{from_commit} #{to_commit} 2>/dev/null` else `git diff --cached #{from_commit} 2>/dev/null` end end end |
#discard ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/smart_box/box.rb', line 287 def discard raise Error, "Box directory does not exist" unless Dir.exist?(@box_dir) # For git-worktree mode, remove the worktree first if %w[git-worktree git_worktree].include?(@metadata.mode) mode_instance.teardown end FileUtils.rm_rf(@box_dir) @metadata.status = "discarded" { id: @id, status: "discarded" } end |
#export_patch(output:, from: nil, to: nil) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/smart_box/box.rb', line 220 def export_patch(output:, from: nil, to: nil) output_path = if output.start_with?("/") output else File.join(Dir.pwd, output) end patch_content = diff(from: from, to: to) File.write(output_path, patch_content) { output: output_path, size: patch_content.bytesize } end |
#git_status_short ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/smart_box/box.rb', line 127 def git_status_short return [] unless Dir.exist?(@workspace_path) Dir.chdir(@workspace_path) do out = `git status --porcelain 2>/dev/null` return [] unless $?.success? out.lines.map(&:chomp).reject(&:empty?) end end |
#rollback(checkpoint_id) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/smart_box/box.rb', line 180 def rollback(checkpoint_id) cp = @metadata.checkpoints.detect { |c| c["id"] == checkpoint_id } raise CheckpointNotFoundError, "Checkpoint '#{checkpoint_id}' not found" unless cp Dir.chdir(@workspace_path) do system("git", "reset", "--hard", cp["git_commit"], out: File::NULL, err: File::NULL) system("git", "clean", "-fd", out: File::NULL, err: File::NULL) end @metadata.load! @metadata.updated_at = Time.now.utc.iso8601 @metadata.save! { box_id: @id, checkpoint_id: checkpoint_id } end |
#run(command, env: {}, timeout: nil, allow_dangerous: false) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/smart_box/box.rb', line 138 def run(command, env: {}, timeout: nil, allow_dangerous: false) runner.run(command, env: env, timeout: timeout, allow_dangerous: allow_dangerous).tap do # Update metadata stats @metadata.load! stats = @metadata.stats stats["commands_count"] = (stats["commands_count"] || 0) + 1 @metadata.updated_at = Time.now.utc.iso8601 @metadata.save! end end |
#source_clean? ⇒ Boolean
300 301 302 303 |
# File 'lib/smart_box/box.rb', line 300 def source_clean? return true unless Dir.exist?(File.join(@source_path, ".git")) Dir.chdir(@source_path) { `git status --porcelain 2>/dev/null`.strip.empty? } end |
#status_summary ⇒ Object
— Instance methods —
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/smart_box/box.rb', line 116 def status_summary { "id" => @id, "mode" => @mode || @metadata.mode, "status" => @metadata.status, "workspace" => @workspace_path, "changed_files" => git_status_short, "checkpoints" => @metadata.checkpoints.map { |cp| { cp["id"] => cp["name"] } } } end |