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 along
18 * with this program. If not, see <http://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 ) )
106 return true;
107 if( aProject )
108 return aProject->TextVarResolver( token );
109 return false;
110 };
111
112 wxString outPath = aPath;
113
114 // Normalize backslash path separators to forward slashes before expanding text variables.
115 // ExpandTextVars treats \${ as an escape sequence, which misinterprets Windows paths like
116 // "subdir\${REVISION}_file.txt" where the backslash is a path separator, not an escape.
117 outPath.Replace( wxT( "\\" ), wxT( "/" ) );
118
119 outPath = ExpandTextVars( outPath, &textResolver );
120
121 if( !m_tempOutputDirectory.IsEmpty() )
122 {
123 if( aPathIsDirectory )
124 {
125 wxFileName fn( outPath );
126
127 if( fn.IsAbsolute() || outPath.IsEmpty() )
128 fn.AssignDir( m_tempOutputDirectory );
129 else
131
132 return fn.GetFullPath();
133 }
134 else
135 {
136 wxFileName fn( outPath );
137 if( fn.IsAbsolute() )
138 {
139 // uhhh, do nothing
140 // its a full path passed by cli, so we return as-is
141 // the job handlers should have fixed empty paths
142 }
143 else
144 {
146 }
147
148 return fn.GetFullPath();
149 }
150 }
151
152 return outPath;
153}
154
155
156wxString JOB::GetFullOutputPath( PROJECT* aProject ) const
157{
159 m_outputPathIsDirectory, aProject );
160}
161
162
163void JOB::SetConfiguredOutputPath( const wxString& aPath )
164{
165 m_outputPath = aPath;
166
167 // A newly configured path must take precedence over any transient working path left over
168 // from a prior run that fell back to a generated filename.
169 m_workingOutputPath.clear();
170}
171
172
173KICOMMON_API void to_json( nlohmann::json& j, const JOB& f )
174{
175 f.ToJson( j );
176}
177
178
179KICOMMON_API void from_json( const nlohmann::json& j, JOB& f )
180{
181 f.FromJson( j );
182}
183
184
185JOB_PARAM_BASE::JOB_PARAM_BASE( const std::string& aJsonPath ) :
186 m_jsonPath( aJsonPath )
187{
188}
std::string m_jsonPath
Definition job.h:47
JOB_PARAM_BASE(const std::string &aJsonPath)
Definition job.cpp:185
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:163
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:156
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:66
virtual bool TextVarResolver(wxString *aToken) const
Definition project.cpp:85
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
Definition common.cpp:63
The common library.
#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:173
KICOMMON_API void from_json(const nlohmann::json &j, JOB &f)
Definition job.cpp:179
#define KICOMMON_API
Definition kicommon.h:27