Class: Pod::Podfile::AddModule

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-hbh/command/module/add_module.rb

Constant Summary collapse

UI =
Pod::UserInterface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_target_definition) ⇒ AddModule

Returns a new instance of AddModule.



14
15
16
# File 'lib/cocoapods-hbh/command/module/add_module.rb', line 14

def initialize(current_target_definition)
  @current_target_definition = current_target_definition
end

Instance Attribute Details

#current_target_definitionTargetDefinition

Returns The current target definition to which the DSL commands apply.

Returns:

  • (TargetDefinition)

    The current target definition to which the DSL commands apply.



12
13
14
# File 'lib/cocoapods-hbh/command/module/add_module.rb', line 12

def current_target_definition
  @current_target_definition
end

Instance Method Details

#get_info(info, key, default) ⇒ Object

从 info map里获取key的值,如果未获取到 返回 default



19
20
21
22
23
24
25
# File 'lib/cocoapods-hbh/command/module/add_module.rb', line 19

def get_info(info, key, default)
  if info.respond_to?("has_key?") && info.has_key?(key)
    return info[key]
  else
    return default
  end
end

#pod_private(moduleName, *requirements) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cocoapods-hbh/command/module/add_module.rb', line 27

def pod_private (moduleName, *requirements) 
  info = requirements.last
  git = get_info(info, :git, nil)
  sub_config = HBHConfig.config.submodule_env
  remoteUrl = sub_config['sub_url'] 
  if git.blank? 
      if moduleName.include?('/')
          name = moduleName.split('/').first
          requirements.last[:git] = "#{remoteUrl}/#{name}.git"
      else
          requirements.last[:git] = "#{remoteUrl}/#{moduleName}.git"
      end
  end
  Pod::UI.puts "#{moduleName}使用私有库".yellow
  current_target_definition.store_pod moduleName, *requirements
end

#pod_submoudle(moduleName, *requirements) ⇒ Object

添加模块 模块名称为podspec的名称,路径默认为Module/下的模块,tag或者branch 优先使用tag,默认develop分支 (moduleName, : true, moduleTag: nil, moduleBranch: ‘develop’, appspecs: nil)



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cocoapods-hbh/command/module/add_module.rb', line 47

def pod_submoudle (moduleName, *requirements)
  info = requirements.last
  is_submodule = get_info(info, :submodule, true)
  if is_submodule 
    if info.nil? then raise "请输入正确的参数" end
    return requirements unless info.is_a?(Hash) 

    moduleTag = get_info(info, :tag, nil)
    moduleBranch = get_info(info, :branch, nil)
    if moduleTag.nil? && moduleBranch.nil?
      Pod::UI.puts "请配置依赖tag或者branch".red
      exit()
    end

    appspecs = get_info(info, :appspecs, nil)
    sub_config = HBHConfig.config.submodule_env
    remoteUrl = sub_config['sub_url'] 
    if remoteUrl.blank?
      Pod::UI.puts "请配置subModule的sub_url".red
      exit()
    end
    moduleRoot = sub_config['sub_path'].blank? ? "Modules" : sub_config['sub_path']

    path_module = "./#{moduleRoot}/#{moduleName}"
    Pod::UI.puts "#{moduleName}使用本地Pod模块".green
    if !File.exist?("#{path_module}/#{moduleName}.podspec")
      `git submodule add #{remoteUrl}#{moduleName}.git ./Modules/#{moduleName}`
      Pod::UI.puts "本地没有#{moduleName}模块,正在下载中..."
      # 设置主项目`git pull`操作时,子git自动执行 `git pull`
      `git config submodule.recurse true`

      # 设置主项目`git push`操作时,子项目自动执行 `git push`
      # `git config push.recurseSubmodules on-demand`
      
      if system "git submodule update --init --recursive" 
        Pod::UI.puts "#{moduleName}模块下载完成...".green
      end
    end
    if moduleTag != nil and !moduleTag.empty?
      result = system("cd #{path_module} && git fetch origin tag #{moduleTag} && git checkout #{moduleTag}")
      raise "发生错误" unless result
      # `cd #{path_module} && git fetch && git checkout #{moduleTag}`
      # Pod::UI.puts "发生错误#{result}".red
     else
      result = system("cd #{path_module} && git fetch origin #{moduleBranch} && git checkout #{moduleBranch} && git pull origin #{moduleBranch}")
      raise "发生错误" unless result

      # Pod::UI.puts "发生错误#{result}".red
      # `cd #{path_module} && git fetch && git checkout #{moduleBranch} && git pull origin #{moduleBranch}`
    end
    if appspecs != nil 
      sub_hash = {:path => path_module, :appspecs => ['Example']}
      last_index = requirements.length-1
      requirements[last_index] = requirements[last_index].merge(sub_hash)
      current_target_definition.store_pod moduleName, *requirements
      # pod moduleName, 
    else
      sub_hash = {:path => path_module}
      last_index = requirements.length-1
      requirements[last_index] = requirements[last_index].merge(sub_hash)
      current_target_definition.store_pod moduleName, *requirements
      # pod moduleName, 
    end
  else
    Pod::UI.puts "#{moduleName}使用远程模块".yellow
    current_target_definition.store_pod moduleName, *requirements

    # if moduleTag != nil 
    #     pod moduleName, :git => "#{remoteUrl}#{moduleName}.git", :tag => moduleTag
    # else
    #     pod moduleName, :git => "#{remoteUrl}#{moduleName}.git", :branch => moduleBranch
    # end
  end
end