Skip to content

Commit

Permalink
Use NuGet.config
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-el-sayed committed Sep 4, 2024
1 parent a0b997c commit b40f536
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 12 deletions.
File renamed without changes.
13 changes: 13 additions & 0 deletions libs/UGridNET/config/NuGet.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="packages" />
</config>
<packageRestore>
<add key="enabled" value="True" />
<add key="automatic" value="True" />
</packageRestore>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
12 changes: 10 additions & 2 deletions libs/UGridNET/dll/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ set(TARGET_NAME ${PROJECT_NAME})

set(APP_CONFIG ${CMAKE_CURRENT_BINARY_DIR}/App.config)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/../cs_proj_templates/App.config.in
${CMAKE_CURRENT_SOURCE_DIR}/../config/App.config.in
${APP_CONFIG}
@ONLY
)

set(NUGET_CONFIG ${CMAKE_CURRENT_BINARY_DIR}/NuGet.config)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/../config/NuGet.config
${NUGET_CONFIG}
COPYONLY
)

set(DIRECTORY_BUILD_PROPS ${CMAKE_CURRENT_BINARY_DIR}/Directory.Build.props)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/../cs_proj_templates/Directory.Build.props.in
${CMAKE_CURRENT_SOURCE_DIR}/../config/Directory.Build.props.in
${DIRECTORY_BUILD_PROPS}
@ONLY
)
Expand Down Expand Up @@ -46,6 +53,7 @@ add_library(
${TARGET_NAME}
SHARED
${APP_CONFIG}
${NUGET_CONFIG}
${DIRECTORY_PACKAGES_PROPS}
${DIRECTORY_BUILD_PROPS}
${SWIG_GENERATED_CSHARP_SRCS}
Expand Down
4 changes: 2 additions & 2 deletions libs/UGridNET/pre_build/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ add_custom_target(
-Force
-NoHttpCache
-Verbosity detailed
-Source https://api.nuget.org/v3/index.json
-ConfigFile ${CMAKE_CURRENT_BINARY_DIR}/../test/NuGet.config
COMMAND ${NUGET_EXECUTABLE} restore ${CMAKE_CURRENT_BINARY_DIR}/../test/UGridNET.Tests.sln
-Force
-NoHttpCache
-Verbosity detailed
-Source https://api.nuget.org/v3/index.json
-ConfigFile ${CMAKE_CURRENT_BINARY_DIR}/../test/NuGet.config
COMMENT "Restoring NuGet packages"
)

Expand Down
12 changes: 10 additions & 2 deletions libs/UGridNET/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ set(TARGET_NAME ${PROJECT_NAME})

set(APP_CONFIG ${CMAKE_CURRENT_BINARY_DIR}/App.config)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/../cs_proj_templates/App.config.in
${CMAKE_CURRENT_SOURCE_DIR}/../config/App.config.in
${APP_CONFIG}
@ONLY
)

set(NUGET_CONFIG ${CMAKE_CURRENT_BINARY_DIR}/NuGet.config)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/../config/NuGet.config
${NUGET_CONFIG}
COPYONLY
)

set(DIRECTORY_BUILD_PROPS ${CMAKE_CURRENT_BINARY_DIR}/Directory.Build.props)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/../cs_proj_templates/Directory.Build.props.in
${CMAKE_CURRENT_SOURCE_DIR}/../config/Directory.Build.props.in
${DIRECTORY_BUILD_PROPS}
@ONLY
)
Expand Down Expand Up @@ -45,6 +52,7 @@ execute_process(
add_library(${TARGET_NAME}
SHARED
${APP_CONFIG}
${NUGET_CONFIG}
${DIRECTORY_PACKAGES_PROPS}
${DIRECTORY_BUILD_PROPS}
src/UGridNETTests.cs
Expand Down
61 changes: 55 additions & 6 deletions scripts/get_package_ids_from_directory_packages_props.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,81 @@
import sys
import xml.etree.ElementTree as ET
from pathlib import Path


def parse_xml(file_path):
def parse_xml(file_path: Path) -> str:
"""
Finds all the PackageVersion elements and joins the underscore-suffixed Include values into a comma-separated string
reason behind underscore is related to how set_target_properties sets the property VS_PACKAGE_REFERENCES
see https://cmake.org/cmake/help/latest/prop_tgt/VS_PACKAGE_REFERENCES.html
set_target_properties( <some_csharp_target> PROPERTIES VS_PACKAGE_REFERENCES "package1_x.y.z;package2_x.y.z;...")
sets the following in a csproj
#
<ItemGroup>
<PackageReference Include="package1" Version="x.y.z" />
<PackageReference Include="package2" Version="x.y.z" />
<PackageReference Include="NUnit3TestAdapter" Version="" />
</ItemGroup>
#
the underscore in between separates the package's name from its version.
#
The packages are defined in Directory.Packages.props as follows:
#
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="package1" Version="x.y.z" />
<PackageVersion Include="package2" Version="x.y.z" />
.
.
.
</ItemGroup>
</Project>
#
but when central package management is used (ManagePackageVersionsCentrally set to true).
it is not allowed to set the version in the csproj PackageReference anymore. So we leave out the version.
This results in
#
<ItemGroup>
<PackageReference Include="package1" Version="" />
<PackageReference Include="package2" Version="" />
<PackageReference Include="NUnit3TestAdapter" Version="" />
</ItemGroup>
#
Visual Studio does not complain about the attribute "Version" having no value.
Args:
- file_path (Path): path to Directory.Packages.props file.
Returns:
- str: Underscore-suffixed semicolon-delimited package references.
"""

# Parse the XML file
tree = ET.parse(file_path)
root = tree.getroot()

# Initialize a list to store Include attribute values
include_values = []

# Find all PackageVersion elements and get their Include attributes
for package in root.findall(".//PackageVersion"):
include_attr = package.get("Include")
if include_attr:
include_values.append(include_attr + "_")

# Join the values into a comma-separated string for easy output
print(";".join(include_values))
return ";".join(include_values)


if __name__ == "__main__":
# The file path is passed as the first command line argument
if len(sys.argv) != 2:
print(
"Usage: get_package_ids_from_directory_packages_prop.py <path_to_xml_file>"
"Usage: python get_package_ids_from_directory_packages_prop.py <path_to_directory_packages_props>"
)
sys.exit(1)

file_path = sys.argv[1]
parse_xml(file_path)
result = parse_xml(file_path)
print(result)

0 comments on commit b40f536

Please sign in to comment.