1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <build_version.h>
#include <env_vars.h>
#include <settings/environment.h>
#include <map>
#include <wx/regex.h>
#include <wx/translation.h>
#include <wx/utils.h>
using STRING_MAP = std::map<wxString, wxString>;
/**
* List of pre-defined environment variables.
*
* @todo Instead of defining these values here, extract them from elsewhere in the program
* (where they are originally defined).
*/
static const ENV_VAR::ENV_VAR_LIST predefinedEnvVars = {
wxS( "KIPRJMOD" ),
ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ),
ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ),
ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ),
ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ),
wxS( "KICAD_USER_TEMPLATE_DIR" ),
wxS( "KICAD_PTEMPLATES" ),
ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ),
};
const wxRegEx versionedEnvVarRegex( wxS( "KICAD[0-9]+_[A-Z0-9_]+(_DIR)?" ) );
bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar )
{
if( versionedEnvVarRegex.Matches( aEnvVar ) )
return true;
for( const wxString& s : predefinedEnvVars )
{
if( s == aEnvVar )
return true;
}
return false;
}
const ENV_VAR::ENV_VAR_LIST& ENV_VAR::GetPredefinedEnvVars()
{
return predefinedEnvVars;
}
wxString ENV_VAR::GetVersionedEnvVarName( const wxString& aBaseName )
{
int version = 0;
std::tie(version, std::ignore, std::ignore) = GetMajorMinorPatchTuple();
return wxString::Format( "KICAD%d_%s", version, aBaseName );
}
std::optional<wxString> ENV_VAR::GetVersionedEnvVarValue( const ENV_VAR_MAP& aMap,
const wxString& aBaseName )
{
wxString exactMatch = ENV_VAR::GetVersionedEnvVarName( aBaseName );
if( aMap.count( exactMatch ) )
return aMap.at( exactMatch ).GetValue();
wxString partialMatch = wxString::Format( "KICAD*_%s", aBaseName );
for( const auto& [k, v] : aMap )
{
if( k.Matches( partialMatch ) )
return v.GetValue();
}
return std::nullopt;
}
static void initialiseEnvVarHelp( STRING_MAP& aMap )
{
// Set up dynamically, as we want to be able to use _() translations,
// which can't be done statically
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) )] =
_( "The base path of locally installed system "
"footprint libraries (.pretty folders).");
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) )] =
_( "The base path of system footprint 3D shapes (.3Dshapes folders).");
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) )] =
_( "The base path of the locally installed symbol libraries.");
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) )] =
_( "A directory containing project templates installed with KiCad.");
aMap[wxS( "KICAD_USER_TEMPLATE_DIR" )] =
_( "Optional. Can be defined if you want to create your own project "
"templates folder.");
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) )] =
_( "A directory containing 3rd party plugins, libraries and other "
"downloadable content.");
aMap[wxS( "KIPRJMOD" )] =
_("Internally defined by KiCad (cannot be edited) and is set "
"to the absolute path of the currently loaded project file. This environment "
"variable can be used to define files and paths relative to the currently loaded "
"project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be defined as a "
"folder containing a project specific footprint library named footprints.pretty." );
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SCRIPTING_DIR" ) )] =
_( "A directory containing system-wide scripts installed with KiCad" );
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "USER_SCRIPTING_DIR" ) )] =
_( "A directory containing user-specific scripts installed with KiCad" );
// Deprecated vars
#define DEP( var ) wxString::Format( _( "Deprecated version of %s." ), var )
aMap[wxS( "KICAD_PTEMPLATES" )] =
DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ) );
aMap[wxS( "KISYS3DMOD" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
aMap[wxS( "KISYSMOD" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ) );
aMap[wxS( "KICAD_SYMBOL_DIR" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ) );
#undef DEP
}
wxString ENV_VAR::LookUpEnvVarHelp( const wxString& aEnvVar )
{
static STRING_MAP envVarHelpText;
if( envVarHelpText.size() == 0 )
initialiseEnvVarHelp( envVarHelpText );
return envVarHelpText[ aEnvVar ];
}
template<>
std::optional<double> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
{
wxString env;
if( wxGetEnv( aEnvVarName, &env ) )
{
double value;
if( env.ToDouble( &value ) )
return value;
}
return std::nullopt;
}
template<>
std::optional<wxString> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
{
std::optional<wxString> optValue;
wxString env;
if( wxGetEnv( aEnvVarName, &env ) )
{
optValue = env;
}
return optValue;
}
|