Class: Pod::GitHooksSync

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-hbh/git_hooks/githooks_sync.rb

Instance Method Summary collapse

Instance Method Details

#copyFileToGit(file_name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cocoapods-hbh/git_hooks/githooks_sync.rb', line 30

def copyFileToGit (file_name)
    git_path = findGitPath()
    if !File.directory?(git_path)
        Pod::UI.puts "没有发现git工程"
        return
    end

    # 在当前ruby项目的githook文件copy到项目里去做拦截
    # 如果项目里配置了 .git-hooks 文件件,将把里面的内容copy到项目里去做拦截
    # 项目里的.git-hooks 配置优先
    hooks_path = "#{File.dirname(__FILE__)}/../../script_source/#{file_name}"

    if !File.exist?(hooks_path)        
        Pod::UI.puts "同步文件不存在"
        return
    end
    git_hooks_path = "#{git_path}/hooks"
    if !File.exist?(git_hooks_path)
        FileUtils.mkdir_p git_hooks_path
    end

    copyHookAndChmod(hooks_path, git_hooks_path, file_name)
    findSubmodulesHooks(hooks_path, git_path, file_name)
end

#copyHookAndChmod(git_hooks_path, path, file_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/cocoapods-hbh/git_hooks/githooks_sync.rb', line 62

def copyHookAndChmod(git_hooks_path, path, file_name)
    if File.exist?("#{path}/#{file_name}")
        # Pod::UI.puts "文件已经存在#{path}/#{file_name}"
        return
    else
        FileUtils.cp_r(git_hooks_path, path)
        FileUtils.chmod("+x", "#{path}/#{file_name}")
    end
 
end

#deleteFileInGit(file_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/cocoapods-hbh/git_hooks/githooks_sync.rb', line 89

def deleteFileInGit (file_name)
    if !File.directory?(findGitPath())
        Pod::UI.puts "没有发现git工程"
        return
    end
    path = ".git/hooks/#{file_name}"
    if File.exist?(path)
        FileUtils.rm(path)
    end
end

#findGitPathObject



22
23
24
25
26
27
28
# File 'lib/cocoapods-hbh/git_hooks/githooks_sync.rb', line 22

def findGitPath
    _git_path = ".git"
    if !File.directory?(_git_path)
        _git_path = "../.git"
    end
    _git_path
end

#findSubmodulesHooks(git_hooks_path, git_path, file_name) ⇒ Object



55
56
57
58
59
60
# File 'lib/cocoapods-hbh/git_hooks/githooks_sync.rb', line 55

def findSubmodulesHooks (git_hooks_path, git_path, file_name)
    subGitHookPath = walk(git_path)
    subGitHookPath.each do |path|
        copyHookAndChmod(git_hooks_path, path, file_name)
    end
end

#syncObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cocoapods-hbh/git_hooks/githooks_sync.rb', line 5

def sync

    copyFileToGit("pre-commit")

    if HBHConfig.config.is_turn_swiftint 
        copyFileToGit("swiftlint.sh")
    else
        deleteFileInGit("swiftlint.sh")
    end

    if HBHConfig.config.is_turn_tinypng 
        copyFileToGit("imageTinypng.rb")
    else
        deleteFileInGit("imageTinypng.rb")
    end
end

#walk(startPath, dirName = "hooks", first = true) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cocoapods-hbh/git_hooks/githooks_sync.rb', line 73

def walk (startPath, dirName = "hooks", first = true)
    @pathArr=Array.new unless !first
    Dir.foreach(startPath) do |x|
        path = File.join(startPath, x)
        if x =="." or x ==".."
          next
        elsif File.directory?(path)
            if path.include?(dirName)
                @pathArr << path
            end
            walk(path, dirName, false)
        end
      end
    @pathArr
end