KiCad PCB EDA Suite
Loading...
Searching...
No Matches
job.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) 2023 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 <jobs/job.h>
22#include <wx/filename.h>
23#include <common.h>
24#include <project.h>
25
26JOB::JOB( const std::string& aType, bool aOutputIsDirectory ) :
27 m_type( aType ),
31 m_outputPathIsDirectory( aOutputIsDirectory ),
34{
35 m_params.emplace_back( new JOB_PARAM<wxString>( "description",
37
39 {
40 m_params.emplace_back( new JOB_PARAM<wxString>( "output_dir",
42 }
43 else
44 {
45 m_params.emplace_back( new JOB_PARAM<wxString>( "output_filename",
47 }
48}
49
50
52{
53 for( JOB_PARAM_BASE* param : m_params )
54 delete param;
55
56 m_params.clear();
57}
58
59
60void JOB::FromJson( const nlohmann::json& j )
61{
62 for( JOB_PARAM_BASE* param : m_params )
63 param->FromJson( j );
64}
65
66
67void JOB::ToJson( nlohmann::json& j ) const
68{
69 for( JOB_PARAM_BASE* param : m_params )
70 param->ToJson( j );
71}
72
73
75{
76 return wxEmptyString;
77}
78
79
81{
82 return _( "Job Settings" );
83}
84
85
86void JOB::SetTempOutputDirectory( const wxString& aBase )
87{
89}
90
91
92void PrependDirectoryToPath( wxFileName& aFileName, const wxString aDirPath )
93{
94 wxFileName fn( aDirPath + wxFileName::GetPathSeparator() + aFileName.GetFullPath() );
95
96 aFileName = fn;
97}
98
99
100wxString JOB::ResolveOutputPath( const wxString& aPath, bool aPathIsDirectory, PROJECT* aProject ) const
101{
102 std::function<bool( wxString* )> textResolver =
103 [&]( wxString* token ) -> bool
104 {
105 if( m_titleBlock.TextVarResolver( token, aProject, INTERNAL ) )
106 return true;
107 if( aProject )
108 return aProject->TextVarResolver( token );
109 return false;
110 };
111
112 wxString outPath = ExpandPathVariables( aPath, aProject, &textResolver );
113
114 if( !m_tempOutputDirectory.IsEmpty() )
115 {
116 if( aPathIsDirectory )
117 {
118 wxFileName fn( outPath );
119
120 if( fn.IsAbsolute() || outPath.IsEmpty() )
121 fn.AssignDir( m_tempOutputDirectory );
122 else
124
125 return fn.GetFullPath();
126 }
127 else
128 {
129 wxFileName fn( outPath );
130 if( fn.IsAbsolute() )
131 {
132 // uhhh, do nothing
133 // its a full path passed by cli, so we return as-is
134 // the job handlers should have fixed empty paths
135 }
136 else
137 {
139 }
140
141 return fn.GetFullPath();
142 }
143 }
144
145 return outPath;
146}
147
148
149wxString JOB::GetFullOutputPath( PROJECT* aProject ) const
150{
152 m_outputPathIsDirectory, aProject );
153}
154
155
156void JOB::SetConfiguredOutputPath( const wxString& aPath )
157{
158 m_outputPath = aPath;
159
160 // A newly configured path must take precedence over any transient working path left over
161 // from a prior run that fell back to a generated filename.
162 m_workingOutputPath.clear();
163}
164
165
166KICOMMON_API void to_json( nlohmann::json& j, const JOB& f )
167{
168 f.ToJson( j );
169}
170
171
172KICOMMON_API void from_json( const nlohmann::json& j, JOB& f )
173{
174 f.FromJson( j );
175}
176
177
178JOB_PARAM_BASE::JOB_PARAM_BASE( const std::string& aJsonPath ) :
179 m_jsonPath( aJsonPath )
180{
181}
std::string m_jsonPath
Definition job.h:47
JOB_PARAM_BASE(const std::string &aJsonPath)
Definition job.cpp:178
An simple container class that lets us dispatch output jobs to kifaces.
Definition job.h:184
void SetConfiguredOutputPath(const wxString &aPath)
Sets the configured output path for the job, this path is always saved to file.
Definition job.cpp:156
virtual void FromJson(const nlohmann::json &j)
Definition job.cpp:60
std::vector< JOB_PARAM_BASE * > m_params
Definition job.h:277
JOB(const std::string &aType, bool aOutputIsDirectory)
Definition job.cpp:26
TITLE_BLOCK m_titleBlock
Definition job.h:263
wxString ResolveOutputPath(const wxString &aPath, bool aPathIsDirectory, PROJECT *aProject) const
Definition job.cpp:100
wxString GetFullOutputPath(PROJECT *aProject) const
Returns the full output path for the job, taking into account the configured output path,...
Definition job.cpp:149
virtual wxString GetDefaultDescription() const
Definition job.cpp:74
wxString m_workingOutputPath
The working output path is a transient path that takes priority over the configured output path when ...
Definition job.h:275
std::string m_type
Definition job.h:261
wxString m_tempOutputDirectory
Definition job.h:265
virtual wxString GetSettingsDialogTitle() const
Definition job.cpp:80
wxString m_description
Definition job.h:269
virtual ~JOB()
Definition job.cpp:51
bool m_outputPathIsDirectory
Definition job.h:268
virtual void ToJson(nlohmann::json &j) const
Definition job.cpp:67
void SetTempOutputDirectory(const wxString &aBase)
Sets the temporary output directory for the job, this is used to prefix with a given output path when...
Definition job.cpp:86
std::map< wxString, wxString > m_varOverrides
Definition job.h:262
wxString m_outputPath
Definition job.h:267
Container for project specific data.
Definition project.h:63
virtual bool TextVarResolver(wxString *aToken) const
Definition project.cpp:177
wxString ExpandPathVariables(const wxString &aPath, const PROJECT *aProject, const std::function< bool(wxString *)> *aTextResolver)
Expand text variables, environment variables and a leading '~' in a user-entered file path.
Definition common.cpp:798
@ INTERNAL
Definition common.h:96
#define _(s)
void PrependDirectoryToPath(wxFileName &aFileName, const wxString aDirPath)
Definition job.cpp:92
KICOMMON_API void to_json(nlohmann::json &j, const JOB &f)
Definition job.cpp:166
KICOMMON_API void from_json(const nlohmann::json &j, JOB &f)
Definition job.cpp:172
#define KICOMMON_API
Definition kicommon.h:27