KiCad PCB EDA Suite
Loading...
Searching...
No Matches
env_vars.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <build_version.h>
21#include <env_vars.h>
23#include <core/kicad_algo.h>
24
25#include <algorithm>
26#include <map>
27
28#include <wx/regex.h>
29#include <wx/translation.h>
30#include <wx/utils.h>
31
38static const std::vector<wxString> predefinedEnvVars = {
39 wxS( "KIPRJMOD" ),
40 ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ),
41 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ),
42 ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ),
43 ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ),
44 wxS( "KICAD_USER_TEMPLATE_DIR" ),
45 wxS( "KICAD_PTEMPLATES" ),
46 ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ),
47};
48
49
50bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar )
51{
52 for( const wxString& s : predefinedEnvVars )
53 {
54 if( s == aEnvVar )
55 return true;
56 }
57
58 return false;
59}
60
61
62const std::vector<wxString>& ENV_VAR::GetPredefinedEnvVars()
63{
64 return predefinedEnvVars;
65}
66
67
68void ENV_VAR::GetEnvVarAutocompleteTokens( wxArrayString* aVars )
69{
70 for( const wxString& var : ENV_VAR::GetPredefinedEnvVars() )
71 {
72 if( !alg::contains( *aVars, var ) )
73 aVars->push_back( var );
74 }
75}
76
77
78wxString ENV_VAR::GetVersionedEnvVarName( const wxString& aBaseName )
79{
80 int version = 0;
81 std::tie(version, std::ignore, std::ignore) = GetMajorMinorPatchTuple();
82
83 return wxString::Format( "KICAD%d_%s", version, aBaseName );
84}
85
86
87bool ENV_VAR::IsVersionedEnvVar( const wxString& aName, const wxString& aBaseName )
88{
89 static const wxString prefix = wxS( "KICAD" );
90 const wxString suffix = wxS( "_" ) + aBaseName;
91
92 if( !aName.StartsWith( prefix ) || !aName.EndsWith( suffix ) )
93 return false;
94
95 wxString version = aName.Mid( prefix.length(), aName.length() - prefix.length() - suffix.length() );
96
97 return !version.IsEmpty()
98 && std::all_of( version.begin(), version.end(),
99 []( wxUniChar c ) { return wxIsdigit( c ); } );
100}
101
102
103std::optional<wxString> ENV_VAR::GetVersionedEnvVarValue( const ENV_VAR_MAP& aMap,
104 const wxString& aBaseName )
105{
106 wxString exactMatch = ENV_VAR::GetVersionedEnvVarName( aBaseName );
107
108 if( aMap.count( exactMatch ) )
109 return aMap.at( exactMatch ).GetValue();
110
111 for( const auto& [k, v] : aMap )
112 {
113 if( ENV_VAR::IsVersionedEnvVar( k, aBaseName ) )
114 return v.GetValue();
115 }
116
117 return std::nullopt;
118}
119
120
121static void initialiseEnvVarHelp( std::map<wxString, wxString>& aMap )
122{
123 // Set up dynamically, as we want to be able to use _() translations,
124 // which can't be done statically
125 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) )] =
126 _( "The base path of locally installed system footprint libraries (.pretty folders)." );
127
128 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) )] =
129 _( "The base path of system footprint 3D shapes (.3Dshapes folders)." );
130
131 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) )] =
132 _( "The base path of the locally installed symbol libraries." );
133
134 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) )] =
135 _( "A directory containing project templates installed with KiCad." );
136
137 aMap[wxS( "KICAD_USER_TEMPLATE_DIR" )] =
138 _( "Optional. Can be defined if you want to create your own project templates folder." );
139
140 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) )] =
141 _( "A directory containing 3rd party plugins, libraries and other downloadable content." );
142
143 aMap[wxS( "KIPRJMOD" )] =
144 _( "Internally defined by KiCad (cannot be edited) and is set to the absolute path of the currently "
145 "loaded project file. This environment variable can be used to define files and paths relative "
146 "to the currently loaded project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be "
147 "defined as a folder containing a project specific footprint library named footprints.pretty." );
148
149 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SCRIPTING_DIR" ) )] =
150 _( "A directory containing system-wide scripts installed with KiCad." );
151
152 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "USER_SCRIPTING_DIR" ) )] =
153 _( "A directory containing user-specific scripts installed with KiCad." );
154
155 // Deprecated vars
156#define DEP( var ) wxString::Format( _( "Deprecated version of %s." ), var )
157
158 aMap[wxS( "KICAD_PTEMPLATES" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ) );
159 aMap[wxS( "KISYS3DMOD" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
160 aMap[wxS( "KISYSMOD" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ) );
161 aMap[wxS( "KICAD_SYMBOL_DIR" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ) );
162
163#undef DEP
164}
165
166
167wxString ENV_VAR::LookUpEnvVarHelp( const wxString& aEnvVar )
168{
169 static std::map<wxString, wxString> envVarHelpText;
170
171 if( envVarHelpText.size() == 0 )
172 initialiseEnvVarHelp( envVarHelpText );
173
174 return envVarHelpText[ aEnvVar ];
175}
176
177
178template<>
179std::optional<double> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
180{
181 wxString env;
182
183 if( wxGetEnv( aEnvVarName, &env ) )
184 {
185 double value;
186
187 if( env.ToDouble( &value ) )
188 return value;
189 }
190
191 return std::nullopt;
192}
193
194
195template<>
196std::optional<wxString> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
197{
198 std::optional<wxString> optValue;
199 wxString env;
200
201 if( wxGetEnv( aEnvVarName, &env ) )
202 optValue = env;
203
204 return optValue;
205}
const std::tuple< int, int, int > & GetMajorMinorPatchTuple()
Get the build version numbers as a tuple.
#define _(s)
static void initialiseEnvVarHelp(std::map< wxString, wxString > &aMap)
Definition env_vars.cpp:121
#define DEP(var)
static const std::vector< wxString > predefinedEnvVars
List of pre-defined environment variables.
Definition env_vars.cpp:38
Functions related to environment variables, including help functions.
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
KICOMMON_API wxString LookUpEnvVarHelp(const wxString &aEnvVar)
Look up long-form help text for a given environment variable.
Definition env_vars.cpp:167
KICOMMON_API void GetEnvVarAutocompleteTokens(wxArrayString *aVars)
Return autocomplete tokens for environment variables for Scintilla.
Definition env_vars.cpp:68
KICOMMON_API std::optional< VAL_TYPE > GetEnvVar(const wxString &aEnvVarName)
Get an environment variable as a specific type, if set correctly.
KICOMMON_API bool IsVersionedEnvVar(const wxString &aName, const wxString &aBaseName)
Test whether a name is a versioned KiCad environment variable for the given base, i....
Definition env_vars.cpp:87
KICOMMON_API std::optional< wxString > GetVersionedEnvVarValue(const std::map< wxString, ENV_VAR_ITEM > &aMap, const wxString &aBaseName)
Attempt to retrieve the value of a versioned environment variable, such as KICAD8_TEMPLATE_DIR.
Definition env_vars.cpp:103
KICOMMON_API const std::vector< wxString > & GetPredefinedEnvVars()
Get the list of pre-defined environment variables.
Definition env_vars.cpp:62
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct a versioned environment variable based on this KiCad major version.
Definition env_vars.cpp:78
KICOMMON_API bool IsEnvVarImmutable(const wxString &aEnvVar)
Determine if an environment variable is "predefined", i.e.
Definition env_vars.cpp:50
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96