Class: BB::StableEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(verify_stable_env = true, isMatrix = false) ⇒ StableEnv

Returns a new instance of StableEnv.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 5

def initialize(verify_stable_env=true, isMatrix=false)
    @cache = BB::Cache.new(nil, verify_stable_env)
    @isMatrix = isMatrix
    configGitPath(@cache.cachePath)
    if verify_stable_env
      # 自动加载stable环境
      loadStaleEnv
      if verify_stable_env_exists == false
        puts "[PodAssistant] ❌工程没有配置stable环境".red
      end
    end
    fetch_stale_git # 自动拉取远端git仓库数据
end

Instance Method Details

#business_stableObject



104
105
106
107
108
109
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 104

def business_stable
  if @businessSpec.nil? && @isMatrix
    return default_matrix_business_stable
  end
  return @businessSpec
end

#cache_pathObject



144
145
146
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 144

def cache_path
  return @cache.cachePath
end

#default_branchObject



92
93
94
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 92

def default_branch
  return "main"
end

#default_matrix_business_stableObject



110
111
112
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 110

def default_matrix_business_stable
  return "global_stable_specs"
end

#fetch_stale_gitObject



70
71
72
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 70

def fetch_stale_git
  system_clone_stable_git
end

#loadStaleEnvObject

stable 环境,执行pod stable



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 20

def loadStaleEnv
  unless File.exist?(File.join(Pathname.pwd, "Podfile"))
    err_msg = "- Error: #{File.join(Pathname.pwd, "Podfile")} is not exit"
    Pod::UI.puts "#{err_msg}".send(:red)
    exit -9001
  end

  #获取podfile 内容
  #1、删除所有注释行,避免干扰
  #2、正则匹配,筛选出stable 方法
  #3、执行stable 方法,获取配置
  podfileContent = File.read(File.join(Pathname.pwd, "Podfile"))
  podfileContent_vaild = podfileContent.lines.reject { |line| line.strip.start_with?("#") }.join
  stableCommand = podfileContent_vaild.match(/^\s*stable!\s*'([^']+)'(?:,\s*(\w+):\s*'([^']+)')*/m)
  unless stableCommand
    puts "- Error: not stable define in the podfile! you can define to podfile:".red
    puts "stable! 'https://git.babybus.co/babybus/ios/Specs/stable-specs.git', specs:'<业务线stbale名称-可选>', tag:'<标签-可选>', branch:'<分支-可选>'".green
    exit -9002
  end
  eval(stableCommand.to_s)
end

#stable!(source, options = {}) ⇒ Object

podfile 约定函数规则,切勿修改

help load podfile option



76
77
78
79
80
81
82
83
84
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 76

def stable!(source, options = {})
  @stable_source = source
  if options.has_key?(:specs) 
    @businessSpec = options[:specs]
  end
  @stable_tag = options[:tag] if options.has_key?(:tag) 
  @stable_branch = options[:branch] if options.has_key?(:branch)
  puts "###stable env => [stable公共源]:#{source} [业务线源名称]:#{@businessSpec} branch:#{@stable_branch} tag:#{@stable_tag}".green
end

#stable_branchObject



95
96
97
98
99
100
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 95

def stable_branch
  if @stable_branch.nil?
    return default_branch
  end
  return @stable_branch
end

#stable_sourceObject



86
87
88
89
90
91
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 86

def stable_source
  if @stable_source.nil?
    @stable_source = StableSource.stable_default_source
  end
  return @stable_source
end

#stable_tagObject



101
102
103
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 101

def stable_tag
  return @stable_tag
end

#system_clone_stable_gitObject

拉取stable仓库代码 hook开发环境,执行pod install/update



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
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 43

def system_clone_stable_git
    cachePath = @cache.cachePath
    puts "fetch stable git=>#{cachePath}"
    if !Dir.exist?(File.join(cachePath))
      puts "git clone=>#{stable_source}"
      clonePath = File.dirname(cachePath)
      FileUtils.mkdir_p clonePath
      system "git clone #{stable_source} #{cachePath}"
    end
    if verify_branch_exists
      if stable_tag || stable_branch
        if stable_branch
          system "cd #{cachePath}; git pull --all; git checkout #{stable_branch}; git reset --hard origin/#{stable_branch}"
        end
        if stable_tag
          system "cd #{cachePath}; git pull --all; git checkout #{stable_tag};"
        end
      else
        result =`git symbolic-ref refs/remotes/origin/HEAD`
        protechBranch = result.split("/").last.strip || default_branch
        system "cd #{cachePath}; git pull --all; git checkout #{protechBranch}; git reset --hard origin/#{protechBranch}"
      end
    else
      puts "❌ stable配置无效分支信息source:#{stable_source} branch:#{stable_branch}".red
      exit
    end
end

#verify_branch_existsObject

分支是否存在



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 130

def verify_branch_exists
  branch = stable_branch
  if branch
    cachePath = @cache.cachePath
    # result = system "cd #{cachePath}; git tag"
    branchs = `cd #{cachePath}; git branch -a`
    if branchs
      branchs = branchs.split("\n")
      return branchs.include?(branch) || branchs.include?('  remotes/origin/' + branch) || branchs.include?('remotes/origin/' + branch)
    end
    puts "verify_branch_exists:#{branchs}".red
  end
  return false
end

#verify_stable_env_existsObject

验证stable环境是否存在



114
115
116
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 114

def verify_stable_env_exists
  return @stable_source.empty? ? false : true
end

#verify_tag_existsObject

tag是否存在



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 118

def verify_tag_exists
  tag = stable_tag
  if tag
    cachePath = @cache.cachePath
    # result = system "cd #{cachePath}; git tag"
    result = `cd #{cachePath}; git tag`
    puts "verify_tag_exists:#{result}".red
    return result.split("\n").include?(tag)
  end
  return false
end