Skip to content

Commit

Permalink
improve proxy to support mirror url
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Apr 23, 2021
1 parent 99a6f53 commit ef17fec
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [#1338](https://github.com/xmake-io/xmake/issues/1338): Support import and export installed packages
* [#1087](https://github.com/xmake-io/xmake/issues/1087): Add `xrepo env shell` and support load envs from `add_requires/xmake.lua`
* [#1313](https://github.com/xmake-io/xmake/issues/1313): Support private package for `add_requires/add_deps`
* [#1358](https://github.com/xmake-io/xmake/issues/1358): Support to set mirror url to speedup download package

### Change

Expand Down Expand Up @@ -986,6 +987,7 @@
* [#1338](https://github.com/xmake-io/xmake/issues/1338): 支持导入导出已安装的包
* [#1087](https://github.com/xmake-io/xmake/issues/1087): 添加 `xrepo env shell` 并且支持从 `add_requires/xmake.lua` 加载包环境
* [#1313](https://github.com/xmake-io/xmake/issues/1313): 为 `add_requires/add_deps` 添加私有包支持
* [#1358](https://github.com/xmake-io/xmake/issues/1358): 支持设置镜像 url 站点加速包下载

### 改进

Expand Down
2 changes: 1 addition & 1 deletion xmake/modules/devel/git/clone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function main(url, opt)

-- use proxy?
local envs
local proxy_conf = proxy.get(url)
local proxy_conf = proxy.config(url)
if proxy_conf then
envs = {ALL_PROXY = proxy_conf}
end
Expand Down
2 changes: 1 addition & 1 deletion xmake/modules/devel/git/ls_remote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function main(reftype, url)

-- use proxy?
local envs
local proxy_conf = proxy.get(url)
local proxy_conf = proxy.config(url)
if proxy_conf then
envs = {ALL_PROXY = proxy_conf}
end
Expand Down
4 changes: 2 additions & 2 deletions xmake/modules/devel/git/pull.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function main(opt)

-- use proxy?
local envs
local proxy_conf = proxy.get()
local proxy_conf = proxy.config()
if proxy_conf then
-- get proxy configuration from the current remote url
local remoteinfo = try { function() return os.iorunv(git.program, {"remote", "-v"}) end }
Expand All @@ -76,7 +76,7 @@ function main(opt)
if #splitinfo > 1 and splitinfo[1] == (opt.remote or "origin") then
local url = splitinfo[2]
if url then
proxy_conf = proxy.get(url)
proxy_conf = proxy.config(url)
end
break
end
Expand Down
4 changes: 2 additions & 2 deletions xmake/modules/net/http/download.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function _curl_download(tool, url, outputfile, opt)
end

-- use proxy?
local proxy_conf = proxy.get(url)
local proxy_conf = proxy.config(url)
if proxy_conf then
table.insert(argv, "-x")
table.insert(argv, proxy_conf)
Expand Down Expand Up @@ -114,7 +114,7 @@ function _wget_download(tool, url, outputfile, opt)
end

-- use proxy?
local proxy_conf = proxy.get(url)
local proxy_conf = proxy.config(url)
if proxy_conf then
table.insert(argv, "-e")
table.insert(argv, "use_proxy=yes")
Expand Down
14 changes: 13 additions & 1 deletion xmake/modules/net/proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ end
-- pac.lua
--
-- @code
-- function mirror(url)
-- return url:gsub("github.com", "hub.fastgit.org")
-- end
-- function main(url, host)
-- if host:find("bintray.com") then
-- return true
Expand Down Expand Up @@ -75,11 +78,20 @@ function _host_pattern(pattern)
return pattern
end

-- get proxy mirror url
function mirror(url)
local proxy_pac = _proxy_pac()
if proxy_pac and proxy_pac.mirror then
return proxy_pac.mirror(url)
end
return url
end

-- get proxy configuration from the given url, [protocol://]host[:port]
--
-- @see https://github.com/xmake-io/xmake/issues/854
--
function get(url)
function config(url)

-- enable proxy for the given url and configuration pattern
if url then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import("lib.detect.find_directory")
import("private.action.require.impl.utils.filter")
import("private.action.require.impl..utils.url_filename")
import("net.http")
import("net.proxy")
import("devel.git")
import("utils.archive")

Expand Down Expand Up @@ -67,13 +68,13 @@ function _checkout(package, url, sourcedir, url_alias)
if package:branch() then

-- only shadow clone this branch
git.clone(url, {depth = 1, recursive = true, longpaths = longpaths, branch = package:branch(), outputdir = packagedir})
git.clone(proxy.mirror(url), {depth = 1, recursive = true, longpaths = longpaths, branch = package:branch(), outputdir = packagedir})

-- download package from revision or tag?
else

-- clone whole history and tags
git.clone(url, {longpaths = longpaths, outputdir = packagedir})
git.clone(proxy.mirror(url), {longpaths = longpaths, outputdir = packagedir})

-- attempt to checkout the given version
local revision = package:revision(url_alias) or package:tag() or package:version_str()
Expand Down Expand Up @@ -134,7 +135,7 @@ function _download(package, url, sourcedir, url_alias, url_excludes)
-- we can use local package from the search directories directly if network is too slow
os.cp(localfile, packagefile)
else
http.download(url, packagefile)
http.download(proxy.mirror(url), packagefile)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import("core.base.option")
import("core.package.package", {alias = "core_package"})
import("lib.detect.find_file")
import("net.http")
import("net.proxy")
import("utils.archive")

-- download resources
Expand Down Expand Up @@ -55,7 +56,7 @@ function _download(package, resource_name, resource_url, resource_hash)
-- we can use local resource from the search directories directly if network is too slow
os.cp(localfile, resource_file)
elseif resource_url:find(string.ipattern("https-://")) or resource_url:find(string.ipattern("ftps-://")) then
http.download(resource_url, resource_file)
http.download(proxy.mirror(resource_url), resource_file)
else
raise("invalid resource url(%s)", resource_url)
end
Expand Down

0 comments on commit ef17fec

Please sign in to comment.