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#include <memory>
36
38
39
40KICOMMON_API std::map<JOBSET_DESTINATION_T, JOBSET_DESTINATION_T_INFO> JobsetDestinationTypeInfos =
41{
42 { JOBSET_DESTINATION_T::FOLDER,
43 { _HKI( "Folder" ), BITMAPS::small_folder, true, "" } },
44 { JOBSET_DESTINATION_T::ARCHIVE,
45 { _HKI( "Archive" ), BITMAPS::zip, false, FILEEXT::ZipFileWildcard() } },
46};
47
48
50 {
51 { JOBSET_DESTINATION_T::FOLDER, "folder" },
52 { JOBSET_DESTINATION_T::ARCHIVE, "archive" }
53 } )
54
55KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_JOB& f )
56{
57 j = nlohmann::json{ { "id", f.m_id },
58 { "type", f.m_type },
59 { "description", f.m_description },
60 { "settings", nlohmann::json::object( {} ) }
61 };
62
63 f.m_job->ToJson( j.at( "settings" ) );
64}
65
66
67KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_JOB& f )
68{
69 j.at( "type" ).get_to( f.m_type );
70 j.at( "id" ).get_to( f.m_id );
71 f.m_description = j.value( "description", "" );
72
73 nlohmann::json settings_obj = j.at( "settings" );
74
75 f.m_job.reset( JOB_REGISTRY::CreateInstance<JOB>( f.m_type ) );
76
77 if( f.m_job != nullptr )
78 {
79 f.m_job->FromJson( settings_obj );
80 }
81}
82
83
84KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_DESTINATION& destination )
85{
86 j = nlohmann::json{ { "id", destination.m_id },
87 { "type", destination.m_type },
88 { "only", destination.m_only },
89 { "description", destination.m_description },
90 { "settings", nlohmann::json::object( {} ) }
91 };
92
93 destination.m_outputHandler->ToJson( j.at( "settings" ) );
94}
95
96
97KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_DESTINATION& destination )
98{
99 // During 9.0 development outputs didn't get ids.
100 if( j.contains( "id" ) )
101 j.at( "id" ).get_to( destination.m_id );
102 else
103 destination.m_id = KIID().AsString();
104
105 j.at( "type" ).get_to( destination.m_type );
106 destination.m_only = j.value( "only", std::vector<wxString>() );
107 destination.m_description = j.value( "description", "" );
108
109 const nlohmann::json& settings_obj = j.at( "settings" );
110
111 destination.InitOutputHandler();
112
113 if( destination.m_outputHandler != nullptr )
114 destination.m_outputHandler->FromJson( settings_obj );
115}
116
117
119 m_type( JOBSET_DESTINATION_T::FOLDER ),
120 m_outputHandler( nullptr ),
121 m_lastRunSuccess(),
122 m_lastRunReporters()
123{
124}
125
126
128 m_id( id ),
129 m_type( type ),
130 m_outputHandler( nullptr ),
131 m_lastRunSuccess(),
132 m_lastRunReporters()
133{
135}
136
137
139{
140 if( m_type == JOBSET_DESTINATION_T::FOLDER )
141 {
142 m_outputHandler = std::make_shared<JOBS_OUTPUT_FOLDER>( );
143 }
144 else if( m_type == JOBSET_DESTINATION_T::ARCHIVE )
145 {
146 m_outputHandler = std::make_shared<JOBS_OUTPUT_ARCHIVE>( );
147 }
148}
149
150
152{
153 return m_description.IsEmpty() ? m_outputHandler->GetDefaultDescription() : m_description;
154}
155
156
158{
159 return m_outputHandler->GetOutputPath();
160}
161
162
163void JOBSET_DESTINATION::SetDescription( const wxString& aDescription )
164{
165 if( aDescription == m_outputHandler->GetDefaultDescription() )
166 m_description = wxEmptyString;
167 else
168 m_description = aDescription;
169}
170
171
172bool JOBSET_JOB::operator==( const JOBSET_JOB & rhs ) const
173{
174 return rhs.m_type == m_type;
175}
176
177
179{
180 return m_description.IsEmpty() ? m_job->GetDefaultDescription() : m_description;
181}
182
183
184void JOBSET_JOB::SetDescription( const wxString& aDescription )
185{
186 if( aDescription == m_job->GetDefaultDescription() )
187 m_description = wxEmptyString;
188 else
189 m_description = aDescription;
190}
191
192
194{
195 return rhs.m_type == m_type;
196}
197
198
199JOBSET::JOBSET( const wxString& aFilename ) :
201 m_dirty( false )
202{
203 m_params.emplace_back( new PARAM_LIST<JOBSET_JOB>( "jobs", &m_jobs, {} ) );
204 m_params.emplace_back( new PARAM_LIST<JOBSET_DESTINATION>( "outputs", &m_destinations, {} ) );
205
206 m_fileNameWithoutPath = wxFileName( aFilename ).GetFullName();
207}
208
209
210wxString JOBSET::getFileExt() const
211{
213}
214
215
216void JOBSET::AddNewJob( wxString aType, JOB* aJob )
217{
218 m_jobs.emplace_back( KIID().AsString(), aType, aJob );
219 SetDirty();
220}
221
222
224{
225 m_destinations.emplace_back( KIID().AsString(), aType );
226 SetDirty();
227
228 return &m_destinations.back();
229}
230
231
233{
234 std::erase_if( m_destinations,
235 [&]( JOBSET_DESTINATION const& destination )
236 {
237 return destination.m_id == aDestination->m_id;
238 } );
239}
240
241
242void JOBSET::MoveJobUp( size_t aJobIdx )
243{
244 if( aJobIdx > 0 )
245 {
246 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx - 1] );
247 SetDirty();
248 }
249}
250
251
252void JOBSET::MoveJobDown( size_t aJobIdx )
253{
254 if( aJobIdx < m_jobs.size() - 1 )
255 {
256 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx + 1] );
257 SetDirty();
258 }
259}
260
261
262void JOBSET::RemoveJob( size_t aJobIdx )
263{
264 m_jobs.erase( m_jobs.begin() + aJobIdx );
265 SetDirty();
266}
267
268
269bool JOBSET::SaveToFile( const wxString& aDirectory, bool aForce )
270{
271 bool success = JSON_SETTINGS::SaveToFile( aDirectory, aForce );
272 if( success )
273 {
274 m_dirty = false;
275 }
276
277 return success;
278}
279
280
281JOBSET_DESTINATION* JOBSET::FindDestination( wxString& aDestinationStr )
282{
283 auto is_matching_dest = [&]( const JOBSET_DESTINATION& destination )
284 {
285 if( destination.m_id == aDestinationStr || destination.m_description == aDestinationStr )
286 return true;
287
288 return false;
289 };
290
291 auto count = std::count_if( m_destinations.begin(), m_destinations.end(), is_matching_dest );
292
293 // we want to intentionally fail if more than one matching dest exists
294 // as theres no good way to handle it
295 if( count != 1 )
296 return nullptr;
297
298 auto it = std::find_if( m_destinations.begin(), m_destinations.end(), is_matching_dest );
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
@ small_folder
wxString m_fileNameWithoutPath
Definition: jobset.h:188
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:269
void SetDirty(bool aFlag=true)
Definition: jobset.h:167
void MoveJobUp(size_t aJobIdx)
Definition: jobset.cpp:242
std::vector< JOBSET_DESTINATION > m_destinations
Definition: jobset.h:185
std::vector< JOBSET_JOB > GetJobsForDestination(JOBSET_DESTINATION *aDestination)
Definition: jobset.cpp:307
std::vector< JOBSET_JOB > m_jobs
Definition: jobset.h:184
void RemoveJob(size_t aJobIdx)
Definition: jobset.cpp:262
void AddNewJob(wxString aType, JOB *aJob)
Definition: jobset.cpp:216
void MoveJobDown(size_t aJobIdx)
Definition: jobset.cpp:252
JOBSET_DESTINATION * AddNewDestination(JOBSET_DESTINATION_T aType)
Definition: jobset.cpp:223
wxString getFileExt() const override
Definition: jobset.cpp:210
bool m_dirty
Definition: jobset.h:187
JOBSET(const wxString &aFilename)
Definition: jobset.cpp:199
void RemoveDestination(JOBSET_DESTINATION *aDestination)
Definition: jobset.cpp:232
JOBSET_DESTINATION * FindDestination(wxString &aDestinationStr)
Attempts to find a destination based on the given string Both the uuid of the destination and descrip...
Definition: jobset.cpp:281
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:84
KICOMMON_API void from_json(const nlohmann::json &j, JOBSET_JOB &f)
Definition: jobset.cpp:67
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:49
const int jobsFileSchemaVersion
Definition: jobset.cpp:37
KICOMMON_API std::map< JOBSET_DESTINATION_T, JOBSET_DESTINATION_T_INFO > JobsetDestinationTypeInfos
Definition: jobset.cpp:40
enum KICOMMON_API JOBSET_DESTINATION_T
Definition: jobset.h:97
FOLDER
Definition: jobset.h:98
SETTINGS_LOC
Definition: json_settings.h:54
#define KICOMMON_API
Definition: kicommon.h:28
void SetDescription(const wxString &aDescription)
Definition: jobset.cpp:163
std::shared_ptr< JOBS_OUTPUT_HANDLER > m_outputHandler
Definition: jobset.h:133
JOBSET_DESTINATION_T m_type
Definition: jobset.h:131
wxString m_description
Definition: jobset.h:132
std::vector< wxString > m_only
Transient property, not stored for now.
Definition: jobset.h:134
void InitOutputHandler()
Definition: jobset.cpp:138
wxString GetPathInfo() const
Definition: jobset.cpp:157
bool operator==(const JOBSET_DESTINATION &rhs) const
Definition: jobset.cpp:193
wxString GetDescription() const
Definition: jobset.cpp:151
wxString m_id
Definition: jobset.h:130
wxString m_id
Definition: jobset.h:84
std::shared_ptr< JOB > m_job
Definition: jobset.h:87
wxString m_description
Definition: jobset.h:86
wxString m_type
Definition: jobset.h:85
bool operator==(const JOBSET_JOB &rhs) const
Definition: jobset.cpp:172
wxString GetDescription() const
Definition: jobset.cpp:178
void SetDescription(const wxString &aDescription)
Definition: jobset.cpp:184
Definition of file extensions used in Kicad.