Class: Pod::Command::Patch::Create

Inherits:
Pod::Command::Patch show all
Defined in:
lib/pod/command/patch/create.rb

Instance Method Summary collapse

Methods inherited from Pod::Command::Patch

#patch_file, #patches_path

Constructor Details

#initialize(argv) ⇒ Create

Returns a new instance of Create.



17
18
19
20
# File 'lib/pod/command/patch/create.rb', line 17

def initialize(argv)
  @name = argv.shift_argument
  super
end

Instance Method Details

#clear_patches_folder_if_emptyObject



27
28
29
30
31
# File 'lib/pod/command/patch/create.rb', line 27

def clear_patches_folder_if_empty
  if Dir.empty?(patches_path)
    FileUtils.remove_dir(patches_path)
  end
end

#runObject



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
74
75
76
# File 'lib/pod/command/patch/create.rb', line 33

def run
  # create patches folder if it doesn't exist
  FileUtils.mkdir_p(patches_path)

  Dir.mktmpdir('cocoapods-patch-', config.project_root) do |work_dir|
    sandbox = Pod::Sandbox.new(work_dir)
    installer = Pod::Installer.new(sandbox, config.podfile, config.lockfile)
    installer.clean_install = true

    installer.prepare
    installer.resolve_dependencies

    UI.puts "Checking if pod exists in project..."
    specs_by_platform = installer.send :specs_for_pod, @name

    if specs_by_platform.empty?
      clear_patches_folder_if_empty
      help! "Given pod does not exist in project. Did you use incorrect pod name?"

      return
    end

    pod_installer = installer.send :create_pod_installer, @name
    pod_installer.install!

    UI.puts "Creating patch"
    theirs = Pathname.new(work_dir).join(@name).relative_path_from(config.project_root)
    ours = config.project_pods_root.join(@name).relative_path_from(config.project_root)
    gen_diff_cmd = "git diff --no-index '#{theirs}' '#{ours}' > '#{patch_file}'"

    did_succeed = system(gen_diff_cmd)
    if not did_succeed.nil?
      if File.empty?(patch_file)
        File.delete(patch_file)
        clear_patches_folder_if_empty
        UI.warn "Error: no changes detected between current pod and original"
      else
        UI.puts "Created patch #{patch_file} 🎉"
      end
    else
      UI.warn "Error: failed to create patch for #{@name}"
    end
  end
end

#validate!Object



22
23
24
25
# File 'lib/pod/command/patch/create.rb', line 22

def validate!
  super
  help! 'A Pod name is required.' unless @name
end