Class: Pod::Command::JxedtCommand::Binary::Clean

Inherits:
Pod::Command::JxedtCommand::Binary show all
Defined in:
lib/cocoapods-jxedt/command/binary/command/clean.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Clean

Returns a new instance of Clean.



20
21
22
23
24
25
# File 'lib/cocoapods-jxedt/command/binary/command/clean.rb', line 20

def initialize(argv)
    @names = argv.option('name', '').split(',')
    @local = argv.flag?('local', false)
    @all = argv.flag?('all', false)
    super
end

Class Method Details

.optionsObject



13
14
15
16
17
18
19
# File 'lib/cocoapods-jxedt/command/binary/command/clean.rb', line 13

def self.options
    [
        ['--name', '删除一个或多个二进制产物,多个以,分隔'],
        ['--local', '只删除本地的二进制产物'],
        ['--all', '删除所有的二进制产物,执行过程需要再次确认'],
    ]
end

Instance Method Details

#get_stdin(message) ⇒ Object



91
92
93
94
95
96
# File 'lib/cocoapods-jxedt/command/binary/command/clean.rb', line 91

def get_stdin(message)
    UI.puts "#{message}".red
    print "请输入--> ".green
    val = STDIN.gets.chomp.strip
    val
end

#runObject



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
# File 'lib/cocoapods-jxedt/command/binary/command/clean.rb', line 32

def run
    podfile = Pod::Config.instance.podfile
    help! '请检查命令执行路径,需要在Podfile文件所在目录执行' if podfile.nil?

    require 'cocoapods-jxedt/git_helper/git_command'

    repo = Jxedt.config.git_remote_repo
    cache_path = Jxedt.config.git_cache_path
    branch = Jxedt.config.cache_branch

    local_cache_dir = Pod::Config.instance.sandbox.root + Jxedt.config.binary_dir

    commander = nil
    commander = Jxedt::GitCommand.new(cache_path) if !@local && Jxedt.config.cache_repo_enabled?
    # fetch
    commander.git_fetch(repo, branch) if commander

    if @names.size > 0
        local_deleted, remote_deleted = [], []
        @names.each do |name|
            local_cache = local_cache_dir + name
            if local_cache.exist?
                local_cache.rmtree
                local_deleted << name
            end

            if commander
                remote_cache_dir = Pathname.new(cache_path) + "GeneratedFrameworks"
                remote_cache = remote_cache_dir + name
                if remote_cache.exist?
                    remote_cache.rmtree
                    remote_deleted << name
                end
            end
        end
        # push
        commander.git_commit_and_push(branch) if commander && remote_deleted.size > 0
        UI.puts "⚠️ ⚠️ ⚠️ 本地缓存文件已清除: #{local_deleted}" if local_deleted.size > 0
        UI.puts "⚠️ ⚠️ ⚠️ 远程缓存文件已清除: #{remote_deleted}" if remote_deleted.size > 0
    else
        random = (0...10).map { (97 + rand(26)).chr }.join
        input = get_stdin("你确认要清除所有缓存吗?包括远程仓库的缓存。确认请输入: #{random}")
        help! "输入错误,自动退出" if random != input

        local_cache_dir.rmtree if local_cache_dir.exist?

        if commander
            remote_cache_dir = Pathname.new(cache_path) + "GeneratedFrameworks"
            if remote_cache_dir.exist?
                remote_cache_dir.rmtree
                # push
                commander.git_commit_and_push(branch)
            end
        end
        UI.puts "⚠️ ⚠️ ⚠️ 所有缓存文件已清除" 
    end
    
end

#validate!Object



27
28
29
30
# File 'lib/cocoapods-jxedt/command/binary/command/clean.rb', line 27

def validate!
    help! "至少要添加一个选项,'--name'或'--all'" if @names.empty? && !@all
    super
end