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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <i18n_utility.h>
22#include <json_common.h>
23
25#include <settings/parameters.h>
27
28#include <jobs/jobset.h>
29#include <jobs/job_registry.h>
30#include <jobs/job_unknown.h>
33#include <kiid.h>
34#include <reporter.h>
35#include <wx/filename.h>
36
37#include <algorithm>
38#include <memory>
39
41
42
43KICOMMON_API std::map<JOBSET_DESTINATION_T, JOBSET_DESTINATION_T_INFO> JobsetDestinationTypeInfos =
44{
45 { JOBSET_DESTINATION_T::FOLDER,
46 { _HKI( "Folder" ), BITMAPS::small_folder, true, "" } },
47 { JOBSET_DESTINATION_T::ARCHIVE,
48 { _HKI( "Archive" ), BITMAPS::zip, false, FILEEXT::ZipFileWildcard() } },
49};
50
51
53 {
54 { JOBSET_DESTINATION_T::FOLDER, "folder" },
55 { JOBSET_DESTINATION_T::ARCHIVE, "archive" }
56 } )
57
58KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_JOB& f )
59{
60 j = nlohmann::json{ { "id", f.m_id },
61 { "type", f.m_type },
62 { "description", f.m_description },
63 { "settings", nlohmann::json::object( {} ) }
64 };
65
66 f.m_job->ToJson( j.at( "settings" ) );
67}
68
69
70KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_JOB& f )
71{
72 j.at( "type" ).get_to( f.m_type );
73 j.at( "id" ).get_to( f.m_id );
74 f.m_description = j.value( "description", "" );
75
76 nlohmann::json settings_obj = j.at( "settings" );
77
79
80 if( f.m_job != nullptr )
81 f.m_job->FromJson( settings_obj );
82 else
83 f.m_job = std::make_shared<JOB_UNKNOWN>( f.m_type.ToStdString(), settings_obj );
84}
85
86
87KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_DESTINATION& destination )
88{
89 j = nlohmann::json{ { "id", destination.m_id },
90 { "type", destination.m_type },
91 { "only", destination.m_only },
92 { "description", destination.m_description },
93 { "settings", nlohmann::json::object( {} ) }
94 };
95
96 destination.m_outputHandler->ToJson( j.at( "settings" ) );
97}
98
99
100KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_DESTINATION& destination )
101{
102 // During 9.0 development outputs didn't get ids.
103 if( j.contains( "id" ) )
104 j.at( "id" ).get_to( destination.m_id );
105 else
106 destination.m_id = KIID().AsString();
107
108 j.at( "type" ).get_to( destination.m_type );
109 destination.m_only = j.value( "only", std::vector<wxString>() );
110 destination.m_description = j.value( "description", "" );
111
112 const nlohmann::json& settings_obj = j.at( "settings" );
113
114 destination.InitOutputHandler();
115
116 if( destination.m_outputHandler != nullptr )
117 destination.m_outputHandler->FromJson( settings_obj );
118}
119
120
128
129
131 m_id( id ),
132 m_type( type ),
133 m_outputHandler( nullptr ),
136{
138}
139
140
142{
143 if( m_type == JOBSET_DESTINATION_T::FOLDER )
144 {
145 m_outputHandler = std::make_shared<JOBS_OUTPUT_FOLDER>( );
146 }
147 else if( m_type == JOBSET_DESTINATION_T::ARCHIVE )
148 {
149 m_outputHandler = std::make_shared<JOBS_OUTPUT_ARCHIVE>( );
150 }
151}
152
153
155{
156 return m_description.IsEmpty() ? m_outputHandler->GetDefaultDescription() : m_description;
157}
158
159
161{
162 return m_outputHandler->GetOutputPath();
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
283JOBSET_DESTINATION* JOBSET::FindDestination( wxString& aDestinationStr )
284{
285 auto is_matching_dest = [&]( const JOBSET_DESTINATION& destination )
286 {
287 if( destination.m_id == aDestinationStr || destination.m_description == aDestinationStr )
288 return true;
289
290 return false;
291 };
292
293 auto count = std::count_if( m_destinations.begin(), m_destinations.end(), is_matching_dest );
294
295 // we want to intentionally fail if more than one matching dest exists
296 // as theres no good way to handle it
297 if( count != 1 )
298 return nullptr;
299
300 auto it = std::find_if( m_destinations.begin(), m_destinations.end(), is_matching_dest );
301
302 if( it != m_destinations.end() )
303 return &(*it);
304
305 return nullptr;
306}
307
308
309std::vector<JOBSET_JOB> JOBSET::GetJobsForDestination( JOBSET_DESTINATION* aDestination )
310{
311 wxASSERT( aDestination != nullptr );
312
313 if( aDestination->m_only.size() == 0 )
314 {
315 return m_jobs;
316 }
317
318 std::vector<JOBSET_JOB> result;
319 for( wxString& onlyId : aDestination->m_only )
320 {
321 auto it = std::find_if( m_jobs.begin(), m_jobs.end(),
322 [&]( const JOBSET_JOB& job )
323 {
324 if( job.m_id == onlyId )
325 return true;
326
327 return false;
328 } );
329
330 if( it != m_jobs.end() )
331 result.push_back( *it );
332 }
333
334 return result;
335}
336
337
338#if !defined( __MINGW32__ )
341#endif
wxString m_fileNameWithoutPath
Definition jobset.h:193
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:172
void MoveJobUp(size_t aJobIdx)
Definition jobset.cpp:244
std::vector< JOBSET_DESTINATION > m_destinations
Definition jobset.h:190
std::vector< JOBSET_JOB > GetJobsForDestination(JOBSET_DESTINATION *aDestination)
Definition jobset.cpp:309
std::vector< JOBSET_JOB > m_jobs
Definition jobset.h:189
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:192
JOBSET(const wxString &aFilename)
Definition jobset.cpp:201
void RemoveDestination(JOBSET_DESTINATION *aDestination)
Definition jobset.cpp:234
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:283
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:184
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:46
wxString AsString() const
Definition kiid.cpp:264
@ NONE
Definition eda_fill.h:42
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:87
KICOMMON_API void from_json(const nlohmann::json &j, JOBSET_JOB &f)
Definition jobset.cpp:70
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:52
const int jobsFileSchemaVersion
Definition jobset.cpp:40
KICOMMON_API std::map< JOBSET_DESTINATION_T, JOBSET_DESTINATION_T_INFO > JobsetDestinationTypeInfos
Definition jobset.cpp:43
enum KICOMMON_API JOBSET_DESTINATION_T
Definition jobset.h:101
FOLDER
Definition jobset.h:102
SETTINGS_LOC
#define KICOMMON_API
Definition kicommon.h:27
#define _HKI(x)
Definition page_info.cpp:40
std::unordered_map< wxString, std::shared_ptr< JOBSET_OUTPUT_REPORTER > > m_lastRunReporters
Definition jobset.h:143
void SetDescription(const wxString &aDescription)
Definition jobset.cpp:165
std::shared_ptr< JOBS_OUTPUT_HANDLER > m_outputHandler
Definition jobset.h:137
JOBSET_DESTINATION_T m_type
Definition jobset.h:135
wxString m_description
Definition jobset.h:136
std::vector< wxString > m_only
Transient property, not stored for now.
Definition jobset.h:138
void InitOutputHandler()
Definition jobset.cpp:141
wxString GetPathInfo() const
Definition jobset.cpp:160
bool operator==(const JOBSET_DESTINATION &rhs) const
Definition jobset.cpp:195
wxString GetDescription() const
Definition jobset.cpp:154
std::optional< bool > m_lastRunSuccess
Definition jobset.h:141
wxString m_id
Definition jobset.h:134
wxString m_id
Definition jobset.h:88
std::shared_ptr< JOB > m_job
Definition jobset.h:91
wxString m_description
Definition jobset.h:90
wxString m_type
Definition jobset.h:89
bool operator==(const JOBSET_JOB &rhs) const
Definition jobset.cpp:174
JOBSET_JOB()
Definition jobset.h:78
wxString GetDescription() const
Definition jobset.cpp:180
void SetDescription(const wxString &aDescription)
Definition jobset.cpp:186
wxString result
Test unit parsing edge cases and error handling.
Definition of file extensions used in Kicad.