35#include <wx/process.h>
36#include <wx/txtstrm.h>
37#include <wx/sstream.h>
38#include <wx/wfstream.h>
39#include <wx/mstream.h>
40#include <wx/tokenzr.h>
78 wxInputStream* inputStream =
process.GetInputStream();
79 wxInputStream* errorStream =
process.GetErrorStream();
82 auto streamToBuf = []( wxInputStream& aIs )
84 wxMemoryOutputStream memOut;
88 buf.AppendData( memOut.GetOutputStreamBuffer()->GetBufferStart(),
89 memOut.GetOutputStreamBuffer()->GetIntPosition() );
94 if( inputStream && errorStream )
96 wxMemoryBuffer memInBuf = streamToBuf( *inputStream );
97 wxMemoryBuffer memErrBuf = streamToBuf( *errorStream );
99 if( !memInBuf.IsEmpty() )
101 wxString str = wxString::FromUTF8( memInBuf, memInBuf.GetDataLen() );
102 wxStringTokenizer tokenizer( str,
"\r\n" );
104 while( tokenizer.HasMoreTokens() )
108 if( !memErrBuf.IsEmpty() )
110 wxString str = wxString::FromUTF8( memErrBuf, memErrBuf.GetDataLen() );
111 wxStringTokenizer tokenizer( str,
"\r\n" );
113 while( tokenizer.HasMoreTokens() )
119 wxFileName defaultLog( aJob->
m_id );
120 defaultLog.SetExt( wxT(
"log" ) );
128 defaultLog.GetFullName() );
130 bool logWritten =
false;
134 wxFileOutputStream procOutput( logPath );
137 logWritten = procOutput.IsOk() && procOutput.WriteAll( memInBuf, memInBuf.GetDataLen() )
138 && procOutput.WriteAll( memErrBuf, memErrBuf.GetDataLen() ) && procOutput.Close();
143 wxString msg = wxString::Format(
_(
"Command finished but its output could not be written to '%s'" ),
156 aReporter->
Report( wxString::Format(
_(
"Command exited with code %d" ),
result ),
165 std::vector<wxString>& aPathsWritten )
172 wxFileName sourceFn(
source );
176 if( !aJob->
m_dest.IsEmpty() )
181 errors, aPathsWritten );
220 bool genOutputs =
true;
222 std::vector<JOBSET_JOB> jobsForDestination =
m_jobsFile->GetJobsForDestination( aDestination );
226 tmp.AssignDir( wxFileName::GetTempDir() );
227 tmp.AppendDir(
KIID().AsString() );
233 wxString tempDirPath = tmp.GetFullPath();
235 if( !wxFileName::Mkdir( tempDirPath, wxS_DIR_DEFAULT ) )
237 msg = wxString::Format( wxT(
"Failed to create temporary directory %s" ), tempDirPath );
249 msg = wxString::Format( wxT(
"Destination precheck failed for destination %s" ),
250 aDestination->
m_id );
257 msg += wxT(
"|--------------------------------\n" );
259 msg += wxString::Format( wxT(
"Running jobs for destination %s" ), aDestination->
m_id );
261 msg += wxT(
"|--------------------------------\n" );
263 msg += wxString::Format( wxT(
"|%-5s | %-50s\n" ), wxT(
"No." ), wxT(
"Description" ) );
267 for(
const JOBSET_JOB& job : jobsForDestination )
269 msg += wxString::Format( wxT(
"|%-5d | %-50s\n" ), jobNum, job.GetDescription() );
273 msg += wxT(
"|--------------------------------\n" );
279 std::vector<wxString> pathsWithOverwriteDisallowed;
280 std::vector<JOB_OUTPUT> outputs;
284 int successCount = 0;
288 for(
const JOBSET_JOB& job : jobsForDestination )
290 msg = wxT(
"|--------------------------------\n" );
292 msg += wxString::Format( wxT(
"| Running job %d: %s" ), jobNum, job.GetDescription() );
295 msg += wxT(
"|--------------------------------\n" );
301 msg.Printf(
_(
"Running job %d: %s" ), jobNum, job.GetDescription() );
310 job.m_job->SetTempOutputDirectory( tempDirPath );
333 if( job.m_job->GetType() ==
"special_execute" )
337 else if( job.m_job->GetType() ==
"special_copyfiles" )
340 std::vector<wxString> pathsWritten;
346 pathsWithOverwriteDisallowed.insert( pathsWithOverwriteDisallowed.end(), pathsWritten.begin(),
347 pathsWritten.end() );
350 else if( job.m_job->GetType() ==
"special_archive" )
356 msg = wxString::Format( wxT(
"Unsupported job type '%s'" ), job.m_type );
366 wxString msg_fmt = wxT(
"\033[32;1m%s\033[0m\n" );
367 msg = wxString::Format( msg_fmt,
_(
"Job successful" ) );
373 wxString msg_fmt = wxT(
"\033[31;1m%s\033[0m\n" );
374 msg = wxString::Format( msg_fmt,
_(
"Job failed" ) );
379 msg += wxT(
"\n\n" );
409 msg = wxString::Format( wxT(
"\n\n\033[33;1m%d %s, %d %s\033[0m\n" ),
411 wxT(
"jobs succeeded" ),
413 wxT(
"job failed" ) );
virtual bool OutputPrecheck()
Checks if the output process can proceed before doing anything else This can include user prompts.
virtual bool HandleOutputs(const wxString &aBaseTempPath, PROJECT *aProject, const std::vector< wxString > &aPathsWithOverwriteDisallowed, const std::vector< JOB_OUTPUT > &aOutputsToHandle, std::optional< wxString > &aResolvedOutputPath)=0
bool RunJobsAllDestinations(bool aBail=false)
JOBS_PROGRESS_REPORTER * m_progressReporter
bool RunJobsForDestination(JOBSET_DESTINATION *aDestination, bool aBail=false)
int runSpecialCopyFiles(const JOB_SPECIAL_COPYFILES *aJob, PROJECT *aProject, std::vector< wxString > &aPathsWritten)
int runSpecialArchive(const JOBSET_JOB *aJob, REPORTER *aReporter, PROJECT *aProject)
int runSpecialExecute(const JOBSET_JOB *aJob, REPORTER *aReporter, PROJECT *aProject)
JOBS_RUNNER(KIWAY *aKiway, JOBSET *aJobsFile, PROJECT *aProject, REPORTER &aReporter, JOBS_PROGRESS_REPORTER *aProgressReporter)
static KIWAY::FACE_T GetKifaceType(const wxString &aName)
bool m_generateErrorOnNoCopy
void SetConfiguredOutputPath(const wxString &aPath)
Sets the configured output path for the job, this path is always saved to file.
wxString ResolveOutputPath(const wxString &aPath, bool aPathIsDirectory, PROJECT *aProject) const
wxString GetFullOutputPath(PROJECT *aProject) const
Returns the full output path for the job, taking into account the configured output path,...
static wxString AppendDefaultFileName(const wxString &aPath, const wxString &aDefaultName)
Complete a resolved file output path that names a folder, either by a trailing separator or by being ...
wxString GetConfiguredOutputPath() const
Returns the configured output path for the job.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
FACE_T
Known KIFACE implementations.
static REPORTER & GetInstance()
static bool EnsurePathExists(const wxString &aPath, bool aPathToFile=false)
Attempts to create a given path if it does not exist.
static bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Create an archive of the project.
Container for project specific data.
virtual const wxString GetProjectPath() const
Return the full path of the project.
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
A pure virtual class used to derive REPORTER objects from.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
wxString ExpandPathVariables(const wxString &aPath, const PROJECT *aProject, const std::function< bool(wxString *)> *aTextResolver)
Expand text variables, environment variables and a leading '~' in a user-entered file path.
wxString ResolveProjectPath(const wxString &aPath, const PROJECT *aProject, const std::function< bool(wxString *)> *aTextResolver)
Expand a user-entered file path as ExpandPathVariables() does, then anchor a relative result at the p...
int ExecuteCommandThroughShell(const wxString &aCommand, wxProcess *aProcess)
Run a user-supplied command line through the platform shell so glob expansion, pipes,...
bool CopyFilesOrDirectory(const wxString &aSourcePath, const wxString &aDestDir, bool aAllowOverwrites, wxString &aErrors, std::vector< wxString > &aPathsWritten)
wxString ExpandExecuteCommand(const wxString &aCommand, const PROJECT *aProject)
Expand an Execute Command string, including the built-in variables.
#define OUTPUT_TMP_PATH_VAR_NAME
static const int ERR_ARGS
static const int ERR_RC_VIOLATIONS
Rules check violation count was greater than 0.
static const int ERR_INVALID_OUTPUT_CONFLICT
static const int ERR_UNKNOWN
static PGM_BASE * process
std::unordered_map< wxString, std::shared_ptr< JOBSET_OUTPUT_REPORTER > > m_lastRunReporters
std::shared_ptr< JOBS_OUTPUT_HANDLER > m_outputHandler
std::optional< wxString > m_lastResolvedOutputPath
std::optional< bool > m_lastRunSuccess
std::unordered_map< wxString, std::optional< bool > > m_lastRunSuccessMap
std::shared_ptr< JOB > m_job
wxString result
Test unit parsing edge cases and error handling.