require_relative 'log_helper' require_relative 'pgyer_shortcut_helper' require_relative 'team_member_config' module Lhj # version info class AppVersionInfo VERSION_REGEX = /^(?[^#]*MARKETING_VERSION\s*=\s*)(?(?[0-9]+)(\.(?[0-9]+))?(\.(?[0-9]+))?(?(\.[0-9]+)*)?(-(?(.+)))?)(?)/ # @param [Symbol] env def initialize(env, build_number, work_path) @env = env @build_number = build_number @work_path = work_path modify_code end def modify_code setup_config setup_shell_script setup_pod_script end def setup_config number = build_app_version_no Dir.chdir(@work_path) do command = %(xcrun agvtool new-version -all #{number}) Actions.sh(command, log: false) end FileUtils.rm_r Dir.glob("#{@work_path}/archive*.zip") end def setup_shell_script project_path = Dir["#{@work_path}/*.xcodeproj"].first project = Xcodeproj::Project.open(project_path) target = project.targets.first handle_list = target.shell_script_build_phases.select { |s| s.display_name == '[CP] Copy Pods Resources' || s.display_name == '[CP] Embed Pods Frameworks' } handle_list.each do |shell| shell.run_only_for_deployment_postprocessing = '0' shell.build_action_mask = '12' end project.save end def setup_pod_script project_path = Dir["#{@work_path}/*.xcodeproj"].first project = Xcodeproj::Project.open(project_path) target = project.targets.first Dir.glob("#{@work_path}/**/Pods-#{target.display_name}-frameworks.sh").each do |file| need_modify = false content = '' File.open(file) do |f| f.each_line do |line| if line =~ /source="\$\(readlink "\${source}"\)"/ content += ' source="$(readlink -f "${source}")"' content += "\n" need_modify = true else content += line end end end File.write(file, content) if need_modify end end # build_app_version (build_app_version_no) (build: build_version) def build_info "#{build_app_version} (#{build_app_version_no}) (build: #{build_version})" end def self.all_app_version(path) vers = [] Dir.glob("#{path}/*.xcodeproj/*.pbxproj").each do |file| File.readlines(file).each do |l| next unless /MARKETING_VERSION/ =~ l vers << fetch_version(l) end end sort_version_list(vers) end def self.fetch_version(line) version_match = VERSION_REGEX.match(line) major = version_match[:major].to_i minor = version_match[:minor].to_i || 0 patch = version_match[:patch].to_i || 0 { value: version_match[:value], major: major, minor: minor, patch: patch } end def self.sort_version_list(list) list.sort do |a, b| ma = a[:major] <=> b[:major] ma = a[:minor] <=> b[:minor] if ma.zero? ma = a[:patch] <=> b[:patch] if ma.zero? ma end end def build_app_version # app version '1.0.0' vers = AppVersionInfo.all_app_version(@work_path) vers.last[:value] end def last_commit_id Lhj::Actions.last_git_commit_hash(true) end def build_app_version_no # xcode build version # ${BUILD_NUMBER} of jenkins num = @build_number || '1' if @env == :release num else # "#{@env.to_s}_#{num}" num end end def build_version # pu gong ying build number client = Lhj::PgyerShortcut.new(@env) version = 0 version = client.build_version.to_i if client.build_version version += 1 version.to_s end def build_updated time = Time.now time.strftime('%Y-%m-%d %H:%M:%S') end def branch_name Dir.chdir(@work_path) do Lhj::LogHelper.instance.current_branch(@work_path) end end def update_log Dir.chdir(@work_path) do message = Lhj::Actions.git_log_last_commits(' %an %ar - %s;', 6, :exclude_merges, 'short', false) handle_message(message) end end def handle_message(m) message = m unless message.empty? message = message.gsub('seconds ', '秒').gsub('minutes ', '分钟').gsub('hours ', '小时').gsub('weeks ', '周').gsub('days ', '天').gsub('ago ', '前') Lhj::TeamMemberConfig.config.each do |key, value| message = message.gsub(key, value) if message =~ /#{key}/ end end message end def custom_modify return if @env != :release Dir["#{@work_path}/**/*.m"].each do |file| str = '' File.open(file) do |f| f.each_line do |line| next if line =~ /todo/ str += line end end File.open(file, 'w+') do |f| f.write(str) end end end end end