From 70a57505ded04e3d0b2de70b84420a5a3d2865a5 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Sun, 22 Jan 2023 15:36:54 -0800 Subject: [PATCH] Migrate lib tables to KICAD7 on open If KiCad does not detect an environmental variable set for the old library table variables AND the library table being opened references the unset env var, we will dynamically update the env var to the new value Fixes https://gitlab.com/kicad/code/kicad/issues/13464 --- common/lib_table_base.cpp | 28 ++++++++++++++++++++++++++++ include/lib_table_base.h | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/common/lib_table_base.cpp b/common/lib_table_base.cpp index 7ed5dc3f87..8cdd1c39b4 100644 --- a/common/lib_table_base.cpp +++ b/common/lib_table_base.cpp @@ -326,6 +326,31 @@ bool LIB_TABLE::InsertRow( LIB_TABLE_ROW* aRow, bool doReplace ) } +bool LIB_TABLE::migrate() +{ + bool table_updated = false; + + for( LIB_TABLE_ROW& row : m_rows ) + { + bool row_updated = false; + wxString uri = row.GetFullURI( true ); + + // If the uri still has a variable in it, that means that the user does not have + // these vars defined. We update the old vars to the KICAD7 versions on load + row_updated |= ( uri.Replace( wxS( "${KICAD5_" ), wxS( "${KICAD7_" ), false ) > 0 ); + row_updated |= ( uri.Replace( wxS( "${KICAD6_" ), wxS( "${KICAD7_" ), false ) > 0 ); + + if( row_updated ) + { + row.SetFullURI( uri ); + table_updated = true; + } + } + + return table_updated; +} + + void LIB_TABLE::Load( const wxString& aFileName ) { // It's OK if footprint library tables are missing. @@ -335,6 +360,9 @@ void LIB_TABLE::Load( const wxString& aFileName ) LIB_TABLE_LEXER lexer( &reader ); Parse( &lexer ); + + if( migrate() && wxFileName::IsFileWritable( aFileName ) ) + Save( aFileName ); } } diff --git a/include/lib_table_base.h b/include/lib_table_base.h index cd77f81b1b..21a813b270 100644 --- a/include/lib_table_base.h +++ b/include/lib_table_base.h @@ -521,6 +521,14 @@ protected: */ LIB_TABLE_ROW* findRow( const wxString& aNickname, bool aCheckIfEnabled = false ) const; + /** + * Updates the env vars from older version of KiCad, provided they do not currently + * resolve to anything + * + * @return True if the tables were modified + */ + bool migrate(); + void reindex() { std::lock_guard lock( m_nickIndexMutex );