lg_pod_plugin 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7a39307dd5e4f73faefa55775b51e880f9f0fda326cc618c62b957ddcba669e
4
- data.tar.gz: 46777e3af13f74c44086743bb48352373539806a22aafc758eb104dde3f4864c
3
+ metadata.gz: e16255ce6cf29df74aa508c022dc083727d4fd78f65d95a0b68b57b10e075767
4
+ data.tar.gz: f1ac6d11e1cae7f98ee7332aca930d2389b9c07799270755dd8fd1b03a9e1cbb
5
5
  SHA512:
6
- metadata.gz: a945450a624206b131df0b963fbd8616b4df179112b54f41625ed90a46b5480823d74b80c0dc75c227f56de3d74fe225cb44243396d11ff7d864399adcbd89a0
7
- data.tar.gz: c72bbba5713477f4f094387b650298768e94ec3de1a831daead4d2eb588654cb58190d3cbd474e983bc7b02c2b3ce144c032f27722ea75802020a513488490a8
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)
@@ -53,7 +53,7 @@ module LgPodPlugin
53
53
  self.delete_table(current_pod.name, current_pod.branch)
54
54
  end
55
55
  array = self.select_tables(name, branch)
56
- if array.count <= 1
56
+ if array.count <= 2
57
57
  return
58
58
  end
59
59
 
@@ -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,22 +1,16 @@
1
1
  require 'git'
2
- require_relative 'cache.rb'
3
- require_relative 'database.rb'
4
- require_relative 'file_path.rb'
2
+ require_relative 'l_cache'
3
+ require_relative 'database'
4
+ require_relative 'file_path'
5
5
 
6
6
  module LgPodPlugin
7
7
 
8
- class Downloader
9
- attr_accessor :git_util
10
- attr_accessor :db
11
- attr_accessor :cache
8
+ class LDownloader
9
+
12
10
  REQUIRED_ATTRS ||= %i[git name commit branch tag options].freeze
13
11
  attr_accessor(*REQUIRED_ATTRS)
14
- def initialize
15
- self.cache = Cache.new
16
- self.db = SqliteDb.instance
17
- super
18
- end
19
- def download_init(name, options = {})
12
+
13
+ def initialize(name, options = {})
20
14
  self.name = name
21
15
  self.options = options
22
16
  self.git = options[:git]
@@ -24,105 +18,78 @@ module LgPodPlugin
24
18
  self.branch = options[:branch]
25
19
  self.commit = options[:commit]
26
20
  end
21
+
22
+ # def check_cache_valid(name, branch)
23
+ # self.db.should_clean_pod_info(name, branch)
24
+ # end
27
25
 
28
- def is_update_pod
29
- cgi = CGI.new
30
- command_keys = cgi.keys
31
- unless command_keys.count > 0
32
- return false
33
- end
34
- first_key = command_keys[0].to_s ||= ""
35
- if first_key.include?("install")
36
- false
37
- elsif first_key.include?("update")
38
- true
26
+ # 预下载处理
27
+ def pre_download_pod
28
+ if self.branch
29
+ LgPodPlugin.log_green "Using `#{name}` (#{branch})"
39
30
  else
40
- false
31
+ LgPodPlugin.log_green "Using `#{name}`"
41
32
  end
42
- end
43
-
44
- def check_cache_valid(name, branch)
45
- self.db.should_clean_pod_info(name, branch)
46
- end
47
-
48
- # 预下载处理
49
- def pre_download_pod(git)
50
- self.git_util = git
51
- is_update = self.is_update_pod
52
- self.git_util.git_init(self.name, self.options)
53
- # if name == "LBase" || name == "LLogger" || name == "LUnityFramework" || name == "LUser"
54
- # pp name
55
- # end
56
- # tag = options[:tag]
57
- git_url = options[:git]
58
- # commit = options[:commit]
59
- branch = options[:branch]
60
- LgPodPlugin.log_green "Using `#{name}` (#{branch})"
61
33
  # 发现本地有缓存, 不需要更新缓存
62
- need_download, new_commit = self.cache.find_pod_cache(name, git_url, branch, is_update)
63
- 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
64
36
  LgPodPlugin.log_green "find the cache of `#{name}`, you can use it now."
65
37
  return
66
- end
67
-
68
- # 检查是否要清空缓存
69
- if is_update
70
- check_cache_valid(name, branch)
71
- end
72
-
73
- # 本地 git 下载 pod 目录
74
- pre_down_load_path = self.cache.get_download_path(name, git_url, branch)
75
- real_pod_path = self.git_util.pre_download_git_remote(pre_down_load_path, branch)
76
- # 本地clone代码失败跳出去
77
- unless real_pod_path.exist?
78
- return
79
- end
80
- # 切换到本地git仓库目录下
81
- FileUtils.chdir(real_pod_path)
82
- unless real_pod_path.glob("*.git")
83
- return
84
- end
85
- # 使用branch克隆代码
86
- git = Git.open(Pathname("./"))
87
- current_branch = git.current_branch
88
- if current_branch == branch # 要 clone 的分支正好等于当前分支
89
- current_commit = git.log(1).to_s
90
- if new_commit != current_commit && is_update
91
- #删除旧的pod 缓存
92
- self.cache.clean_old_cache(name, git_url, current_commit)
93
- LgPodPlugin.log_green "git pull #{name} origin/#{current_branch}"
94
- self.git_util.should_pull(git, current_branch, new_commit)
95
- current_commit = new_commit
96
- end
97
- hash_map = {:git => git_url}
98
- if current_commit
99
- hash_map[:commit] = current_commit
100
- end
101
- SqliteDb.instance.insert_table(name, branch, current_commit, nil, real_pod_path)
102
- LgPodPlugin::Cache.cache_pod(name,real_pod_path,is_update,hash_map)
103
38
  else
104
- branch_exist = git.branches.local.find {|e| e.to_s == branch}
105
- if branch_exist
106
- LgPodPlugin.log_green "git switch #{name} #{git_url} -b #{branch}"
107
- self.git_util.git_switch(branch)
108
- else
109
- LgPodPlugin.log_green "git checkout #{name} #{git_url} -b #{branch}"
110
- self.git_util.git_checkout(branch)
111
- end
112
- current_commit = git.log(1).to_s
113
- if current_commit != new_commit
114
- LgPodPlugin.log_green "git pull #{name} #{git_url} -b #{branch}"
115
- self.git_util.should_pull(git, current_branch, new_commit)
116
- current_commit = new_commit
117
- end
118
- hash_map = {:git => git_url}
119
- if current_commit
120
- hash_map[:commit] = current_commit
121
- end
122
- SqliteDb.instance.insert_table(name, branch, current_commit, nil, real_pod_path)
123
- LgPodPlugin::Cache.cache_pod(name,real_pod_path,is_update,hash_map)
39
+ LgPodPlugin.log_green "find the new commit of `#{name}`, Git downloading now."
124
40
  end
41
+ # 本地 git 下载 pod 目录
42
+ LRequest.shared.git_util.pre_download_git_repository
125
43
 
44
+ # 本地clone代码失败跳出去
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
126
93
 
127
94
  end
128
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,21 +1,16 @@
1
1
  require 'pp'
2
2
  require 'git'
3
+ require_relative 'request'
4
+ require_relative 'l_cache'
3
5
 
4
6
  module LgPodPlugin
5
7
 
6
- class GitUtil
7
- attr_accessor :git
8
- attr_accessor :tag
9
- attr_accessor :path
10
- attr_accessor :name
11
- attr_accessor :commit
12
- attr_accessor :branch
13
- attr_accessor :temp_git_path
14
- def initialize
15
- super
16
- end
8
+ class LGitUtil
9
+
10
+ REQUIRED_ATTRS ||= %i[git tag path name commit branch].freeze
11
+ attr_accessor(*REQUIRED_ATTRS)
17
12
 
18
- def git_init(name, options = {})
13
+ def initialize(name, options = {})
19
14
  self.name = name
20
15
  self.git = options[:git]
21
16
  self.tag = options[:tag]
@@ -24,54 +19,62 @@ module LgPodPlugin
24
19
  self.commit = options[:commit]
25
20
  end
26
21
 
27
- def git_clone(path)
28
- if self.branch
29
- temp_git_path = path.join("l-temp-pod")
30
- LgPodPlugin.log_blue "git clone --template= --single-branch --depth 1 --branch #{self.branch} #{self.git}"
31
- system("git clone --template= --single-branch --depth 1 --branch #{self.branch} #{self.git} #{temp_git_path}")
32
- temp_git_path
33
- else
34
- 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")
35
38
  end
39
+ return path.join(temp_name)
36
40
  end
37
41
 
38
- def git_checkout(branch)
39
- system("git checkout -b #{branch}")
40
- end
41
-
42
- def git_switch(branch)
43
- system("git switch #{branch}")
44
- 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
45
49
 
46
- #noinspection RubyNilAnalysis
47
- def pre_download_git_remote(path, branch)
48
- lg_pod_path = Pathname(path)
49
- root_path = FileManager.download_director
50
- pod_info_db = SqliteDb.instance.select_table(self.name, branch)
51
- if lg_pod_path.exist? && pod_info_db.branch
52
- 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
53
57
  end
58
+ return hash_map
59
+ end
54
60
 
55
- FileUtils.chdir(root_path)
56
- temp_path = root_path.join("tmp")
61
+ def pre_download_git_repository
62
+ temp_path = LFileManager.download_director.join("temp")
57
63
  if temp_path.exist?
58
- FileUtils.rm_r(temp_path)
64
+ FileUtils.rm_rf(temp_path)
65
+ end
66
+ lg_pod_path = LRequest.shared.cache.cache_root
67
+ unless lg_pod_path.exist?
68
+ lg_pod_path.mkdir(0700)
59
69
  end
60
- temp_path.mkdir(0700)
61
- FileUtils.chdir(temp_path)
62
- #clone仓库
63
- get_temp_folder = git_clone(temp_path)
70
+ get_temp_folder = git_clone_repository(lg_pod_path)
64
71
  #下载 git 仓库失败
65
72
  unless get_temp_folder.exist?
66
73
  return nil
67
74
  end
68
- pod_root_director = FileManager.download_pod_path(name)
69
- unless pod_root_director.exist?
70
- FileUtils.mkdir(pod_root_director)
71
- end
72
- FileUtils.mv(get_temp_folder, lg_pod_path)
73
- temp_path.rmdir
74
- lg_pod_path
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)
75
78
  end
76
79
 
77
80
  # 本地pod库git操作
@@ -104,29 +107,45 @@ module LgPodPlugin
104
107
  end
105
108
 
106
109
  # 获取最新的一条 commit 信息
107
- def self.git_ls_remote_refs(git, branch)
108
- last_commit = nil
109
- LgPodPlugin.log_green "git ls-remote #{git} #{branch}"
110
- sha = %x(git ls-remote #{git} #{branch}).split(" ").first
111
- if sha
112
- last_commit = sha
113
- 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]
114
127
  else
115
- ls = Git.ls_remote(git, :refs => true )
116
- find_branch = ls["branches"][branch]
117
- if find_branch
118
- last_commit = find_branch[:sha]
119
- 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
120
138
  end
121
- return nil
139
+ return [new_branch , new_commit]
122
140
  end
123
141
  end
124
142
 
125
143
  # 是否pull 代码
126
144
  def should_pull(git, branch, new_commit = nil)
127
- git_url = git.remote.url
145
+ new_barnch = branch ||= self.branch
146
+ git_url = git.remote.url ||= self.git
128
147
  if new_commit == nil
129
- 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)
130
149
  end
131
150
  local_commit = git.log(1).to_s #本地最后一条 commit hash 值
132
151
  if local_commit != new_commit