Class: UsePackwerk::Private::FileMoveOperation

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/use_packwerk/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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/use_packwerk/private/file_move_operation.rb', line 29

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

  # We join the pack name with the rest of the path...
  path_parts = filepath_without_pack_name.split('/')
  Pathname.new(origin_pack.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



18
19
20
21
22
23
24
25
26
# File 'lib/use_packwerk/private/file_move_operation.rb', line 18

def self.destination_pathname_for_package_move(origin_pathname, new_package_root)
  origin_pack = ParsePackwerk.package_from_path(origin_pathname)

  if origin_pack.name == ParsePackwerk::ROOT_PACKAGE_NAME
    new_package_root.join(origin_pathname).cleanpath
  else
    Pathname.new(origin_pathname.to_s.gsub(origin_pack.name, new_package_root.to_s)).cleanpath
  end
end

Instance Method Details

#origin_packObject



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

def origin_pack
  ParsePackwerk.package_from_path(origin_pathname)
end

#spec_file_move_operationObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/use_packwerk/private/file_move_operation.rb', line 51

def spec_file_move_operation
  # 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 origin_pathname.extname == '.rake'
    new_origin_pathname = origin_pathname.sub('/lib/', '/spec/lib/').sub(%r{^lib/}, 'spec/lib/').sub('.rake', '_spec.rb')
    new_destination_pathname = destination_pathname.sub('/lib/', '/spec/lib/').sub(%r{^lib/}, 'spec/lib/').sub('.rake', '_spec.rb')
  else
    new_origin_pathname = origin_pathname.sub('/app/', '/spec/').sub(%r{^app/}, 'spec/').sub('.rb', '_spec.rb')
    new_destination_pathname = destination_pathname.sub('/app/', '/spec/').sub(%r{^app/}, 'spec/').sub('.rb', '_spec.rb')
  end
  FileMoveOperation.new(
    origin_pathname: new_origin_pathname,
    destination_pathname: new_destination_pathname,
    destination_pack: destination_pack
  )
end