KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <mark.roszko@gmail.com>
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 <nlohmann/json.hpp>
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
165void JOBSET_DESTINATION::SetDescription( const wxString& aDescription )
166{
167 if( aDescription == m_outputHandler->GetDefaultDescription() )
168 m_description = wxEmptyString;
169 else
170 m_description = aDescription;
171}
172
173
174bool JOBSET_JOB::operator==( const JOBSET_JOB & rhs ) const
175{
176 return rhs.m_type == m_type;
177}
178
179
181{
182 return m_description.IsEmpty() ? m_job->GetDefaultDescription() : m_description;
183}
184
185
186void JOBSET_JOB::SetDescription( const wxString& aDescription )
187{
188 if( aDescription == m_job->GetDefaultDescription() )
189 m_description = wxEmptyString;
190 else
191 m_description = aDescription;
192}
193
194
196{
197 return rhs.m_type == m_type;
198}
199
200
201JOBSET::JOBSET( const wxString& aFilename ) :
203 m_dirty( false )
204{
205 m_params.emplace_back( new PARAM_LIST<JOBSET_JOB>( "jobs", &m_jobs, {} ) );
206 m_params.emplace_back( new PARAM_LIST<JOBSET_DESTINATION>( "outputs", &m_destinations, {} ) );
207
208 m_fileNameWithoutPath = wxFileName( aFilename ).GetFullName();
209}
210
211
212wxString JOBSET::getFileExt() const
213{
215}
216
217
218void JOBSET::AddNewJob( wxString aType, JOB* aJob )
219{
220 m_jobs.emplace_back( KIID().AsString(), aType, aJob );
221 SetDirty();
222}
223
224
226{
227 m_destinations.emplace_back( KIID().AsString(), aType );
228 SetDirty();
229
230 return &m_destinations.back();
231}
232
233
235{
236 std::erase_if( m_destinations,
237 [&]( JOBSET_DESTINATION const& destination )
238 {
239 return destination.m_id == aDestination->m_id;
240 } );
241}
242
243
244void JOBSET::MoveJobUp( size_t aJobIdx )
245{
246 if( aJobIdx > 0 )
247 {
248 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx - 1] );
249 SetDirty();
250 }
251}
252
253
254void JOBSET::MoveJobDown( size_t aJobIdx )
255{
256 if( aJobIdx < m_jobs.size() - 1 )
257 {
258 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx + 1] );
259 SetDirty();
260 }
261}
262
263
264void JOBSET::RemoveJob( size_t aJobIdx )
265{
266 m_jobs.erase( m_jobs.begin() + aJobIdx );
267 SetDirty();
268}
269
270
271bool JOBSET::SaveToFile( const wxString& aDirectory, bool aForce )
272{
273 bool success = JSON_SETTINGS::SaveToFile( aDirectory, aForce );
274 if( success )
275 {
276 m_dirty = false;
277 }
278
279 return success;
280}
281
282
284{
285 auto it = std::find_if( m_destinations.begin(), m_destinations.end(),
286 [&]( const JOBSET_DESTINATION& destination )
287 {
288 if( destination.m_id == aDestination )
289 return true;
290
291 return false;
292 } );
293
294 if( it != m_destinations.end() )
295 return &(*it);
296
297 return nullptr;
298}
299
300
301std::vector<JOBSET_JOB> JOBSET::GetJobsForDestination( JOBSET_DESTINATION* aDestination )
302{
303 wxASSERT( aDestination != nullptr );
304
305 if( aDestination->m_only.size() == 0 )
306 {
307 return m_jobs;
308 }
309
310 std::vector<JOBSET_JOB> result;
311 for( wxString& onlyId : aDestination->m_only )
312 {
313 auto it = std::find_if( m_jobs.begin(), m_jobs.end(),
314 [&]( const JOBSET_JOB& job )
315 {
316 if( job.m_id == onlyId )
317 return true;
318
319 return false;
320 } );
321
322 if( it != m_jobs.end() )
323 result.push_back( *it );
324 }
325
326 return result;
327}
328
329
330#if !defined( __MINGW32__ )
333#endif
const char * name
Definition: DXF_plotter.cpp:59
@ small_folder
wxString m_fileNameWithoutPath
Definition: jobset.h:146
JOBSET_DESTINATION * GetDestination(wxString &aDestination)
Definition: jobset.cpp:283
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:271
void SetDirty(bool aFlag=true)
Definition: jobset.h:125
void MoveJobUp(size_t aJobIdx)
Definition: jobset.cpp:244
std::vector< JOBSET_DESTINATION > m_destinations
Definition: jobset.h:143
std::vector< JOBSET_JOB > GetJobsForDestination(JOBSET_DESTINATION *aDestination)
Definition: jobset.cpp:301
std::vector< JOBSET_JOB > m_jobs
Definition: jobset.h:142
void RemoveJob(size_t aJobIdx)
Definition: jobset.cpp:264
void AddNewJob(wxString aType, JOB *aJob)
Definition: jobset.cpp:218
void MoveJobDown(size_t aJobIdx)
Definition: jobset.cpp:254
JOBSET_DESTINATION * AddNewDestination(JOBSET_DESTINATION_T aType)
Definition: jobset.cpp:225
wxString getFileExt() const override
Definition: jobset.cpp:212
bool m_dirty
Definition: jobset.h:145
JOBSET(const wxString &aFilename)
Definition: jobset.cpp:201
void RemoveDestination(JOBSET_DESTINATION *aDestination)
Definition: jobset.cpp:234
virtual wxString GetDefaultDescription() const
Definition: jobs_output.h:56
virtual void ToJson(nlohmann::json &j) const =0
virtual void FromJson(const nlohmann::json &j)=0
An simple container class that lets us dispatch output jobs to kifaces.
Definition: job.h:182
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)
Transient property, not stored for now.
Definition: jobset.cpp:165
std::unordered_map< wxString, REPORTER * > m_lastRunReporters
Definition: jobset.h:98
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
bool operator==(const JOBSET_DESTINATION &rhs) const
Definition: jobset.cpp:195
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:174
wxString GetDescription() const
Definition: jobset.cpp:180
void SetDescription(const wxString &aDescription)
Definition: jobset.cpp:186
Definition of file extensions used in Kicad.