Skip to content

Commit 1251264

Browse files
committed
refactor(nuget): add download handling for source
1 parent 4bda9ae commit 1251264

File tree

2 files changed

+64
-83
lines changed

2 files changed

+64
-83
lines changed

lua/mason-core/installer/compiler/compilers/nuget.lua

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,83 @@
11
local Result = require "mason-core.result"
2+
local common = require "mason-core.installer.managers.common"
3+
local util = require "mason-core.installer.registry.util"
4+
local expr = require "mason-core.installer.registry.expr"
5+
local nuget = require "mason-core.installer.managers.nuget"
26
local _ = require "mason-core.functional"
37

48
local M = {}
59

6-
---@param source RegistryPackageSource
10+
---@class NugetPackageSource : RegistryPackageSource
11+
---@field download FileDownloadSpec
12+
13+
---@param source NugetPackageSource
714
---@param purl Purl
815
function M.parse(source, purl)
16+
return Result.try(function (try)
17+
local repository_url = _.path({ "qualifiers", "repository_url" }, purl)
918

10-
local repository_url = _.path({ "qualifiers", "repository_url" }, purl)
19+
local download_item = nil
20+
if source.download then
1121

12-
if not repository_url then
13-
repository_url = "https://api.nuget.org/v3/index.json"
14-
end
22+
if not repository_url then
23+
-- if not set we need to provide repository url because we need it for
24+
-- download url discovery
25+
repository_url = "https://api.nuget.org/v3/index.json"
26+
end
27+
28+
local index_file = try(nuget.fetch_nuget_endpoint(repository_url))
29+
30+
local resource = vim.iter(index_file.resources)
31+
:find(function (v)
32+
return v['@type'] == 'PackageBaseAddress/3.0.0'
33+
end)
34+
35+
assert(resource, "could not get PackageBaseAddress resource from nuget index file")
36+
37+
local package_base_address = resource["@id"]
38+
local package_lowercase = purl.name:lower()
1539

16-
---@class ParsedNugetSource : ParsedPackageSource
17-
---@field repository_url string Custom repository URL to pull from
18-
local parsed_source = {
19-
package = purl.name,
20-
version = purl.version,
21-
repository_url = repository_url
22-
}
40+
local nupkg_download_url = string.format("%s%s/%s/%s.%s.nupkg",
41+
package_base_address,
42+
package_lowercase,
43+
purl.version,
44+
package_lowercase,
45+
purl.version)
2346

24-
return Result.success(parsed_source)
47+
local expr_ctx = { version = purl.version }
48+
49+
---@type FileDownloadSpec
50+
local download_spec = try(util.coalesce_by_target(try(expr.tbl_interpolate(source.download, expr_ctx)), {}))
51+
52+
download_item = {
53+
download_url = nupkg_download_url,
54+
out_file = download_spec.file
55+
}
56+
end
57+
58+
---@class ParsedNugetSource : ParsedPackageSource
59+
---@field download? DownloadItem
60+
---@field repository_url string Custom repository URL to pull from
61+
local parsed_source = {
62+
package = purl.name,
63+
version = purl.version,
64+
download = download_item,
65+
repository_url = repository_url
66+
}
67+
68+
return parsed_source
69+
end)
2570
end
2671

2772
---@async
2873
---@param ctx InstallContext
2974
---@param source ParsedNugetSource
3075
function M.install(ctx, source)
31-
local nuget = require "mason-core.installer.managers.nuget"
32-
return nuget.install(source.package, source.version, source.repository_url)
76+
if source.download then
77+
return common.download_files(ctx, {source.download})
78+
else
79+
return nuget.install(source.package, source.version, source.repository_url)
80+
end
3381
end
3482

3583
---@async

lua/mason-core/installer/managers/nuget.lua

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,16 @@
11
local Result = require "mason-core.result"
22
local installer = require "mason-core.installer"
3-
local log = require "mason-core.log"
43
local platform = require "mason-core.platform"
54
local fetch = require "mason-core.fetch"
6-
local common = require "mason-core.installer.managers.common"
75

86
local M = {}
97

10-
---@async
11-
---@param package string
12-
---@param version string
13-
---@nodiscard
14-
function M.install(package, version, repository_url)
15-
log.fmt_debug("nuget: install %s %s", package, version)
16-
local ctx = installer.context()
17-
18-
local index_file = M.fetch_nuget_endpoint(repository_url):get_or_throw()
19-
20-
assert(index_file, "nuget index file could not be retrieved")
21-
22-
local resource = vim.iter(index_file.resources)
23-
:find(function (v)
24-
return v['@type'] == 'PackageBaseAddress/3.0.0'
25-
end)
26-
27-
assert(resource, "could not get PackageBaseAddress resource from nuget index file")
28-
29-
local package_base_address = resource["@id"]
30-
local package_lowercase = package:lower()
31-
32-
local nuspec_url = string.format("%s%s/%s/%s.nuspec",
33-
package_base_address,
34-
package_lowercase,
35-
version,
36-
package_lowercase)
37-
38-
assert(nuspec_url, "nuspec url should be set")
39-
40-
local nuspec_file = M.fetch_nuget_endpoint_xml(nuspec_url):get_or_throw()
41-
42-
ctx.stdio_sink.stdout(("Installing nuget package %s@%s…\n"):format(package, version))
43-
44-
local is_dotnet_tool = string.match(nuspec_file, "<packageType%s+name%s*=%s*\"DotnetTool\"%s*/>")
45-
if is_dotnet_tool then
46-
return M.install_dotnet_tool(package, version, repository_url)
47-
else
48-
local nupkg_download_url = string.format("%s%s/%s/%s.%s.nupkg",
49-
package_base_address,
50-
package_lowercase,
51-
version,
52-
package_lowercase,
53-
version)
54-
55-
local download_item = {
56-
download_url = nupkg_download_url,
57-
out_file = string.format("%s-%s.nupkg", package, version)
58-
}
59-
60-
return common.download_files(ctx, { download_item })
61-
end
62-
end
63-
648
---@async
659
---@param package string
6610
---@param version string
6711
---@param repository_url string
6812
---@nodiscard
69-
function M.install_dotnet_tool(package, version, repository_url)
13+
function M.install(package, version, repository_url)
7014
local ctx = installer.context()
7115

7216
local args = {
@@ -100,17 +44,6 @@ function M.fetch_nuget_endpoint(repository_url)
10044
}):map_catching(vim.json.decode)
10145
end
10246

103-
---@async
104-
---@param repository_url string
105-
---@return Result
106-
function M.fetch_nuget_endpoint_xml(repository_url)
107-
return fetch(repository_url, {
108-
headers = {
109-
Accept = "application/xml",
110-
},
111-
})
112-
end
113-
11447
---@param bin string
11548
function M.bin_path(bin)
11649
return Result.pcall(platform.when, {

0 commit comments

Comments
 (0)