1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:27:45 +00:00

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.
This commit is contained in:
Ben Wiederhake 2023-05-13 18:55:15 +02:00 committed by Jelle Raaijmakers
parent 07dd719e3e
commit f9a24eb7eb
4 changed files with 16 additions and 20 deletions

View file

@ -80,10 +80,10 @@ DeprecatedString GitRepo::command(Vector<DeprecatedString> const& command_parts)
DeprecatedString GitRepo::command_wrapper(Vector<DeprecatedString> const& command_parts, DeprecatedString const& chdir) 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) if (result.is_error() || result.value().exit_code != 0)
return {}; return {};
return result.value().output; return DeprecatedString(result.value().output.bytes());
} }
bool GitRepo::git_is_installed() bool GitRepo::git_is_installed()

View file

@ -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+)\).*)~~~"); 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; RegexResult result;
if (!parse_library_definition.search(line, result)) if (!parse_library_definition.search(line, result))
continue; continue;
@ -234,10 +234,10 @@ void ProjectBuilder::for_each_library_dependencies(Function<void(DeprecatedStrin
warnln("{}", res.error()); warnln("{}", res.error());
return; return;
} }
auto libraries = StringView(res.value().output).split_view('\n');
static Regex<ECMA262> const parse_library_definition(R"~~~(.+:target_link_libraries\((\w+) ([\w\s]+)\).*)~~~"); 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; RegexResult result;
if (!parse_library_definition.search(line, result)) if (!parse_library_definition.search(line, result))
continue; continue;

View file

@ -7,7 +7,7 @@
#include "Command.h" #include "Command.h"
#include <AK/Format.h> #include <AK/Format.h>
#include <AK/ScopeGuard.h> #include <AK/ScopeGuard.h>
#include <LibCore/DeprecatedFile.h> #include <LibCore/File.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -72,16 +72,13 @@ ErrorOr<CommandResult> command(DeprecatedString const& program, Vector<Deprecate
close(stdout_pipe[1]); close(stdout_pipe[1]);
close(stderr_pipe[1]); close(stderr_pipe[1]);
auto read_all_from_pipe = [](int pipe[2]) { auto read_all_from_pipe = [](int pipe[2]) -> ErrorOr<ByteBuffer> {
auto result_file = Core::DeprecatedFile::construct(); auto result_file_or_error = Core::File::adopt_fd(pipe[0], Core::File::OpenMode::Read, Core::File::ShouldCloseFileDescriptor::Yes);
if (!result_file->open(pipe[0], Core::OpenMode::ReadOnly, Core::DeprecatedFile::ShouldCloseFileDescriptor::Yes)) { auto result_file = TRY(result_file_or_error);
perror("open"); return result_file->read_until_eof();
VERIFY_NOT_REACHED();
}
return DeprecatedString::copy(result_file->read_all());
}; };
auto output = read_all_from_pipe(stdout_pipe); auto output = TRY(read_all_from_pipe(stdout_pipe));
auto error = read_all_from_pipe(stderr_pipe); auto error = TRY(read_all_from_pipe(stderr_pipe));
int wstatus { 0 }; int wstatus { 0 };
waitpid(pid, &wstatus, 0); waitpid(pid, &wstatus, 0);

View file

@ -6,19 +6,18 @@
#pragma once #pragma once
#include <AK/DeprecatedString.h> #include <AK/ByteBuffer.h>
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <AK/Optional.h> #include <AK/Optional.h>
#include <AK/String.h>
#include <spawn.h> #include <spawn.h>
namespace Core { namespace Core {
// If the executed command fails, the returned String will be in the null state.
struct CommandResult { struct CommandResult {
int exit_code { 0 }; int exit_code { 0 };
DeprecatedString output; ByteBuffer output;
DeprecatedString error; ByteBuffer error;
}; };
ErrorOr<CommandResult> command(DeprecatedString const& program, Vector<DeprecatedString> const& arguments, Optional<LexicalPath> chdir); ErrorOr<CommandResult> command(DeprecatedString const& program, Vector<DeprecatedString> const& arguments, Optional<LexicalPath> chdir);