diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx.de> | 2023-05-13 18:55:15 +0200 |
---|---|---|
committer | Jelle Raaijmakers <jelle@gmta.nl> | 2023-05-19 23:31:20 +0200 |
commit | f9a24eb7ebc9e4b560f2fe5b31fd20fa1cd80608 (patch) | |
tree | 449ff50c9b78977327264759606aedc7c2f83943 /Userland | |
parent | 07dd719e3eb77df96ec574dfbe988932a77bd001 (diff) | |
download | serenity-f9a24eb7ebc9e4b560f2fe5b31fd20fa1cd80608.zip |
LibCore: Migrate Command from Deprecated{File,String}
This gives us free error-propagation in Core::command(...) and
HackStudio::ProjectBuilder::for_each_library_dependencies.
The comment about "String will be in the null state" has been misleading
for a long time, so it is removed.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/DevTools/HackStudio/Git/GitRepo.cpp | 4 | ||||
-rw-r--r-- | Userland/DevTools/HackStudio/ProjectBuilder.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Command.cpp | 17 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Command.h | 9 |
4 files changed, 16 insertions, 20 deletions
diff --git a/Userland/DevTools/HackStudio/Git/GitRepo.cpp b/Userland/DevTools/HackStudio/Git/GitRepo.cpp index eb14f9cef1..c289434eaf 100644 --- a/Userland/DevTools/HackStudio/Git/GitRepo.cpp +++ b/Userland/DevTools/HackStudio/Git/GitRepo.cpp @@ -80,10 +80,10 @@ DeprecatedString GitRepo::command(Vector<DeprecatedString> const& command_parts) DeprecatedString GitRepo::command_wrapper(Vector<DeprecatedString> const& command_parts, DeprecatedString const& chdir) { - auto result = Core::command("git", command_parts, LexicalPath(chdir)); + auto const result = Core::command("git", command_parts, LexicalPath(chdir)); if (result.is_error() || result.value().exit_code != 0) return {}; - return result.value().output; + return DeprecatedString(result.value().output.bytes()); } bool GitRepo::git_is_installed() diff --git a/Userland/DevTools/HackStudio/ProjectBuilder.cpp b/Userland/DevTools/HackStudio/ProjectBuilder.cpp index a41a9006b4..ae8e3e019c 100644 --- a/Userland/DevTools/HackStudio/ProjectBuilder.cpp +++ b/Userland/DevTools/HackStudio/ProjectBuilder.cpp @@ -209,7 +209,7 @@ void ProjectBuilder::for_each_library_definition(Function<void(DeprecatedString, } static Regex<ECMA262> const parse_library_definition(R"~~~(.+:serenity_lib[c]?\((\w+) (\w+)\).*)~~~"); - for (auto& line : res.value().output.split('\n')) { + for (auto& line : StringView(res.value().output).split_view('\n')) { RegexResult result; if (!parse_library_definition.search(line, result)) continue; @@ -234,10 +234,10 @@ void ProjectBuilder::for_each_library_dependencies(Function<void(DeprecatedStrin warnln("{}", res.error()); return; } + auto libraries = StringView(res.value().output).split_view('\n'); static Regex<ECMA262> const parse_library_definition(R"~~~(.+:target_link_libraries\((\w+) ([\w\s]+)\).*)~~~"); - for (auto& line : res.value().output.split('\n')) { - + for (auto& line : libraries) { RegexResult result; if (!parse_library_definition.search(line, result)) continue; diff --git a/Userland/Libraries/LibCore/Command.cpp b/Userland/Libraries/LibCore/Command.cpp index ee89735247..f6ffcce0aa 100644 --- a/Userland/Libraries/LibCore/Command.cpp +++ b/Userland/Libraries/LibCore/Command.cpp @@ -7,7 +7,7 @@ #include "Command.h" #include <AK/Format.h> #include <AK/ScopeGuard.h> -#include <LibCore/DeprecatedFile.h> +#include <LibCore/File.h> #include <fcntl.h> #include <stdio.h> #include <sys/wait.h> @@ -72,16 +72,13 @@ ErrorOr<CommandResult> command(DeprecatedString const& program, Vector<Deprecate close(stdout_pipe[1]); close(stderr_pipe[1]); - auto read_all_from_pipe = [](int pipe[2]) { - auto result_file = Core::DeprecatedFile::construct(); - if (!result_file->open(pipe[0], Core::OpenMode::ReadOnly, Core::DeprecatedFile::ShouldCloseFileDescriptor::Yes)) { - perror("open"); - VERIFY_NOT_REACHED(); - } - return DeprecatedString::copy(result_file->read_all()); + auto read_all_from_pipe = [](int pipe[2]) -> ErrorOr<ByteBuffer> { + auto result_file_or_error = Core::File::adopt_fd(pipe[0], Core::File::OpenMode::Read, Core::File::ShouldCloseFileDescriptor::Yes); + auto result_file = TRY(result_file_or_error); + return result_file->read_until_eof(); }; - auto output = read_all_from_pipe(stdout_pipe); - auto error = read_all_from_pipe(stderr_pipe); + auto output = TRY(read_all_from_pipe(stdout_pipe)); + auto error = TRY(read_all_from_pipe(stderr_pipe)); int wstatus { 0 }; waitpid(pid, &wstatus, 0); diff --git a/Userland/Libraries/LibCore/Command.h b/Userland/Libraries/LibCore/Command.h index 6fa7cf7a99..170acc11b0 100644 --- a/Userland/Libraries/LibCore/Command.h +++ b/Userland/Libraries/LibCore/Command.h @@ -6,19 +6,18 @@ #pragma once -#include <AK/DeprecatedString.h> +#include <AK/ByteBuffer.h> #include <AK/LexicalPath.h> #include <AK/Optional.h> +#include <AK/String.h> #include <spawn.h> namespace Core { -// If the executed command fails, the returned String will be in the null state. - struct CommandResult { int exit_code { 0 }; - DeprecatedString output; - DeprecatedString error; + ByteBuffer output; + ByteBuffer error; }; ErrorOr<CommandResult> command(DeprecatedString const& program, Vector<DeprecatedString> const& arguments, Optional<LexicalPath> chdir); |