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.

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