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 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  "KIPRJMOD",
38  "KICAD6_SYMBOL_DIR",
39  "KICAD6_3DMODEL_DIR",
40  "KICAD6_FOOTPRINT_DIR",
41  "KICAD6_TEMPLATE_DIR",
42  "KICAD_USER_TEMPLATE_DIR",
43  "KICAD_PTEMPLATES",
44 };
45 
46 
47 bool IsEnvVarImmutable( const wxString& aEnvVar )
48 {
49  for( const auto& s: predefined_env_vars )
50  {
51  if( s == aEnvVar )
52  return true;
53  }
54 
55  return false;
56 }
57 
58 
60 {
61  return predefined_env_vars;
62 }
63 
64 
66 {
67  // Set up dynamically, as we want to be able to use _() translations,
68  // which can't be done statically
69  aMap["KICAD6_FOOTPRINT_DIR"] =
70  _( "The base path of locally installed system "
71  "footprint libraries (.pretty folders).");
72  aMap["KICAD6_3DMODEL_DIR"] =
73  _( "The base path of system footprint 3D shapes (.3Dshapes folders).");
74  aMap["KICAD6_SYMBOL_DIR"] =
75  _( "The base path of the locally installed symbol libraries.");
76  aMap["KICAD6_TEMPLATE_DIR"] =
77  _( "A directory containing project templates installed with KiCad.");
78  aMap["KICAD_USER_TEMPLATE_DIR"] =
79  _( "Optional. Can be defined if you want to create your own project "
80  "templates folder.");
81  aMap["KIPRJMOD"] =
82  _("Internally defined by KiCad (cannot be edited) and is set "
83  "to the absolute path of the currently loaded project file. This environment "
84  "variable can be used to define files and paths relative to the currently loaded "
85  "project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be defined as a "
86  "folder containing a project specific footprint library named footprints.pretty." );
87  aMap["KICAD6_SCRIPTING_DIR"] =
88  _( "A directory containing system-wide scripts installed with KiCad" );
89  aMap["KICAD6_USER_SCRIPTING_DIR"] =
90  _( "A directory containing user-specific scripts installed with KiCad" );
91 
92  // Deprecated vars
93  aMap["KICAD_PTEMPLATES"] =
94  _( "Deprecated version of KICAD_TEMPLATE_DIR.");
95  aMap["KISYS3DMOD"] =
96  _( "Deprecated version of KICAD6_3DMODEL_DIR." );
97  aMap["KISYSMOD"] =
98  _( "Deprecated version of KICAD6_FOOTPRINT_DIR." );
99  aMap["KICAD_SYMBOL_DIR"] =
100  _( "Deprecated version of KICAD_SYMBOL_DIR.");
101 }
102 
103 
104 wxString LookUpEnvVarHelp( const wxString& aEnvVar )
105 {
106  static STRING_MAP env_var_help_text;
107 
108  if( env_var_help_text.size() == 0 )
109  initialiseEnvVarHelp( env_var_help_text );
110 
111  return env_var_help_text[aEnvVar];
112 }
113 
114 
115 template<>
116 OPT<double> GetEnvVar( const wxString& aEnvVarName )
117 {
118  OPT<double> opt_value;
119 
120  wxString env;
121  if( wxGetEnv( aEnvVarName, &env ) )
122  {
123  double value;
124  if( env.ToDouble( &value ) )
125  {
126  opt_value = value;
127  }
128  }
129 
130  return opt_value;
131 }
132 
133 template<>
134 OPT<wxString> GetEnvVar( const wxString& aEnvVarName )
135 {
136  OPT<wxString> opt_value;
137 
138  wxString env;
139  if( wxGetEnv( aEnvVarName, &env ) )
140  {
141  opt_value = env;
142  }
143 
144  return opt_value;
145 }
bool IsEnvVarImmutable(const wxString &aEnvVar)
Determine if an environment variable is "predefined", i.e.
Definition: env_vars.cpp:47
const ENV_VAR_LIST & GetPredefinedEnvVars()
Get the list of pre-defined environment variables.
Definition: env_vars.cpp:59
std::vector< wxString > ENV_VAR_LIST
Definition: env_vars.h:34
static const ENV_VAR_LIST predefined_env_vars
Definition: env_vars.cpp:36
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:104
void initialiseEnvVarHelp(STRING_MAP &aMap)
Definition: env_vars.cpp:65
OPT< double > GetEnvVar(const wxString &aEnvVarName)
Get an environment variable as a specific type, if set correctly.
Definition: env_vars.cpp:116
#define _(s)
Definition: 3d_actions.cpp:33
boost::optional< T > OPT
Definition: optional.h:7
Functions related to environment variables, including help functions.