Skip to content

Commit

Permalink
Command line parser prerequisites (#853)
Browse files Browse the repository at this point in the history
* Expected-ize read_lines.

* Introduce IReadLines as a smaller interface for things that just want to read lines.

* Extract HelpTableFormatter and consistently use -s in source file names.

* Rename IReadLines to ILineReader, as requested by Victor.

* Name the numbers in HelpTableFormatter::format as requested by Victor.

* Fix NPM authenticate.
  • Loading branch information
BillyONeal authored Jan 13, 2023
1 parent 36ed4a8 commit 17bb1b8
Show file tree
Hide file tree
Showing 56 changed files with 264 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ BraceWrapping:

IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^(<vcpkg/base/system_headers\.h>|"pch\.h")$'
- Regex: '^(<vcpkg/base/system-headers\.h>|"pch\.h")$'
Priority: -1
- Regex: '^<catch2/catch\.hpp>$'
Priority: 1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ node_modules/
/ce/test/**/*.map
/ce/test/**/*.js
/ce/ce/vcpkg-ce.build.log
/ce/common/config/rush/.npmrc
/ce/common/config/rush/pnpm-lock.yaml
/ce/test/vcpkg-ce.test.build.log
/ce/common/temp
Expand Down
38 changes: 21 additions & 17 deletions azure-pipelines/pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,31 @@ jobs:
git -C "$env:VCPKG_ROOT" checkout $sha
- task: CodeQL3000Init@0
displayName: 'CodeQL Initialize'
- task: Powershell@2
displayName: 'Format C++'
inputs:
filePath: 'azure-pipelines/Format-CxxCode.ps1'
pwsh: true
- task: Powershell@2
displayName: 'Create Diff'
inputs:
filePath: azure-pipelines/Create-PRDiff.ps1
arguments: '-DiffFile $(DiffFile)'
pwsh: true
- task: PublishBuildArtifacts@1
condition: failed()
displayName: 'Publish Format and Messages File Diff'
inputs:
PathtoPublish: '$(DiffFile)'
ArtifactName: 'format.diff'
- task: UseNode@1
displayName: Use Node 16 or later
inputs:
version: "16.x"
- task: npmAuthenticate@0
inputs:
workingFile: 'ce\common\config\rush\.npmrc'
# The working directory change is to get the above .npmrc used when installing rush
- script: npm install -g @microsoft/rush
displayName: Install Rush
workingDirectory: ce\common\config\rush
Expand All @@ -153,22 +174,5 @@ jobs:
filePath: 'azure-pipelines/end-to-end-tests.ps1'
workingDirectory: '$(Build.SourcesDirectory)/build.x86.debug'
pwsh: true
- task: Powershell@2
displayName: 'Format C++'
inputs:
filePath: 'azure-pipelines/Format-CxxCode.ps1'
pwsh: true
- task: Powershell@2
displayName: 'Create Diff'
inputs:
filePath: azure-pipelines/Create-PRDiff.ps1
arguments: '-DiffFile $(DiffFile)'
pwsh: true
- task: PublishBuildArtifacts@1
condition: failed()
displayName: 'Publish Format and Messages File Diff'
inputs:
PathtoPublish: '$(DiffFile)'
ArtifactName: 'format.diff'
- task: CodeQL3000Finalize@0
displayName: 'CodeQL Finalize'
2 changes: 1 addition & 1 deletion include/pch.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <vcpkg/base/system_headers.h>
#include <vcpkg/base/system-headers.h>

#if defined(_MSC_VER)
// pch.h only is used for performance with MSVC
Expand Down
2 changes: 1 addition & 1 deletion include/vcpkg-test/util.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <vcpkg/base/system_headers.h>
#include <vcpkg/base/system-headers.h>

#include <catch2/catch.hpp>

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion include/vcpkg/base/checks.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <vcpkg/base/basic_checks.h>
#include <vcpkg/base/basic-checks.h>
#include <vcpkg/base/messages.h>
#include <vcpkg/base/strings.h>

Expand Down
26 changes: 26 additions & 0 deletions include/vcpkg/base/cmd-parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <vcpkg/base/fwd/cmd-parser.h>

#include <vcpkg/base/stringview.h>

#include <string>

namespace vcpkg
{
struct HelpTableFormatter
{
// Adds a table entry with a key `col1` and value `col2`
void format(StringView col1, StringView col2);
// Adds an example block; typically just the text with no indenting
void example(StringView example_text);
// Adds a header typically placed at the top of several table entries
void header(StringView name);
// Adds a blank line
void blank();
// Adds a line of text
void text(StringView text, int indent = 0);

std::string m_str;
};
}
File renamed without changes.
2 changes: 1 addition & 1 deletion include/vcpkg/base/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <vcpkg/base/checks.h>
#include <vcpkg/base/lineinfo.h>
#include <vcpkg/base/stringview.h>
#include <vcpkg/base/to_string.h>
#include <vcpkg/base/to-string.h>

#include <functional>
#include <system_error>
Expand Down
13 changes: 9 additions & 4 deletions include/vcpkg/base/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,19 @@ namespace vcpkg

uint64_t get_filesystem_stats();

struct Filesystem
struct ILineReader
{
virtual ExpectedL<std::vector<std::string>> read_lines(const Path& file_path) const = 0;

protected:
~ILineReader();
};

struct Filesystem : ILineReader
{
virtual std::string read_contents(const Path& file_path, std::error_code& ec) const = 0;
std::string read_contents(const Path& file_path, LineInfo li) const;

virtual std::vector<std::string> read_lines(const Path& file_path, std::error_code& ec) const = 0;
std::vector<std::string> read_lines(const Path& file_path, LineInfo li) const;

virtual Path find_file_recursively_up(const Path& starting_dir,
const Path& filename,
std::error_code& ec) const = 0;
Expand Down
5 changes: 5 additions & 0 deletions include/vcpkg/base/fwd/cmd-parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
namespace vcpkg
{
struct HelpTableFormatter;
}
1 change: 1 addition & 0 deletions include/vcpkg/base/fwd/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace vcpkg
struct ReadFilePointer;
struct WriteFilePointer;
struct IExclusiveFileLock;
struct ILineReader;
struct Filesystem;
struct NotExtensionCaseSensitive;
struct NotExtensionCaseInsensitive;
Expand Down
5 changes: 4 additions & 1 deletion include/vcpkg/base/lineinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ namespace vcpkg
}

#define VCPKG_LINE_INFO \
vcpkg::LineInfo { __LINE__, __FILE__ }
vcpkg::LineInfo \
{ \
__LINE__, __FILE__ \
}
2 changes: 1 addition & 1 deletion include/vcpkg/base/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <vcpkg/base/fwd/optional.h>

#include <vcpkg/base/basic_checks.h>
#include <vcpkg/base/basic-checks.h>
#include <vcpkg/base/lineinfo.h>
#include <vcpkg/base/pragmas.h>

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion include/vcpkg/base/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vcpkg/base/lineinfo.h>
#include <vcpkg/base/optional.h>
#include <vcpkg/base/stringview.h>
#include <vcpkg/base/to_string.h>
#include <vcpkg/base/to-string.h>

#include <errno.h>
#include <inttypes.h>
Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 2 additions & 12 deletions include/vcpkg/vcpkgcmdarguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vcpkg/fwd/vcpkgcmdarguments.h>
#include <vcpkg/fwd/vcpkgpaths.h>

#include <vcpkg/base/cmd-parser.h>
#include <vcpkg/base/files.h>
#include <vcpkg/base/optional.h>
#include <vcpkg/base/span.h>
Expand Down Expand Up @@ -74,17 +75,6 @@ namespace vcpkg

std::string create_example_string(const std::string& command_and_arguments);

struct HelpTableFormatter
{
void format(StringView col1, StringView col2);
void example(StringView example_text);
void header(StringView name);
void blank();
void text(StringView text, int indent = 0);

std::string m_str;
};

struct FeatureFlagSettings
{
bool registries;
Expand All @@ -95,7 +85,7 @@ namespace vcpkg

struct VcpkgCmdArguments
{
static VcpkgCmdArguments create_from_command_line(const Filesystem& fs,
static VcpkgCmdArguments create_from_command_line(const ILineReader& fs,
const int argc,
const CommandLineCharType* const* const argv);
static VcpkgCmdArguments create_from_arg_sequence(const std::string* arg_begin, const std::string* arg_end);
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg-test/catch.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define CATCH_CONFIG_RUNNER
#include <catch2/catch.hpp>

#include <vcpkg/base/basic_checks.h>
#include <vcpkg/base/basic-checks.h>
#include <vcpkg/base/messages.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/base/system.h>
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg-test/ci-baseline.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <catch2/catch.hpp>

#include <vcpkg/base/basic_checks.h>
#include <vcpkg/base/basic-checks.h>
#include <vcpkg/base/parse.h>

#include <vcpkg/build.h>
Expand Down
40 changes: 40 additions & 0 deletions src/vcpkg-test/cmd-parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <catch2/catch.hpp>

#include <vcpkg/base/cmd-parser.h>

using namespace vcpkg;

TEST_CASE ("Smoke test help table formatter", "[cmd_parser]")
{
HelpTableFormatter uut;

uut.header("This is a header");
uut.format("short-arg", "short help text");
uut.format("a-really-long-arg-that-does-not-fit-in-the-first-column-and-keeps-going", "shorty");
uut.format("short-arg",
"some really long help text that does not fit on the same line because we have a 100 character line "
"limit and oh god it keeps going and going");
uut.format("a-really-long-arg-combined-with-some-really-long-help-text",
"another instance of that really long help text goes here to demonstrate that the worst case combo can "
"be accommodated");

uut.blank();
uut.example("some example command");
uut.text("this is some text");

const char* const expected = R"(This is a header:
short-arg short help text
a-really-long-arg-that-does-not-fit-in-the-first-column-and-keeps-going
shorty
short-arg some really long help text that does not fit on the same line
because we have a 100 character line limit and oh god it keeps
going and going
a-really-long-arg-combined-with-some-really-long-help-text
another instance of that really long help text goes here to
demonstrate that the worst case combo can be accommodated
some example command
this is some text)";

CHECK(uut.m_str == expected);
}
2 changes: 1 addition & 1 deletion src/vcpkg-test/files.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <vcpkg/base/system_headers.h>
#include <vcpkg/base/system-headers.h>

#include <catch2/catch.hpp>

Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg-test/messages.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <catch2/catch.hpp>

#include <vcpkg/base/json.h>
#include <vcpkg/base/setup_messages.h>
#include <vcpkg/base/setup-messages.h>

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

Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg-test/strings.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <catch2/catch.hpp>

#include <vcpkg/base/api_stable_format.h>
#include <vcpkg/base/api-stable-format.h>
#include <vcpkg/base/expected.h>
#include <vcpkg/base/strings.h>

Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg-test/system.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <vcpkg/base/system_headers.h>
#include <vcpkg/base/system-headers.h>

#include <catch2/catch.hpp>

Expand Down
4 changes: 2 additions & 2 deletions src/vcpkg.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <vcpkg/base/system_headers.h>
#include <vcpkg/base/system-headers.h>

#include <vcpkg/base/chrono.h>
#include <vcpkg/base/files.h>
#include <vcpkg/base/json.h>
#include <vcpkg/base/pragmas.h>
#include <vcpkg/base/setup_messages.h>
#include <vcpkg/base/setup-messages.h>
#include <vcpkg/base/strings.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/base/system.process.h>
Expand Down
Loading

0 comments on commit 17bb1b8

Please sign in to comment.