diff --git a/common/jobs/job_sym_upgrade.h b/common/jobs/job_sym_upgrade.h new file mode 100644 index 0000000000..77900396eb --- /dev/null +++ b/common/jobs/job_sym_upgrade.h @@ -0,0 +1,42 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2022 Mark Roszko + * Copyright (C) 1992-2022 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 . + */ + +#ifndef JOB_SYM_UPGRADE_H +#define JOB_SYM_UPGRADE_H + +#include +#include "job.h" + +class JOB_SYM_UPGRADE : public JOB +{ +public: + JOB_SYM_UPGRADE( bool aIsCli ) : + JOB( "symupgrade", aIsCli ), + m_libraryPath(), + m_force( false ) + { + } + + wxString m_libraryPath; + + bool m_force; +}; + +#endif \ No newline at end of file diff --git a/eeschema/eeschema_jobs_handler.cpp b/eeschema/eeschema_jobs_handler.cpp index ddf587d288..16ef765eba 100644 --- a/eeschema/eeschema_jobs_handler.cpp +++ b/eeschema/eeschema_jobs_handler.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -37,6 +38,9 @@ #include +#include +#include + #include #include #include @@ -57,6 +61,8 @@ EESCHEMA_JOBS_HANDLER::EESCHEMA_JOBS_HANDLER() std::bind( &EESCHEMA_JOBS_HANDLER::JobExportPdf, this, std::placeholders::_1 ) ); Register( "svg", std::bind( &EESCHEMA_JOBS_HANDLER::JobExportSvg, this, std::placeholders::_1 ) ); + Register( "symupgrade", + std::bind( &EESCHEMA_JOBS_HANDLER::JobExportSymLibUpgrade, this, std::placeholders::_1 ) ); } @@ -309,4 +315,47 @@ int EESCHEMA_JOBS_HANDLER::JobExportBom( JOB* aJob ) } return CLI::EXIT_CODES::ERR_UNKNOWN; +} + + +int EESCHEMA_JOBS_HANDLER::JobExportSymLibUpgrade( JOB* aJob ) +{ + JOB_SYM_UPGRADE* upgradeJob = dynamic_cast( aJob ); + + SCH_SEXPR_PLUGIN_CACHE schLibrary( upgradeJob->m_libraryPath ); + + try + { + schLibrary.Load(); + } + catch( ... ) + { + wxFprintf( stderr, _( "Unable to load library\n" ) ); + return CLI::EXIT_CODES::ERR_UNKNOWN; + } + + bool shouldSave = upgradeJob->m_force + || schLibrary.GetFileFormatVersionAtLoad() < SEXPR_SYMBOL_LIB_FILE_VERSION; + + if( shouldSave ) + { + wxPrintf( _( "Saving symbol library in updated format\n" ) ); + + try + { + schLibrary.SetModified(); + schLibrary.Save(); + } + catch( ... ) + { + wxFprintf( stderr, _( "Unable to save library\n" ) ); + return CLI::EXIT_CODES::ERR_UNKNOWN; + } + } + else + { + wxPrintf( _( "Symbol library was not updated\n" ) ); + } + + return CLI::EXIT_CODES::OK; } \ No newline at end of file diff --git a/eeschema/eeschema_jobs_handler.h b/eeschema/eeschema_jobs_handler.h index 97af4293dd..6e2b273364 100644 --- a/eeschema/eeschema_jobs_handler.h +++ b/eeschema/eeschema_jobs_handler.h @@ -43,6 +43,7 @@ public: int JobExportNetlist( JOB* aJob ); int JobExportPdf( JOB* aJob ); int JobExportSvg( JOB* aJob ); + int JobExportSymLibUpgrade( JOB* aJob ); /** * Configures the SCH_RENDER_SETTINGS object with the correct data to be used with plotting diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.cpp index 7702bc272a..0f9f5c8ede 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.cpp @@ -38,7 +38,7 @@ SCH_SEXPR_PLUGIN_CACHE::SCH_SEXPR_PLUGIN_CACHE( const wxString& aFullPathAndFileName ) : SCH_LIB_PLUGIN_CACHE( aFullPathAndFileName ) { - m_versionMajor = -1; + m_fileFormatVersionAtLoad = 0; } @@ -76,6 +76,7 @@ void SCH_SEXPR_PLUGIN_CACHE::Load() // Remember the file modification time of library file when the cache snapshot was made, // so that in a networked environment we will reload the cache as needed. m_fileModTime = GetLibModificationTime(); + SetFileFormatVersionAtLoad( parser.GetParsedRequiredVersion() ); } diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.h b/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.h index 910229ac5d..17284e73b4 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.h +++ b/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.h @@ -56,9 +56,14 @@ public: static void SaveSymbol( LIB_SYMBOL* aSymbol, OUTPUTFORMATTER& aFormatter, int aNestLevel = 0, const wxString& aLibName = wxEmptyString ); + void SetFileFormatVersionAtLoad( int aVersion ) { m_fileFormatVersionAtLoad = aVersion; } + int GetFileFormatVersionAtLoad() const { return m_fileFormatVersionAtLoad; } + private: friend SCH_SEXPR_PLUGIN; + int m_fileFormatVersionAtLoad; + static void saveSymbolDrawItem( LIB_ITEM* aItem, OUTPUTFORMATTER& aFormatter, int aNestLevel ); static void saveField( LIB_FIELD* aField, OUTPUTFORMATTER& aFormatter, int aNestLevel ); @@ -69,8 +74,6 @@ private: static void saveDcmInfoAsFields( LIB_SYMBOL* aSymbol, OUTPUTFORMATTER& aFormatter, int& aNextFreeFieldId, int aNestLevel ); - - int m_versionMajor; }; #endif // _SCH_SEXPR_LIB_PLUGIN_CACHE_ diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_parser.h b/eeschema/sch_plugins/kicad/sch_sexpr_parser.h index a9dce98c3a..a307b4eb71 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_parser.h +++ b/eeschema/sch_plugins/kicad/sch_sexpr_parser.h @@ -108,6 +108,8 @@ public: void ParseSchematic( SCH_SHEET* aSheet, bool aIsCopyablyOnly = false, int aFileVersion = SEXPR_SCHEMATIC_FILE_VERSION ); + int GetParsedRequiredVersion() const { return m_requiredVersion; } + private: void checkpoint(); diff --git a/kicad/CMakeLists.txt b/kicad/CMakeLists.txt index 35f830ec9f..1115eec15d 100644 --- a/kicad/CMakeLists.txt +++ b/kicad/CMakeLists.txt @@ -32,6 +32,7 @@ set( KICAD_SRCS cli/command_export_sch_pdf.cpp cli/command_export_sch_svg.cpp cli/command_sch_export.cpp + cli/command_sym_upgrade.cpp dialogs/dialog_template_selector_base.cpp dialogs/dialog_template_selector.cpp dialogs/panel_kicad_launcher_base.cpp diff --git a/kicad/cli/command_fp_upgrade.cpp b/kicad/cli/command_fp_upgrade.cpp index 8bd4a926e8..b20a1ef671 100644 --- a/kicad/cli/command_fp_upgrade.cpp +++ b/kicad/cli/command_fp_upgrade.cpp @@ -33,7 +33,8 @@ CLI::FP_UPGRADE_COMMAND::FP_UPGRADE_COMMAND() : EXPORT_PCB_BASE_COMMAND( "upgrade" ) { m_argParser.add_argument( ARG_FORCE ) - .help( UTF8STDSTR( _( "Forces the footprint to be resaved regardless of versioning" ) ) ) + .help( UTF8STDSTR( + _( "Forces the footprint library to be resaved regardless of versioning" ) ) ) .implicit_value( true ) .default_value( false ); } @@ -47,11 +48,11 @@ int CLI::FP_UPGRADE_COMMAND::Perform( KIWAY& aKiway ) if( !wxDir::Exists( fpJob->m_libraryPath ) ) { - wxFprintf( stderr, _( "Footprint file does not exist or is not accessible\n" ) ); + wxFprintf( stderr, _( "Footprint path does not exist or is not accessible\n" ) ); return EXIT_CODES::ERR_INVALID_INPUT_FILE; } int exitCode = aKiway.ProcessJob( KIWAY::FACE_PCB, fpJob.get() ); return exitCode; -} \ No newline at end of file +} diff --git a/kicad/cli/command_sym.h b/kicad/cli/command_sym.h new file mode 100644 index 0000000000..0ff86004a4 --- /dev/null +++ b/kicad/cli/command_sym.h @@ -0,0 +1,34 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2022 Mark Roszko + * Copyright (C) 1992-2022 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 . + */ + +#ifndef COMMAND_SYM_H +#define COMMAND_SYM_H + +#include "command.h" + +namespace CLI +{ +struct SYM_COMMAND : public COMMAND +{ + SYM_COMMAND() : COMMAND( "sym" ) {} +}; +} + +#endif \ No newline at end of file diff --git a/kicad/cli/command_sym_upgrade.cpp b/kicad/cli/command_sym_upgrade.cpp new file mode 100644 index 0000000000..ba3b90c539 --- /dev/null +++ b/kicad/cli/command_sym_upgrade.cpp @@ -0,0 +1,58 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2022 Mark Roszko + * Copyright (C) 1992-2022 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 . + */ + +#include "command_sym_upgrade.h" +#include +#include "jobs/job_sym_upgrade.h" +#include +#include +#include +#include + +#include + +#define ARG_FORCE "--force" + +CLI::SYM_UPGRADE_COMMAND::SYM_UPGRADE_COMMAND() : EXPORT_PCB_BASE_COMMAND( "upgrade" ) +{ + m_argParser.add_argument( ARG_FORCE ) + .help( UTF8STDSTR( + _( "Forces the symbol library to be resaved regardless of versioning" ) ) ) + .implicit_value( true ) + .default_value( false ); +} + + +int CLI::SYM_UPGRADE_COMMAND::Perform( KIWAY& aKiway ) +{ + std::unique_ptr symJob = std::make_unique( true ); + + symJob->m_libraryPath = FROM_UTF8( m_argParser.get( ARG_INPUT ).c_str() ); + + if( !wxFile::Exists( symJob->m_libraryPath ) ) + { + wxFprintf( stderr, _( "Symbol file does not exist or is not accessible\n" ) ); + return EXIT_CODES::ERR_INVALID_INPUT_FILE; + } + + int exitCode = aKiway.ProcessJob( KIWAY::FACE_SCH, symJob.get() ); + + return exitCode; +} \ No newline at end of file diff --git a/kicad/cli/command_sym_upgrade.h b/kicad/cli/command_sym_upgrade.h new file mode 100644 index 0000000000..05c2e145d2 --- /dev/null +++ b/kicad/cli/command_sym_upgrade.h @@ -0,0 +1,37 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2022 Mark Roszko + * Copyright (C) 1992-2022 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 . + */ + +#ifndef COMMAND_SYM_UPGRADE_H +#define COMMAND_SYM_UPGRADE_H + +#include "command_export_pcb_base.h" + +namespace CLI +{ +class SYM_UPGRADE_COMMAND : public EXPORT_PCB_BASE_COMMAND +{ +public: + SYM_UPGRADE_COMMAND(); + + int Perform( KIWAY& aKiway ) override; +}; +} // namespace CLI + +#endif \ No newline at end of file diff --git a/kicad/kicad_cli.cpp b/kicad/kicad_cli.cpp index f81d26b878..31916db9d5 100644 --- a/kicad/kicad_cli.cpp +++ b/kicad/kicad_cli.cpp @@ -63,6 +63,8 @@ #include "cli/command_fp_upgrade.h" #include "cli/command_sch.h" #include "cli/command_sch_export.h" +#include "cli/command_sym.h" +#include "cli/command_sym_upgrade.h" #include "cli/exit_codes.h" // a dummy to quiet linking with EDA_BASE_FRAME::config(); @@ -127,6 +129,8 @@ static CLI::EXPORT_SCH_PDF_COMMAND exportSchPdfCmd{}; static CLI::EXPORT_SCH_SVG_COMMAND exportSchSvgCmd{}; static CLI::FP_COMMAND fpCmd{}; static CLI::FP_UPGRADE_COMMAND fpUpgradeCmd{}; +static CLI::SYM_COMMAND symCmd{}; +static CLI::SYM_UPGRADE_COMMAND symUpgradeCmd{}; static std::vector commandStack = { { @@ -166,6 +170,14 @@ static std::vector commandStack = { } } }, + { + &symCmd, + { + { + &symUpgradeCmd + } + } + }, };