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.

186 lines
5.8 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 <build_version.h>
  20. #include <env_vars.h>
  21. #include <settings/environment.h>
  22. #include <map>
  23. #include <wx/regex.h>
  24. #include <wx/translation.h>
  25. #include <wx/utils.h>
  26. using STRING_MAP = std::map<wxString, wxString>;
  27. /*
  28. * List of pre-defined environment variables
  29. *
  30. * TODO - Instead of defining these values here,
  31. * extract them from elsewhere in the program
  32. * (where they are originally defined)
  33. */
  34. static const ENV_VAR::ENV_VAR_LIST predefinedEnvVars = {
  35. wxS( "KIPRJMOD" ),
  36. ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ),
  37. ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ),
  38. ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ),
  39. ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ),
  40. wxS( "KICAD_USER_TEMPLATE_DIR" ),
  41. wxS( "KICAD_PTEMPLATES" ),
  42. ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ),
  43. };
  44. const wxRegEx versionedEnvVarRegex( wxS( "KICAD[0-9]+_[A-Z0-9_]+(_DIR)?" ) );
  45. bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar )
  46. {
  47. if( versionedEnvVarRegex.Matches( aEnvVar ) )
  48. return true;
  49. for( const wxString& s : predefinedEnvVars )
  50. {
  51. if( s == aEnvVar )
  52. return true;
  53. }
  54. return false;
  55. }
  56. const ENV_VAR::ENV_VAR_LIST& ENV_VAR::GetPredefinedEnvVars()
  57. {
  58. return predefinedEnvVars;
  59. }
  60. wxString ENV_VAR::GetVersionedEnvVarName( const wxString& aBaseName )
  61. {
  62. int version = 0;
  63. std::tie(version, std::ignore, std::ignore) = GetMajorMinorPatchTuple();
  64. return wxString::Format( "KICAD%d_%s", version, aBaseName );
  65. }
  66. std::optional<wxString> ENV_VAR::GetVersionedEnvVarValue( const ENV_VAR_MAP& aMap,
  67. const wxString& aBaseName )
  68. {
  69. wxString exactMatch = ENV_VAR::GetVersionedEnvVarName( aBaseName );
  70. if( aMap.count( exactMatch ) )
  71. return aMap.at( exactMatch ).GetValue();
  72. wxString partialMatch = wxString::Format( "KICAD*_%s", aBaseName );
  73. for( const auto& [k, v] : aMap )
  74. {
  75. if( k.Matches( partialMatch ) )
  76. return v.GetValue();
  77. }
  78. return std::nullopt;
  79. }
  80. static void initialiseEnvVarHelp( STRING_MAP& aMap )
  81. {
  82. // Set up dynamically, as we want to be able to use _() translations,
  83. // which can't be done statically
  84. aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) )] =
  85. _( "The base path of locally installed system "
  86. "footprint libraries (.pretty folders).");
  87. aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) )] =
  88. _( "The base path of system footprint 3D shapes (.3Dshapes folders).");
  89. aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) )] =
  90. _( "The base path of the locally installed symbol libraries.");
  91. aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) )] =
  92. _( "A directory containing project templates installed with KiCad.");
  93. aMap[wxS( "KICAD_USER_TEMPLATE_DIR" )] =
  94. _( "Optional. Can be defined if you want to create your own project "
  95. "templates folder.");
  96. aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) )] =
  97. _( "A directory containing 3rd party plugins, libraries and other "
  98. "downloadable content.");
  99. aMap[wxS( "KIPRJMOD" )] =
  100. _("Internally defined by KiCad (cannot be edited) and is set "
  101. "to the absolute path of the currently loaded project file. This environment "
  102. "variable can be used to define files and paths relative to the currently loaded "
  103. "project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be defined as a "
  104. "folder containing a project specific footprint library named footprints.pretty." );
  105. aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SCRIPTING_DIR" ) )] =
  106. _( "A directory containing system-wide scripts installed with KiCad" );
  107. aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "USER_SCRIPTING_DIR" ) )] =
  108. _( "A directory containing user-specific scripts installed with KiCad" );
  109. // Deprecated vars
  110. aMap[wxS( "KICAD_PTEMPLATES" )] =
  111. _( "Deprecated version of KICAD_TEMPLATE_DIR.");
  112. aMap[wxS( "KISYS3DMOD" )] =
  113. _( "Deprecated version of KICAD7_3DMODEL_DIR." );
  114. aMap[wxS( "KISYSMOD" )] =
  115. _( "Deprecated version of KICAD7_FOOTPRINT_DIR." );
  116. aMap[wxS( "KICAD_SYMBOL_DIR" )] =
  117. _( "Deprecated version of KICAD_SYMBOL_DIR.");
  118. }
  119. wxString ENV_VAR::LookUpEnvVarHelp( const wxString& aEnvVar )
  120. {
  121. static STRING_MAP envVarHelpText;
  122. if( envVarHelpText.size() == 0 )
  123. initialiseEnvVarHelp( envVarHelpText );
  124. return envVarHelpText[ aEnvVar ];
  125. }
  126. template<>
  127. std::optional<double> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
  128. {
  129. wxString env;
  130. if( wxGetEnv( aEnvVarName, &env ) )
  131. {
  132. double value;
  133. if( env.ToDouble( &value ) )
  134. return value;
  135. }
  136. return std::nullopt;
  137. }
  138. template<>
  139. std::optional<wxString> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
  140. {
  141. std::optional<wxString> optValue;
  142. wxString env;
  143. if( wxGetEnv( aEnvVarName, &env ) )
  144. {
  145. optValue = env;
  146. }
  147. return optValue;
  148. }