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