Skip to content

Commit

Permalink
StringLiteral for command switches and settings (microsoft#1308)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas1664 committed Dec 20, 2023
1 parent f104fe6 commit bfda089
Show file tree
Hide file tree
Showing 20 changed files with 54 additions and 43 deletions.
3 changes: 2 additions & 1 deletion include/vcpkg/commands.build.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include <vcpkg/base/fwd/system.process.h>

#include <vcpkg/fwd/binarycaching.h>
#include <vcpkg/fwd/build.h>
#include <vcpkg/fwd/cmakevars.h>
#include <vcpkg/fwd/dependencies.h>
#include <vcpkg/fwd/portfileprovider.h>
#include <vcpkg/fwd/vcpkgcmdarguments.h>

#include <vcpkg/base/cache.h>
#include <vcpkg/base/files.h>
Expand All @@ -19,7 +21,6 @@
#include <vcpkg/packagespec.h>
#include <vcpkg/statusparagraphs.h>
#include <vcpkg/triplet.h>
#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/vcpkgpaths.h>

#include <array>
Expand Down
2 changes: 1 addition & 1 deletion include/vcpkg/commands.z-extract.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace vcpkg
}
};

ExpectedL<StripSetting> get_strip_setting(std::map<std::string, std::string, std::less<>> settings);
ExpectedL<StripSetting> get_strip_setting(const std::map<StringLiteral, std::string, std::less<>>& settings);

struct ExtractedArchive
{
Expand Down
6 changes: 3 additions & 3 deletions include/vcpkg/commands.z-generate-message-map.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include <vcpkg/base/fwd/files.h>
#include <vcpkg/base/fwd/messages.h>

#include <vcpkg/base/files.h>
#include <vcpkg/base/stringview.h>
#include <vcpkg/fwd/vcpkgcmdarguments.h>

#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/base/stringview.h>

#include <vector>

Expand Down
8 changes: 4 additions & 4 deletions include/vcpkg/vcpkgcmdarguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace vcpkg
{
struct ParsedArguments
{
std::set<std::string, std::less<>> switches;
std::map<std::string, std::string, std::less<>> settings;
std::map<std::string, std::vector<std::string>, std::less<>> multisettings;
std::set<StringLiteral, std::less<>> switches;
std::map<StringLiteral, std::string, std::less<>> settings;
std::map<StringLiteral, std::vector<std::string>, std::less<>> multisettings;

const std::string* read_setting(StringLiteral setting) const noexcept;

Expand Down Expand Up @@ -374,7 +374,7 @@ namespace vcpkg
ParsedArguments parse_arguments(const CommandMetadata& command_metadata) const;

void imbue_from_environment();
void imbue_from_fake_environment(const std::map<std::string, std::string, std::less<>>& env);
void imbue_from_fake_environment(const std::map<StringLiteral, std::string, std::less<>>& env);

// Applies recursive settings from the environment or sets a global environment variable
// to be consumed by subprocesses; may only be called once per process.
Expand Down
4 changes: 2 additions & 2 deletions src/vcpkg-test/arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ TEST_CASE ("Combine asset cache params", "[arguments]")
v = VcpkgCmdArguments::create_from_arg_sequence(t.data(), t.data() + t.size());
REQUIRE(v.asset_sources_template() == "x-azurl,value");

std::map<std::string, std::string, std::less<>> envmap = {
{VcpkgCmdArguments::ASSET_SOURCES_ENV.to_string(), "x-azurl,value1"},
std::map<StringLiteral, std::string, std::less<>> envmap = {
{VcpkgCmdArguments::ASSET_SOURCES_ENV, "x-azurl,value1"},
};
v = VcpkgCmdArguments::create_from_arg_sequence(nullptr, nullptr);
v.imbue_from_fake_environment(envmap);
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg-test/commands.extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ TEST_CASE ("Testing strip auto's get_common_prefix_count", "z-extract")

TEST_CASE ("Testing get_strip_setting", "z-extract")
{
std::map<std::string, std::string, std::less<>> settings;
std::map<StringLiteral, std::string, std::less<>> settings;

SECTION ("Test no strip")
{
Expand Down
17 changes: 9 additions & 8 deletions src/vcpkg-test/dependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <vcpkg/dependencies.h>
#include <vcpkg/portfileprovider.h>
#include <vcpkg/sourceparagraph.h>
#include <vcpkg/vcpkgcmdarguments.h>

#include <memory>
#include <vector>
Expand Down Expand Up @@ -2466,14 +2467,14 @@ TEST_CASE ("dependency graph API snapshot: host and target")
ActionPlan plan;
plan.install_actions.push_back(std::move(install_a));
plan.install_actions.push_back(std::move(install_a_host));
std::map<std::string, std::string, std::less<>> envmap = {
{VcpkgCmdArguments::GITHUB_JOB_ENV.to_string(), "123"},
{VcpkgCmdArguments::GITHUB_RUN_ID_ENV.to_string(), "123"},
{VcpkgCmdArguments::GITHUB_REF_ENV.to_string(), "refs/heads/main"},
{VcpkgCmdArguments::GITHUB_REPOSITORY_ENV.to_string(), "owner/repo"},
{VcpkgCmdArguments::GITHUB_SHA_ENV.to_string(), "abc123"},
{VcpkgCmdArguments::GITHUB_TOKEN_ENV.to_string(), "abc"},
{VcpkgCmdArguments::GITHUB_WORKFLOW_ENV.to_string(), "test"},
std::map<StringLiteral, std::string, std::less<>> envmap = {
{VcpkgCmdArguments::GITHUB_JOB_ENV, "123"},
{VcpkgCmdArguments::GITHUB_RUN_ID_ENV, "123"},
{VcpkgCmdArguments::GITHUB_REF_ENV, "refs/heads/main"},
{VcpkgCmdArguments::GITHUB_REPOSITORY_ENV, "owner/repo"},
{VcpkgCmdArguments::GITHUB_SHA_ENV, "abc123"},
{VcpkgCmdArguments::GITHUB_TOKEN_ENV, "abc"},
{VcpkgCmdArguments::GITHUB_WORKFLOW_ENV, "test"},
};
auto v = VcpkgCmdArguments::create_from_arg_sequence(nullptr, nullptr);
v.imbue_from_fake_environment(envmap);
Expand Down
1 change: 1 addition & 0 deletions src/vcpkg/binarycaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <vcpkg/documentation.h>
#include <vcpkg/metrics.h>
#include <vcpkg/tools.h>
#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/vcpkgpaths.h>

#include <memory>
Expand Down
1 change: 1 addition & 0 deletions src/vcpkg/commands.build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <vcpkg/spdx.h>
#include <vcpkg/statusparagraphs.h>
#include <vcpkg/tools.h>
#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/vcpkglib.h>
#include <vcpkg/vcpkgpaths.h>

Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/commands.ci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ namespace
});
}

void parse_exclusions(const std::map<std::string, std::string, std::less<>>& settings,
void parse_exclusions(const std::map<StringLiteral, std::string, std::less<>>& settings,
StringLiteral opt,
Triplet triplet,
ExclusionsMap& exclusions_map)
Expand Down
1 change: 1 addition & 0 deletions src/vcpkg/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <vcpkg/commands.z-preregister-telemetry.h>
#include <vcpkg/commands.z-print-config.h>
#include <vcpkg/commands.z-upload-metrics.h>
#include <vcpkg/vcpkgcmdarguments.h>

namespace vcpkg
{
Expand Down
1 change: 1 addition & 0 deletions src/vcpkg/commands.export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <vcpkg/portfileprovider.h>
#include <vcpkg/registries.h>
#include <vcpkg/tools.h>
#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/vcpkglib.h>
#include <vcpkg/vcpkgpaths.h>

Expand Down
6 changes: 3 additions & 3 deletions src/vcpkg/commands.help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ namespace vcpkg
void help_topic_valid_triplet(const TripletDatabase& database)
{
std::map<StringView, std::vector<const TripletFile*>> triplets_per_location;
vcpkg::Util::group_by(database.available_triplets,
&triplets_per_location,
[](const TripletFile& triplet_file) -> StringView { return triplet_file.location; });
Util::group_by(database.available_triplets,
&triplets_per_location,
[](const TripletFile& triplet_file) -> StringView { return triplet_file.location; });

LocalizedString result;
result.append(msgBuiltInTriplets).append_raw('\n');
Expand Down
1 change: 1 addition & 0 deletions src/vcpkg/commands.install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <vcpkg/paragraphs.h>
#include <vcpkg/portfileprovider.h>
#include <vcpkg/tools.h>
#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/vcpkglib.h>
#include <vcpkg/vcpkgpaths.h>
#include <vcpkg/xunitwriter.h>
Expand Down
1 change: 1 addition & 0 deletions src/vcpkg/commands.remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vcpkg/installedpaths.h>
#include <vcpkg/portfileprovider.h>
#include <vcpkg/registries.h>
#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/vcpkglib.h>
#include <vcpkg/vcpkgpaths.h>

Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/commands.z-applocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace vcpkg;

namespace
{
WriteFilePointer maybe_create_log(const std::map<std::string, std::string, std::less<>>& settings,
WriteFilePointer maybe_create_log(const std::map<StringLiteral, std::string, std::less<>>& settings,
StringLiteral setting,
const Filesystem& fs)
{
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/commands.z-extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace vcpkg
nullptr,
};

ExpectedL<StripSetting> get_strip_setting(std::map<std::string, std::string, std::less<>> settings)
ExpectedL<StripSetting> get_strip_setting(const std::map<StringLiteral, std::string, std::less<>>& settings)
{
auto iter = settings.find(OPTION_STRIP);
if (iter == settings.end())
Expand Down
2 changes: 2 additions & 0 deletions src/vcpkg/commands.z-generate-message-map.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <vcpkg/base/files.h>
#include <vcpkg/base/json.h>
#include <vcpkg/base/messages.h>
#include <vcpkg/base/setup-messages.h>
#include <vcpkg/base/strings.h>
#include <vcpkg/base/util.h>

#include <vcpkg/commands.z-generate-message-map.h>
#include <vcpkg/vcpkgcmdarguments.h>

using namespace vcpkg;

Expand Down
3 changes: 2 additions & 1 deletion src/vcpkg/configure-environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ namespace
constexpr const StringLiteral* ArtifactTargetPlatformSwitchNamesStorage[] = {
&SWITCH_TARGET_X86, &SWITCH_TARGET_X64, &SWITCH_TARGET_ARM, &SWITCH_TARGET_ARM64};

bool more_than_one_mapped(View<const StringLiteral*> candidates, const std::set<std::string, std::less<>>& switches)
bool more_than_one_mapped(View<const StringLiteral*> candidates,
const std::set<StringLiteral, std::less<>>& switches)
{
bool seen = false;
for (auto&& candidate : candidates)
Expand Down
32 changes: 16 additions & 16 deletions src/vcpkg/vcpkgcmdarguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,31 @@ namespace
for (const auto& switch_ : command_metadata.options.switches)
{
bool parse_result;
auto name = switch_.name.to_string();
ZStringView name = switch_.name;
StabilityTag tag = StabilityTag::Standard;
if (Strings::starts_with(name, "x-"))
{
name.erase(0, 2);
name = name.substr(2);
tag = StabilityTag::Experimental;
}
else if (Strings::starts_with(name, "z-"))
{
name.erase(0, 2);
name = name.substr(2);
tag = StabilityTag::ImplementationDetail;
}

if (switch_.helpmsg)
{
if (cmd_parser.parse_switch(name, tag, parse_result, switch_.helpmsg.to_string()) && parse_result)
{
output.switches.emplace(switch_.name.to_string());
output.switches.emplace(switch_.name);
}
}
else
{
if (cmd_parser.parse_switch(name, tag, parse_result) && parse_result)
{
output.switches.emplace(switch_.name.to_string());
output.switches.emplace(switch_.name);
}
}
}
Expand All @@ -117,48 +117,48 @@ namespace
std::string maybe_parse_result;
for (const auto& option : command_metadata.options.settings)
{
auto name = option.name.to_string();
ZStringView name = option.name;
StabilityTag tag = StabilityTag::Standard;
if (Strings::starts_with(name, "x-"))
{
name.erase(0, 2);
name = name.substr(2);
tag = StabilityTag::Experimental;
}
else if (Strings::starts_with(name, "z-"))
{
name.erase(0, 2);
name = name.substr(2);
tag = StabilityTag::ImplementationDetail;
}

if (option.helpmsg)
{
if (cmd_parser.parse_option(name, tag, maybe_parse_result, option.helpmsg.to_string()))
{
output.settings.emplace(option.name.to_string(), std::move(maybe_parse_result));
output.settings.emplace(option.name, std::move(maybe_parse_result));
}
}
else
{
if (cmd_parser.parse_option(name, tag, maybe_parse_result))
{
output.settings.emplace(option.name.to_string(), std::move(maybe_parse_result));
output.settings.emplace(option.name, std::move(maybe_parse_result));
}
}
}
}

for (const auto& option : command_metadata.options.multisettings)
{
auto name = option.name.to_string();
ZStringView name = option.name;
StabilityTag tag = StabilityTag::Standard;
if (Strings::starts_with(name, "x-"))
{
name.erase(0, 2);
name = name.substr(2);
tag = StabilityTag::Experimental;
}
else if (Strings::starts_with(name, "z-"))
{
name.erase(0, 2);
name = name.substr(2);
tag = StabilityTag::ImplementationDetail;
}

Expand All @@ -167,14 +167,14 @@ namespace
{
if (cmd_parser.parse_multi_option(name, tag, maybe_parse_result, option.helpmsg.to_string()))
{
output.multisettings.emplace(option.name.to_string(), std::move(maybe_parse_result));
output.multisettings.emplace(option.name, std::move(maybe_parse_result));
}
}
else
{
if (cmd_parser.parse_multi_option(name, tag, maybe_parse_result))
{
output.multisettings.emplace(option.name.to_string(), std::move(maybe_parse_result));
output.multisettings.emplace(option.name, std::move(maybe_parse_result));
}
}
}
Expand Down Expand Up @@ -519,7 +519,7 @@ namespace vcpkg
}

void VcpkgCmdArguments::imbue_from_environment() { imbue_from_environment_impl(&vcpkg::get_environment_variable); }
void VcpkgCmdArguments::imbue_from_fake_environment(const std::map<std::string, std::string, std::less<>>& env)
void VcpkgCmdArguments::imbue_from_fake_environment(const std::map<StringLiteral, std::string, std::less<>>& env)
{
imbue_from_environment_impl([&env](ZStringView var) -> Optional<std::string> {
auto it = env.find(var);
Expand Down

0 comments on commit bfda089

Please sign in to comment.