Skip to content

Commit

Permalink
Merge pull request #2025 from hanagasira/nuget_refactor
Browse files Browse the repository at this point in the history
Fix missing targets file issue in some C++ nuget packages
  • Loading branch information
samsinsane authored Apr 12, 2023
2 parents 31f81fc + 53bc271 commit d150691
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 43 deletions.
5 changes: 4 additions & 1 deletion modules/vstudio/tests/vc2010/test_ensure_nuget_imports.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

if _OPTIONS["test-all"] then
function suite.structureIsCorrect()
nuget { "boost:1.59.0-b1", "sdl2.v140:2.0.3", "sdl2.v140.redist:2.0.3" }
nuget { "boost:1.59.0-b1", "sdl2.v140:2.0.3", "sdl2.v140.redist:2.0.3", "WinPixEventRuntime:1.0.220810001", "Microsoft.Direct3D.D3D12:1.608.2" }
prepare()
test.capture [[
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand All @@ -54,6 +54,9 @@ if _OPTIONS["test-all"] then
<Error Condition="!Exists('packages\boost.1.59.0-b1\build\native\boost.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\boost.1.59.0-b1\build\native\boost.targets'))" />
<Error Condition="!Exists('packages\sdl2.v140.2.0.3\build\native\sdl2.v140.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\sdl2.v140.2.0.3\build\native\sdl2.v140.targets'))" />
<Error Condition="!Exists('packages\sdl2.v140.redist.2.0.3\build\native\sdl2.v140.redist.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\sdl2.v140.redist.2.0.3\build\native\sdl2.v140.redist.targets'))" />
<Error Condition="!Exists('packages\WinPixEventRuntime.1.0.220810001\build\WinPixEventRuntime.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\WinPixEventRuntime.1.0.220810001\build\WinPixEventRuntime.targets'))" />
<Error Condition="!Exists('packages\Microsoft.Direct3D.D3D12.1.608.2\build\native\Microsoft.Direct3D.D3D12.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Direct3D.D3D12.1.608.2\build\native\Microsoft.Direct3D.D3D12.props'))" />
<Error Condition="!Exists('packages\Microsoft.Direct3D.D3D12.1.608.2\build\native\Microsoft.Direct3D.D3D12.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Direct3D.D3D12.1.608.2\build\native\Microsoft.Direct3D.D3D12.targets'))" />
</Target>
]]
end
Expand Down
16 changes: 16 additions & 0 deletions modules/vstudio/tests/vc2010/test_extension_settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@
vc2010.importExtensionSettings(prj)
end

--
-- Writes entries only for nuget packages with props files.
--

if _OPTIONS["test-all"] then
function suite.importOnlyNugetPackagesWithProps()
nuget { "boost:1.59.0-b1", "sdl2.v140:2.0.3", "sdl2.v140.redist:2.0.3", "WinPixEventRuntime:1.0.220810001", "Microsoft.Direct3D.D3D12:1.608.2" }
prepare()
test.capture [[
<ImportGroup Label="ExtensionSettings">
<Import Project="packages\Microsoft.Direct3D.D3D12.1.608.2\build\native\Microsoft.Direct3D.D3D12.props" Condition="Exists('packages\Microsoft.Direct3D.D3D12.1.608.2\build\native\Microsoft.Direct3D.D3D12.props')" />
</ImportGroup>
]]
end
end


--
-- Writes an empty element when no custom rules are specified.
Expand Down
4 changes: 3 additions & 1 deletion modules/vstudio/tests/vc2010/test_extension_targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@

if _OPTIONS["test-all"] then
function suite.addsImport_onEachNuGetPackage()
nuget { "boost:1.59.0-b1", "sdl2.v140:2.0.3", "sdl2.v140.redist:2.0.3" }
nuget { "boost:1.59.0-b1", "sdl2.v140:2.0.3", "sdl2.v140.redist:2.0.3", "WinPixEventRuntime:1.0.220810001", "Microsoft.Direct3D.D3D12:1.608.2" }
prepare()
test.capture [[
<ImportGroup Label="ExtensionTargets">
<Import Project="packages\boost.1.59.0-b1\build\native\boost.targets" Condition="Exists('packages\boost.1.59.0-b1\build\native\boost.targets')" />
<Import Project="packages\sdl2.v140.2.0.3\build\native\sdl2.v140.targets" Condition="Exists('packages\sdl2.v140.2.0.3\build\native\sdl2.v140.targets')" />
<Import Project="packages\sdl2.v140.redist.2.0.3\build\native\sdl2.v140.redist.targets" Condition="Exists('packages\sdl2.v140.redist.2.0.3\build\native\sdl2.v140.redist.targets')" />
<Import Project="packages\WinPixEventRuntime.1.0.220810001\build\WinPixEventRuntime.targets" Condition="Exists('packages\WinPixEventRuntime.1.0.220810001\build\WinPixEventRuntime.targets')" />
<Import Project="packages\Microsoft.Direct3D.D3D12.1.608.2\build\native\Microsoft.Direct3D.D3D12.targets" Condition="Exists('packages\Microsoft.Direct3D.D3D12.1.608.2\build\native\Microsoft.Direct3D.D3D12.targets')" />
</ImportGroup>
]]
end
Expand Down
4 changes: 3 additions & 1 deletion modules/vstudio/tests/vc2010/test_nuget_packages_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@
--

function suite.structureIsCorrect()
nuget { "boost:1.59.0-b1", "sdl2.v140:2.0.3", "sdl2.v140.redist:2.0.3" }
nuget { "boost:1.59.0-b1", "sdl2.v140:2.0.3", "sdl2.v140.redist:2.0.3", "WinPixEventRuntime:1.0.220810001", "Microsoft.Direct3D.D3D12:1.608.2" }
prepare()
test.capture [[
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="boost" version="1.59.0-b1" targetFramework="native" />
<package id="sdl2.v140" version="2.0.3" targetFramework="native" />
<package id="sdl2.v140.redist" version="2.0.3" targetFramework="native" />
<package id="WinPixEventRuntime" version="1.0.220810001" targetFramework="native" />
<package id="Microsoft.Direct3D.D3D12" version="1.608.2" targetFramework="native" />
</packages>
]]
end
64 changes: 30 additions & 34 deletions modules/vstudio/vs2010_nuget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,31 @@
return
end

if p.project.isdotnet(prj) then
-- Using the local file listing for "packageEntries" might
-- not exactly match what we would get from the API but this
-- doesn't matter. At the moment of writing, we're only
-- interested in knowing what DLL files the package
-- contains.
-- Using the local file listing for "packageEntries" might
-- not exactly match what we would get from the API but this
-- doesn't matter. At the moment of writing, we're only
-- interested in knowing what DLL files the package
-- contains.

packageAPIInfo.packageEntries = {}
packageAPIInfo.packageEntries = {}

for _, file in ipairs(os.matchfiles(path.translate(path.join(versionPath, "**")))) do
local extension = path.getextension(file)
for _, file in ipairs(os.matchfiles(path.translate(path.join(versionPath, "**")))) do
local extension = path.getextension(file)

if extension ~= ".nupkg" and extension ~= ".sha512" then
table.insert(packageAPIInfo.packageEntries, path.translate(path.getrelative(versionPath, file)))
end
if extension ~= ".nupkg" and extension ~= ".sha512" then
table.insert(packageAPIInfo.packageEntries, path.translate(path.getrelative(versionPath, file)))
end
end

if #packageAPIInfo.packageEntries == 0 then
return
end
if #packageAPIInfo.packageEntries == 0 then
return
end

if nuspec:match("<frameworkAssemblies>(.+)</frameworkAssemblies>") then
p.warn("NuGet package '%s' may depend on .NET Framework assemblies - package dependencies are currently unimplemented", id)
end
if nuspec:match("<frameworkAssemblies>(.+)</frameworkAssemblies>") then
p.warn("NuGet package '%s' may depend on .NET Framework assemblies - package dependencies are currently unimplemented", id)
end


if nuspec:match("<dependencies>(.+)</dependencies>") then
p.warn("NuGet package '%s' may depend on other packages - package dependencies are currently unimplemented", id)
end
Expand Down Expand Up @@ -283,33 +282,30 @@
packageAPIInfo.verbatimVersion = response.verbatimVersion
packageAPIInfo.version = response.version

-- C++ packages don't have this, but C# packages have a
-- packageEntries field that lists all the files in the
-- package. We need to look at this to figure out what
-- DLLs to reference in the project file.

if prj.language == "C#" and not response.packageEntries then
if not response.packageEntries then
p.error("NuGet package '%s' version '%s' has no file listing. This package might be too old to be using this API or it might be a C++ package instead of a .NET Framework package.", id, response.version)
end

if prj.language == "C#" then
packageAPIInfo.packageEntries = {}

for _, item in ipairs(response.packageEntries) do
if not item.fullName then
p.error("Failed to understand NuGet API response (package '%s' version '%s' packageEntry has no fullName)", id, version)
end
packageAPIInfo.packageEntries = {}

table.insert(packageAPIInfo.packageEntries, path.translate(item.fullName))
for _, item in ipairs(response.packageEntries) do
if not item.fullName then
p.error("Failed to understand NuGet API response (package '%s' version '%s' packageEntry has no fullName)", id, version)
end

if #packageAPIInfo.packageEntries == 0 then
p.error("NuGet package '%s' file listing is empty", id)
end
table.insert(packageAPIInfo.packageEntries, path.translate(item.fullName))
end

if response.frameworkAssemblyGroup then
p.warn("NuGet package '%s' may depend on .NET Framework assemblies - package dependencies are currently unimplemented", id)
end
if #packageAPIInfo.packageEntries == 0 then
p.error("NuGet package '%s' file listing is empty", id)
end

if response.frameworkAssemblyGroup then
p.warn("NuGet package '%s' may depend on .NET Framework assemblies - package dependencies are currently unimplemented", id)
end

if response.dependencyGroups then
Expand Down
42 changes: 36 additions & 6 deletions modules/vstudio/vs2010_vcxproj.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2148,16 +2148,28 @@
end
end

local function nuGetTargetsFile(prj, package)
local function nuGetTargetsFile(prj, package, extension)
local packageAPIInfo = vstudio.nuget2010.packageAPIInfo(prj, package)
return p.vstudio.path(prj, p.filename(prj.workspace, string.format("packages\\%s.%s\\build\\native\\%s.targets", vstudio.nuget2010.packageId(package), packageAPIInfo.verbatimVersion or packageAPIInfo.version, vstudio.nuget2010.packageId(package))))
if not packageAPIInfo.packageEntries then
return nil
end
for _, entry in ipairs(packageAPIInfo.packageEntries) do
if path.getextension(entry) == extension then
local packageRootPath = p.filename(prj.workspace, string.format("packages\\%s.%s\\", vstudio.nuget2010.packageId(package), packageAPIInfo.verbatimVersion or packageAPIInfo.version))
return p.vstudio.path(prj, path.join(packageRootPath, entry))
end
end

return nil
end

function m.importNuGetTargets(prj)
if not vstudio.nuget2010.supportsPackageReferences(prj) then
for i = 1, #prj.nuget do
local targetsFile = nuGetTargetsFile(prj, prj.nuget[i])
p.x('<Import Project="%s" Condition="Exists(\'%s\')" />', targetsFile, targetsFile)
local targetsFile = nuGetTargetsFile(prj, prj.nuget[i], ".targets")
if targetsFile then
p.x('<Import Project="%s" Condition="Exists(\'%s\')" />', targetsFile, targetsFile)
end
end
end
end
Expand All @@ -2178,8 +2190,14 @@
p.pop('</PropertyGroup>')

for i = 1, #prj.nuget do
local targetsFile = nuGetTargetsFile(prj, prj.nuget[i])
p.x('<Error Condition="!Exists(\'%s\')" Text="$([System.String]::Format(\'$(ErrorText)\', \'%s\'))" />', targetsFile, targetsFile)
local propsFile = nuGetTargetsFile(prj, prj.nuget[i], ".props")
if propsFile then
p.x('<Error Condition="!Exists(\'%s\')" Text="$([System.String]::Format(\'$(ErrorText)\', \'%s\'))" />', propsFile, propsFile)
end
local targetsFile = nuGetTargetsFile(prj, prj.nuget[i], ".targets")
if targetsFile then
p.x('<Error Condition="!Exists(\'%s\')" Text="$([System.String]::Format(\'$(ErrorText)\', \'%s\'))" />', targetsFile, targetsFile)
end
end
p.pop('</Target>')
end
Expand All @@ -2201,6 +2219,7 @@
return {
m.importGroupSettings,
m.importRuleSettings,
m.importNuGetProps,
m.importBuildCustomizationsProps
}
end
Expand Down Expand Up @@ -2230,6 +2249,17 @@
end
end

function m.importNuGetProps(prj)
if not vstudio.nuget2010.supportsPackageReferences(prj) then
for i = 1, #prj.nuget do
local propsFile = nuGetTargetsFile(prj, prj.nuget[i], ".props")
if propsFile then
p.x('<Import Project="%s" Condition="Exists(\'%s\')" />', propsFile, propsFile)
end
end
end
end


function m.importBuildCustomizationsProps(prj)
for i, build in ipairs(prj.buildcustomizations) do
Expand Down

0 comments on commit d150691

Please sign in to comment.