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

#initializeStableEnv

Returns a new instance of StableEnv.



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

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

Instance Method Details

#business_stableObject



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

def business_stable
  return @businessSpec
end

#default_branchObject



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

def default_branch
  return "main"
end

#fetch_stale_gitObject



67
68
69
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 67

def fetch_stale_git
  system_clone_stable_git
end

#loadStaleEnvObject

stable 环境,执行pod stable



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

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



73
74
75
76
77
78
79
80
81
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 73

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



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

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

#stable_sourceObject



83
84
85
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 83

def stable_source
  return @stable_source
end

#stable_tagObject



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

def stable_tag
  return @stable_tag
end

#system_clone_stable_gitObject

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



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

def system_clone_stable_git
    cachePath = @cache.cachePath
    puts "hook [pod install/update] fetch stable git=>#{cachePath}"
    if !Dir.exist?(File.join(cachePath))
      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 checkout #{stable_branch}; git reset --hard origin/#{stable_branch}; git pull --all"
        end
        if stable_tag
          system "cd #{cachePath}; 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 checkout #{protechBranch}; git reset --hard origin/#{protechBranch}; git pull --all"
      end
    else
      puts "❌ stable配置无效分支信息source:#{@stable_source} branch:#{stable_branch}".red
      exit
    end
end

#verify_branch_existsObject

分支是否存在



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

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环境是否存在



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

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

#verify_tag_existsObject

tag是否存在



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cocoapods-bb-PodAssistant/helpers/stable_env_helper.rb', line 106

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