Class: Packs::Private::FileMoveOperation

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/packs/private/file_move_operation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.destination_pathname_for_new_public_api(origin_pathname) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/packs/private/file_move_operation.rb', line 33

def self.destination_pathname_for_new_public_api(origin_pathname)
  origin_pack = get_origin_pack(origin_pathname)
  if origin_pack
    filepath_without_pack_name = origin_pathname.to_s.gsub("#{origin_pack.name}/", '')
  else
    filepath_without_pack_name = origin_pathname.to_s
  end

  # We join the pack name with the rest of the path...
  path_parts = filepath_without_pack_name.split('/')
  Pathname.new(origin_pack&.name || ParsePackwerk::ROOT_PACKAGE_NAME).join(
    # ... keeping the "app" or "spec"
    T.must(path_parts[0]),
    # ... substituting "controllers," "services," etc. with "public"
    'public',
    # ... then the rest is the same
    T.must(path_parts[2..]).join('/')
    # and we take the cleanpath so `./app/...` becomes `app/...`
  ).cleanpath
end

.destination_pathname_for_package_move(origin_pathname, new_package_root) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/packs/private/file_move_operation.rb', line 23

def self.destination_pathname_for_package_move(origin_pathname, new_package_root)
  origin_pack = get_origin_pack(origin_pathname)
  if origin_pack
    Pathname.new(origin_pathname.to_s.gsub(origin_pack.name, new_package_root.to_s)).cleanpath
  else
    new_package_root.join(origin_pathname).cleanpath
  end
end

.get_filepath_without_pack_name(filepath, pack) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/packs/private/file_move_operation.rb', line 87

def self.get_filepath_without_pack_name(filepath, pack)
  if pack
    filepath.to_s.gsub("#{pack.name}/", '')
  else
    filepath.to_s
  end
end

.get_origin_pack(origin_pathname) ⇒ Object



18
19
20
# File 'lib/packs/private/file_move_operation.rb', line 18

def self.get_origin_pack(origin_pathname)
  Packs.for_file(origin_pathname)
end

Instance Method Details

#origin_packObject



13
14
15
# File 'lib/packs/private/file_move_operation.rb', line 13

def origin_pack
  self.class.get_origin_pack(origin_pathname)
end

#spec_file_move_operationsObject



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
# File 'lib/packs/private/file_move_operation.rb', line 55

def spec_file_move_operations
  path_parts = filepath_without_pack_name.split('/')
  folder = T.must(path_parts[0])
  file_extension = T.must(filepath_without_pack_name.split('.').last)

  # This could probably be implemented by some "strategy pattern" where different extension types are handled by different helpers
  # Such a thing could also include, for example, when moving a controller, moving its ERB view too.
  if folder == 'app'
    new_origin_pathname = spec_pathname_for_app(origin_pathname, file_extension)
    new_destination_pathname = spec_pathname_for_app(destination_pathname, file_extension)
  else
    new_origin_pathname = spec_pathname_for_non_app(origin_pathname, file_extension, folder)
    new_destination_pathname = spec_pathname_for_non_app(destination_pathname, file_extension, folder)
  end

  ops = [
    FileMoveOperation.new(
      origin_pathname: new_origin_pathname,
      destination_pathname: new_destination_pathname,
      destination_pack: destination_pack
    ),
  ]

  # For controllers, also include the request spec (spec/requests/<name>_spec.rb) if it exists.
  # Request specs are named without "controller" suffix (e.g. my_controller -> my_spec)
  request_spec_op = request_spec_file_move_operation
  ops << request_spec_op if request_spec_op

  ops
end