class ActionHandler

require “sjpush/version” !/usr/bin/env ruby

Public Class Methods

getCurBranch() click to toggle source

获取当前的分支

# File lib/sjpush.rb, line 8
def getCurBranch()
    # strip! 删除开头和末尾的空白字符
    commands = IO.readlines(".git/HEAD").first.strip!;
    commands.sub!(/ref: refs\/heads\//, "")
end
getPodspec() click to toggle source
# File lib/sjpush.rb, line 16
def getPodspec()
    Dir["*.podspec"].last
end
getPodspecName() click to toggle source
# File lib/sjpush.rb, line 65
def getPodspecName()
    s = searchPodspec()
    
    File.new(s, "r").each_line do |line|
        regex = "s.name(?:[^']+)'([^']+)'*"
        if /#{regex}/ =~ line
            @name = $1
            break
        end
    end
    
    return @name
end
getPodspecVersion() click to toggle source
# File lib/sjpush.rb, line 51
def getPodspecVersion()
    s = searchPodspec()

    File.new(s, "r").each_line do |line|
        regex = "s.version(?:[^']+)'([^']+)'*"
        if /#{regex}/ =~ line
            @version = $1.to_i
            break
        end
    end
    
    return @version
end
new() click to toggle source
# File lib/sjpush.rb, line 80
def initialize()
    @commands = String.new
end
searchPodspec() click to toggle source
# File lib/sjpush.rb, line 20
def searchPodspec()
    s = ActionHandler.getPodspec()
    if s.nil?
        puts "\n已退出, 未搜索到 podspec 文件"
        exit
    end
    
    return s
end
updatePodspecVerAction() click to toggle source
# File lib/sjpush.rb, line 30
def updatePodspecVerAction()
    s = searchPodspec()
    
    contents = String.new
    File.new(s, "r").each_line do |line|
        regex = "s.version([^']+)'([^']+)'*"
        if /#{regex}/ =~ line
            v = ($2.to_i + 1).to_s
            regex = "'[^']+'"
            line = line.sub!(/#{regex}/, "'#{v}'")
        end
        
        contents += line
    end
    file = File.new(s, "w")
    file.syswrite(contents)
    file.close
    
    puts "\npodspec 版本已更新"
end

Public Instance Methods

addCommand(order) click to toggle source
  • Actions -

# File lib/sjpush.rb, line 102
def addCommand(order)
    if @commands.length == 0
        @commands << order
    else
        @commands << " && #{order}"
    end
end
addNewTagAction() click to toggle source
# File lib/sjpush.rb, line 120
def addNewTagAction()
    puts "\n请输入新的标签:"
    newTag = gets.strip!
    
    submit = getSubmitInfo()
    addCommand "git tag -a '#{newTag}' -m '#{submit}'"
    addCommand "git push origin #{newTag}"
end
addNewTagForPodspecVersionAction() click to toggle source
# File lib/sjpush.rb, line 129
def addNewTagForPodspecVersionAction()
    submit = getSubmitInfo()
    version = ActionHandler.getPodspecVersion()
    name = ActionHandler.getPodspecName()
    tag = "#{name}-#{version}"
    addCommand "git tag -a '#{tag}' -m '#{submit}'"
    addCommand "git push origin #{tag}"
end
commitAction() click to toggle source
# File lib/sjpush.rb, line 110
def commitAction()
    submit = getSubmitInfo()
    addCommand "git add ."
    addCommand "git commit -m '#{submit}'"
end
deleteTagAction() click to toggle source
# File lib/sjpush.rb, line 138
def deleteTagAction()
    puts "\n请输入要删除的标签:"
    tag = gets.strip!
    addCommand "git tag -d #{tag}"
    addCommand "git push origin :#{tag}"
end
directReleaseAction() click to toggle source

一键发布

# File lib/sjpush.rb, line 236
def directReleaseAction()
    ActionHandler.updatePodspecVerAction()
    getSubmitInfo()
    commitAction()
    pushAction()
    addNewTagForPodspecVersionAction()
    podReleaseAction()
end
executeCommands() click to toggle source
# File lib/sjpush.rb, line 156
    def executeCommands
        puts <<-DESC
        
        
        =========================正则执行, 请稍等...=========================
        =========================正则执行, 请稍等...=========================
        
        
        DESC
        system @commands
        puts "\n操作完成"
    end
getCocoapodsRepo() click to toggle source
# File lib/sjpush.rb, line 92
def getCocoapodsRepo()
    if @repo.nil?
        puts "\n请输入仓库名:"
        @repo = gets.strip!
    end
    return @repo
end
getSubmitInfo() click to toggle source
# File lib/sjpush.rb, line 84
def getSubmitInfo()
    if @commitInfo.nil?
        puts "\n请输入提交信息:"
        @commitInfo = gets.strip!
    end
    return @commitInfo
end
handleSeq(seq) click to toggle source
# File lib/sjpush.rb, line 170
def handleSeq(seq)
    if seq == $seq_commit.to_i
        commitAction()
    elsif seq == $seq_push_current_branch.to_i
        pushAction()
    elsif seq == $seq_add_tag.to_i
        puts "\n是否使用Podspec中的版本作为标签? [ Yes / NO ]"
        r = gets
        if r.casecmp("Yes") == 1
            addNewTagForPodspecVersionAction()
        else
            addNewTagAction()
        end
    elsif seq == $seq_release.to_i
        podReleaseAction()
    elsif seq == $seq_delete_tag.to_i
        deleteTagAction()
    elsif seq == $seq_update_podspec_version.to_i
        ActionHandler.updatePodspecVerAction()
    elsif seq == $seq_lazy_protocol.to_i
        require 'sjProtocol'
        exit
    elsif seq == $seq_lazy_property.to_i
        require 'sjScript'
        exit
    elsif seq = $seq_direct_release.to_i
        directReleaseAction()
    end
    
    nextCommand(seq)
end
nextCommand(beforeSeq) click to toggle source
# File lib/sjpush.rb, line 203
def nextCommand(beforeSeq)
    if beforeSeq == $seq_commit.to_i
        # 询问是否推送到主干上
        # ActionHandler - Push
        puts "\n是否推送到 #{ActionHandler.getCurBranch()}? [ Yes / NO ]"
        r = gets
        if r.casecmp("Yes") == 1
            handleSeq($seq_push_current_branch.to_i)
        end
    elsif beforeSeq == $seq_push_current_branch.to_i
        puts "\n是否添加标签? [ Yes / NO ]"
        r = gets
        if r.casecmp("Yes") == 1
            handleSeq($seq_add_tag.to_i)
        end
    elsif beforeSeq == $seq_add_tag.to_i
        # 询问是否发布pod
        # Pod - Release
        puts "\n是否发布pod版本? [ Yes / NO ]"
        r = gets
        if r.casecmp("Yes") == 1
            handleSeq($seq_release.to_i)
        end
    elsif beforeSeq == $seq_update_podspec_version.to_i
        puts "\n是否提交变更? [ Yes / NO ]"
        r = gets
        if r.casecmp("Yes") == 1
            handleSeq($seq_commit.to_i)
        end
    end
end
podReleaseAction() click to toggle source
# File lib/sjpush.rb, line 145
def podReleaseAction()
    spec = ActionHandler.getPodspec()
    if spec.nil?
        puts "\n已退出, 未搜索到 podspec 文件"
        exit
    end
    
    repo = getCocoapodsRepo()
    addCommand "pod repo push #{repo} #{spec} --allow-warnings --use-libraries --verbose --skip-import-validation"
end
pushAction() click to toggle source
# File lib/sjpush.rb, line 116
def pushAction()
    addCommand "git push origin #{ActionHandler.getCurBranch()}"
end