lg_pod_plugin 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61fa41fbaf71702b19bf2ef604d2283f68d472e5c47428890398463556a985f7
4
- data.tar.gz: 9c45761ced23924ce2e70557245d70eb883b4f7158c63acfeb3f0547af2e1fdc
3
+ metadata.gz: e16255ce6cf29df74aa508c022dc083727d4fd78f65d95a0b68b57b10e075767
4
+ data.tar.gz: f1ac6d11e1cae7f98ee7332aca930d2389b9c07799270755dd8fd1b03a9e1cbb
5
5
  SHA512:
6
- metadata.gz: 549679243cd9e06a3d4626c06c79733bd779817e018bc5c46eb75f1731f7c5806f98bc02956ffd2e696b34e8ca1fb29f3fffb33feb231b9cb07180c3189ad586
7
- data.tar.gz: 00d3b2bf9cf9d22c87141e63872b2dd466658d14972b1e8b4a4ae2a5f23d3fbb96195971cddc8063bc38cc97b4d7468422c9ccbe330e70aac6af80ce1126dcc0
6
+ metadata.gz: 2f5dafc9da762041041bd32f38dfe4cc5d1ef399ff5caaf443574f048fc8a14b915b76ae1ec22916ad458a0f53433877ec8301079e725da097e8736c886cee71
7
+ data.tar.gz: bfc2c0c5835737c9c2cecc3002f61baffcc96ef75057dea47bc7aea9131c1e8df6ca58c54b307e25f8d3b9c5a318b583583586c010952001343f824eb6701813
@@ -1,16 +1,16 @@
1
1
  require "sqlite3"
2
2
  require 'singleton'
3
- require_relative 'cache'
3
+ require_relative 'l_cache'
4
4
 
5
5
  module LgPodPlugin
6
6
 
7
- class SqliteDb
7
+ class LSqliteDb
8
8
  include Singleton
9
9
  REQUIRED_ATTRS ||= %i[db table_name].freeze
10
10
  attr_accessor(*REQUIRED_ATTRS)
11
11
  # 初始化 db
12
12
  def initialize
13
- root_path = FileManager.download_director.join("database")
13
+ root_path = LFileManager.download_director.join("database")
14
14
  db_file_path = root_path.join("my.db")
15
15
  if !root_path.exist? || !db_file_path.exist?
16
16
  FileUtils.mkdir(root_path)
@@ -68,7 +68,7 @@ module LgPodPlugin
68
68
  end
69
69
 
70
70
  def select_table(name, branch)
71
- pod_info = CachePodInfo.new
71
+ pod_info = LCachePodInfo.new
72
72
  self.db.execute( "select * from #{self.table_name} where name = '#{name}' and branch = '#{branch}'; ") do |row|
73
73
  pod_info.name = row[1]
74
74
  pod_info.branch = row[2]
@@ -83,7 +83,7 @@ module LgPodPlugin
83
83
  def select_tables(name, branch)
84
84
  array = []
85
85
  self.db.execute( "select * from #{self.table_name} where name = '#{name}' and branch != '#{branch}' order by update_time;") do |row|
86
- pod_info = CachePodInfo.new
86
+ pod_info = LCachePodInfo.new
87
87
  pod_info.name = row[1]
88
88
  pod_info.branch = row[2]
89
89
  pod_info.sha = row[3]
@@ -1,137 +1,95 @@
1
1
  require 'git'
2
- require_relative 'cache'
2
+ require_relative 'l_cache'
3
3
  require_relative 'database'
4
4
  require_relative 'file_path'
5
5
 
6
6
  module LgPodPlugin
7
7
 
8
- class Downloader
8
+ class LDownloader
9
9
 
10
- REQUIRED_ATTRS ||= %i[git name commit branch tag options git_util db cache is_cache].freeze
10
+ REQUIRED_ATTRS ||= %i[git name commit branch tag options].freeze
11
11
  attr_accessor(*REQUIRED_ATTRS)
12
12
 
13
- def initialize
14
- self.cache = Cache.new
15
- self.db = SqliteDb.instance
16
- super
17
- end
18
-
19
- def download_init(name, options = {})
20
- hash_map = options
13
+ def initialize(name, options = {})
21
14
  self.name = name
22
- self.git = hash_map[:git]
23
- self.tag = hash_map[:tag]
24
- self.branch = hash_map[:branch]
25
- self.commit = hash_map[:commit]
26
- self.is_cache = hash_map[:depth]
27
- # 通过ls-remote获取 head 指向 branch
28
- if !self.branch && self.git
29
- ls = Git.ls_remote(self.git, :head => true)
30
- head_ref = ls["head"][:sha]
31
- ls["branches"].each do |key, value|
32
- sha = value[:sha]
33
- next if sha != head_ref
34
- self.branch = key
35
- hash_map[:branch] = key
36
- break
37
- end
38
- end
39
- self.options = hash_map
15
+ self.options = options
16
+ self.git = options[:git]
17
+ self.tag = options[:tag]
18
+ self.branch = options[:branch]
19
+ self.commit = options[:commit]
40
20
  end
21
+
22
+ # def check_cache_valid(name, branch)
23
+ # self.db.should_clean_pod_info(name, branch)
24
+ # end
41
25
 
42
- def is_update_pod
43
- cgi = CGI.new
44
- command_keys = cgi.keys
45
- unless command_keys.count > 0
46
- return false
47
- end
48
- first_key = command_keys[0].to_s ||= ""
49
- if first_key.include?("install")
50
- false
51
- elsif first_key.include?("update")
52
- true
26
+ # 预下载处理
27
+ def pre_download_pod
28
+ if self.branch
29
+ LgPodPlugin.log_green "Using `#{name}` (#{branch})"
53
30
  else
54
- false
31
+ LgPodPlugin.log_green "Using `#{name}`"
55
32
  end
56
- end
57
-
58
- def check_cache_valid(name, branch)
59
- self.db.should_clean_pod_info(name, branch)
60
- end
61
-
62
- # 预下载处理
63
- def pre_download_pod(git)
64
- self.git_util = git
65
- is_update = self.is_update_pod
66
- self.git_util.git_init(self.name, self.options)
67
- git_url = options[:git]
68
- # commit = options[:commit]
69
- branch = options[:branch]
70
- LgPodPlugin.log_green "Using `#{name}` (#{branch})"
71
33
  # 发现本地有缓存, 不需要更新缓存
72
- need_download, new_commit = self.cache.find_pod_cache(name, git_url, branch, is_update)
73
- unless need_download
34
+ need_download = LRequest.shared.cache.find_pod_cache(self.name, self.git, self.branch, self.tag, self.commit, LRequest.shared.is_update)
35
+ if !need_download
74
36
  LgPodPlugin.log_green "find the cache of `#{name}`, you can use it now."
75
37
  return
38
+ else
39
+ LgPodPlugin.log_green "find the new commit of `#{name}`, Git downloading now."
76
40
  end
77
-
78
- # 检查是否要清空缓存
79
- if is_update
80
- check_cache_valid(name, branch)
81
- end
82
-
83
41
  # 本地 git 下载 pod 目录
84
- pre_down_load_path = self.cache.get_download_path(name, git_url, branch)
85
- real_pod_path = self.git_util.pre_download_git_remote(pre_down_load_path, branch)
42
+ LRequest.shared.git_util.pre_download_git_repository
43
+
86
44
  # 本地clone代码失败跳出去
87
- unless real_pod_path.exist?
88
- return
89
- end
90
- # 切换到本地git仓库目录下
91
- FileUtils.chdir(real_pod_path)
92
- unless real_pod_path.glob("*.git")
93
- return
94
- end
95
- # 使用branch克隆代码
96
- git = Git.open(Pathname("./"))
97
- current_branch = git.current_branch
98
- if current_branch == branch # 要 clone 的分支正好等于当前分支
99
- current_commit = git.log(1).to_s
100
- if new_commit != current_commit && is_update
101
- #删除旧的pod 缓存
102
- self.cache.clean_old_cache(name, git_url, current_commit)
103
- LgPodPlugin.log_green "git pull #{name} origin/#{current_branch}"
104
- self.git_util.should_pull(git, current_branch, new_commit)
105
- current_commit = new_commit
106
- end
107
- hash_map = { :git => git_url }
108
- if current_commit
109
- hash_map[:commit] = current_commit
110
- end
111
- SqliteDb.instance.insert_table(name, branch, current_commit, nil, real_pod_path)
112
- LgPodPlugin::Cache.cache_pod(name, real_pod_path, is_update, hash_map)
113
- else
114
- branch_exist = git.branches.local.find { |e| e.to_s == branch }
115
- if branch_exist
116
- LgPodPlugin.log_green "git switch #{name} #{git_url} -b #{branch}"
117
- self.git_util.git_switch(branch)
118
- else
119
- LgPodPlugin.log_green "git checkout #{name} #{git_url} -b #{branch}"
120
- self.git_util.git_checkout(branch)
121
- end
122
- current_commit = git.log(1).to_s
123
- if current_commit != new_commit
124
- LgPodPlugin.log_green "git pull #{name} #{git_url} -b #{branch}"
125
- self.git_util.should_pull(git, current_branch, new_commit)
126
- current_commit = new_commit
127
- end
128
- hash_map = { :git => git_url }
129
- if current_commit
130
- hash_map[:commit] = current_commit
131
- end
132
- SqliteDb.instance.insert_table(name, branch, current_commit, nil, real_pod_path)
133
- LgPodPlugin::Cache.cache_pod(name, real_pod_path, is_update, hash_map)
134
- end
45
+ # unless real_pod_path.exist?
46
+ # return
47
+ # end
48
+ # # 切换到本地git仓库目录下
49
+ # FileUtils.chdir(real_pod_path)
50
+ # unless real_pod_path.glob("*.git")
51
+ # return
52
+ # end
53
+ # # 使用branch克隆代码
54
+ # git = Git.open(Pathname("./"))
55
+ # current_branch = git.current_branch
56
+ # if current_branch == branch # 要 clone 的分支正好等于当前分支
57
+ # current_commit = git.log(1).to_s
58
+ # if new_commit != current_commit && is_update
59
+ # #删除旧的pod 缓存
60
+ # self.cache.clean_old_cache(name, git_url, current_commit)
61
+ # LgPodPlugin.log_green "git pull #{name} origin/#{current_branch}"
62
+ # self.git_util.should_pull(git, current_branch, new_commit)
63
+ # current_commit = new_commit
64
+ # end
65
+ # hash_map = { :git => git_url }
66
+ # if current_commit
67
+ # hash_map[:commit] = current_commit
68
+ # end
69
+ # LSqliteDb.instance.insert_table(name, branch, current_commit, nil, real_pod_path)
70
+ # LgPodPlugin::LCache.cache_pod(name, real_pod_path, is_update, hash_map)
71
+ # else
72
+ # branch_exist = git.branches.local.find { |e| e.to_s == branch }
73
+ # if branch_exist
74
+ # LgPodPlugin.log_green "git switch #{name} #{git_url} -b #{branch}"
75
+ # self.git_util.git_switch(branch)
76
+ # else
77
+ # LgPodPlugin.log_green "git checkout #{name} #{git_url} -b #{branch}"
78
+ # self.git_util.git_checkout(branch)
79
+ # end
80
+ # current_commit = git.log(1).to_s
81
+ # if current_commit != new_commit
82
+ # LgPodPlugin.log_green "git pull #{name} #{git_url} -b #{branch}"
83
+ # self.git_util.should_pull(git, current_branch, new_commit)
84
+ # current_commit = new_commit
85
+ # end
86
+ # hash_map = { :git => git_url }
87
+ # if current_commit
88
+ # hash_map[:commit] = current_commit
89
+ # end
90
+ # LSqliteDb.instance.insert_table(name, branch, current_commit, nil, real_pod_path)
91
+ # LgPodPlugin::LCache.cache_pod(name, real_pod_path, is_update, hash_map)
92
+ # end
135
93
 
136
94
  end
137
95
 
@@ -1,7 +1,7 @@
1
1
 
2
2
  module LgPodPlugin
3
3
 
4
- class FileManager
4
+ class LFileManager
5
5
 
6
6
  def self.cache_director
7
7
  Pathname(File.join(Dir.home, "Library/Caches"))
@@ -9,7 +9,7 @@ module LgPodPlugin
9
9
 
10
10
  # 本地下载路径 ~Library/Caches/LgPodPlugin
11
11
  def self.download_director
12
- cache_path = self.cache_director.join("LgPodPlugin")
12
+ cache_path = self.cache_director.join("LgPodPlugin")
13
13
  unless cache_path.exist?
14
14
  # pp "文件路径不存在"
15
15
  cache_path.mkdir(0700)
@@ -17,8 +17,12 @@ module LgPodPlugin
17
17
  cache_path
18
18
  end
19
19
 
20
- def self.download_pod_path(name)
21
- download_director.join(name)
20
+ # pod缓存工作目录, 根据项目所在路径计算所得 确保唯一
21
+ def self.cache_workspace(root)
22
+ timestamp = "_#{Time.now.to_i}_"
23
+ key = root.to_path + timestamp + "#{(rand() * 10000000).to_i}"
24
+ director = Digest::MD5.hexdigest(key)
25
+ return self.download_director.join(director)
22
26
  end
23
27
 
24
28
  end
@@ -1,92 +1,80 @@
1
1
  require 'pp'
2
2
  require 'git'
3
- require_relative 'cache'
3
+ require_relative 'request'
4
+ require_relative 'l_cache'
4
5
 
5
6
  module LgPodPlugin
6
7
 
7
- class GitUtil
8
- # attr_accessor :git
9
- # attr_accessor :tag
10
- # attr_accessor :path
11
- # attr_accessor :name
12
- # attr_accessor :commit
13
- # attr_accessor :branch
14
- # attr_accessor :temp_git_path
15
- REQUIRED_ATTRS ||= %i[git tag path name commit branch temp_git_path is_cache].freeze
16
- attr_accessor(*REQUIRED_ATTRS)
8
+ class LGitUtil
17
9
 
18
- def initialize
19
- super
20
- end
10
+ REQUIRED_ATTRS ||= %i[git tag path name commit branch].freeze
11
+ attr_accessor(*REQUIRED_ATTRS)
21
12
 
22
- def git_init(name, options = {})
13
+ def initialize(name, options = {})
23
14
  self.name = name
24
15
  self.git = options[:git]
25
16
  self.tag = options[:tag]
26
17
  self.path = options[:path]
27
18
  self.branch = options[:branch]
28
19
  self.commit = options[:commit]
29
- self.is_cache = options[:depth]
30
20
  end
31
21
 
32
- def git_clone(path)
33
- if self.branch
34
- temp_git_path = path.join("l-temp-pod")
35
- LgPodPlugin.log_yellow "git clone --template= --single-branch --depth 1 --branch #{self.branch} #{self.git}"
36
- system("git clone --template= --single-branch --depth 1 --branch #{self.branch} #{self.git} #{temp_git_path}")
37
- temp_git_path
38
- else
39
- nil
22
+ def git_clone_repository(path)
23
+ FileUtils.chdir(path)
24
+ temp_name = "lg_temp_pod"
25
+ if self.git && self.tag
26
+ LgPodPlugin.log_blue "git clone --tag #{self.tag} #{self.git}"
27
+ system("git clone --depth=1 -b #{self.tag} #{self.git} #{temp_name}")
28
+ elsif self.git && self.branch
29
+ LgPodPlugin.log_blue "git clone --depth=1 --branch #{self.branch} #{self.git}"
30
+ system("git clone --depth=1 -b #{self.branch} #{self.git} #{temp_name}")
31
+ elsif self.git && self.commit
32
+ LgPodPlugin.log_blue "git clone #{self.git}"
33
+ git = Git.init(temp_name)
34
+ FileUtils.chdir(temp_name)
35
+ system("git remote add origin #{self.git}")
36
+ system("git fetch origin #{self.commit}")
37
+ system("git reset --hard FETCH_HEAD")
40
38
  end
39
+ return path.join(temp_name)
41
40
  end
42
41
 
43
- def git_checkout(branch)
44
- system("git checkout -b #{branch}")
45
- end
46
-
47
- def git_switch(branch)
48
- system("git switch #{branch}")
49
- end
42
+ # def git_checkout(branch)
43
+ # system("git checkout -b #{branch}")
44
+ # end
45
+ #
46
+ # def git_switch(branch)
47
+ # system("git switch #{branch}")
48
+ # end
50
49
 
51
- #noinspection RubyNilAnalysis
52
- def pre_download_git_remote(path, branch)
53
- lg_pod_path = Pathname(path)
54
- root_path = FileManager.download_director
55
- pod_info_db = SqliteDb.instance.select_table(self.name, branch)
56
- if lg_pod_path.exist? && pod_info_db.branch
57
- return lg_pod_path
50
+ def request_params
51
+ hash_map = {:git => git}
52
+ if git && tag
53
+ hash_map[:tag] = tag
54
+ hash_map[:commit] = self.commit
55
+ else
56
+ hash_map[:commit] = commit
58
57
  end
58
+ return hash_map
59
+ end
59
60
 
60
- FileUtils.chdir(root_path)
61
- temp_path = root_path.join("temp")
61
+ def pre_download_git_repository
62
+ temp_path = LFileManager.download_director.join("temp")
62
63
  if temp_path.exist?
63
- FileUtils.rm_r(temp_path)
64
+ FileUtils.rm_rf(temp_path)
64
65
  end
65
- temp_path.mkdir(0700)
66
- FileUtils.chdir(temp_path)
67
- #clone仓库
68
- get_temp_folder = git_clone(temp_path)
66
+ lg_pod_path = LRequest.shared.cache.cache_root
67
+ unless lg_pod_path.exist?
68
+ lg_pod_path.mkdir(0700)
69
+ end
70
+ get_temp_folder = git_clone_repository(lg_pod_path)
69
71
  #下载 git 仓库失败
70
72
  unless get_temp_folder.exist?
71
73
  return nil
72
74
  end
73
-
74
- if self.is_cache
75
- pod_root_director = FileManager.download_pod_path(name)
76
- unless pod_root_director.exist?
77
- FileUtils.mkdir(pod_root_director)
78
- end
79
- FileUtils.mv(get_temp_folder, lg_pod_path)
80
- temp_path.rmdir
81
- lg_pod_path
82
- else
83
- commit = GitUtil.git_ls_remote_refs(self.git, self.branch)
84
- LgPodPlugin::Cache.cache_pod(self.name, get_temp_folder, true, {:git => self.git, :commit => commit})
85
- system("cd ..")
86
- system("rm -rf #{get_temp_folder.to_path}")
87
- return lg_pod_path
88
- end
89
-
75
+ LgPodPlugin::LCache.cache_pod(self.name, get_temp_folder, self.request_params)
76
+ FileUtils.chdir(LFileManager.download_director)
77
+ FileUtils.rm_rf(lg_pod_path)
90
78
  end
91
79
 
92
80
  # 本地pod库git操作
@@ -119,29 +107,45 @@ module LgPodPlugin
119
107
  end
120
108
 
121
109
  # 获取最新的一条 commit 信息
122
- def self.git_ls_remote_refs(git, branch)
123
- last_commit = nil
124
- LgPodPlugin.log_yellow "git ls-remote #{git} #{branch}"
125
- sha = %x(git ls-remote #{git} #{branch}).split(" ").first
126
- if sha
127
- last_commit = sha
128
- return last_commit
110
+ def self.git_ls_remote_refs(git, branch, tag, commit)
111
+ if branch
112
+ LgPodPlugin.log_yellow "git ls-remote #{git} #{branch}"
113
+ new_commit = %x(git ls-remote #{git} #{branch}).split(" ").first
114
+ return [branch, new_commit]
115
+ end
116
+ ls = Git.ls_remote(git, :head => true )
117
+ if tag
118
+ map = ls["tags"]
119
+ keys = map.keys
120
+ idx = keys.index("#{tag}")
121
+ unless idx
122
+ return [nil, nil]
123
+ end
124
+ key = keys[idx]
125
+ new_commit = map[key][:sha]
126
+ return [nil, new_commit]
129
127
  else
130
- ls = Git.ls_remote(git, :refs => true )
131
- find_branch = ls["branches"][branch]
132
- if find_branch
133
- last_commit = find_branch[:sha]
134
- return last_commit
128
+ new_commit = nil
129
+ new_branch = nil
130
+ find_commit = commit ||= ls["head"][:sha]
131
+ ls["branches"].each do |key, value|
132
+ sha = value[:sha]
133
+ next if sha != find_commit
134
+ new_branch = key
135
+ new_commit = find_commit
136
+ return [new_branch, new_commit]
137
+ break
135
138
  end
136
- return nil
139
+ return [new_branch , new_commit]
137
140
  end
138
141
  end
139
142
 
140
143
  # 是否pull 代码
141
144
  def should_pull(git, branch, new_commit = nil)
142
- git_url = git.remote.url
145
+ new_barnch = branch ||= self.branch
146
+ git_url = git.remote.url ||= self.git
143
147
  if new_commit == nil
144
- new_commit = GitUtil.git_ls_remote_refs(git_url, branch)
148
+ new_branch, new_commit = LGitUtil.git_ls_remote_refs(git_url, new_barnch,nil, nil)
145
149
  end
146
150
  local_commit = git.log(1).to_s #本地最后一条 commit hash 值
147
151
  if local_commit != new_commit