Class: Pod::Command::Transform

Inherits:
Pod::Command show all
Defined in:
lib/cocoapods-transform/command/transform.rb

Overview

TODO:

Create a PR to add your plugin to CocoaPods/cocoapods.org in the plugins.json file, once your plugin is released.

This is an example of a cocoapods plugin adding a top-level subcommand to the ‘pod’ command.

You can also create subcommands of existing or new commands. Say you wanted to add a subcommand to list to show newly deprecated pods, (e.g. ‘pod list deprecated`), there are a few things that would need to change.

  • move this file to lib/pod/command/list/deprecated.rb and update the class to exist in the the Pod::Command::List namespace

  • change this class to extend from List instead of Command. This tells the plugin system that it is a subcommand of list.

  • edit lib/cocoapods_plugins.rb to require this file

Instance Method Summary collapse

Instance Method Details

#parse_Podfile(podfile_path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cocoapods-transform/command/transform.rb', line 48

def parse_Podfile(podfile_path)
  active_pods = Array.new
  inactive_pods = Array.new
  File.open(podfile_path, 'r') do |f|
    active_pod_reg = /^[\t ]*pod *('\w+')[\n\r,]/
    inactive_pod_reg = /^[\t ]*#[\t ]*pod *('\w+')[\n\r,]/
    f.each_line do |line|
      if inactive_pod_reg.match(line)
        inactive_pods.push($1)
      elsif active_pod_reg.match(line)
      active_pods.push($1)
      end
    end
  end

  return active_pods & inactive_pods
end

#podfile_pathObject



38
39
40
41
42
43
44
45
46
# File 'lib/cocoapods-transform/command/transform.rb', line 38

def podfile_path
  podfile_path = Dir.pwd+'/Podfile'
  if File.exist?(podfile_path)
    return podfile_path
  else
    help! 'Make sure a Podfile at current directory!'
    return
  end
end

#runObject



32
33
34
35
36
# File 'lib/cocoapods-transform/command/transform.rb', line 32

def run
  path = podfile_path
  pods = parse_Podfile(path)
  transform(path, pods)
end

#transform(podfile_path, pods) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cocoapods-transform/command/transform.rb', line 66

def transform(podfile_path, pods)
  if pods.length == 0
    help! 'Podfile no valid pod to transform!'
    return
  end
  content = File.read(podfile_path)
  for pod in pods
    content = content.sub(/^[\t ]*pod +#{pod}/, "#####pod #{pod}")
    content = content.sub(/^[\t ]*#[\t ]*pod +#{pod}/, "pod #{pod}")
    content = content.sub(/^#####pod +#{pod}/, "#pod #{pod}")
  end

  File.open(podfile_path, 'w') do |f|
    f.puts(content)
  end
end

#validate!Object



28
29
30
# File 'lib/cocoapods-transform/command/transform.rb', line 28

def validate!
  super
end