From 7b0bb59b37e512f36dbdb13ee974e94691737537 Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Tue, 16 Jan 2024 17:20:45 -0500 Subject: [PATCH] Remove hard-coded versioned env vars in most places --- 3d-viewer/3d_viewer/eda_3d_viewer_frame.h | 5 -- .../dialog_global_lib_table_config.cpp | 11 +++- common/env_vars.cpp | 61 +++++++++++++++---- common/filename_resolver.cpp | 11 +++- common/fp_lib_table.cpp | 25 +++++--- common/settings/common_settings.cpp | 33 +++++----- common/settings/settings_manager.cpp | 6 +- eeschema/symbol_lib_table.cpp | 25 +++++--- include/env_vars.h | 21 +++++++ kicad/kicad.cpp | 15 +++-- kicad/pcm/pcm.cpp | 6 +- kicad/tools/kicad_manager_control.cpp | 10 +-- pcbnew/dialogs/panel_fp_lib_table.cpp | 5 +- .../dialogs/panel_fp_properties_3d_model.cpp | 13 +++- scripting/python_scripting.cpp | 19 ++++-- 15 files changed, 187 insertions(+), 79 deletions(-) diff --git a/3d-viewer/3d_viewer/eda_3d_viewer_frame.h b/3d-viewer/3d_viewer/eda_3d_viewer_frame.h index 9a5a73aee5..222a69986c 100644 --- a/3d-viewer/3d_viewer/eda_3d_viewer_frame.h +++ b/3d-viewer/3d_viewer/eda_3d_viewer_frame.h @@ -41,11 +41,6 @@ #include // 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) // Forward declarations diff --git a/common/dialogs/dialog_global_lib_table_config.cpp b/common/dialogs/dialog_global_lib_table_config.cpp index 6ed4a1ceb6..e3a91a3e39 100644 --- a/common/dialogs/dialog_global_lib_table_config.cpp +++ b/common/dialogs/dialog_global_lib_table_config.cpp @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -109,8 +110,14 @@ bool DIALOG_GLOBAL_LIB_TABLE_CONFIG::TransferDataToWindow() GlobalPathsAppend( &ss, m_faceType ); - wxString templatePath = - Pgm().GetLocalEnvVariables().at( wxT( "KICAD7_TEMPLATE_DIR" ) ).GetValue(); + wxString templatePath; + const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables(); + + if( std::optional v = ENV_VAR::GetVersionedEnvVarValue( envVars, + wxT( "TEMPLATE_DIR" ) ) ) + { + templatePath = *v; + } if( !templatePath.IsEmpty() ) ss.AddPaths( templatePath, 0 ); diff --git a/common/env_vars.cpp b/common/env_vars.cpp index f8e11193a6..50e3f367d6 100644 --- a/common/env_vars.cpp +++ b/common/env_vars.cpp @@ -17,10 +17,13 @@ * with this program. If not, see . */ +#include #include +#include #include +#include #include #include @@ -35,18 +38,23 @@ using STRING_MAP = std::map; */ static const ENV_VAR::ENV_VAR_LIST predefinedEnvVars = { wxS( "KIPRJMOD" ), - wxS( "KICAD7_SYMBOL_DIR" ), - wxS( "KICAD7_3DMODEL_DIR" ), - wxS( "KICAD7_FOOTPRINT_DIR" ), - wxS( "KICAD7_TEMPLATE_DIR" ), + 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" ), - 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 ) { + if( versionedEnvVarRegex.Matches( aEnvVar ) ) + return true; + for( const wxString& s : predefinedEnvVars ) { 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 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[wxS( "KICAD7_FOOTPRINT_DIR" )] = + aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) )] = _( "The base path of locally installed system " "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)."); - aMap[wxS( "KICAD7_SYMBOL_DIR" )] = + aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) )] = _( "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."); aMap[wxS( "KICAD_USER_TEMPLATE_DIR" )] = _( "Optional. Can be defined if you want to create your own project " "templates folder."); - aMap[wxS( "KICAD7_3RD_PARTY" )] = + aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) )] = _( "A directory containing 3rd party plugins, libraries and other " "downloadable content."); 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 " "project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be defined as a " "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" ); - aMap[wxS( "KICAD7_USER_SCRIPTING_DIR" )] = + aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "USER_SCRIPTING_DIR" ) )] = _( "A directory containing user-specific scripts installed with KiCad" ); // Deprecated vars diff --git a/common/filename_resolver.cpp b/common/filename_resolver.cpp index a0d7c082f1..10f9637d5c 100644 --- a/common/filename_resolver.cpp +++ b/common/filename_resolver.cpp @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -338,7 +339,8 @@ wxString FILENAME_RESOLVER::ResolvePath( const wxString& aFileName, const wxStri if( !tname.StartsWith( wxS( ":" ) ) ) { 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( tname ); fullPath = ExpandEnvVarSubstitutions( fullPath, m_project ); @@ -437,7 +439,10 @@ bool FILENAME_RESOLVER::addPath( const SEARCH_PATH& aPath ) 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( "${KISYS3DMOD}" ) || aPath.m_Pathvar == wxS( "$(KISYS3DMOD)" ) ) { @@ -812,7 +817,7 @@ bool FILENAME_RESOLVER::GetKicadPaths( std::list< wxString >& paths ) const } if( !hasKisys3D ) - paths.emplace_back( wxS("KICAD7_3DMODEL_DIR") ); + paths.emplace_back( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) ); return true; } diff --git a/common/fp_lib_table.cpp b/common/fp_lib_table.cpp index 35a0d69ba0..1127f73dd2 100644 --- a/common/fp_lib_table.cpp +++ b/common/fp_lib_table.cpp @@ -25,6 +25,7 @@ #include +#include #include #include #include @@ -502,7 +503,7 @@ FOOTPRINT* FP_LIB_TABLE::FootprintLoadWithOptionalNickname( const LIB_ID& aFootp 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 ); // consider a directory to be a lib if it's name ends with .pretty and - // it is under $KICAD7_3RD_PARTY/footprints// i.e. has nested level of at least +3 + // it is under $KICADn_3RD_PARTY/footprints// i.e. has nested level of at least +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(); parts.RemoveAt( 0, m_prefix_dir_count ); - parts.Insert( wxS( "${KICAD7_3RD_PARTY}" ), 0 ); + parts.Insert( versionedPath, 0 ); wxString libPath = wxJoin( parts, '/' ); @@ -588,11 +592,12 @@ bool FP_LIB_TABLE::LoadGlobalTable( FP_LIB_TABLE& aTable ) SystemDirsAppend( &ss ); - wxString templatePath = - Pgm().GetLocalEnvVariables().at( wxT( "KICAD7_TEMPLATE_DIR" ) ).GetValue(); + const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables(); + std::optional v = ENV_VAR::GetVersionedEnvVarValue( envVars, + wxT( "TEMPLATE_DIR" ) ); - if( !templatePath.IsEmpty() ) - ss.AddPaths( templatePath, 0 ); + if( v && !v->IsEmpty() ) + ss.AddPaths( *v, 0 ); 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(); KICAD_SETTINGS* settings = mgr.GetAppSettings(); - wxString packagesPath = Pgm().GetLocalEnvVariables().at( wxT( "KICAD7_3RD_PARTY" ) ).GetValue(); + const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables(); + wxString packagesPath; + + if( std::optional v = ENV_VAR::GetVersionedEnvVarValue( env, wxT( "3RD_PARTY" ) ) ) + packagesPath = *v; if( settings->m_PcmLibAutoAdd ) { diff --git a/common/settings/common_settings.cpp b/common/settings/common_settings.cpp index d0c18e0974..53c0fc438f 100644 --- a/common/settings/common_settings.cpp +++ b/common/settings/common_settings.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -36,14 +37,7 @@ ///! The following environment variables will never be migrated from a previous version -const std::set envVarBlacklist = - { - wxT( "KICAD7_SYMBOL_DIR" ), - wxT( "KICAD7_FOOTPRINT_DIR" ), - wxT( "KICAD7_TEMPLATES_DIR" ), - wxT( "KICAD7_3DMODEL_DIR" ) - }; - +const wxRegEx versionedEnvVarRegex( wxS( "KICAD[0-9]+_[A-Z0-9_]+(_DIR)?" ) ); ///! Update the schema version whenever a migration is required const int commonSchemaVersion = 3; @@ -543,7 +537,7 @@ bool COMMON_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg ) while( aCfg->GetNextEntry( key, index ) ) { - if( envVarBlacklist.count( key ) ) + if( versionedEnvVarRegex.Matches( key ) ) { wxLogTrace( traceSettings, wxT( "Migrate Env: %s is blacklisted; skipping." ), key ); @@ -633,21 +627,23 @@ void COMMON_SETTINGS::InitializeEnvironment() wxFileName path( basePath ); path.AppendDir( wxT( "footprints" ) ); - addVar( wxT( "KICAD7_FOOTPRINT_DIR" ), path.GetFullPath() ); + addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ), path.GetFullPath() ); path = basePath; 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( "KICAD7_3RD_PARTY" ), PATHS::GetDefault3rdPartyPath() ); + addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ), + PATHS::GetDefault3rdPartyPath() ); path = basePath; 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 ) ) continue; - // Don't add KICAD7_3DMODEL_DIR, one of its legacy equivalents, or KIPRJMOD from a - // config file. They're system variables are are defined at runtime. - if( al.m_Alias == wxS( "${KICAD7_3DMODEL_DIR}" ) || al.m_Alias == wxS( "${KIPRJMOD}" ) + // Don't add KICADn_3DMODEL_DIR, one of its legacy equivalents, or KIPRJMOD from a + // config file. They're system variables which are defined at runtime. + 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( "$(KISYS3DMOD)" ) ) { diff --git a/common/settings/settings_manager.cpp b/common/settings/settings_manager.cpp index 438607fc77..f838f2afac 100644 --- a/common/settings/settings_manager.cpp +++ b/common/settings/settings_manager.cpp @@ -615,7 +615,11 @@ bool SETTINGS_MANAGER::MigrateIfNeeded() wxT( "KICAD7_SYMBOL_DIR" ), wxT( "KICAD7_3DMODEL_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 wxT( "KICAD_PTEMPLATES" ), diff --git a/eeschema/symbol_lib_table.cpp b/eeschema/symbol_lib_table.cpp index e0b5dff749..d6d3c46fdc 100644 --- a/eeschema/symbol_lib_table.cpp +++ b/eeschema/symbol_lib_table.cpp @@ -24,6 +24,7 @@ */ +#include #include #include #include @@ -546,7 +547,7 @@ LIB_SYMBOL* SYMBOL_LIB_TABLE::LoadSymbolWithOptionalNickname( const LIB_ID& aLib 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 ); // consider a file to be a lib if it's name ends with .kicad_sym and - // it is under $KICAD7_3RD_PARTY/symbols// i.e. has nested level of at least +2 + // it is under $KICADn_3RD_PARTY/symbols// i.e. has nested level of at least +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(); parts.RemoveAt( 0, m_prefix_dir_count ); - parts.Insert( "${KICAD7_3RD_PARTY}", 0 ); + parts.Insert( versionedPath, 0 ); parts.Add( file.GetFullName() ); wxString libPath = wxJoin( parts, '/' ); @@ -633,11 +637,12 @@ bool SYMBOL_LIB_TABLE::LoadGlobalTable( SYMBOL_LIB_TABLE& aTable ) SystemDirsAppend( &ss ); - wxString templatePath = - Pgm().GetLocalEnvVariables().at( wxT( "KICAD7_TEMPLATE_DIR" ) ).GetValue(); + const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables(); + std::optional v = ENV_VAR::GetVersionedEnvVarValue( envVars, + wxT( "TEMPLATE_DIR" ) ); - if( !templatePath.IsEmpty() ) - ss.AddPaths( templatePath, 0 ); + if( v && !v->IsEmpty() ) + ss.AddPaths( *v, 0 ); wxString fileName = ss.FindValidPath( global_tbl_name ); @@ -658,7 +663,11 @@ bool SYMBOL_LIB_TABLE::LoadGlobalTable( SYMBOL_LIB_TABLE& aTable ) 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 v = ENV_VAR::GetVersionedEnvVarValue( vars, wxT( "3RD_PARTY" ) ) ) + packagesPath = *v; if( settings->m_PcmLibAutoAdd ) { diff --git a/include/env_vars.h b/include/env_vars.h index 3d5e3940ca..ee7b78b2ac 100644 --- a/include/env_vars.h +++ b/include/env_vars.h @@ -26,9 +26,12 @@ #define ENV_VARS_H #include +#include #include #include +class ENV_VAR_ITEM; + namespace ENV_VAR { using ENV_VAR_LIST = std::vector; @@ -47,6 +50,24 @@ namespace ENV_VAR */ 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*_, 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 GetVersionedEnvVarValue( const std::map& aMap, + const wxString& aBaseName ); + /** * Look up long-form help text for a given environment variable. * diff --git a/kicad/kicad.cpp b/kicad/kicad.cpp index ca94ec724a..8c4ac5cf7b 100644 --- a/kicad/kicad.cpp +++ b/kicad/kicad.cpp @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -198,18 +199,20 @@ bool PGM_KICAD::OnPgmInit() m_bm.m_search.AddPaths( fn.GetPath() ); } - // The KICAD7_TEMPLATE_DIR takes precedence over the search stack template path. - ENV_VAR_MAP_CITER it = GetLocalEnvVariables().find( "KICAD7_TEMPLATE_DIR" ); - - if( it != GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString ) - m_bm.m_search.Insert( it->second.GetValue(), 0 ); + // The versioned TEMPLATE_DIR takes precedence over the search stack template path. + if( std::optional v = ENV_VAR::GetVersionedEnvVarValue( GetLocalEnvVariables(), + wxT( "TEMPLATE_DIR" ) ) ) + { + 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 // The default user search path is inside KIPLATFORM::ENV::GetDocumentsPath() m_bm.m_search.Insert( PATHS::GetUserTemplatesPath(), 0 ); // ...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 ) m_bm.m_search.Insert( it->second.GetValue(), 0 ); diff --git a/kicad/pcm/pcm.cpp b/kicad/pcm/pcm.cpp index ba466cac5f..dbbb7d7e3e 100644 --- a/kicad/pcm/pcm.cpp +++ b/kicad/pcm/pcm.cpp @@ -24,6 +24,7 @@ #include #include "core/wx_stl_compat.h" +#include #include #include "build_version.h" #include "paths.h" @@ -188,10 +189,9 @@ void PLUGIN_CONTENT_MANAGER::ReadEnvVar() { // Get 3rd party path const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables(); - auto it = env.find( wxT( "KICAD7_3RD_PARTY" ) ); - if( it != env.end() && !it->second.GetValue().IsEmpty() ) - m_3rdparty_path = it->second.GetValue(); + if( std::optional v = ENV_VAR::GetVersionedEnvVarValue( env, wxT( "3RD_PARTY" ) ) ) + m_3rdparty_path = *v; else m_3rdparty_path = PATHS::GetDefault3rdPartyPath(); } diff --git a/kicad/tools/kicad_manager_control.cpp b/kicad/tools/kicad_manager_control.cpp index 04694e7709..b100cd806f 100644 --- a/kicad/tools/kicad_manager_control.cpp +++ b/kicad/tools/kicad_manager_control.cpp @@ -19,6 +19,7 @@ */ #include +#include #include #include #include @@ -213,16 +214,17 @@ int KICAD_MANAGER_CONTROL::NewFromTemplate( const TOOL_EVENT& aEvent ) wxFileName templatePath; // KiCad system template path. - ENV_VAR_MAP_CITER it = Pgm().GetLocalEnvVariables().find( "KICAD7_TEMPLATE_DIR" ); + std::optional 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 ); } // 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 ) { diff --git a/pcbnew/dialogs/panel_fp_lib_table.cpp b/pcbnew/dialogs/panel_fp_lib_table.cpp index eb8f392cf2..a08b3f3ac6 100644 --- a/pcbnew/dialogs/panel_fp_lib_table.cpp +++ b/pcbnew/dialogs/panel_fp_lib_table.cpp @@ -39,7 +39,8 @@ #include #include -#include <3d_viewer/eda_3d_viewer_frame.h> // for KICAD7_3DMODEL_DIR +#include +#include <3d_viewer/eda_3d_viewer_frame.h> #include #include #include @@ -1164,7 +1165,7 @@ void PANEL_FP_LIB_TABLE::populateEnvironReadOnlyTable() unique.insert( FP_LIB_TABLE::GlobalPathEnvVariableName() ); // 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 ) { diff --git a/pcbnew/dialogs/panel_fp_properties_3d_model.cpp b/pcbnew/dialogs/panel_fp_properties_3d_model.cpp index 137d56bfc3..0f490a3079 100644 --- a/pcbnew/dialogs/panel_fp_properties_3d_model.cpp +++ b/pcbnew/dialogs/panel_fp_properties_3d_model.cpp @@ -27,6 +27,7 @@ #include #include <3d_viewer/eda_3d_viewer_frame.h> +#include #include #include #include @@ -77,7 +78,10 @@ PANEL_FP_PROPERTIES_3D_MODEL::PANEL_FP_PROPERTIES_3D_MODEL( PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings(); 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 wxGridCellAttr* attr = new wxGridCellAttr; @@ -145,7 +149,7 @@ bool PANEL_FP_PROPERTIES_3D_MODEL::TransferDataFromWindow() void PANEL_FP_PROPERTIES_3D_MODEL::ReloadModelsFromFootprint() { wxString default_path; - wxGetEnv( KICAD7_3DMODEL_DIR, &default_path ); + wxGetEnv( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ), &default_path ); #ifdef __WINDOWS__ default_path.Replace( wxT( "/" ), wxT( "\\" ) ); @@ -296,8 +300,11 @@ void PANEL_FP_PROPERTIES_3D_MODEL::OnAdd3DModel( wxCommandEvent& ) // variable and fall back to the project path if necessary. 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(); + } } if( !sidx.empty() ) diff --git a/scripting/python_scripting.cpp b/scripting/python_scripting.cpp index 56e337d9dc..fef827a75b 100644 --- a/scripting/python_scripting.cpp +++ b/scripting/python_scripting.cpp @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -578,20 +579,28 @@ wxString SCRIPTING::PyScriptingPath( PATH_TYPE aPathType ) case STOCK: path = PATHS::GetStockScriptingPath(); break; + case USER: path = PATHS::GetUserScriptingPath(); 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() ) - path = it->second.GetValue(); + case THIRDPARTY: + { + const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables(); + + if( std::optional v = ENV_VAR::GetVersionedEnvVarValue( env, + wxT( "3RD_PARTY" ) ) ) + { + path = *v; + } else + { path = PATHS::GetDefault3rdPartyPath(); + } break; } + } wxFileName scriptPath( path ); scriptPath.MakeAbsolute();