KiCad PCB EDA Suite
Loading...
Searching...
No Matches
job_special_execute.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
22#include <jobs/job_registry.h>
23#include <common.h>
24#include <i18n_utility.h>
25#include <paths.h>
26#include <project.h>
29#include <wx/dir.h>
30#include <wx/filename.h>
31#include <wx/utils.h>
32
34 JOB( "special_execute", false ),
35 m_command(),
36 m_ignoreExitcode( false ),
37 m_recordOutput( true )
38{
39 m_params.emplace_back( new JOB_PARAM<wxString>( "command", &m_command, m_command ) );
40 m_params.emplace_back( new JOB_PARAM<bool>( "ignore_exit_code", &m_ignoreExitcode, m_ignoreExitcode ) );
41 m_params.emplace_back(
42 new JOB_PARAM<bool>( "record_output", &m_recordOutput, m_recordOutput ) );
43}
44
46{
47 return wxString( "Execute command" );
48}
49
50
51wxString FindBundledPython( const std::vector<wxString>& aToolDirs )
52{
53#ifdef __WXMSW__
54 const wxChar* names[] = { wxS( "python.exe" ) };
55#else
56 // A framework bin directory ships python3 only
57 const wxChar* names[] = { wxS( "python3" ), wxS( "python" ) };
58#endif
59
60 for( const wxString& dir : aToolDirs )
61 {
62 for( const wxChar* name : names )
63 {
64 wxFileName candidate( dir, name );
65
66 if( candidate.FileExists() )
67 return candidate.GetFullPath();
68 }
69 }
70
71 return wxEmptyString;
72}
73
74
75// After a rename the only file of its kind in the project folder is the one the user means
76static wxString onlyFileWithExt( const wxString& aDir, const wxString& aExt )
77{
78 wxArrayString files;
79 wxDir::GetAllFiles( aDir, &files, wxS( "*." ) + aExt, wxDIR_FILES );
80
81 return files.size() == 1 ? files[0] : wxString();
82}
83
84
85static wxString projectBoard( const PROJECT& aProject )
86{
87 wxString dir = aProject.GetProjectPath();
88 wxString ext = wxString::FromUTF8( FILEEXT::KiCadPcbFileExtension );
89 wxFileName board( dir, aProject.GetProjectName(), ext );
90
91 if( !board.FileExists() )
92 {
93 wxString only = onlyFileWithExt( dir, ext );
94
95 if( !only.IsEmpty() )
96 return only;
97 }
98
99 return board.GetFullPath();
100}
101
102
103static wxString projectSchematic( const PROJECT& aProject )
104{
105 wxString dir = aProject.GetProjectPath();
106 wxString ext = wxString::FromUTF8( FILEEXT::KiCadSchematicFileExtension );
107
108 for( const TOP_LEVEL_SHEET_INFO& sheet : aProject.GetProjectFile().GetTopLevelSheets() )
109 {
110 wxFileName top( sheet.filename );
111 top.MakeAbsolute( dir );
112
113 if( top.FileExists() )
114 return top.GetFullPath();
115 }
116
117 wxFileName schematic( dir, aProject.GetProjectName(), ext );
118
119 if( !schematic.FileExists() )
120 {
121 wxString only = onlyFileWithExt( dir, ext );
122
123 if( !only.IsEmpty() )
124 return only;
125 }
126
127 return schematic.GetFullPath();
128}
129
130
131std::map<wxString, wxString> GetExecuteBuiltinVariables( const PROJECT* aProject )
132{
133 std::map<wxString, wxString> vars;
134
136
137 if( python.IsEmpty() )
138 {
139#ifdef __WXMSW__
140 python = wxS( "python" );
141#else
142 python = wxS( "python3" );
143#endif
144 }
145
146 vars[EXEC_PYTHON_VAR_NAME] = python;
147
148 if( aProject && !aProject->GetProjectFullName().IsEmpty() )
149 {
150 vars[EXEC_PROJECT_PCB_VAR_NAME] = projectBoard( *aProject );
151 vars[EXEC_PROJECT_SCH_VAR_NAME] = projectSchematic( *aProject );
152 }
153
154 return vars;
155}
156
157
158std::map<wxString, wxString> GetExecuteBuiltinVariableHelp()
159{
160 return {
161 { EXEC_PYTHON_VAR_NAME, _( "Python interpreter shipped with KiCad" ) },
162 { EXEC_PROJECT_PCB_VAR_NAME, _( "Board file of the project" ) },
163 { EXEC_PROJECT_SCH_VAR_NAME, _( "Top-level schematic file of the project" ) },
164 };
165}
166
167
168wxString ExpandExecuteCommand( const wxString& aCommand, const PROJECT* aProject )
169{
170 wxString command = ExpandEnvVarSubstitutions( aCommand, aProject );
171
172 // Text substitution because other threads expand variables, so the environment stays untouched
173 // Done last so a user variable can refer to a built-in
174 for( const auto& [name, value] : GetExecuteBuiltinVariables( aProject ) )
175 {
176 wxString existing = name;
177
178 if( wxGetEnv( name, nullptr ) || ( aProject && aProject->TextVarResolver( &existing ) ) )
179 continue;
180
181 for( const wxString& form : { wxS( "${" ) + name + wxS( "}" ), wxS( "$(" ) + name + wxS( ")" ) } )
182 {
183 size_t pos = 0;
184
185 while( ( pos = command.find( form, pos ) ) != wxString::npos )
186 {
187 if( pos > 0 && command[pos - 1] == wxS( '\\' ) )
188 {
189 pos += form.length();
190 continue;
191 }
192
193 command.replace( pos, form.length(), value );
194 pos += value.length();
195 }
196 }
197 }
198
199 return command;
200}
201
202
203REGISTER_JOB( special_execute, _HKI( "Special: Execute Command" ), KIWAY::KIWAY_FACE_COUNT,
const char * name
wxString GetDefaultDescription() const override
std::vector< JOB_PARAM_BASE * > m_params
Definition job.h:285
JOB(const std::string &aType, bool aOutputIsDirectory)
Definition job.cpp:26
@ KIWAY_FACE_COUNT
Definition kiway.h:361
static std::vector< wxString > GetToolDirectories(const wxString &aExecutableDir)
Return the directories holding tools shipped alongside KiCad, most specific first.
Definition paths.cpp:689
static wxString GetExecutableDir()
Return the directory holding the running executable itself.
Definition paths.cpp:669
std::vector< TOP_LEVEL_SHEET_INFO > & GetTopLevelSheets()
Container for project specific data.
Definition project.h:63
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition project.cpp:271
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:277
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition project.cpp:289
virtual bool TextVarResolver(wxString *aToken) const
Definition project.cpp:178
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:201
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject, bool aAllowEscapes)
Replace any environment variable & text variable references with their values.
Definition common.cpp:777
#define _(s)
static const std::string KiCadSchematicFileExtension
static const std::string KiCadPcbFileExtension
Some functions to handle hotkeys in KiCad.
#define REGISTER_JOB(job_name, title, face, T)
std::map< wxString, wxString > GetExecuteBuiltinVariableHelp()
Describe each variable returned by GetExecuteBuiltinVariables() for the user.
static wxString projectSchematic(const PROJECT &aProject)
wxString ExpandExecuteCommand(const wxString &aCommand, const PROJECT *aProject)
Expand an Execute Command string, including the built-in variables.
wxString FindBundledPython(const std::vector< wxString > &aToolDirs)
Find the interpreter shipped with KiCad in the given tool directories.
static wxString projectBoard(const PROJECT &aProject)
std::map< wxString, wxString > GetExecuteBuiltinVariables(const PROJECT *aProject)
Return the variables an Execute Command job can use beyond the environment and project variables.
static wxString onlyFileWithExt(const wxString &aDir, const wxString &aExt)
#define EXEC_PROJECT_PCB_VAR_NAME
#define EXEC_PROJECT_SCH_VAR_NAME
#define EXEC_PYTHON_VAR_NAME
#define _HKI(x)
Definition page_info.cpp:40
Information about a top-level schematic sheet.
KIBIS top(path, &reporter)
Definition of file extensions used in Kicad.