aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorras0219 <533828+ras0219@users.noreply.github.com>2020-08-31 22:37:10 -0700
committerGitHub <noreply@github.com>2020-08-31 22:37:10 -0700
commita0536798eeb0786a4a27078c1b797ab7b6c41dd1 (patch)
treeaf7c5e848fd7dc37199e5a287033431f76931f5c
parent46e25a10d7295d3bc107d6f51ce0e2851877f393 (diff)
downloadvcpkg-a0536798eeb0786a4a27078c1b797ab7b6c41dd1.tar.gz
vcpkg-a0536798eeb0786a4a27078c1b797ab7b6c41dd1.zip
[vcpkg] Implement 'repository' tag for NuGet binary caching (#13228)
This tag is required to correctly interface with GitHub Packages, because GHP shares a single remote for the entire organization and uses the repository field to associate a package with a particular repository. This tag will be automatically generated if the default GitHub Actions environment variables are present (GITHUB_XYZ) or if the user defines VCPKG_NUGET_REPOSITORY. Co-authored-by: Robert Schumacher <roschuma@microsoft.com>
-rw-r--r--toolsrc/include/vcpkg/binarycaching.private.h15
-rw-r--r--toolsrc/src/vcpkg-test/binarycaching.cpp82
-rw-r--r--toolsrc/src/vcpkg/binarycaching.cpp39
3 files changed, 126 insertions, 10 deletions
diff --git a/toolsrc/include/vcpkg/binarycaching.private.h b/toolsrc/include/vcpkg/binarycaching.private.h
index 1b5d587de..46d9d6741 100644
--- a/toolsrc/include/vcpkg/binarycaching.private.h
+++ b/toolsrc/include/vcpkg/binarycaching.private.h
@@ -31,9 +31,22 @@ namespace vcpkg
std::string nupkg_filename() const { return Strings::concat(id, '.', version, ".nupkg"); }
};
+ namespace details
+ {
+ struct NuGetRepoInfo
+ {
+ std::string repo;
+ std::string branch;
+ std::string commit;
+ };
+
+ NuGetRepoInfo get_nuget_repo_info_from_env();
+ }
+
std::string generate_nuspec(const VcpkgPaths& paths,
const Dependencies::InstallPlanAction& action,
- const NugetReference& ref);
+ const NugetReference& ref,
+ details::NuGetRepoInfo rinfo = details::get_nuget_repo_info_from_env());
struct XmlSerializer
{
diff --git a/toolsrc/src/vcpkg-test/binarycaching.cpp b/toolsrc/src/vcpkg-test/binarycaching.cpp
index 55532d14d..0b529f959 100644
--- a/toolsrc/src/vcpkg-test/binarycaching.cpp
+++ b/toolsrc/src/vcpkg-test/binarycaching.cpp
@@ -15,6 +15,18 @@
using namespace vcpkg;
+#define REQUIRE_EQUAL_TEXT(lhs, rhs) \
+ { \
+ auto lhs_lines = Strings::split((lhs), '\n'); \
+ auto rhs_lines = Strings::split((rhs), '\n'); \
+ for (size_t i = 0; i < lhs_lines.size() && i < rhs_lines.size(); ++i) \
+ { \
+ INFO("on line: " << i); \
+ REQUIRE(lhs_lines[i] == rhs_lines[i]); \
+ } \
+ REQUIRE(lhs_lines.size() == rhs_lines.size()); \
+ }
+
TEST_CASE ("reformat_version semver-ish", "[reformat_version]")
{
REQUIRE(reformat_version("0.0.0", "abitag") == "0.0.0-abitag");
@@ -81,13 +93,14 @@ Build-Depends: bzip
REQUIRE(ref.nupkg_filename() == "zlib2_x64-windows.1.5.0-packageabi.nupkg");
- auto nuspec = generate_nuspec(paths, ipa, ref);
+ {
+ auto nuspec = generate_nuspec(paths, ipa, ref, {});
#ifdef _WIN32
#define PKGPATH "C:\\zlib2_x64-windows\\**"
#else
#define PKGPATH "/zlib2_x64-windows/**"
#endif
- std::string expected = R"(<package>
+ std::string expected = R"(<package>
<metadata>
<id>zlib2_x64-windows</id>
<version>1.5.0-packageabi</version>
@@ -106,14 +119,67 @@ Dependencies:
<files><file src=")" PKGPATH R"(" target=""/></files>
</package>
)";
- auto expected_lines = Strings::split(expected, '\n');
- auto nuspec_lines = Strings::split(nuspec, '\n');
- for (size_t i = 0; i < expected_lines.size() && i < nuspec_lines.size(); ++i)
+ REQUIRE_EQUAL_TEXT(nuspec, expected);
+ }
+
{
- INFO("on line: " << i);
- REQUIRE(nuspec_lines[i] == expected_lines[i]);
+ auto nuspec = generate_nuspec(paths, ipa, ref, {"urlvalue"});
+#ifdef _WIN32
+#define PKGPATH "C:\\zlib2_x64-windows\\**"
+#else
+#define PKGPATH "/zlib2_x64-windows/**"
+#endif
+ std::string expected = R"(<package>
+ <metadata>
+ <id>zlib2_x64-windows</id>
+ <version>1.5.0-packageabi</version>
+ <authors>vcpkg</authors>
+ <description>NOT FOR DIRECT USE. Automatically generated cache package.
+
+a spiffy compression library wrapper
+
+Version: 1.5
+Triplet/Compiler hash: tripletabi
+Features: a, b
+Dependencies:
+</description>
+ <packageTypes><packageType name="vcpkg"/></packageTypes>
+ <repository type="git" url="urlvalue"/>
+ </metadata>
+ <files><file src=")" PKGPATH R"(" target=""/></files>
+</package>
+)";
+ REQUIRE_EQUAL_TEXT(nuspec, expected);
+ }
+ {
+ auto nuspec = generate_nuspec(paths, ipa, ref, {"urlvalue", "branchvalue", "commitvalue"});
+#ifdef _WIN32
+#define PKGPATH "C:\\zlib2_x64-windows\\**"
+#else
+#define PKGPATH "/zlib2_x64-windows/**"
+#endif
+ std::string expected = R"(<package>
+ <metadata>
+ <id>zlib2_x64-windows</id>
+ <version>1.5.0-packageabi</version>
+ <authors>vcpkg</authors>
+ <description>NOT FOR DIRECT USE. Automatically generated cache package.
+
+a spiffy compression library wrapper
+
+Version: 1.5
+Triplet/Compiler hash: tripletabi
+Features: a, b
+Dependencies:
+</description>
+ <packageTypes><packageType name="vcpkg"/></packageTypes>
+ <repository type="git" url="urlvalue" branch="branchvalue" commit="commitvalue"/>
+ </metadata>
+ <files><file src=")" PKGPATH R"(" target=""/></files>
+</package>
+)";
+ REQUIRE_EQUAL_TEXT(nuspec, expected);
}
- REQUIRE(nuspec_lines.size() == expected_lines.size());
}
TEST_CASE ("XmlSerializer", "[XmlSerializer]")
diff --git a/toolsrc/src/vcpkg/binarycaching.cpp b/toolsrc/src/vcpkg/binarycaching.cpp
index 6b17461cc..7bccb0a6c 100644
--- a/toolsrc/src/vcpkg/binarycaching.cpp
+++ b/toolsrc/src/vcpkg/binarycaching.cpp
@@ -973,9 +973,27 @@ std::string vcpkg::reformat_version(const std::string& version, const std::strin
return Strings::concat("0.0.0-", abi_tag);
}
+details::NuGetRepoInfo details::get_nuget_repo_info_from_env()
+{
+ auto vcpkg_nuget_repository = System::get_environment_variable("VCPKG_NUGET_REPOSITORY");
+ if (auto p = vcpkg_nuget_repository.get())
+ {
+ return {std::move(*p)};
+ }
+ auto gh_repo = System::get_environment_variable("GITHUB_REPOSITORY").value_or("");
+ if (gh_repo.empty()) return {};
+ auto gh_server = System::get_environment_variable("GITHUB_SERVER_URL").value_or("");
+ if (gh_server.empty()) return {};
+
+ return {Strings::concat(gh_server, '/', gh_repo, ".git"),
+ System::get_environment_variable("GITHUB_REF").value_or(""),
+ System::get_environment_variable("GITHUB_SHA").value_or("")};
+}
+
std::string vcpkg::generate_nuspec(const VcpkgPaths& paths,
const Dependencies::InstallPlanAction& action,
- const vcpkg::NugetReference& ref)
+ const vcpkg::NugetReference& ref,
+ details::NuGetRepoInfo rinfo)
{
auto& spec = action.spec;
auto& scf = *action.source_control_file_location.value_or_exit(VCPKG_LINE_INFO).source_control_file;
@@ -1006,6 +1024,13 @@ std::string vcpkg::generate_nuspec(const VcpkgPaths& paths,
xml.open_tag("packageTypes");
xml.start_complex_open_tag("packageType").text_attr("name", "vcpkg").finish_self_closing_complex_tag();
xml.close_tag("packageTypes").line_break();
+ if (!rinfo.repo.empty())
+ {
+ xml.start_complex_open_tag("repository").text_attr("type", "git").text_attr("url", rinfo.repo);
+ if (!rinfo.branch.empty()) xml.text_attr("branch", rinfo.branch);
+ if (!rinfo.commit.empty()) xml.text_attr("commit", rinfo.commit);
+ xml.finish_self_closing_complex_tag().line_break();
+ }
xml.close_tag("metadata").line_break();
xml.open_tag("files");
xml.start_complex_open_tag("file")
@@ -1047,6 +1072,18 @@ void vcpkg::help_topic_binary_caching(const VcpkgPaths&)
"downloading binaries and whether on-demand builds will be uploaded to that remote. It can be specified "
"as 'read', 'write', or 'readwrite'.");
tbl.blank();
+ tbl.text("The `nuget` and `nugetconfig` source providers additionally respect certain environment variables while "
+ "generating nuget packages. The `metadata.repository` field will be optionally generated like:\n"
+ "\n"
+ " <repository type=\"git\" url=\"$VCPKG_NUGET_REPOSITORY\"/>\n"
+ "or\n"
+ " <repository type=\"git\"\n"
+ " url=\"${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git\"\n"
+ " branch=\"${GITHUB_REF}\"\n"
+ " commit=\"${GITHUB_SHA}\"/>\n"
+ "\n"
+ "if the appropriate environment variables are defined and non-empty.\n");
+ tbl.blank();
System::print2(tbl.m_str);
const auto& maybe_cachepath = default_cache_path();
if (auto p = maybe_cachepath.get())