aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src
diff options
context:
space:
mode:
authorBilly O'Neal <bion@microsoft.com>2020-09-11 13:52:18 -0700
committerGitHub <noreply@github.com>2020-09-11 13:52:18 -0700
commit6b97dbfce2159e40d71fb69b1fe05c822c43aa90 (patch)
treebefe679c8d5cb8df85be798dccb2b770cfcda2f9 /toolsrc/src
parent0896bb867b61cc35dd3d1850171b92bd2a32cf67 (diff)
downloadvcpkg-6b97dbfce2159e40d71fb69b1fe05c822c43aa90.tar.gz
vcpkg-6b97dbfce2159e40d71fb69b1fe05c822c43aa90.zip
[vcpkg] Merge the vcpkg metadata uploader into the vcpkg binary (#13421)
Diffstat (limited to 'toolsrc/src')
-rw-r--r--toolsrc/src/vcpkg-test/commands.cpp108
-rw-r--r--toolsrc/src/vcpkg/base/system.process.cpp19
-rw-r--r--toolsrc/src/vcpkg/commands.cpp8
-rw-r--r--toolsrc/src/vcpkg/commands.edit.cpp3
-rw-r--r--toolsrc/src/vcpkg/commands.upload-metrics.cpp29
-rw-r--r--toolsrc/src/vcpkg/metrics.cpp33
-rw-r--r--toolsrc/src/vcpkgmetricsuploader.cpp22
7 files changed, 120 insertions, 102 deletions
diff --git a/toolsrc/src/vcpkg-test/commands.cpp b/toolsrc/src/vcpkg-test/commands.cpp
index 2c3779877..c19fd30ea 100644
--- a/toolsrc/src/vcpkg-test/commands.cpp
+++ b/toolsrc/src/vcpkg-test/commands.cpp
@@ -2,68 +2,80 @@
#include <vcpkg/commands.contact.h>
#include <vcpkg/commands.h>
+#include <vcpkg/commands.upload-metrics.h>
#include <vcpkg/commands.version.h>
+#include <stddef.h>
+
using namespace vcpkg;
-TEST_CASE ("test commands are constructible", "[commands]")
+namespace
{
- Commands::Contact::ContactCommand contact{};
- Commands::Version::VersionCommand version{};
-}
+ template<class CommandListT, size_t ExpectedCount>
+ void check_all_commands(const CommandListT& actual_commands, const char* const (&expected_commands)[ExpectedCount])
+ {
+ CHECK(actual_commands.size() == ExpectedCount); // makes sure this test is updated if we add a command
+ for (const char* expected_command : expected_commands)
+ {
+ CHECK(Commands::find(StringView{expected_command, strlen(expected_command)}, actual_commands) != nullptr);
+ }
+
+ CHECK(Commands::find("x-never-will-exist", actual_commands) == nullptr);
+ }
+} // unnamed namespace
+
+// clang-format tries to wrap the following lists inappropriately
+// clang-format off
TEST_CASE ("get_available_basic_commands works", "[commands]")
{
- auto commands_list = Commands::get_available_basic_commands();
- CHECK(commands_list.size() == 2);
- CHECK(Commands::find("version", commands_list) != nullptr);
- CHECK(Commands::find("contact", commands_list) != nullptr);
- CHECK(Commands::find("aang", commands_list) == nullptr);
+ check_all_commands(Commands::get_available_basic_commands(), {
+ "contact",
+ "version",
+#if VCPKG_ENABLE_X_UPLOAD_METRICS_COMMAND
+ "x-upload-metrics",
+#endif // VCPKG_ENABLE_X_UPLOAD_METRICS_COMMAND
+ });
}
TEST_CASE ("get_available_paths_commands works", "[commands]")
{
- auto commands_list = Commands::get_available_paths_commands();
- CHECK(commands_list.size() == 19);
-
- CHECK(Commands::find("/?", commands_list) != nullptr);
- CHECK(Commands::find("help", commands_list) != nullptr);
- CHECK(Commands::find("search", commands_list) != nullptr);
- CHECK(Commands::find("list", commands_list) != nullptr);
- CHECK(Commands::find("integrate", commands_list) != nullptr);
- CHECK(Commands::find("owns", commands_list) != nullptr);
- CHECK(Commands::find("update", commands_list) != nullptr);
- CHECK(Commands::find("edit", commands_list) != nullptr);
- CHECK(Commands::find("create", commands_list) != nullptr);
- CHECK(Commands::find("cache", commands_list) != nullptr);
- CHECK(Commands::find("portsdiff", commands_list) != nullptr);
- CHECK(Commands::find("autocomplete", commands_list) != nullptr);
- CHECK(Commands::find("hash", commands_list) != nullptr);
- CHECK(Commands::find("fetch", commands_list) != nullptr);
- CHECK(Commands::find("x-ci-clean", commands_list) != nullptr);
- CHECK(Commands::find("x-history", commands_list) != nullptr);
- CHECK(Commands::find("x-package-info", commands_list) != nullptr);
- CHECK(Commands::find("x-vsinstances", commands_list) != nullptr);
- CHECK(Commands::find("x-format-manifest", commands_list) != nullptr);
-
- CHECK(Commands::find("korra", commands_list) == nullptr);
+ check_all_commands(Commands::get_available_paths_commands(), {
+ "/?",
+ "help",
+ "search",
+ "list",
+ "integrate",
+ "owns",
+ "update",
+ "edit",
+ "create",
+ "cache",
+ "portsdiff",
+ "autocomplete",
+ "hash",
+ "fetch",
+ "x-ci-clean",
+ "x-history",
+ "x-package-info",
+ "x-vsinstances",
+ "x-format-manifest",
+ });
}
TEST_CASE ("get_available_commands_type_a works", "[commands]")
{
- auto commands_list = Commands::get_available_triplet_commands();
- CHECK(commands_list.size() == 10);
-
- CHECK(Commands::find("install", commands_list) != nullptr);
- CHECK(Commands::find("x-set-installed", commands_list) != nullptr);
- CHECK(Commands::find("ci", commands_list) != nullptr);
- CHECK(Commands::find("remove", commands_list) != nullptr);
- CHECK(Commands::find("upgrade", commands_list) != nullptr);
- CHECK(Commands::find("build", commands_list) != nullptr);
- CHECK(Commands::find("env", commands_list) != nullptr);
- CHECK(Commands::find("build-external", commands_list) != nullptr);
- CHECK(Commands::find("export", commands_list) != nullptr);
- CHECK(Commands::find("depend-info", commands_list) != nullptr);
-
- CHECK(Commands::find("mai", commands_list) == nullptr);
+ check_all_commands(Commands::get_available_triplet_commands(), {
+ "install",
+ "x-set-installed",
+ "ci",
+ "remove",
+ "upgrade",
+ "build",
+ "env",
+ "build-external",
+ "export",
+ "depend-info",
+ });
}
+// clang-format on
diff --git a/toolsrc/src/vcpkg/base/system.process.cpp b/toolsrc/src/vcpkg/base/system.process.cpp
index c78838caf..a0cdf0365 100644
--- a/toolsrc/src/vcpkg/base/system.process.cpp
+++ b/toolsrc/src/vcpkg/base/system.process.cpp
@@ -462,13 +462,15 @@ namespace vcpkg
return GetLastError();
}
- static ExpectedT<ProcessInfo, unsigned long> windows_create_process(const StringView cmd_line,
- const Environment& env,
- DWORD dwCreationFlags) noexcept
+ static ExpectedT<ProcessInfo, unsigned long> windows_create_windowless_process(const StringView cmd_line,
+ const Environment& env,
+ DWORD dwCreationFlags) noexcept
{
STARTUPINFOW startup_info;
memset(&startup_info, 0, sizeof(STARTUPINFOW));
startup_info.cb = sizeof(STARTUPINFOW);
+ startup_info.dwFlags = STARTF_USESHOWWINDOW;
+ startup_info.wShowWindow = SW_HIDE;
return windows_create_process(cmd_line, env, dwCreationFlags, startup_info);
}
@@ -543,17 +545,18 @@ namespace vcpkg
#endif
#if defined(_WIN32)
- void System::cmd_execute_no_wait(StringView cmd_line)
+ void System::cmd_execute_background(StringView cmd_line)
{
auto timer = Chrono::ElapsedTimer::create_started();
- auto process_info = windows_create_process(cmd_line, {}, DETACHED_PROCESS | CREATE_BREAKAWAY_FROM_JOB);
+ auto process_info = windows_create_windowless_process(
+ cmd_line, {}, CREATE_NEW_CONSOLE | CREATE_NO_WINDOW | CREATE_BREAKAWAY_FROM_JOB);
if (!process_info.get())
{
- Debug::print("cmd_execute_no_wait() failed with error code ", process_info.error(), "\n");
+ Debug::print("cmd_execute_background() failed with error code ", process_info.error(), "\n");
}
- Debug::print("cmd_execute_no_wait() took ", static_cast<int>(timer.microseconds()), " us\n");
+ Debug::print("cmd_execute_background() took ", static_cast<int>(timer.microseconds()), " us\n");
}
Environment System::cmd_execute_modify_env(const ZStringView cmd_line, const Environment& env)
@@ -597,7 +600,7 @@ namespace vcpkg
#if defined(_WIN32)
using vcpkg::g_ctrl_c_state;
g_ctrl_c_state.transition_to_spawn_process();
- auto proc_info = windows_create_process(cmd_line, env, 0);
+ auto proc_info = windows_create_windowless_process(cmd_line, env, 0);
auto long_exit_code = [&]() -> unsigned long {
if (auto p = proc_info.get())
return p->wait();
diff --git a/toolsrc/src/vcpkg/commands.cpp b/toolsrc/src/vcpkg/commands.cpp
index 466cc11c0..d01e1973e 100644
--- a/toolsrc/src/vcpkg/commands.cpp
+++ b/toolsrc/src/vcpkg/commands.cpp
@@ -24,6 +24,7 @@
#include <vcpkg/commands.search.h>
#include <vcpkg/commands.setinstalled.h>
#include <vcpkg/commands.upgrade.h>
+#include <vcpkg/commands.upload-metrics.h>
#include <vcpkg/commands.version.h>
#include <vcpkg/commands.xvsinstances.h>
#include <vcpkg/export.h>
@@ -38,9 +39,16 @@ namespace vcpkg::Commands
{
static const Version::VersionCommand version{};
static const Contact::ContactCommand contact{};
+#if VCPKG_ENABLE_X_UPLOAD_METRICS_COMMAND
+ static const UploadMetrics::UploadMetricsCommand upload_metrics{};
+#endif // VCPKG_ENABLE_X_UPLOAD_METRICS_COMMAND
+
static std::vector<PackageNameAndFunction<const BasicCommand*>> t = {
{"version", &version},
{"contact", &contact},
+#if VCPKG_ENABLE_X_UPLOAD_METRICS_COMMAND
+ {"x-upload-metrics", &upload_metrics},
+#endif // VCPKG_ENABLE_X_UPLOAD_METRICS_COMMAND
};
return t;
}
diff --git a/toolsrc/src/vcpkg/commands.edit.cpp b/toolsrc/src/vcpkg/commands.edit.cpp
index 7ee74eae0..8dbebc4e5 100644
--- a/toolsrc/src/vcpkg/commands.edit.cpp
+++ b/toolsrc/src/vcpkg/commands.edit.cpp
@@ -259,7 +259,8 @@ namespace vcpkg::Commands::Edit
#ifdef _WIN32
if (editor_exe == "Code.exe" || editor_exe == "Code - Insiders.exe")
{
- System::cmd_execute_no_wait(Strings::concat("cmd /c \"", cmd_line, " <NUL\""));
+ // note that we are invoking cmd silently but Code.exe is relaunched from there
+ System::cmd_execute_background(Strings::concat("cmd /c \"", cmd_line, " <NUL\""));
Checks::exit_success(VCPKG_LINE_INFO);
}
#endif
diff --git a/toolsrc/src/vcpkg/commands.upload-metrics.cpp b/toolsrc/src/vcpkg/commands.upload-metrics.cpp
new file mode 100644
index 000000000..6ac7cfcb6
--- /dev/null
+++ b/toolsrc/src/vcpkg/commands.upload-metrics.cpp
@@ -0,0 +1,29 @@
+#include <vcpkg/commands.upload-metrics.h>
+
+#if VCPKG_ENABLE_X_UPLOAD_METRICS_COMMAND
+#include <vcpkg/base/checks.h>
+#include <vcpkg/base/files.h>
+
+#include <vcpkg/metrics.h>
+#include <vcpkg/vcpkgcmdarguments.h>
+
+using namespace vcpkg;
+
+namespace vcpkg::Commands::UploadMetrics
+{
+ const CommandStructure COMMAND_STRUCTURE = {
+ create_example_string("x-upload-metrics metrics.txt"),
+ 1,
+ 1,
+ };
+
+ void UploadMetricsCommand::perform_and_exit(const VcpkgCmdArguments& args, Files::Filesystem& fs) const
+ {
+ (void)args.parse_arguments(COMMAND_STRUCTURE);
+ const auto& payload_path = args.command_arguments[0];
+ auto payload = fs.read_contents(payload_path).value_or_exit(VCPKG_LINE_INFO);
+ Metrics::g_metrics.lock()->upload(payload);
+ Checks::exit_success(VCPKG_LINE_INFO);
+ }
+}
+#endif // VCPKG_ENABLE_X_UPLOAD_METRICS_COMMAND
diff --git a/toolsrc/src/vcpkg/metrics.cpp b/toolsrc/src/vcpkg/metrics.cpp
index 019b3a9c2..241531df3 100644
--- a/toolsrc/src/vcpkg/metrics.cpp
+++ b/toolsrc/src/vcpkg/metrics.cpp
@@ -491,44 +491,31 @@ namespace vcpkg::Metrics
const fs::path temp_folder_path = fs::path(temp_folder) / "vcpkg";
const fs::path temp_folder_path_exe =
- temp_folder_path / Strings::format("vcpkgmetricsuploader-%s.exe", Commands::Version::base_version());
+ temp_folder_path / Strings::format("vcpkg-%s.exe", Commands::Version::base_version());
#endif
-#if defined(_WIN32)
-
- const fs::path exe_path = [&fs]() -> fs::path {
- auto vcpkgdir = System::get_exe_path_of_current_process().parent_path();
- auto path = vcpkgdir / "vcpkgmetricsuploader.exe";
- if (fs.exists(path)) return path;
-
- path = vcpkgdir / "scripts" / "vcpkgmetricsuploader.exe";
- if (fs.exists(path)) return path;
-
- return "";
- }();
-
std::error_code ec;
+#if defined(_WIN32)
fs.create_directories(temp_folder_path, ec);
if (ec) return;
- fs.copy_file(exe_path, temp_folder_path_exe, fs::copy_options::skip_existing, ec);
+ fs.copy_file(
+ System::get_exe_path_of_current_process(), temp_folder_path_exe, fs::copy_options::skip_existing, ec);
if (ec) return;
#else
if (!fs.exists("/tmp")) return;
const fs::path temp_folder_path = "/tmp/vcpkg";
- std::error_code ec;
- fs.create_directory(temp_folder_path, ec);
- // ignore error
- ec.clear();
+ fs.create_directory(temp_folder_path, ignore_errors);
#endif
const fs::path vcpkg_metrics_txt_path = temp_folder_path / ("vcpkg" + generate_random_UUID() + ".txt");
fs.write_contents(vcpkg_metrics_txt_path, payload, ec);
if (ec) return;
#if defined(_WIN32)
- const std::string cmd_line = Strings::format("cmd /c \"start \"vcpkgmetricsuploader.exe\" \"%s\" \"%s\"\"",
- fs::u8string(temp_folder_path_exe),
- fs::u8string(vcpkg_metrics_txt_path));
- System::cmd_execute_no_wait(cmd_line);
+ System::CmdLineBuilder builder;
+ builder.path_arg(temp_folder_path_exe);
+ builder.string_arg("x-upload-metrics");
+ builder.path_arg(vcpkg_metrics_txt_path);
+ System::cmd_execute_background(builder.extract());
#else
auto escaped_path = Strings::escape_string(fs::u8string(vcpkg_metrics_txt_path), '\'', '\\');
const std::string cmd_line = Strings::format(
diff --git a/toolsrc/src/vcpkgmetricsuploader.cpp b/toolsrc/src/vcpkgmetricsuploader.cpp
deleted file mode 100644
index 800ffb5d2..000000000
--- a/toolsrc/src/vcpkgmetricsuploader.cpp
+++ /dev/null
@@ -1,22 +0,0 @@
-#include <vcpkg/base/system_headers.h>
-
-#include <vcpkg/base/checks.h>
-#include <vcpkg/base/files.h>
-
-#include <vcpkg/metrics.h>
-
-#include <shellapi.h>
-
-using namespace vcpkg;
-
-int WINAPI WinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR, _In_ int)
-{
- int argCount;
- LPWSTR* szArgList = CommandLineToArgvW(GetCommandLineW(), &argCount);
-
- Checks::check_exit(VCPKG_LINE_INFO, argCount == 2, "Requires exactly one argument, the path to the payload file");
- auto v = Files::get_real_filesystem().read_contents(szArgList[1]).value_or_exit(VCPKG_LINE_INFO);
- Metrics::g_metrics.lock()->upload(v);
- LocalFree(szArgList);
- return 0;
-}