Remove hard-coded versioned env vars in most places

This commit is contained in:
Jon Evans 2024-01-16 17:20:45 -05:00
parent 0cd59dc278
commit 7b0bb59b37
15 changed files with 187 additions and 79 deletions

View File

@ -41,11 +41,6 @@
#include <dialogs/dialog_color_picker.h> // for CUSTOM_COLORS_LIST definition #include <dialogs/dialog_color_picker.h> // for CUSTOM_COLORS_LIST definition
/// A variable name whose value holds the path of 3D shape files.
/// Currently an environment variable, eventually a project variable.
#define KICAD7_3DMODEL_DIR wxT( "KICAD7_3DMODEL_DIR" )
#define KICAD_DEFAULT_3D_DRAWFRAME_STYLE (wxDEFAULT_FRAME_STYLE | wxWANTS_CHARS) #define KICAD_DEFAULT_3D_DRAWFRAME_STYLE (wxDEFAULT_FRAME_STYLE | wxWANTS_CHARS)
// Forward declarations // Forward declarations

View File

@ -20,6 +20,7 @@
#include <dialogs/dialog_global_lib_table_config.h> #include <dialogs/dialog_global_lib_table_config.h>
#include <env_vars.h>
#include <pgm_base.h> #include <pgm_base.h>
#include <search_stack.h> #include <search_stack.h>
#include <systemdirsappend.h> #include <systemdirsappend.h>
@ -109,8 +110,14 @@ bool DIALOG_GLOBAL_LIB_TABLE_CONFIG::TransferDataToWindow()
GlobalPathsAppend( &ss, m_faceType ); GlobalPathsAppend( &ss, m_faceType );
wxString templatePath = wxString templatePath;
Pgm().GetLocalEnvVariables().at( wxT( "KICAD7_TEMPLATE_DIR" ) ).GetValue(); const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
if( std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( envVars,
wxT( "TEMPLATE_DIR" ) ) )
{
templatePath = *v;
}
if( !templatePath.IsEmpty() ) if( !templatePath.IsEmpty() )
ss.AddPaths( templatePath, 0 ); ss.AddPaths( templatePath, 0 );

View File

@ -17,10 +17,13 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <build_version.h>
#include <env_vars.h> #include <env_vars.h>
#include <settings/environment.h>
#include <map> #include <map>
#include <wx/regex.h>
#include <wx/translation.h> #include <wx/translation.h>
#include <wx/utils.h> #include <wx/utils.h>
@ -35,18 +38,23 @@ using STRING_MAP = std::map<wxString, wxString>;
*/ */
static const ENV_VAR::ENV_VAR_LIST predefinedEnvVars = { static const ENV_VAR::ENV_VAR_LIST predefinedEnvVars = {
wxS( "KIPRJMOD" ), wxS( "KIPRJMOD" ),
wxS( "KICAD7_SYMBOL_DIR" ), ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ),
wxS( "KICAD7_3DMODEL_DIR" ), ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ),
wxS( "KICAD7_FOOTPRINT_DIR" ), ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ),
wxS( "KICAD7_TEMPLATE_DIR" ), ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ),
wxS( "KICAD_USER_TEMPLATE_DIR" ), wxS( "KICAD_USER_TEMPLATE_DIR" ),
wxS( "KICAD_PTEMPLATES" ), wxS( "KICAD_PTEMPLATES" ),
wxS( "KICAD7_3RD_PARTY" ), ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ),
}; };
const wxRegEx versionedEnvVarRegex( wxS( "KICAD[0-9]+_[A-Z0-9_]+(_DIR)?" ) );
bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar ) bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar )
{ {
if( versionedEnvVarRegex.Matches( aEnvVar ) )
return true;
for( const wxString& s : predefinedEnvVars ) for( const wxString& s : predefinedEnvVars )
{ {
if( s == aEnvVar ) if( s == aEnvVar )
@ -63,23 +71,52 @@ const ENV_VAR::ENV_VAR_LIST& ENV_VAR::GetPredefinedEnvVars()
} }
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 ) static void initialiseEnvVarHelp( STRING_MAP& aMap )
{ {
// Set up dynamically, as we want to be able to use _() translations, // Set up dynamically, as we want to be able to use _() translations,
// which can't be done statically // which can't be done statically
aMap[wxS( "KICAD7_FOOTPRINT_DIR" )] = aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) )] =
_( "The base path of locally installed system " _( "The base path of locally installed system "
"footprint libraries (.pretty folders)."); "footprint libraries (.pretty folders).");
aMap[wxS( "KICAD7_3DMODEL_DIR" )] = aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) )] =
_( "The base path of system footprint 3D shapes (.3Dshapes folders)."); _( "The base path of system footprint 3D shapes (.3Dshapes folders).");
aMap[wxS( "KICAD7_SYMBOL_DIR" )] = aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) )] =
_( "The base path of the locally installed symbol libraries."); _( "The base path of the locally installed symbol libraries.");
aMap[wxS( "KICAD7_TEMPLATE_DIR" )] = aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) )] =
_( "A directory containing project templates installed with KiCad."); _( "A directory containing project templates installed with KiCad.");
aMap[wxS( "KICAD_USER_TEMPLATE_DIR" )] = aMap[wxS( "KICAD_USER_TEMPLATE_DIR" )] =
_( "Optional. Can be defined if you want to create your own project " _( "Optional. Can be defined if you want to create your own project "
"templates folder."); "templates folder.");
aMap[wxS( "KICAD7_3RD_PARTY" )] = aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) )] =
_( "A directory containing 3rd party plugins, libraries and other " _( "A directory containing 3rd party plugins, libraries and other "
"downloadable content."); "downloadable content.");
aMap[wxS( "KIPRJMOD" )] = aMap[wxS( "KIPRJMOD" )] =
@ -88,9 +125,9 @@ static void initialiseEnvVarHelp( STRING_MAP& aMap )
"variable can be used to define files and paths relative to the currently loaded " "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 " "project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be defined as a "
"folder containing a project specific footprint library named footprints.pretty." ); "folder containing a project specific footprint library named footprints.pretty." );
aMap[wxS( "KICAD7_SCRIPTING_DIR" )] = aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SCRIPTING_DIR" ) )] =
_( "A directory containing system-wide scripts installed with KiCad" ); _( "A directory containing system-wide scripts installed with KiCad" );
aMap[wxS( "KICAD7_USER_SCRIPTING_DIR" )] = aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "USER_SCRIPTING_DIR" ) )] =
_( "A directory containing user-specific scripts installed with KiCad" ); _( "A directory containing user-specific scripts installed with KiCad" );
// Deprecated vars // Deprecated vars

View File

@ -31,6 +31,7 @@
#include <trace_helpers.h> #include <trace_helpers.h>
#include <common.h> #include <common.h>
#include <env_vars.h>
#include <filename_resolver.h> #include <filename_resolver.h>
#include <confirm.h> #include <confirm.h>
#include <wx_filename.h> #include <wx_filename.h>
@ -338,7 +339,8 @@ wxString FILENAME_RESOLVER::ResolvePath( const wxString& aFileName, const wxStri
if( !tname.StartsWith( wxS( ":" ) ) ) if( !tname.StartsWith( wxS( ":" ) ) )
{ {
wxFileName fpath; wxFileName fpath;
wxString fullPath( wxS( "${KICAD7_3DMODEL_DIR}" ) ); wxString fullPath( wxString::Format( wxS( "${%s}" ),
ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) ) );
fullPath.Append( fpath.GetPathSeparator() ); fullPath.Append( fpath.GetPathSeparator() );
fullPath.Append( tname ); fullPath.Append( tname );
fullPath = ExpandEnvVarSubstitutions( fullPath, m_project ); fullPath = ExpandEnvVarSubstitutions( fullPath, m_project );
@ -437,7 +439,10 @@ bool FILENAME_RESOLVER::addPath( const SEARCH_PATH& aPath )
if( !path.DirExists() ) if( !path.DirExists() )
{ {
if( aPath.m_Pathvar == wxS( "${KICAD7_3DMODEL_DIR}" ) wxString versionedPath = wxString::Format( wxS( "${%s}" ),
ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
if( aPath.m_Pathvar == versionedPath
|| aPath.m_Pathvar == wxS( "${KIPRJMOD}" ) || aPath.m_Pathvar == wxS( "$(KIPRJMOD)" ) || aPath.m_Pathvar == wxS( "${KIPRJMOD}" ) || aPath.m_Pathvar == wxS( "$(KIPRJMOD)" )
|| aPath.m_Pathvar == wxS( "${KISYS3DMOD}" ) || aPath.m_Pathvar == wxS( "$(KISYS3DMOD)" ) ) || aPath.m_Pathvar == wxS( "${KISYS3DMOD}" ) || aPath.m_Pathvar == wxS( "$(KISYS3DMOD)" ) )
{ {
@ -812,7 +817,7 @@ bool FILENAME_RESOLVER::GetKicadPaths( std::list< wxString >& paths ) const
} }
if( !hasKisys3D ) if( !hasKisys3D )
paths.emplace_back( wxS("KICAD7_3DMODEL_DIR") ); paths.emplace_back( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
return true; return true;
} }

View File

@ -25,6 +25,7 @@
#include <kiface_base.h> #include <kiface_base.h>
#include <env_vars.h>
#include <footprint_info.h> #include <footprint_info.h>
#include <lib_id.h> #include <lib_id.h>
#include <lib_table_lexer.h> #include <lib_table_lexer.h>
@ -502,7 +503,7 @@ FOOTPRINT* FP_LIB_TABLE::FootprintLoadWithOptionalNickname( const LIB_ID& aFootp
const wxString FP_LIB_TABLE::GlobalPathEnvVariableName() const wxString FP_LIB_TABLE::GlobalPathEnvVariableName()
{ {
return wxS( "KICAD7_FOOTPRINT_DIR" ); return ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) );
} }
@ -526,12 +527,15 @@ public:
wxFileName dir = wxFileName::DirName( dirPath ); wxFileName dir = wxFileName::DirName( dirPath );
// consider a directory to be a lib if it's name ends with .pretty and // consider a directory to be a lib if it's name ends with .pretty and
// it is under $KICAD7_3RD_PARTY/footprints/<pkgid>/ i.e. has nested level of at least +3 // it is under $KICADn_3RD_PARTY/footprints/<pkgid>/ i.e. has nested level of at least +3
if( dirPath.EndsWith( wxS( ".pretty" ) ) && dir.GetDirCount() >= m_prefix_dir_count + 3 ) if( dirPath.EndsWith( wxS( ".pretty" ) ) && dir.GetDirCount() >= m_prefix_dir_count + 3 )
{ {
wxString versionedPath = wxString::Format( wxS( "${%s}" ),
ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ) );
wxArrayString parts = dir.GetDirs(); wxArrayString parts = dir.GetDirs();
parts.RemoveAt( 0, m_prefix_dir_count ); parts.RemoveAt( 0, m_prefix_dir_count );
parts.Insert( wxS( "${KICAD7_3RD_PARTY}" ), 0 ); parts.Insert( versionedPath, 0 );
wxString libPath = wxJoin( parts, '/' ); wxString libPath = wxJoin( parts, '/' );
@ -588,11 +592,12 @@ bool FP_LIB_TABLE::LoadGlobalTable( FP_LIB_TABLE& aTable )
SystemDirsAppend( &ss ); SystemDirsAppend( &ss );
wxString templatePath = const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
Pgm().GetLocalEnvVariables().at( wxT( "KICAD7_TEMPLATE_DIR" ) ).GetValue(); std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( envVars,
wxT( "TEMPLATE_DIR" ) );
if( !templatePath.IsEmpty() ) if( v && !v->IsEmpty() )
ss.AddPaths( templatePath, 0 ); ss.AddPaths( *v, 0 );
wxString fileName = ss.FindValidPath( global_tbl_name ); wxString fileName = ss.FindValidPath( global_tbl_name );
@ -611,7 +616,11 @@ bool FP_LIB_TABLE::LoadGlobalTable( FP_LIB_TABLE& aTable )
SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager(); SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>(); KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>();
wxString packagesPath = Pgm().GetLocalEnvVariables().at( wxT( "KICAD7_3RD_PARTY" ) ).GetValue(); const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables();
wxString packagesPath;
if( std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( env, wxT( "3RD_PARTY" ) ) )
packagesPath = *v;
if( settings->m_PcmLibAutoAdd ) if( settings->m_PcmLibAutoAdd )
{ {

View File

@ -22,6 +22,7 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <env_vars.h>
#include <paths.h> #include <paths.h>
#include <search_stack.h> #include <search_stack.h>
#include <settings/settings_manager.h> #include <settings/settings_manager.h>
@ -36,14 +37,7 @@
///! The following environment variables will never be migrated from a previous version ///! The following environment variables will never be migrated from a previous version
const std::set<wxString> envVarBlacklist = const wxRegEx versionedEnvVarRegex( wxS( "KICAD[0-9]+_[A-Z0-9_]+(_DIR)?" ) );
{
wxT( "KICAD7_SYMBOL_DIR" ),
wxT( "KICAD7_FOOTPRINT_DIR" ),
wxT( "KICAD7_TEMPLATES_DIR" ),
wxT( "KICAD7_3DMODEL_DIR" )
};
///! Update the schema version whenever a migration is required ///! Update the schema version whenever a migration is required
const int commonSchemaVersion = 3; const int commonSchemaVersion = 3;
@ -543,7 +537,7 @@ bool COMMON_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
while( aCfg->GetNextEntry( key, index ) ) while( aCfg->GetNextEntry( key, index ) )
{ {
if( envVarBlacklist.count( key ) ) if( versionedEnvVarRegex.Matches( key ) )
{ {
wxLogTrace( traceSettings, wxLogTrace( traceSettings,
wxT( "Migrate Env: %s is blacklisted; skipping." ), key ); wxT( "Migrate Env: %s is blacklisted; skipping." ), key );
@ -633,21 +627,23 @@ void COMMON_SETTINGS::InitializeEnvironment()
wxFileName path( basePath ); wxFileName path( basePath );
path.AppendDir( wxT( "footprints" ) ); path.AppendDir( wxT( "footprints" ) );
addVar( wxT( "KICAD7_FOOTPRINT_DIR" ), path.GetFullPath() ); addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ), path.GetFullPath() );
path = basePath; path = basePath;
path.AppendDir( wxT( "3dmodels" ) ); path.AppendDir( wxT( "3dmodels" ) );
addVar( wxT( "KICAD7_3DMODEL_DIR" ), path.GetFullPath() ); addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ), path.GetFullPath() );
addVar( wxT( "KICAD7_TEMPLATE_DIR" ), PATHS::GetStockTemplatesPath() ); addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ),
PATHS::GetStockTemplatesPath() );
addVar( wxT( "KICAD_USER_TEMPLATE_DIR" ), PATHS::GetUserTemplatesPath() ); addVar( wxT( "KICAD_USER_TEMPLATE_DIR" ), PATHS::GetUserTemplatesPath() );
addVar( wxT( "KICAD7_3RD_PARTY" ), PATHS::GetDefault3rdPartyPath() ); addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ),
PATHS::GetDefault3rdPartyPath() );
path = basePath; path = basePath;
path.AppendDir( wxT( "symbols" ) ); path.AppendDir( wxT( "symbols" ) );
addVar( wxT( "KICAD7_SYMBOL_DIR" ), path.GetFullPath() ); addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ), path.GetFullPath() );
} }
@ -725,9 +721,12 @@ bool COMMON_SETTINGS::readLegacy3DResolverCfg( const wxString&
if( !getLegacy3DHollerith( cfgLine, idx, al.m_Alias ) ) if( !getLegacy3DHollerith( cfgLine, idx, al.m_Alias ) )
continue; continue;
// Don't add KICAD7_3DMODEL_DIR, one of its legacy equivalents, or KIPRJMOD from a // Don't add KICADn_3DMODEL_DIR, one of its legacy equivalents, or KIPRJMOD from a
// config file. They're system variables are are defined at runtime. // config file. They're system variables which are defined at runtime.
if( al.m_Alias == wxS( "${KICAD7_3DMODEL_DIR}" ) || al.m_Alias == wxS( "${KIPRJMOD}" ) wxString versionedPath = wxString::Format( wxS( "${%s}" ),
ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
if( al.m_Alias == versionedPath || al.m_Alias == wxS( "${KIPRJMOD}" )
|| al.m_Alias == wxS( "$(KIPRJMOD)" ) || al.m_Alias == wxS( "${KISYS3DMOD}" ) || al.m_Alias == wxS( "$(KIPRJMOD)" ) || al.m_Alias == wxS( "${KISYS3DMOD}" )
|| al.m_Alias == wxS( "$(KISYS3DMOD)" ) ) || al.m_Alias == wxS( "$(KISYS3DMOD)" ) )
{ {

View File

@ -615,7 +615,11 @@ bool SETTINGS_MANAGER::MigrateIfNeeded()
wxT( "KICAD7_SYMBOL_DIR" ), wxT( "KICAD7_SYMBOL_DIR" ),
wxT( "KICAD7_3DMODEL_DIR" ), wxT( "KICAD7_3DMODEL_DIR" ),
wxT( "KICAD7_FOOTPRINT_DIR" ), wxT( "KICAD7_FOOTPRINT_DIR" ),
wxT( "KICAD7_TEMPLATE_DIR" ), // Stores the default library table to be copied wxT( "KICAD7_TEMPLATE_DIR" ),
wxT( "KICAD8_SYMBOL_DIR" ),
wxT( "KICAD8_3DMODEL_DIR" ),
wxT( "KICAD8_FOOTPRINT_DIR" ),
wxT( "KICAD8_TEMPLATE_DIR" ),
// Deprecated keys // Deprecated keys
wxT( "KICAD_PTEMPLATES" ), wxT( "KICAD_PTEMPLATES" ),

View File

@ -24,6 +24,7 @@
*/ */
#include <env_vars.h>
#include <lib_id.h> #include <lib_id.h>
#include <lib_table_lexer.h> #include <lib_table_lexer.h>
#include <paths.h> #include <paths.h>
@ -546,7 +547,7 @@ LIB_SYMBOL* SYMBOL_LIB_TABLE::LoadSymbolWithOptionalNickname( const LIB_ID& aLib
const wxString SYMBOL_LIB_TABLE::GlobalPathEnvVariableName() const wxString SYMBOL_LIB_TABLE::GlobalPathEnvVariableName()
{ {
return "KICAD7_SYMBOL_DIR"; return ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) );
} }
@ -568,12 +569,15 @@ public:
wxFileName file = wxFileName::FileName( aFilePath ); wxFileName file = wxFileName::FileName( aFilePath );
// consider a file to be a lib if it's name ends with .kicad_sym and // consider a file to be a lib if it's name ends with .kicad_sym and
// it is under $KICAD7_3RD_PARTY/symbols/<pkgid>/ i.e. has nested level of at least +2 // it is under $KICADn_3RD_PARTY/symbols/<pkgid>/ i.e. has nested level of at least +2
if( file.GetExt() == wxT( "kicad_sym" ) && file.GetDirCount() >= m_prefix_dir_count + 2 ) if( file.GetExt() == wxT( "kicad_sym" ) && file.GetDirCount() >= m_prefix_dir_count + 2 )
{ {
wxString versionedPath = wxString::Format( wxS( "${%s}" ),
ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ) );
wxArrayString parts = file.GetDirs(); wxArrayString parts = file.GetDirs();
parts.RemoveAt( 0, m_prefix_dir_count ); parts.RemoveAt( 0, m_prefix_dir_count );
parts.Insert( "${KICAD7_3RD_PARTY}", 0 ); parts.Insert( versionedPath, 0 );
parts.Add( file.GetFullName() ); parts.Add( file.GetFullName() );
wxString libPath = wxJoin( parts, '/' ); wxString libPath = wxJoin( parts, '/' );
@ -633,11 +637,12 @@ bool SYMBOL_LIB_TABLE::LoadGlobalTable( SYMBOL_LIB_TABLE& aTable )
SystemDirsAppend( &ss ); SystemDirsAppend( &ss );
wxString templatePath = const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
Pgm().GetLocalEnvVariables().at( wxT( "KICAD7_TEMPLATE_DIR" ) ).GetValue(); std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( envVars,
wxT( "TEMPLATE_DIR" ) );
if( !templatePath.IsEmpty() ) if( v && !v->IsEmpty() )
ss.AddPaths( templatePath, 0 ); ss.AddPaths( *v, 0 );
wxString fileName = ss.FindValidPath( global_tbl_name ); wxString fileName = ss.FindValidPath( global_tbl_name );
@ -658,7 +663,11 @@ bool SYMBOL_LIB_TABLE::LoadGlobalTable( SYMBOL_LIB_TABLE& aTable )
wxCHECK( settings, false ); wxCHECK( settings, false );
wxString packagesPath = Pgm().GetLocalEnvVariables().at( wxT( "KICAD7_3RD_PARTY" ) ).GetValue(); wxString packagesPath;
const ENV_VAR_MAP& vars = Pgm().GetLocalEnvVariables();
if( std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( vars, wxT( "3RD_PARTY" ) ) )
packagesPath = *v;
if( settings->m_PcmLibAutoAdd ) if( settings->m_PcmLibAutoAdd )
{ {

View File

@ -26,9 +26,12 @@
#define ENV_VARS_H #define ENV_VARS_H
#include <wx/string.h> #include <wx/string.h>
#include <map>
#include <vector> #include <vector>
#include <optional> #include <optional>
class ENV_VAR_ITEM;
namespace ENV_VAR namespace ENV_VAR
{ {
using ENV_VAR_LIST = std::vector<wxString>; using ENV_VAR_LIST = std::vector<wxString>;
@ -47,6 +50,24 @@ namespace ENV_VAR
*/ */
const ENV_VAR_LIST& GetPredefinedEnvVars(); const ENV_VAR_LIST& GetPredefinedEnvVars();
/**
* Constructs a versioned environment variable based on this KiCad major version
* @param aBaseName is the suffix, like TEMPLATE_DIR
* @return an environment variable name, like KICAD8_TEMPLATE_DIR
*/
wxString GetVersionedEnvVarName( const wxString& aBaseName );
/**
* Attempts to retrieve the value of a versioned environment variable, such as
* KICAD8_TEMPLATE_DIR. If this value exists in the map, it will be returned. If not, the
* map will be searched for keys matching KICAD*_<aBaseName>, and the first match's value will
* be returned. If there are no matches, std::nullopt will be returned.
* @param aMap is an ENV_VAR_MAP (@see environment.h)
* @param aBaseName is the suffix for the environment variable (@see GetVersionedEnvVarName)
*/
std::optional<wxString> GetVersionedEnvVarValue( const std::map<wxString, ENV_VAR_ITEM>& aMap,
const wxString& aBaseName );
/** /**
* Look up long-form help text for a given environment variable. * Look up long-form help text for a given environment variable.
* *

View File

@ -35,6 +35,7 @@
#include <wx/msgdlg.h> #include <wx/msgdlg.h>
#include <wx/cmdline.h> #include <wx/cmdline.h>
#include <env_vars.h>
#include <file_history.h> #include <file_history.h>
#include <hotkeys_basic.h> #include <hotkeys_basic.h>
#include <kiway.h> #include <kiway.h>
@ -198,18 +199,20 @@ bool PGM_KICAD::OnPgmInit()
m_bm.m_search.AddPaths( fn.GetPath() ); m_bm.m_search.AddPaths( fn.GetPath() );
} }
// The KICAD7_TEMPLATE_DIR takes precedence over the search stack template path. // The versioned TEMPLATE_DIR takes precedence over the search stack template path.
ENV_VAR_MAP_CITER it = GetLocalEnvVariables().find( "KICAD7_TEMPLATE_DIR" ); if( std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( GetLocalEnvVariables(),
wxT( "TEMPLATE_DIR" ) ) )
if( it != GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString ) {
m_bm.m_search.Insert( it->second.GetValue(), 0 ); if( !v->IsEmpty() )
m_bm.m_search.Insert( *v, 0 );
}
// We've been adding system (installed default) search paths so far, now for user paths // We've been adding system (installed default) search paths so far, now for user paths
// The default user search path is inside KIPLATFORM::ENV::GetDocumentsPath() // The default user search path is inside KIPLATFORM::ENV::GetDocumentsPath()
m_bm.m_search.Insert( PATHS::GetUserTemplatesPath(), 0 ); m_bm.m_search.Insert( PATHS::GetUserTemplatesPath(), 0 );
// ...but the user can override that default with the KICAD_USER_TEMPLATE_DIR env var // ...but the user can override that default with the KICAD_USER_TEMPLATE_DIR env var
it = GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" ); ENV_VAR_MAP_CITER it = GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" );
if( it != GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString ) if( it != GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString )
m_bm.m_search.Insert( it->second.GetValue(), 0 ); m_bm.m_search.Insert( it->second.GetValue(), 0 );

View File

@ -24,6 +24,7 @@
#include <kicad_curl/kicad_curl.h> #include <kicad_curl/kicad_curl.h>
#include "core/wx_stl_compat.h" #include "core/wx_stl_compat.h"
#include <env_vars.h>
#include <background_jobs_monitor.h> #include <background_jobs_monitor.h>
#include "build_version.h" #include "build_version.h"
#include "paths.h" #include "paths.h"
@ -188,10 +189,9 @@ void PLUGIN_CONTENT_MANAGER::ReadEnvVar()
{ {
// Get 3rd party path // Get 3rd party path
const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables(); const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables();
auto it = env.find( wxT( "KICAD7_3RD_PARTY" ) );
if( it != env.end() && !it->second.GetValue().IsEmpty() ) if( std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( env, wxT( "3RD_PARTY" ) ) )
m_3rdparty_path = it->second.GetValue(); m_3rdparty_path = *v;
else else
m_3rdparty_path = PATHS::GetDefault3rdPartyPath(); m_3rdparty_path = PATHS::GetDefault3rdPartyPath();
} }

View File

@ -19,6 +19,7 @@
*/ */
#include <wildcards_and_files_ext.h> #include <wildcards_and_files_ext.h>
#include <env_vars.h>
#include <executable_names.h> #include <executable_names.h>
#include <pgm_base.h> #include <pgm_base.h>
#include <policy_keys.h> #include <policy_keys.h>
@ -213,16 +214,17 @@ int KICAD_MANAGER_CONTROL::NewFromTemplate( const TOOL_EVENT& aEvent )
wxFileName templatePath; wxFileName templatePath;
// KiCad system template path. // KiCad system template path.
ENV_VAR_MAP_CITER it = Pgm().GetLocalEnvVariables().find( "KICAD7_TEMPLATE_DIR" ); std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( Pgm().GetLocalEnvVariables(),
wxT( "TEMPLATE_DIR" ) );
if( it != Pgm().GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString ) if( v && !v->IsEmpty() )
{ {
templatePath.AssignDir( it->second.GetValue() ); templatePath.AssignDir( *v );
ps->AddTemplatesPage( _( "System Templates" ), templatePath ); ps->AddTemplatesPage( _( "System Templates" ), templatePath );
} }
// User template path. // User template path.
it = Pgm().GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" ); ENV_VAR_MAP_CITER it = Pgm().GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" );
if( it != Pgm().GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString ) if( it != Pgm().GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString )
{ {

View File

@ -39,7 +39,8 @@
#include <wx/filedlg.h> #include <wx/filedlg.h>
#include <project.h> #include <project.h>
#include <3d_viewer/eda_3d_viewer_frame.h> // for KICAD7_3DMODEL_DIR #include <env_vars.h>
#include <3d_viewer/eda_3d_viewer_frame.h>
#include <panel_fp_lib_table.h> #include <panel_fp_lib_table.h>
#include <lib_id.h> #include <lib_id.h>
#include <fp_lib_table.h> #include <fp_lib_table.h>
@ -1164,7 +1165,7 @@ void PANEL_FP_LIB_TABLE::populateEnvironReadOnlyTable()
unique.insert( FP_LIB_TABLE::GlobalPathEnvVariableName() ); unique.insert( FP_LIB_TABLE::GlobalPathEnvVariableName() );
// This special environment variable is used to locate 3d shapes // This special environment variable is used to locate 3d shapes
unique.insert( KICAD7_3DMODEL_DIR ); unique.insert( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
for( const wxString& evName : unique ) for( const wxString& evName : unique )
{ {

View File

@ -27,6 +27,7 @@
#include <panel_fp_properties_3d_model.h> #include <panel_fp_properties_3d_model.h>
#include <3d_viewer/eda_3d_viewer_frame.h> #include <3d_viewer/eda_3d_viewer_frame.h>
#include <env_vars.h>
#include <bitmaps.h> #include <bitmaps.h>
#include <widgets/grid_icon_text_helpers.h> #include <widgets/grid_icon_text_helpers.h>
#include <widgets/grid_text_button_helpers.h> #include <widgets/grid_text_button_helpers.h>
@ -77,7 +78,10 @@ PANEL_FP_PROPERTIES_3D_MODEL::PANEL_FP_PROPERTIES_3D_MODEL(
PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>(); PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
if( cfg->m_lastFootprint3dDir.IsEmpty() ) if( cfg->m_lastFootprint3dDir.IsEmpty() )
wxGetEnv( KICAD7_3DMODEL_DIR, &cfg->m_lastFootprint3dDir ); {
wxGetEnv( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ),
&cfg->m_lastFootprint3dDir );
}
// Icon showing warning/error information // Icon showing warning/error information
wxGridCellAttr* attr = new wxGridCellAttr; wxGridCellAttr* attr = new wxGridCellAttr;
@ -145,7 +149,7 @@ bool PANEL_FP_PROPERTIES_3D_MODEL::TransferDataFromWindow()
void PANEL_FP_PROPERTIES_3D_MODEL::ReloadModelsFromFootprint() void PANEL_FP_PROPERTIES_3D_MODEL::ReloadModelsFromFootprint()
{ {
wxString default_path; wxString default_path;
wxGetEnv( KICAD7_3DMODEL_DIR, &default_path ); wxGetEnv( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ), &default_path );
#ifdef __WINDOWS__ #ifdef __WINDOWS__
default_path.Replace( wxT( "/" ), wxT( "\\" ) ); default_path.Replace( wxT( "/" ), wxT( "\\" ) );
@ -296,9 +300,12 @@ void PANEL_FP_PROPERTIES_3D_MODEL::OnAdd3DModel( wxCommandEvent& )
// variable and fall back to the project path if necessary. // variable and fall back to the project path if necessary.
if( initialpath.IsEmpty() ) if( initialpath.IsEmpty() )
{ {
if( !wxGetEnv( wxT( "KICAD7_3DMODEL_DIR" ), &initialpath ) || initialpath.IsEmpty() ) if( !wxGetEnv( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ), &initialpath )
|| initialpath.IsEmpty() )
{
initialpath = prj.GetProjectPath(); initialpath = prj.GetProjectPath();
} }
}
if( !sidx.empty() ) if( !sidx.empty() )
{ {

View File

@ -37,6 +37,7 @@
#include <string> #include <string>
#include <eda_base_frame.h> #include <eda_base_frame.h>
#include <env_vars.h>
#include <gal/color4d.h> #include <gal/color4d.h>
#include <gestfich.h> #include <gestfich.h>
#include <trace_helpers.h> #include <trace_helpers.h>
@ -578,20 +579,28 @@ wxString SCRIPTING::PyScriptingPath( PATH_TYPE aPathType )
case STOCK: case STOCK:
path = PATHS::GetStockScriptingPath(); path = PATHS::GetStockScriptingPath();
break; break;
case USER: case USER:
path = PATHS::GetUserScriptingPath(); path = PATHS::GetUserScriptingPath();
break; break;
case THIRDPARTY:
const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables();
auto it = env.find( "KICAD7_3RD_PARTY" );
if( it != env.end() && !it->second.GetValue().IsEmpty() ) case THIRDPARTY:
path = it->second.GetValue(); {
const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables();
if( std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( env,
wxT( "3RD_PARTY" ) ) )
{
path = *v;
}
else else
{
path = PATHS::GetDefault3rdPartyPath(); path = PATHS::GetDefault3rdPartyPath();
}
break; break;
} }
}
wxFileName scriptPath( path ); wxFileName scriptPath( path );
scriptPath.MakeAbsolute(); scriptPath.MakeAbsolute();