KiCad PCB EDA Suite
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 (C) 2018-2021 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 along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <env_vars.h>
21 
22 #include <map>
23 
24 #include <wx/translation.h>
25 #include <wx/utils.h>
26 
27 using STRING_MAP = std::map<wxString, wxString>;
28 
29 /*
30  * List of pre-defined environment variables
31  *
32  * TODO - Instead of defining these values here,
33  * extract them from elsewhere in the program
34  * (where they are originally defined)
35  */
37  wxT( "KIPRJMOD" ),
38  wxT( "KICAD6_SYMBOL_DIR" ),
39  wxT( "KICAD6_3DMODEL_DIR" ),
40  wxT( "KICAD6_FOOTPRINT_DIR" ),
41  wxT( "KICAD6_TEMPLATE_DIR" ),
42  wxT( "KICAD_USER_TEMPLATE_DIR" ),
43  wxT( "KICAD_PTEMPLATES" ),
44  wxT( "KICAD6_3RD_PARTY" ),
45 };
46 
47 
48 bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar )
49 {
50  for( const auto& s : predefinedEnvVars )
51  {
52  if( s == aEnvVar )
53  return true;
54  }
55 
56  return false;
57 }
58 
59 
61 {
62  return predefinedEnvVars;
63 }
64 
65 
66 static void initialiseEnvVarHelp( STRING_MAP& aMap )
67 {
68  // Set up dynamically, as we want to be able to use _() translations,
69  // which can't be done statically
70  aMap[wxT( "KICAD6_FOOTPRINT_DIR" )] =
71  _( "The base path of locally installed system "
72  "footprint libraries (.pretty folders).");
73  aMap[wxT( "KICAD6_3DMODEL_DIR" )] =
74  _( "The base path of system footprint 3D shapes (.3Dshapes folders).");
75  aMap[wxT( "KICAD6_SYMBOL_DIR" )] =
76  _( "The base path of the locally installed symbol libraries.");
77  aMap[wxT( "KICAD6_TEMPLATE_DIR" )] =
78  _( "A directory containing project templates installed with KiCad.");
79  aMap[wxT( "KICAD_USER_TEMPLATE_DIR" )] =
80  _( "Optional. Can be defined if you want to create your own project "
81  "templates folder.");
82  aMap[wxT( "KICAD6_3RD_PARTY" )] =
83  _( "A directory containing 3rd party plugins, libraries and other "
84  "downloadable content.");
85  aMap[wxT( "KIPRJMOD" )] =
86  _("Internally defined by KiCad (cannot be edited) and is set "
87  "to the absolute path of the currently loaded project file. This environment "
88  "variable can be used to define files and paths relative to the currently loaded "
89  "project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be defined as a "
90  "folder containing a project specific footprint library named footprints.pretty." );
91  aMap[wxT( "KICAD6_SCRIPTING_DIR" )] =
92  _( "A directory containing system-wide scripts installed with KiCad" );
93  aMap[wxT( "KICAD6_USER_SCRIPTING_DIR" )] =
94  _( "A directory containing user-specific scripts installed with KiCad" );
95 
96  // Deprecated vars
97  aMap[wxT( "KICAD_PTEMPLATES" )] =
98  _( "Deprecated version of KICAD_TEMPLATE_DIR.");
99  aMap[wxT( "KISYS3DMOD" )] =
100  _( "Deprecated version of KICAD6_3DMODEL_DIR." );
101  aMap[wxT( "KISYSMOD" )] =
102  _( "Deprecated version of KICAD6_FOOTPRINT_DIR." );
103  aMap[wxT( "KICAD_SYMBOL_DIR" )] =
104  _( "Deprecated version of KICAD_SYMBOL_DIR.");
105 }
106 
107 
108 wxString ENV_VAR::LookUpEnvVarHelp( const wxString& aEnvVar )
109 {
110  static STRING_MAP envVarHelpText;
111 
112  if( envVarHelpText.size() == 0 )
113  initialiseEnvVarHelp( envVarHelpText );
114 
115  return envVarHelpText[ aEnvVar ];
116 }
117 
118 
119 template<>
120 OPT<double> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
121 {
122  wxString env;
123 
124  if( wxGetEnv( aEnvVarName, &env ) )
125  {
126  double value;
127 
128  if( env.ToDouble( &value ) )
129  return value;
130  }
131 
132  return NULLOPT;
133 }
134 
135 
136 template<>
137 OPT<wxString> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
138 {
139  OPT<wxString> optValue;
140 
141  wxString env;
142 
143  if( wxGetEnv( aEnvVarName, &env ) )
144  {
145  optValue = env;
146  }
147 
148  return optValue;
149 }
static void initialiseEnvVarHelp(STRING_MAP &aMap)
Definition: env_vars.cpp:66
bool IsEnvVarImmutable(const wxString &aEnvVar)
Determine if an environment variable is "predefined", i.e.
Definition: env_vars.cpp:48
static const ENV_VAR::ENV_VAR_LIST predefinedEnvVars
Definition: env_vars.cpp:36
std::vector< wxString > ENV_VAR_LIST
Definition: env_vars.h:34
const auto NULLOPT
Definition: optional.h:9
#define _(s)
std::map< wxString, wxString > STRING_MAP
Definition: env_vars.cpp:27
wxString LookUpEnvVarHelp(const wxString &aEnvVar)
Look up long-form help text for a given environment variable.
Definition: env_vars.cpp:108
const ENV_VAR_LIST & GetPredefinedEnvVars()
Get the list of pre-defined environment variables.
Definition: env_vars.cpp:60
boost::optional< T > OPT
Definition: optional.h:7
Functions related to environment variables, including help functions.
OPT< VAL_TYPE > GetEnvVar(const wxString &aEnvVarName)
Get an environment variable as a specific type, if set correctly.