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#include <wx/filename.h>
34
35#include <algorithm>
36#include <memory>
37
39
40
41KICOMMON_API std::map<JOBSET_DESTINATION_T, JOBSET_DESTINATION_T_INFO> JobsetDestinationTypeInfos =
42{
43 { JOBSET_DESTINATION_T::FOLDER,
44 { _HKI( "Folder" ), BITMAPS::small_folder, true, "" } },
45 { JOBSET_DESTINATION_T::ARCHIVE,
46 { _HKI( "Archive" ), BITMAPS::zip, false, FILEEXT::ZipFileWildcard() } },
47};
48
49
51 {
52 { JOBSET_DESTINATION_T::FOLDER, "folder" },
53 { JOBSET_DESTINATION_T::ARCHIVE, "archive" }
54 } )
55
56KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_JOB& f )
57{
58 j = nlohmann::json{ { "id", f.m_id },
59 { "type", f.m_type },
60 { "description", f.m_description },
61 { "settings", nlohmann::json::object( {} ) }
62 };
63
64 f.m_job->ToJson( j.at( "settings" ) );
65}
66
67
68KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_JOB& f )
69{
70 j.at( "type" ).get_to( f.m_type );
71 j.at( "id" ).get_to( f.m_id );
72 f.m_description = j.value( "description", "" );
73
74 nlohmann::json settings_obj = j.at( "settings" );
75
77
78 if( f.m_job != nullptr )
79 {
80 f.m_job->FromJson( settings_obj );
81 }
82}
83
84
85KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_DESTINATION& destination )
86{
87 j = nlohmann::json{ { "id", destination.m_id },
88 { "type", destination.m_type },
89 { "only", destination.m_only },
90 { "description", destination.m_description },
91 { "settings", nlohmann::json::object( {} ) }
92 };
93
94 destination.m_outputHandler->ToJson( j.at( "settings" ) );
95}
96
97
98KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_DESTINATION& destination )
99{
100 // During 9.0 development outputs didn't get ids.
101 if( j.contains( "id" ) )
102 j.at( "id" ).get_to( destination.m_id );
103 else
104 destination.m_id = KIID().AsString();
105
106 j.at( "type" ).get_to( destination.m_type );
107 destination.m_only = j.value( "only", std::vector<wxString>() );
108 destination.m_description = j.value( "description", "" );
109
110 const nlohmann::json& settings_obj = j.at( "settings" );
111
112 destination.InitOutputHandler();
113
114 if( destination.m_outputHandler != nullptr )
115 destination.m_outputHandler->FromJson( settings_obj );
116}
117
118
126
127
129 m_id( id ),
130 m_type( type ),
131 m_outputHandler( nullptr ),
134{
136}
137
138
140{
141 if( m_type == JOBSET_DESTINATION_T::FOLDER )
142 {
143 m_outputHandler = std::make_shared<JOBS_OUTPUT_FOLDER>( );
144 }
145 else if( m_type == JOBSET_DESTINATION_T::ARCHIVE )
146 {
147 m_outputHandler = std::make_shared<JOBS_OUTPUT_ARCHIVE>( );
148 }
149}
150
151
153{
154 return m_description.IsEmpty() ? m_outputHandler->GetDefaultDescription() : m_description;
155}
156
157
159{
160 return m_outputHandler->GetOutputPath();
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
wxString m_fileNameWithoutPath
Definition jobset.h:192
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:171
void MoveJobUp(size_t aJobIdx)
Definition jobset.cpp:242
std::vector< JOBSET_DESTINATION > m_destinations
Definition jobset.h:189
std::vector< JOBSET_JOB > GetJobsForDestination(JOBSET_DESTINATION *aDestination)
Definition jobset.cpp:307
std::vector< JOBSET_JOB > m_jobs
Definition jobset.h:188
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:191
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
virtual void ToJson(nlohmann::json &j) const =0
virtual void FromJson(const nlohmann::json &j)=0
static T * CreateInstance(const wxString &aName)
An simple container class that lets us dispatch output jobs to kifaces.
Definition job.h:183
virtual void FromJson(const nlohmann::json &j)
Definition job.cpp:60
virtual void ToJson(nlohmann::json &j) const
Definition job.cpp:67
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
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
@ 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:85
KICOMMON_API void from_json(const nlohmann::json &j, JOBSET_JOB &f)
Definition jobset.cpp:68
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:50
const int jobsFileSchemaVersion
Definition jobset.cpp:38
KICOMMON_API std::map< JOBSET_DESTINATION_T, JOBSET_DESTINATION_T_INFO > JobsetDestinationTypeInfos
Definition jobset.cpp:41
enum KICOMMON_API JOBSET_DESTINATION_T
Definition jobset.h:100
FOLDER
Definition jobset.h:101
SETTINGS_LOC
#define KICOMMON_API
Definition kicommon.h:28
#define _HKI(x)
Definition page_info.cpp:44
std::unordered_map< wxString, std::shared_ptr< JOBSET_OUTPUT_REPORTER > > m_lastRunReporters
Definition jobset.h:142
void SetDescription(const wxString &aDescription)
Definition jobset.cpp:163
std::shared_ptr< JOBS_OUTPUT_HANDLER > m_outputHandler
Definition jobset.h:136
JOBSET_DESTINATION_T m_type
Definition jobset.h:134
wxString m_description
Definition jobset.h:135
std::vector< wxString > m_only
Transient property, not stored for now.
Definition jobset.h:137
void InitOutputHandler()
Definition jobset.cpp:139
wxString GetPathInfo() const
Definition jobset.cpp:158
bool operator==(const JOBSET_DESTINATION &rhs) const
Definition jobset.cpp:193
wxString GetDescription() const
Definition jobset.cpp:152
std::optional< bool > m_lastRunSuccess
Definition jobset.h:140
wxString m_id
Definition jobset.h:133
wxString m_id
Definition jobset.h:87
std::shared_ptr< JOB > m_job
Definition jobset.h:90
wxString m_description
Definition jobset.h:89
wxString m_type
Definition jobset.h:88
bool operator==(const JOBSET_JOB &rhs) const
Definition jobset.cpp:172
JOBSET_JOB()
Definition jobset.h:77
wxString GetDescription() const
Definition jobset.cpp:178
void SetDescription(const wxString &aDescription)
Definition jobset.cpp:184
wxString result
Test unit parsing edge cases and error handling.
Definition of file extensions used in Kicad.