Class: Textus::Action::DataMv

Inherits:
Base
  • Object
show all
Defined in:
lib/textus/action/data_mv.rb

Class Method Summary collapse

Methods inherited from Base

inherited, proposal_from

Methods included from Contract::DSL

#arg, #cli, #cli_stdin, #contract, #contract?, #summary, #surfaces, #verb, #view

Class Method Details

.call(container:, call:, from:, to:, dry_run: false) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/textus/action/data_mv.rb', line 20

def self.call(container:, call:, from:, to:, dry_run: false, **) # rubocop:disable Lint/UnusedMethodArgument
  manifest = container.manifest
  geom = container.geometry

  return Failure(code: :usage_error, message: "from and to required") if from.nil? || to.nil? || from.empty? || to.empty?
  return Failure(code: :usage_error, message: "data lane '#{from}' not declared") unless manifest.data.declared_lane_kinds.key?(from)

  dest_dir = geom.lane_path(to)
  return Failure(code: :usage_error, message: "destination 'data/#{to}' already exists") if File.exist?(dest_dir)

  affected_keys = manifest.data.entries.select { |entry| entry.lane == from }.map(&:key)

  steps = [{ "op" => "rename_zone", "from" => from, "to" => to }]
  steps += affected_keys.map do |key|
    { "op" => "mv", "from" => key, "to" => "#{to}#{key[from.length..]}" }
  end

  plan = Textus::Store::Jobs::Plan.new(steps: steps, warnings: [])
  return Success(plan) if dry_run

  rewrite_manifest!(geom, from:, to:)
  FileUtils.mv(geom.lane_path(from), dest_dir)
  Success(plan)
end

.rewrite_manifest!(geom, from:, to:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/textus/action/data_mv.rb', line 45

def self.rewrite_manifest!(geom, from:, to:)
  path = geom.manifest_path
  raw = YAML.safe_load_file(path, permitted_classes: [Symbol], aliases: false)
  raw["lanes"].each { |lane| lane["name"] = to if lane["name"] == from }
  raw["entries"].each do |entry|
    entry["lane"] = to if entry["lane"] == from
    entry["key"] = entry["key"].sub(/\A#{Regexp.escape(from)}(\.|\z)/, "#{to}\\1")
    entry["path"] = entry["path"].sub(%r{\A(data/)?#{Regexp.escape(from)}(/|\z)}, "\\1#{to}\\2")
  end
  File.write(path, YAML.dump(raw))
end