KiCad PCB EDA Suite
Loading...
Searching...
No Matches
jobset.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2024 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <i18n_utility.h>
22#include <json_common.h>
23
24#include <settings/parameters.h>
26
27#include <jobs/jobset.h>
28#include <jobs/job_registry.h>
31#include <kiid.h>
32#include <reporter.h>
33
34#include <algorithm>
35
37
38
39KICOMMON_API std::map<JOBSET_DESTINATION_T, JOBSET_DESTINATION_T_INFO> JobsetDestinationTypeInfos =
40{
41 { JOBSET_DESTINATION_T::FOLDER,
42 { _HKI( "Folder" ), BITMAPS::small_folder, true, "" } },
43 { JOBSET_DESTINATION_T::ARCHIVE,
44 { _HKI( "Archive" ), BITMAPS::zip, false, FILEEXT::ZipFileWildcard() } },
45};
46
47
49 {
50 { JOBSET_DESTINATION_T::FOLDER, "folder" },
51 { JOBSET_DESTINATION_T::ARCHIVE, "archive" }
52 } )
53
54KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_JOB& f )
55{
56 j = nlohmann::json{ { "id", f.m_id },
57 { "type", f.m_type },
58 { "description", f.m_description },
59 { "settings", nlohmann::json::object( {} ) }
60 };
61
62 f.m_job->ToJson( j.at( "settings" ) );
63}
64
65
66KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_JOB& f )
67{
68 j.at( "type" ).get_to( f.m_type );
69 j.at( "id" ).get_to( f.m_id );
70 f.m_description = j.value( "description", "" );
71
72 nlohmann::json settings_obj = j.at( "settings" );
73
74 f.m_job.reset( JOB_REGISTRY::CreateInstance<JOB>( f.m_type ) );
75
76 if( f.m_job != nullptr )
77 {
78 f.m_job->FromJson( settings_obj );
79 }
80}
81
82
83KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_DESTINATION& destination )
84{
85 j = nlohmann::json{ { "id", destination.m_id },
86 { "type", destination.m_type },
87 { "only", destination.m_only },
88 { "description", destination.m_description },
89 { "settings", nlohmann::json::object( {} ) }
90 };
91
92 destination.m_outputHandler->ToJson( j.at( "settings" ) );
93}
94
95
96KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_DESTINATION& destination )
97{
98 // During 9.0 development outputs didn't get ids.
99 if( j.contains( "id" ) )
100 j.at( "id" ).get_to( destination.m_id );
101 else
102 destination.m_id = KIID().AsString();
103
104 j.at( "type" ).get_to( destination.m_type );
105 destination.m_only = j.value( "only", std::vector<wxString>() );
106 destination.m_description = j.value( "description", "" );
107
108 const nlohmann::json& settings_obj = j.at( "settings" );
109
110 destination.InitOutputHandler();
111
112 if( destination.m_outputHandler != nullptr )
113 destination.m_outputHandler->FromJson( settings_obj );
114}
115
116
118 m_type( JOBSET_DESTINATION_T::FOLDER ),
119 m_outputHandler( nullptr ),
120 m_lastRunSuccess(),
121 m_lastRunReporters()
122{
123}
124
125
127 m_id( id ),
128 m_type( type ),
129 m_outputHandler( nullptr ),
130 m_lastRunSuccess(),
131 m_lastRunReporters()
132{
134}
135
136
138{
139 for( auto& [name, reporter] : m_lastRunReporters )
140 delete reporter;
141
142 m_lastRunReporters.clear();
143}
144
145
147{
148 if( m_type == JOBSET_DESTINATION_T::FOLDER )
149 {
151 }
152 else if( m_type == JOBSET_DESTINATION_T::ARCHIVE )
153 {
155 }
156}
157
158
160{
162}
163
164
166{
168}
169
170
171void JOBSET_DESTINATION::SetDescription( const wxString& aDescription )
172{
173 if( aDescription == m_outputHandler->GetDefaultDescription() )
174 m_description = wxEmptyString;
175 else
176 m_description = aDescription;
177}
178
179
180bool JOBSET_JOB::operator==( const JOBSET_JOB & rhs ) const
181{
182 return rhs.m_type == m_type;
183}
184
185
187{
188 return m_description.IsEmpty() ? m_job->GetDefaultDescription() : m_description;
189}
190
191
192void JOBSET_JOB::SetDescription( const wxString& aDescription )
193{
194 if( aDescription == m_job->GetDefaultDescription() )
195 m_description = wxEmptyString;
196 else
197 m_description = aDescription;
198}
199
200
202{
203 return rhs.m_type == m_type;
204}
205
206
207JOBSET::JOBSET( const wxString& aFilename ) :
209 m_dirty( false )
210{
211 m_params.emplace_back( new PARAM_LIST<JOBSET_JOB>( "jobs", &m_jobs, {} ) );
212 m_params.emplace_back( new PARAM_LIST<JOBSET_DESTINATION>( "outputs", &m_destinations, {} ) );
213
214 m_fileNameWithoutPath = wxFileName( aFilename ).GetFullName();
215}
216
217
218wxString JOBSET::getFileExt() const
219{
221}
222
223
224void JOBSET::AddNewJob( wxString aType, JOB* aJob )
225{
226 m_jobs.emplace_back( KIID().AsString(), aType, aJob );
227 SetDirty();
228}
229
230
232{
233 m_destinations.emplace_back( KIID().AsString(), aType );
234 SetDirty();
235
236 return &m_destinations.back();
237}
238
239
241{
242 std::erase_if( m_destinations,
243 [&]( JOBSET_DESTINATION const& destination )
244 {
245 return destination.m_id == aDestination->m_id;
246 } );
247}
248
249
250void JOBSET::MoveJobUp( size_t aJobIdx )
251{
252 if( aJobIdx > 0 )
253 {
254 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx - 1] );
255 SetDirty();
256 }
257}
258
259
260void JOBSET::MoveJobDown( size_t aJobIdx )
261{
262 if( aJobIdx < m_jobs.size() - 1 )
263 {
264 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx + 1] );
265 SetDirty();
266 }
267}
268
269
270void JOBSET::RemoveJob( size_t aJobIdx )
271{
272 m_jobs.erase( m_jobs.begin() + aJobIdx );
273 SetDirty();
274}
275
276
277bool JOBSET::SaveToFile( const wxString& aDirectory, bool aForce )
278{
279 bool success = JSON_SETTINGS::SaveToFile( aDirectory, aForce );
280 if( success )
281 {
282 m_dirty = false;
283 }
284
285 return success;
286}
287
288
290{
291 auto it = std::find_if( m_destinations.begin(), m_destinations.end(),
292 [&]( const JOBSET_DESTINATION& destination )
293 {
294 if( destination.m_id == aDestination )
295 return true;
296
297 return false;
298 } );
299
300 if( it != m_destinations.end() )
301 return &(*it);
302
303 return nullptr;
304}
305
306
307std::vector<JOBSET_JOB> JOBSET::GetJobsForDestination( JOBSET_DESTINATION* aDestination )
308{
309 wxASSERT( aDestination != nullptr );
310
311 if( aDestination->m_only.size() == 0 )
312 {
313 return m_jobs;
314 }
315
316 std::vector<JOBSET_JOB> result;
317 for( wxString& onlyId : aDestination->m_only )
318 {
319 auto it = std::find_if( m_jobs.begin(), m_jobs.end(),
320 [&]( const JOBSET_JOB& job )
321 {
322 if( job.m_id == onlyId )
323 return true;
324
325 return false;
326 } );
327
328 if( it != m_jobs.end() )
329 result.push_back( *it );
330 }
331
332 return result;
333}
334
335
336#if !defined( __MINGW32__ )
339#endif
const char * name
Definition: DXF_plotter.cpp:62
@ small_folder
wxString m_fileNameWithoutPath
Definition: jobset.h:148
JOBSET_DESTINATION * GetDestination(wxString &aDestination)
Definition: jobset.cpp:289
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then writes the contents of the JSON document to a file.
Definition: jobset.cpp:277
void SetDirty(bool aFlag=true)
Definition: jobset.h:127
void MoveJobUp(size_t aJobIdx)
Definition: jobset.cpp:250
std::vector< JOBSET_DESTINATION > m_destinations
Definition: jobset.h:145
std::vector< JOBSET_JOB > GetJobsForDestination(JOBSET_DESTINATION *aDestination)
Definition: jobset.cpp:307
std::vector< JOBSET_JOB > m_jobs
Definition: jobset.h:144
void RemoveJob(size_t aJobIdx)
Definition: jobset.cpp:270
void AddNewJob(wxString aType, JOB *aJob)
Definition: jobset.cpp:224
void MoveJobDown(size_t aJobIdx)
Definition: jobset.cpp:260
JOBSET_DESTINATION * AddNewDestination(JOBSET_DESTINATION_T aType)
Definition: jobset.cpp:231
wxString getFileExt() const override
Definition: jobset.cpp:218
bool m_dirty
Definition: jobset.h:147
JOBSET(const wxString &aFilename)
Definition: jobset.cpp:207
void RemoveDestination(JOBSET_DESTINATION *aDestination)
Definition: jobset.cpp:240
virtual wxString GetDefaultDescription() const
Definition: jobs_output.h:56
virtual void ToJson(nlohmann::json &j) const =0
wxString GetOutputPath() const
Definition: jobs_output.h:54
virtual void FromJson(const nlohmann::json &j)=0
An simple container class that lets us dispatch output jobs to kifaces.
Definition: job.h:183
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
Definition: kiid.h:49
wxString AsString() const
Definition: kiid.cpp:246
#define _HKI(x)
@ NONE
Definition: eda_shape.h:69
static const std::string KiCadJobSetFileExtension
static wxString ZipFileWildcard()
Some functions to handle hotkeys in KiCad.
KICOMMON_API void to_json(nlohmann::json &j, const JOBSET_DESTINATION &destination)
Definition: jobset.cpp:83
KICOMMON_API void from_json(const nlohmann::json &j, JOBSET_JOB &f)
Definition: jobset.cpp:66
NLOHMANN_JSON_SERIALIZE_ENUM(JOBSET_DESTINATION_T, { { JOBSET_DESTINATION_T::FOLDER, "folder" }, { JOBSET_DESTINATION_T::ARCHIVE, "archive" } }) KICOMMON_API void to_json(nlohmann
Definition: jobset.cpp:48
const int jobsFileSchemaVersion
Definition: jobset.cpp:36
KICOMMON_API std::map< JOBSET_DESTINATION_T, JOBSET_DESTINATION_T_INFO > JobsetDestinationTypeInfos
Definition: jobset.cpp:39
enum KICOMMON_API JOBSET_DESTINATION_T
Definition: jobset.h:59
FOLDER
Definition: jobset.h:60
SETTINGS_LOC
Definition: json_settings.h:54
#define KICOMMON_API
Definition: kicommon.h:28
void SetDescription(const wxString &aDescription)
Definition: jobset.cpp:171
std::unordered_map< wxString, REPORTER * > m_lastRunReporters
Definition: jobset.h:100
JOBSET_DESTINATION_T m_type
Definition: jobset.h:87
wxString m_description
Definition: jobset.h:88
JOBS_OUTPUT_HANDLER * m_outputHandler
Definition: jobset.h:89
std::vector< wxString > m_only
Definition: jobset.h:90
void InitOutputHandler()
Definition: jobset.cpp:146
wxString GetPathInfo() const
Transient property, not stored for now.
Definition: jobset.cpp:165
bool operator==(const JOBSET_DESTINATION &rhs) const
Definition: jobset.cpp:201
wxString GetDescription() const
Definition: jobset.cpp:159
wxString m_id
Definition: jobset.h:86
wxString m_id
Definition: jobset.h:46
std::shared_ptr< JOB > m_job
Definition: jobset.h:49
wxString m_description
Definition: jobset.h:48
wxString m_type
Definition: jobset.h:47
bool operator==(const JOBSET_JOB &rhs) const
Definition: jobset.cpp:180
wxString GetDescription() const
Definition: jobset.cpp:186
void SetDescription(const wxString &aDescription)
Definition: jobset.cpp:192
Definition of file extensions used in Kicad.