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 (C) 2024 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 <nlohmann/json.hpp>
22
23#include <settings/parameters.h>
25
26#include <jobs/jobset.h>
27#include <jobs/job_registry.h>
30#include <kiid.h>
31
32#include <algorithm>
33
35
37 {
38 { JOBSET_OUTPUT_TYPE::FOLDER, "folder" },
39 { JOBSET_OUTPUT_TYPE::ARCHIVE, "archive" }
40 } )
41
42KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_JOB& f )
43{
44 j = nlohmann::json{ { "id", f.m_id },
45 { "type", f.m_type },
46 { "settings", nlohmann::json::object( {} ) }
47 };
48
49 f.m_job->ToJson( j.at( "settings" ) );
50}
51
52
53KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_JOB& f )
54{
55 j.at( "type" ).get_to( f.m_type );
56 j.at( "id" ).get_to( f.m_id );
57
58 nlohmann::json settings_obj = j.at( "settings" );
59
60 f.m_job.reset( JOB_REGISTRY::CreateInstance<JOB>( f.m_type ) );
61
62 if( f.m_job != nullptr )
63 {
64 f.m_job->FromJson( settings_obj );
65 }
66}
67
68
69KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_OUTPUT& f )
70{
71 j = nlohmann::json{ { "type", f.m_type }, { "settings", nlohmann::json::object( {} ) } };
72
73 f.m_outputHandler->ToJson( j.at( "settings" ) );
74}
75
76
77KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_OUTPUT& f )
78{
79 j.at( "type" ).get_to( f.m_type );
80 f.m_only = j.value( "only", std::vector<wxString>() );
81
82 nlohmann::json settings_obj = j.at( "settings" );
83
85
86 if( f.m_outputHandler != nullptr )
87 {
88 f.m_outputHandler->FromJson( settings_obj );
89 }
90}
91
92
94 m_type( JOBSET_OUTPUT_TYPE::FOLDER ),
95 m_lastRunSuccess()
96{
97}
98
99
101 m_id( id ),
102 m_type( type ),
103 m_lastRunSuccess()
104{
106}
107
108
110{
111 if( m_type == JOBSET_OUTPUT_TYPE::FOLDER )
112 {
114 }
115 else if( m_type == JOBSET_OUTPUT_TYPE::ARCHIVE )
116 {
118 }
119}
120
121
122bool JOBSET_JOB::operator==( const JOBSET_JOB & rhs ) const
123{
124 return rhs.m_type == m_type;
125}
126
127
129{
130 return rhs.m_type == m_type;
131}
132
133
134JOBSET::JOBSET( const wxString& aFilename ) :
136 m_dirty( false )
137{
138 m_params.emplace_back( new PARAM_LIST<JOBSET_JOB>( "jobs", &m_jobs, {} ) );
139 m_params.emplace_back( new PARAM_LIST<JOBSET_OUTPUT>( "outputs", &m_outputs, {} ) );
140
141 m_fileNameWithoutPath = wxFileName( aFilename ).GetFullName();
142}
143
144
145wxString JOBSET::getFileExt() const
146{
148}
149
150
151void JOBSET::AddNewJob( wxString aType, JOB* aJob )
152{
153 m_jobs.emplace_back( KIID().AsString(), aType, aJob );
154 SetDirty();
155}
156
157
159{
160 m_outputs.emplace_back( KIID().AsString(), aType);
161 SetDirty();
162
163 return &m_outputs.back();
164}
165
166
168{
169 std::erase_if( m_outputs,
170 [&]( JOBSET_OUTPUT const& output )
171 {
172 return output.m_id == aOutput->m_id;
173 } );
174}
175
176
177void JOBSET::MoveJobUp( size_t aJobIdx )
178{
179 if( aJobIdx > 0 )
180 {
181 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx - 1] );
182 SetDirty();
183 }
184}
185
186
187void JOBSET::MoveJobDown( size_t aJobIdx )
188{
189 if( aJobIdx < m_jobs.size() - 1 )
190 {
191 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx + 1] );
192 SetDirty();
193 }
194}
195
196
197void JOBSET::RemoveJob( size_t aJobIdx )
198{
199 m_jobs.erase( m_jobs.begin() + aJobIdx );
200 SetDirty();
201}
202
203
204bool JOBSET::SaveToFile( const wxString& aDirectory, bool aForce )
205{
206 bool success = JSON_SETTINGS::SaveToFile( aDirectory, aForce );
207 if( success )
208 {
209 m_dirty = false;
210 }
211
212 return success;
213}
214
215
216JOBSET_OUTPUT* JOBSET::GetOutput( wxString& aOutput )
217{
218 auto it = std::find_if( m_outputs.begin(), m_outputs.end(),
219 [&]( const JOBSET_OUTPUT& output )
220 {
221 if( output.m_id == aOutput )
222 return true;
223
224 return false;
225 } );
226
227 if( it != m_outputs.end() )
228 return &(*it);
229
230 return nullptr;
231}
232
233
234std::vector<JOBSET_JOB> JOBSET::GetJobsForOutput( JOBSET_OUTPUT* aOutput )
235{
236 wxASSERT( aOutput != nullptr );
237
238 if( aOutput->m_only.size() == 0 )
239 {
240 return m_jobs;
241 }
242
243 std::vector<JOBSET_JOB> result;
244 for( wxString& onlyId : aOutput->m_only )
245 {
246 auto it = std::find_if( m_jobs.begin(), m_jobs.end(),
247 [&]( const JOBSET_JOB& job )
248 {
249 if( job.m_id == onlyId )
250 return true;
251
252 return false;
253 } );
254
255 if( it != m_jobs.end() )
256 result.push_back( *it );
257 }
258
259 return result;
260}
261
262
263#if !defined( __MINGW32__ )
266#endif
wxString m_fileNameWithoutPath
Definition: jobset.h:112
std::vector< JOBSET_JOB > GetJobsForOutput(JOBSET_OUTPUT *aOutput)
Definition: jobset.cpp:234
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:204
void SetDirty()
Definition: jobset.h:91
void RemoveOutput(JOBSET_OUTPUT *aOutput)
Definition: jobset.cpp:167
void MoveJobUp(size_t aJobIdx)
Definition: jobset.cpp:177
JOBSET_OUTPUT * GetOutput(wxString &aOutput)
Definition: jobset.cpp:216
std::vector< JOBSET_JOB > m_jobs
Definition: jobset.h:108
void RemoveJob(size_t aJobIdx)
Definition: jobset.cpp:197
std::vector< JOBSET_OUTPUT > m_outputs
Definition: jobset.h:109
void AddNewJob(wxString aType, JOB *aJob)
Definition: jobset.cpp:151
void MoveJobDown(size_t aJobIdx)
Definition: jobset.cpp:187
wxString getFileExt() const override
Definition: jobset.cpp:145
bool m_dirty
Definition: jobset.h:111
JOBSET(const wxString &aFilename)
Definition: jobset.cpp:134
JOBSET_OUTPUT * AddNewJobOutput(JOBSET_OUTPUT_TYPE aType)
Definition: jobset.cpp:158
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:79
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
static const std::string KiCadJobSetFileExtension
NLOHMANN_JSON_SERIALIZE_ENUM(JOBSET_OUTPUT_TYPE, { { JOBSET_OUTPUT_TYPE::FOLDER, "folder" }, { JOBSET_OUTPUT_TYPE::ARCHIVE, "archive" } }) KICOMMON_API void to_json(nlohmann
Definition: jobset.cpp:36
KICOMMON_API void from_json(const nlohmann::json &j, JOBSET_JOB &f)
Definition: jobset.cpp:53
KICOMMON_API void to_json(nlohmann::json &j, const JOBSET_OUTPUT &f)
Definition: jobset.cpp:69
const int jobsFileSchemaVersion
Definition: jobset.cpp:34
JOBSET_OUTPUT_TYPE
Definition: jobset.h:45
SETTINGS_LOC
Definition: json_settings.h:54
#define KICOMMON_API
Definition: kicommon.h:28
@ NONE
No connection to this item.
wxString m_id
Definition: jobset.h:37
std::shared_ptr< JOB > m_job
Definition: jobset.h:39
wxString m_type
Definition: jobset.h:38
bool operator==(const JOBSET_JOB &rhs) const
Definition: jobset.cpp:122
std::vector< wxString > m_only
Transient property, not stored for now.
Definition: jobset.h:61
void InitOutputHandler()
Definition: jobset.cpp:109
wxString m_id
Definition: jobset.h:58
JOBS_OUTPUT_HANDLER * m_outputHandler
Definition: jobset.h:60
bool operator==(const JOBSET_OUTPUT &rhs) const
Definition: jobset.cpp:128
JOBSET_OUTPUT_TYPE m_type
Definition: jobset.h:59
Definition of file extensions used in Kicad.