Class: Pod::Command::MPAAS::Init

Inherits:
Pod::Command::MPAAS show all
Defined in:
lib/cocoapods-mPaaS/command/mpaas/init.rb

Constant Summary collapse

PLUGIN =
'plugin "cocoapods-mPaaS"'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Init

Returns a new instance of Init.



21
22
23
24
25
# File 'lib/cocoapods-mPaaS/command/mpaas/init.rb', line 21

def initialize(argv)
  @all_targets = argv.flag?('all-targets')
  @custom_repo_url = argv.option('repo-url')
  super
end

Class Method Details

.optionsObject



14
15
16
17
18
19
# File 'lib/cocoapods-mPaaS/command/mpaas/init.rb', line 14

def self.options
  [
    ['--all-targets', 'Configure mPaaS for all targets.'],
    ['--repo-url=URL', 'Custom podspec repo URL (default: gitee mPaaS repo).']
  ].concat(super)
end

Instance Method Details

#runObject



27
28
29
30
31
32
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
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
# File 'lib/cocoapods-mPaaS/command/mpaas/init.rb', line 27

def run
  repo_url = @custom_repo_url || CocoapodsmPaaS::MPAAS_REPO_URL
  source_line = "source \"#{repo_url}\""

  LogTools.p_green Pod::MPAAS::Localization.t('mpaas.init_command.check_config_file_exists')
  project_dir = Dir::pwd
  meta_file_path = find_config_file(project_dir)

  unless meta_file_path.length > 0 && File::exist?(meta_file_path)

    config_file_name = "meta.config"
    meta_file_path = File.join(project_dir, config_file_name)
      # 如果 meta.config 也没有的话
      unless File::exist?(meta_file_path)
        # Pod::UI.puts "No config file found !!!".red
        help! "No config file found !!!"
      else
        LogTools.p "config file path : #{meta_file_path}"
      end
  else
    LogTools.p "config file path : #{meta_file_path}"
  end

  LogTools.p_green Pod::MPAAS::Localization.t('mpaas.init_command.check_process_podfile')
  podfile_path = File.join(project_dir, "Podfile")
  LogTools.p "Podfile path: #{podfile_path}"

  plugin_full = PLUGIN
  # 处理下 --all-targets
  if @all_targets
    podfile = Pod::Podfile.from_file(podfile_path)

    plugin_option = ', :targets => ['
    root_targetDefinition_array = podfile.root_target_definitions
    if root_targetDefinition_array && root_targetDefinition_array.size > 0
      root_targetDefinition_array.each do |root_targetDefinition|
        children = root_targetDefinition.children
        if children
          children.each_index do |index|
            plugin_option += ('"' + children[index].name + '"')

            if index != (children.size - 1)
              plugin_option += ', '
            end
          end
        end
      end
    end

    plugin_option += ']'
    plugin_full += plugin_option
  end

  # 先简单文本处理,todo ruby解析或者正则
  io = File::open(podfile_path).readlines
  override = File.new(podfile_path, "w")

  # 先把 plugin 和 source 加上去
  override.puts("# mPaaS Pods Begin")
  override.puts(plugin_full)
  override.puts(source_line)
  if @custom_repo_url
    override.puts("mPaaS_repo_url '#{repo_url}'")
  end
  override.puts('mPaaS_baseline ' + '\'x.x.x\'' + ' ' + Pod::MPAAS::Localization.t('mpaas.init_command.replace_baseline_note'))
  override.puts("# mPaaS Pods End")
  override.puts("# ---------------------------------------------------------------------")

  io.each do |line|
    unless line.include?("cocoapods-mPaaS") || line.include?("mPaaS_repo_url") || line.include?(CocoapodsmPaaS::MPAAS_REPO_URL) || (@custom_repo_url && line.include?(@custom_repo_url)) || line.include?("mPaaS Pods") || line.include?("use_pod_for_mPaaS") || line.include?("mPaaS_baseline") || line.include?("# -------------------------------------------------------")
      override.puts line
    end
  end

  override.close

  LogTools.p_green 'Added "plugin in current Podfile, Done"'
  LogTools.p plugin_full
  LogTools.p_green 'Added "source in current Podfile, Done"'
  LogTools.p source_line
  LogTools.p_green Pod::MPAAS::Localization.t('mpaas.init_command.configuration_complete')
end