Class: Pod::Command::Mod::Add

Inherits:
Pod::Command::Mod show all
Defined in:
lib/cocoapods-modularization/command/mod/add.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Add

Returns a new instance of Add.



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

def initialize(argv)
  @name = argv.shift_argument
  @version = argv.option('version', '0.1.0')
  @targets = argv.option('targets', '').split(',')
  @use_binary = argv.flag?('use-binary')
  super
end

Class Method Details

.optionsObject



17
18
19
20
21
22
23
# File 'lib/cocoapods-modularization/command/mod/add.rb', line 17

def self.options 
  [
    ['--version=VERSION', 'Version of the component, default version is 0.1.0'],
    ['--targets=TARGETA,TARGETB', 'Targets need to insert component.if this option not specified, this command will add component into all targets'],
    ['--use-binary', 'Use this flag if the component supposed to be a binary, otherwise a source version will be added into dependencies']
  ].concat(super)
end

Instance Method Details

#runObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cocoapods-modularization/command/mod/add.rb', line 55

def run
  begin
    UI.puts("Adding #{@name} into dependencies index")
    Meta::MetaAccessor.edit_meta do |meta_hash| 
      # add index to dependencies
      target_definitions = meta_hash['target_definitions']
      target_definitions.each { |target| internal_add_dep(target, @name, @targets) } if target_definitions.kind_of?(Array)
    end

    UI.puts("Adding #{@name} into meta")
    Meta::MetaAccessor.set_dep(@name, @use_binary, @version)
    UI.puts("#{@name} added")

    Meta::MetaReference.encode_podfile
    UI.puts("Podfile encoded")
  rescue Exception => e
    UI.puts e
  end
end

#validate!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cocoapods-modularization/command/mod/add.rb', line 33

def validate!
  super
  help! "meta not found, run `pod sync` to fix this issue" unless File.exists?(Meta::MetaConstants.meta_path)
  help! "data not found, run `pod sync` to fix this issue" unless File.exists?(Meta::MetaConstants.data_path)

  help! 'A name for the Pod is required.' unless @name
  help! 'The Pod name cannot contain spaces.' if @name =~ /\s/
  help! 'The Pod name cannot contain plusses.' if @name =~ /\+/
  help! "The Pod name cannot begin with a '.'" if @name[0, 1] == '.'
  
  help! "version (#{@version}) illlegal" unless @version =~ /\d+\.\d+\.\d+/
  
  return if @targets.empty? 
  podfile = Pod::Podfile.from_file(Meta::MetaReference.podfile_path_in_dir(Meta::MetaReference.installation_root))
  target_definitions = podfile.target_definitions.map { |target| target.to_a.at(0) }
  target_not_hits = Array.new
  @targets.each do |target|
    target_not_hits << target unless target_definitions.include?(target)
  end
  help! "Targets: #{target_not_hits} not found in Podfile" unless target_not_hits.empty?
end