feat(install): generate NuGet packages
This commit is contained in:
1 parent
97709d37a5
commit
b7c224cf67
7 files changed
+172
-5
No files matched your search
@@ -635,6 +635,131 @@ write_dmg_archive(const std::filesystem::path &output, const std::filesystem::pa
|
||||
return {};
|
||||
}
|
||||
|
||||
// ── NuGet package (.nupkg) ─────────────────────────────────────────────────────────────────
|
||||
// A .nupkg is an OPC ZIP (a nuspec manifest plus content parts). Native assets follow the
|
||||
// `runtimes/<rid>/native` convention; public headers go to `build/native/include`.
|
||||
std::string nuget_rid(std::string_view os, std::string_view architecture) {
|
||||
const std::string arch = architecture == "aarch64" || architecture == "arm64" ? "arm64"
|
||||
: architecture == "x86" || architecture == "i686" ? "x86"
|
||||
: "x64";
|
||||
const auto lower_os = Void::Str::to_lower_ascii(os);
|
||||
if (lower_os == "windows" || lower_os == "win32") {
|
||||
return "win-" + arch;
|
||||
}
|
||||
if (lower_os == "darwin" || lower_os == "macos") {
|
||||
return "osx-" + arch;
|
||||
}
|
||||
return "linux-" + arch;
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
write_nuget_package(const std::filesystem::path &output, const InstallOptions &options,
|
||||
const Graph::BuildGraph &graph,
|
||||
const std::vector<Package::ArchiveEntrySpec> &entries) {
|
||||
const std::string id = options.package_name;
|
||||
const std::string version =
|
||||
options.package_version.empty() ? std::string{"1.0.0"} : options.package_version;
|
||||
const std::string rid = nuget_rid(graph.profile.os, graph.profile.architecture);
|
||||
const std::string authors =
|
||||
options.maintainer.empty() ? std::string{"Void"} : options.maintainer;
|
||||
const std::string description = options.package_description.empty()
|
||||
? options.package_name
|
||||
: options.package_description;
|
||||
|
||||
// Map the installed tree onto the NuGet layout.
|
||||
std::vector<std::pair<std::string, std::filesystem::path>> mapped;
|
||||
std::set<std::string> extensions;
|
||||
for (const auto &entry : entries) {
|
||||
const auto rel = entry.archive_path;
|
||||
std::string dest;
|
||||
if (rel.starts_with("include/")) {
|
||||
dest = "build/native/include/" + rel.substr(std::string{"include/"}.size());
|
||||
} else {
|
||||
dest = "runtimes/" + rid + "/native/" + rel;
|
||||
}
|
||||
mapped.emplace_back(dest, entry.source_file);
|
||||
const auto dot = dest.rfind('.');
|
||||
if (dot != std::string::npos && dot > dest.rfind('/')) {
|
||||
extensions.insert(dest.substr(dot + 1));
|
||||
}
|
||||
}
|
||||
|
||||
const std::string nuspec =
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
|
||||
"<package xmlns=\"http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd\">\n"
|
||||
" <metadata>\n"
|
||||
" <id>" +
|
||||
wix_xml_escape(id) + "</id>\n"
|
||||
" <version>" + wix_xml_escape(version) + "</version>\n"
|
||||
" <authors>" + wix_xml_escape(authors) + "</authors>\n"
|
||||
" <description>" + wix_xml_escape(description) + "</description>\n"
|
||||
" <tags>native;c++;vbs</tags>\n"
|
||||
" </metadata>\n"
|
||||
"</package>\n";
|
||||
|
||||
std::string content_types =
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
|
||||
"<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">\n"
|
||||
" <Default Extension=\"rels\" "
|
||||
"ContentType=\"application/vnd.openxmlformats-package.relationships+xml\" />\n"
|
||||
" <Default Extension=\"nuspec\" ContentType=\"application/octet\" />\n";
|
||||
for (const auto &ext : extensions) {
|
||||
content_types += " <Default Extension=\"" + wix_xml_escape(ext) +
|
||||
"\" ContentType=\"application/octet\" />\n";
|
||||
}
|
||||
content_types += "</Types>\n";
|
||||
|
||||
const std::string rels =
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
|
||||
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n"
|
||||
" <Relationship Type=\"http://schemas.microsoft.com/packaging/2010/07/manifest\" "
|
||||
"Target=\"/" +
|
||||
wix_xml_escape(id) +
|
||||
".nuspec\" Id=\"Re0\" />\n"
|
||||
"</Relationships>\n";
|
||||
|
||||
const auto staging = output.parent_path() / (output.filename().string() + ".stage");
|
||||
std::error_code ec;
|
||||
std::filesystem::remove_all(staging, ec);
|
||||
std::filesystem::create_directories(staging, ec);
|
||||
if (ec) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::map_errno_to_system_code(ec.value()),
|
||||
std::format("cannot create NuGet staging directory '{}'", staging.string()),
|
||||
ec.value())};
|
||||
}
|
||||
auto cleanup = Void::Scope::on_exit([&] {
|
||||
std::error_code cleanup_ec;
|
||||
std::filesystem::remove_all(staging, cleanup_ec);
|
||||
});
|
||||
|
||||
std::vector<Package::ArchiveEntrySpec> zip_entries;
|
||||
const auto stage_file = [&](std::string_view name,
|
||||
std::string_view content) -> std::optional<std::filesystem::path> {
|
||||
const auto path = staging / std::filesystem::path{name};
|
||||
std::error_code file_ec;
|
||||
std::filesystem::create_directories(path.parent_path(), file_ec);
|
||||
if (auto written = Platform::atomic_write_file(path, content, true); !written) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return path;
|
||||
};
|
||||
const auto nuspec_path = stage_file(id + ".nuspec", nuspec);
|
||||
const auto types_path = stage_file("[Content_Types].xml", content_types);
|
||||
const auto rels_path = stage_file("_rels/.rels", rels);
|
||||
if (!nuspec_path || !types_path || !rels_path) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error, "cannot stage NuGet metadata")};
|
||||
}
|
||||
zip_entries.push_back({"[Content_Types].xml", *types_path, 0644u});
|
||||
zip_entries.push_back({"_rels/.rels", *rels_path, 0644u});
|
||||
zip_entries.push_back({id + ".nuspec", *nuspec_path, 0644u});
|
||||
for (const auto &[name, source] : mapped) {
|
||||
zip_entries.push_back({name, source, 0644u});
|
||||
}
|
||||
return Package::create_stored_zip(output, zip_entries);
|
||||
}
|
||||
|
||||
// Debian package: ar container with debian-binary, control.tar and data.tar (uncompressed).
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
write_deb_package(const std::filesystem::path &output, const InstallOptions &options,
|
||||
@@ -1090,6 +1215,12 @@ install_project(const Graph::ProjectDescription &description, const Graph::Build
|
||||
if (auto packed = write_dmg_archive(*options.archive_path, payload); !packed) {
|
||||
return std::unexpected{packed.error()};
|
||||
}
|
||||
} else if (options.archive_format == "nuget") {
|
||||
if (auto packed =
|
||||
write_nuget_package(*options.archive_path, options, graph, archive_entries);
|
||||
!packed) {
|
||||
return std::unexpected{packed.error()};
|
||||
}
|
||||
} else {
|
||||
const bool archive_as_script = options.archive_format == "sh";
|
||||
const auto archive_target = archive_as_script
|
||||
|
||||
Reference in new issue
Block a user