4#ifndef _Stroika_Foundation_Execution_ProcessRunner_h_
5#define _Stroika_Foundation_Execution_ProcessRunner_h_ 1
7#include "Stroika/Foundation/StroikaPreComp.h"
14#include "Stroika/Foundation/Common/Common.h"
15#include "Stroika/Foundation/Containers/Mapping.h"
16#include "Stroika/Foundation/Containers/Sequence.h"
18#include "Stroika/Foundation/Execution/CommandLine.h"
19#include "Stroika/Foundation/Execution/Process.h"
20#include "Stroika/Foundation/Execution/Signals.h"
26#include "Stroika/Foundation/Execution/ProgressMonitor.h"
91 using Characters::String;
92 using Containers::Mapping;
93 using Containers::Sequence;
135#if qStroika_Platform_Windows
136 CommandLine::WrapInShell::eWindowsCMD
138 CommandLine::WrapInShell::eBash
151 optional<filesystem::path> fWorkingDirectory;
178 bool fDetached{
false};
180#if qStroika_Platform_POSIX
186 optional<mode_t> fChildUMask{027};
189#if qStroika_Platform_Windows
194 bool fCreateNoWindow : 1 {
true};
212#if qCompilerAndStdLib_default_member_initializer_needed_within_enclosing_class_Buggy
229#if qStroika_Platform_POSIX
230 using ExitStatusType = uint8_t;
231#elif qStroika_Platform_Windows
232 using ExitStatusType = DWORD;
234 using ExitStatusType = int;
244 nonvirtual
void SetCommandLine (
const CommandLine& args);
249 nonvirtual Options GetOptions ()
const;
250 nonvirtual
void SetOptions (
const Options& o);
259 optional<ExitStatusType> fExitStatus;
260 optional<SignalID> fTerminatedByUncaughtSignalNumber;
267 nonvirtual
String ToString ()
const;
327 nonvirtual tuple<Characters::String, Characters::String>
Run (
const Characters::String& cmdStdInValue =
""sv,
332 class BackgroundProcess;
357 nonvirtual function<void ()> CreateSimpleRunnable_ ();
364 struct DetailedRunnableRep_ {
382 nonvirtual tuple<function<void ()>, shared_ptr<DetailedRunnableRep_>> CreateDetailedRunnable_ ();
385#if qStroika_Platform_POSIX
386 static void Process_Runner_POSIX_ (
const shared_ptr<DetailedRunnableRep_>& runneeDetails,
387 [[maybe_unused]]
const optional<filesystem::path>& executable,
const CommandLine& cmdLine,
390#elif qStroika_Platform_Windows
391 static void Process_Runner_Windows_ (
const shared_ptr<DetailedRunnableRep_>& runneeDetails,
392 const optional<filesystem::path>& executable,
const CommandLine& cmdLine,
398 optional<filesystem::path> fExecutable_;
407 [[deprecated (
"Since Stroika v3.0d12 - pass stdin/stdout/stderr to ProcessRunner Run() method (if needed)")]]
ProcessRunner (
410 [[deprecated (
"Since Stroika v3.0d12 - pass stdin/stdout/stderr to ProcessRunner Run() method (if needed)")]]
ProcessRunner (
413 [[deprecated (
"Since Stroika v3.0d12 - pass stdin/stdout/stderr to ProcessRunner Run() method (if needed)")]]
ProcessRunner (
419 this->fStdOut_ = out;
420 this->fStdErr_ = error;
423 [[deprecated (
"Since Stroika v3.0d12 - use other overloads for ProcessRunner")]] ProcessRunner (
426 : ProcessRunner{executable, CommandLine{args}}
429 this->fStdOut_ = out;
430 this->fStdErr_ = error;
433 [[deprecated (
"Since Stroika v3.0d12 - pass in/out/error streams(can be nullptr) to Run method instead of CTOR")]]
void
434 Run (optional<ProcessResultType>* processResult, ProgressMonitor::Updater progress =
nullptr,
Time::DurationSeconds timeout = Time::kInfinity);
436 [[deprecated (
"Since Stroika v3.0d12 pass in/out/error(can be nullptr) in RunInbackground() method")]] BackgroundProcess
442 [[maybe_unused]] ProgressMonitor::Updater progress);
449 [[deprecated (
"Since Stroika v3.0d23d")]] tuple<Characters::String, Characters::String>
450 Run (
const Characters::String& cmdStdInValue,
const StringOptions& stringOpts, ProgressMonitor::Updater progress,
456 [[deprecated (
"Since Stroika v3.0d12 - use GetOptions().fWorkingDirectory")]] optional<filesystem::path> GetWorkingDirectory ()
const;
457 [[deprecated (
"Since Stroika v3.0d12 - use SetOptions({.fWorkingDirectory})")]]
void SetWorkingDirectory (
const optional<filesystem::path>& d);
468 [[deprecated (
"Since Stroika v3.0d12 - pass in/out/error streams(can be nullptr) to Run method instead of CTOR")]]
void
477 [[deprecated (
"Since Stroika v3.0d12 - pass in/out/error streams(can be nullptr) to Run method instead of CTOR")]]
void
486 [[deprecated (
"Since Stroika v3.0d12 - pass in/out/error streams(can be nullptr) to Run method instead of CTOR")]]
void
607 virtual ~Rep_ () =
default;
609 shared_ptr<DetailedRunnableRep_> fDetailedRunnableRep_;
611 shared_ptr<Rep_> fRep_;
625#include "ProcessRunner.inl"
time_point< RealtimeClock, DurationSeconds > TimePointSeconds
TimePointSeconds is a simpler approach to chrono::time_point, which doesn't require using templates e...
chrono::duration< double > DurationSeconds
chrono::duration<double> - a time span (length of time) measured in seconds, but high precision.
#define Stroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE
[[msvc::no_unique_address]] isn't always broken in MSVC. Annotate with this on things where its not b...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
A Mapping uniquely associates two elements: a key and a value (use Assocation to allow multiple value...
A generalization of a vector: a container whose elements are keyed by the natural numbers.
NOT a real mutex - just a debugging infrastructure support tool so in debug builds can be assured thr...
Exception<> is a replacement (subclass) for any std c++ exception class (e.g. the default 'std::excep...
Exception()=delete
construct with just a message - BASE_EXCEPTION gets whatever it minimally needs.
nonvirtual void WaitForStarted(Time::DurationSeconds timeout=Time::kInfinity) const
wait until GetChildProcessID () returns a valid answer, or until the process failed to start (in whic...
nonvirtual optional< ProcessResultType > GetProcessResult() const
nonvirtual String ToString() const
nonvirtual void PropagateIfException() const
nonvirtual void WaitForDone(Time::DurationSeconds timeout=Time::kInfinity) const
nonvirtual void Join(Time::DurationSeconds timeout=Time::kInfinity) const
Join () does WaitForDone () and throw exception if there was any error (see PropagateIfException).
nonvirtual void JoinUntil(Time::TimePointSeconds timeoutAt) const
WaitForDoneUntil () and throw exception if there was any error (see PropagateIfException).
nonvirtual void Terminate()
optional< pid_t > GetChildProcessID() const
maybe missing if process not yet (or ever successfully) launched. Child process may have already exit...
const String fFailureMessage
Run an external command, with stdin/stdout/stderr as strings or as streams - like perl backticks.
Streams::InputStream::Ptr< byte > GetStdIn() const
Streams::OutputStream::Ptr< byte > GetStdOut() const
nonvirtual void Run(const Streams::InputStream::Ptr< byte > &in, const Streams::OutputStream::Ptr< byte > &out=nullptr, const Streams::OutputStream::Ptr< byte > &error=nullptr, Time::DurationSeconds timeout=Time::kInfinity)
Run the command synchronously, returning its output, and THROW on any failure.
nonvirtual BackgroundProcess RunInBackground(const Streams::InputStream::Ptr< byte > &in=nullptr, const Streams::OutputStream::Ptr< byte > &out=nullptr, const Streams::OutputStream::Ptr< byte > &error=nullptr)
Run the given external command/process (set by constructor) - with the given arguments in the backgro...
Streams::OutputStream::Ptr< byte > GetStdErr() const
ProcessRunner(const String &commandLine, const Options &o={})
Run directly if simple, else through kDefaultShell.
Wrap any object with Synchronized<> and it can be used similarly to the base type,...
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.
EXPECTED::value_type ThrowIfFailed(const EXPECTED &e)
Run () options for mapping Strings - what code page converters to use.
optional< Characters::CodeCvt<> > fInputCodeCvt
optional< Characters::CodeCvt<> > fOutputCodeCvt