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.

149 lines
4.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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. #include <env_vars.h>
  20. #include <map>
  21. #include <wx/translation.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::ENV_VAR_LIST predefinedEnvVars = {
  32. wxS( "KIPRJMOD" ),
  33. wxS( "KICAD7_SYMBOL_DIR" ),
  34. wxS( "KICAD7_3DMODEL_DIR" ),
  35. wxS( "KICAD7_FOOTPRINT_DIR" ),
  36. wxS( "KICAD7_TEMPLATE_DIR" ),
  37. wxS( "KICAD_USER_TEMPLATE_DIR" ),
  38. wxS( "KICAD_PTEMPLATES" ),
  39. wxS( "KICAD7_3RD_PARTY" ),
  40. };
  41. bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar )
  42. {
  43. for( const wxString& s : predefinedEnvVars )
  44. {
  45. if( s == aEnvVar )
  46. return true;
  47. }
  48. return false;
  49. }
  50. const ENV_VAR::ENV_VAR_LIST& ENV_VAR::GetPredefinedEnvVars()
  51. {
  52. return predefinedEnvVars;
  53. }
  54. static 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[wxS( "KICAD7_FOOTPRINT_DIR" )] =
  59. _( "The base path of locally installed system "
  60. "footprint libraries (.pretty folders).");
  61. aMap[wxS( "KICAD7_3DMODEL_DIR" )] =
  62. _( "The base path of system footprint 3D shapes (.3Dshapes folders).");
  63. aMap[wxS( "KICAD7_SYMBOL_DIR" )] =
  64. _( "The base path of the locally installed symbol libraries.");
  65. aMap[wxS( "KICAD7_TEMPLATE_DIR" )] =
  66. _( "A directory containing project templates installed with KiCad.");
  67. aMap[wxS( "KICAD_USER_TEMPLATE_DIR" )] =
  68. _( "Optional. Can be defined if you want to create your own project "
  69. "templates folder.");
  70. aMap[wxS( "KICAD7_3RD_PARTY" )] =
  71. _( "A directory containing 3rd party plugins, libraries and other "
  72. "downloadable content.");
  73. aMap[wxS( "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. aMap[wxS( "KICAD7_SCRIPTING_DIR" )] =
  80. _( "A directory containing system-wide scripts installed with KiCad" );
  81. aMap[wxS( "KICAD7_USER_SCRIPTING_DIR" )] =
  82. _( "A directory containing user-specific scripts installed with KiCad" );
  83. // Deprecated vars
  84. aMap[wxS( "KICAD_PTEMPLATES" )] =
  85. _( "Deprecated version of KICAD_TEMPLATE_DIR.");
  86. aMap[wxS( "KISYS3DMOD" )] =
  87. _( "Deprecated version of KICAD7_3DMODEL_DIR." );
  88. aMap[wxS( "KISYSMOD" )] =
  89. _( "Deprecated version of KICAD7_FOOTPRINT_DIR." );
  90. aMap[wxS( "KICAD_SYMBOL_DIR" )] =
  91. _( "Deprecated version of KICAD_SYMBOL_DIR.");
  92. }
  93. wxString ENV_VAR::LookUpEnvVarHelp( const wxString& aEnvVar )
  94. {
  95. static STRING_MAP envVarHelpText;
  96. if( envVarHelpText.size() == 0 )
  97. initialiseEnvVarHelp( envVarHelpText );
  98. return envVarHelpText[ aEnvVar ];
  99. }
  100. template<>
  101. std::optional<double> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
  102. {
  103. wxString env;
  104. if( wxGetEnv( aEnvVarName, &env ) )
  105. {
  106. double value;
  107. if( env.ToDouble( &value ) )
  108. return value;
  109. }
  110. return std::nullopt;
  111. }
  112. template<>
  113. std::optional<wxString> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
  114. {
  115. std::optional<wxString> optValue;
  116. wxString env;
  117. if( wxGetEnv( aEnvVarName, &env ) )
  118. {
  119. optValue = env;
  120. }
  121. return optValue;
  122. }