You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.8 KiB

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