Class: Pod::Command::Dependmanager::Add

Inherits:
Pod::Command::Dependmanager show all
Defined in:
lib/cocoapods-dependManager/command/add.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Add

Returns a new instance of Add.



34
35
36
37
38
39
40
41
42
43
# File 'lib/cocoapods-dependManager/command/add.rb', line 34

def initialize(argv)
  @git_tag = argv.option('tag')
  @git_commit = argv.option('commit')
  @git_branch = argv.option('branch')
  @target = argv.option('target')
  @podfile_name = argv.option('podfile')
  @name = argv.shift_argument
  @source = argv.shift_argument
  super
end

Class Method Details

.optionsObject



24
25
26
27
28
29
30
31
32
# File 'lib/cocoapods-dependManager/command/add.rb', line 24

def self.options
  [
    ['--target=TARGET', 'The target where you want to add the dependency'],
    ['--tag=TAG', 'The git tag you want to depend'],
    ['--commit=COMMIT', 'The git commit you want to depend'],
    ['--branch=BRANCH', 'The git branch you want to depend'],
    ['--podfile=PODFILE', 'podfile name, defalut Podfile'],
  ].concat(super)
end

Instance Method Details

#git_url?(name) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/cocoapods-dependManager/command/add.rb', line 96

def git_url?(name)
  http_url?(name) && name.end_with?('.git')
end

#http_url?(name) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/cocoapods-dependManager/command/add.rb', line 91

def http_url?(name)
  prefixs = ['http://', 'https://']
  prefixs.any? { |prefix| name.start_with?(prefix) }
end

#local_path?(name) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/cocoapods-dependManager/command/add.rb', line 104

def local_path?(name)
  prefixs = ['/', '~/', './']
  prefixs.any? { |prefix| name.start_with?(prefix) }
end

#podspec_url?(name) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/cocoapods-dependManager/command/add.rb', line 100

def podspec_url?(name)
  http_url?(name) && name.end_with?('.podspec')
end

#requirementsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cocoapods-dependManager/command/add.rb', line 73

def requirements
  if not @source
    requirements = nil
  elsif git_url?(@source)
    requirements = {:git => @source}
    requirements[:tag] = @git_tag if @git_tag
    requirements[:commit] = @git_commit if @git_commit
    requirements[:branch] = @git_branch if @git_branch
  elsif podspec_url?(@source)
    requirements = {:podspec => @source}
  elsif local_path?(@source)
    requirements = {:path => @source}
  else
    requirements = @source
  end
  requirements
end

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cocoapods-dependManager/command/add.rb', line 51

def run
#   verify_podfile_exists!
  podfile_path = ''
  if @podfile_name
    podfile_path = Pathname.pwd + @podfile_name 
  else
    podfile_path = Pathname.pwd + 'Podfile'  
  end
  podfile = Podfile.from_file(podfile_path)
  contents ||= File.open(podfile_path, 'r:utf-8') { |f| f.read }

  dependency = Dependency.new(@name, self.requirements)

  podfile.target_definitions.each do |name, definition|
    if name != "Pods" && (@target == nil || @target == name)
      newTargetContents = CocoapodsDepend::Converter.target_dependencies_to_ruby(definition.name, definition.dependencies.push(dependency))
      contents = contents.gsub(/^target\s[\"|']#{name}[\"|'].+?end\n[\n]?/m, (newTargetContents + "\n\n"))
    end
  end
  podfile_path.open('w') { |f| f << contents}
end

#validate!Object



45
46
47
48
# File 'lib/cocoapods-dependManager/command/add.rb', line 45

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