kicad-source/common/jobs/jobset.cpp

339 lines
8.8 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2024 Mark Roszko <mark.roszko@gmail.com>
* 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/>.
*/
2025-01-12 12:11:56 +00:00
#include <i18n_utility.h>
#include <json_common.h>
#include <settings/parameters.h>
#include <wildcards_and_files_ext.h>
#include <jobs/jobset.h>
#include <jobs/job_registry.h>
#include <jobs/jobs_output_folder.h>
#include <jobs/jobs_output_archive.h>
#include <kiid.h>
2024-12-23 23:07:08 -05:00
#include <reporter.h>
#include <algorithm>
#include <memory>
const int jobsFileSchemaVersion = 1;
2025-01-12 12:11:56 +00:00
KICOMMON_API std::map<JOBSET_DESTINATION_T, JOBSET_DESTINATION_T_INFO> JobsetDestinationTypeInfos =
2025-03-03 18:34:20 +00:00
{
{ JOBSET_DESTINATION_T::FOLDER,
2025-01-12 12:11:56 +00:00
{ _HKI( "Folder" ), BITMAPS::small_folder, true, "" } },
2025-03-03 18:34:20 +00:00
{ JOBSET_DESTINATION_T::ARCHIVE,
2025-01-12 12:11:56 +00:00
{ _HKI( "Archive" ), BITMAPS::zip, false, FILEEXT::ZipFileWildcard() } },
};
2025-03-03 18:34:20 +00:00
NLOHMANN_JSON_SERIALIZE_ENUM( JOBSET_DESTINATION_T,
{
2025-03-03 18:34:20 +00:00
{ JOBSET_DESTINATION_T::FOLDER, "folder" },
{ JOBSET_DESTINATION_T::ARCHIVE, "archive" }
} )
KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_JOB& f )
{
j = nlohmann::json{ { "id", f.m_id },
{ "type", f.m_type },
{ "description", f.m_description },
{ "settings", nlohmann::json::object( {} ) }
};
f.m_job->ToJson( j.at( "settings" ) );
}
KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_JOB& f )
{
j.at( "type" ).get_to( f.m_type );
j.at( "id" ).get_to( f.m_id );
f.m_description = j.value( "description", "" );
nlohmann::json settings_obj = j.at( "settings" );
2024-11-09 22:49:54 -05:00
f.m_job.reset( JOB_REGISTRY::CreateInstance<JOB>( f.m_type ) );
if( f.m_job != nullptr )
{
f.m_job->FromJson( settings_obj );
}
}
2025-03-03 18:34:20 +00:00
KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_DESTINATION& destination )
{
2025-03-03 18:34:20 +00:00
j = nlohmann::json{ { "id", destination.m_id },
{ "type", destination.m_type },
{ "only", destination.m_only },
{ "description", destination.m_description },
2024-12-29 07:16:38 -05:00
{ "settings", nlohmann::json::object( {} ) }
};
2025-03-03 18:34:20 +00:00
destination.m_outputHandler->ToJson( j.at( "settings" ) );
}
2025-03-03 18:34:20 +00:00
KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_DESTINATION& destination )
{
2025-01-03 13:32:07 +00:00
// During 9.0 development outputs didn't get ids.
if( j.contains( "id" ) )
2025-03-03 18:34:20 +00:00
j.at( "id" ).get_to( destination.m_id );
2025-01-03 13:32:07 +00:00
else
2025-03-03 18:34:20 +00:00
destination.m_id = KIID().AsString();
2025-01-03 13:32:07 +00:00
2025-03-03 18:34:20 +00:00
j.at( "type" ).get_to( destination.m_type );
destination.m_only = j.value( "only", std::vector<wxString>() );
destination.m_description = j.value( "description", "" );
2025-01-03 13:32:07 +00:00
const nlohmann::json& settings_obj = j.at( "settings" );
2025-03-03 18:34:20 +00:00
destination.InitOutputHandler();
2025-03-03 18:34:20 +00:00
if( destination.m_outputHandler != nullptr )
destination.m_outputHandler->FromJson( settings_obj );
}
2025-03-03 18:34:20 +00:00
JOBSET_DESTINATION::JOBSET_DESTINATION() :
m_type( JOBSET_DESTINATION_T::FOLDER ),
m_outputHandler( nullptr ),
m_lastRunSuccess(),
m_lastRunReporters()
{
}
2025-03-03 18:34:20 +00:00
JOBSET_DESTINATION::JOBSET_DESTINATION( const wxString& id, JOBSET_DESTINATION_T type ) :
m_id( id ),
m_type( type ),
m_outputHandler( nullptr ),
m_lastRunSuccess(),
m_lastRunReporters()
{
InitOutputHandler();
}
2025-03-03 18:34:20 +00:00
void JOBSET_DESTINATION::InitOutputHandler()
{
2025-03-03 18:34:20 +00:00
if( m_type == JOBSET_DESTINATION_T::FOLDER )
{
m_outputHandler = std::make_shared<JOBS_OUTPUT_FOLDER>( );
}
2025-03-03 18:34:20 +00:00
else if( m_type == JOBSET_DESTINATION_T::ARCHIVE )
{
m_outputHandler = std::make_shared<JOBS_OUTPUT_ARCHIVE>( );
}
}
2025-03-03 18:34:20 +00:00
wxString JOBSET_DESTINATION::GetDescription() const
2024-12-29 07:16:38 -05:00
{
return m_description.IsEmpty() ? m_outputHandler->GetDefaultDescription() : m_description;
}
wxString JOBSET_DESTINATION::GetPathInfo() const
{
return m_outputHandler->GetOutputPath();
}
2025-03-03 18:34:20 +00:00
void JOBSET_DESTINATION::SetDescription( const wxString& aDescription )
2024-12-29 07:16:38 -05:00
{
if( aDescription == m_outputHandler->GetDefaultDescription() )
m_description = wxEmptyString;
else
m_description = aDescription;
}
bool JOBSET_JOB::operator==( const JOBSET_JOB & rhs ) const
{
return rhs.m_type == m_type;
}
wxString JOBSET_JOB::GetDescription() const
{
return m_description.IsEmpty() ? m_job->GetDefaultDescription() : m_description;
}
void JOBSET_JOB::SetDescription( const wxString& aDescription )
{
if( aDescription == m_job->GetDefaultDescription() )
m_description = wxEmptyString;
else
m_description = aDescription;
}
2025-03-03 18:34:20 +00:00
bool JOBSET_DESTINATION::operator==( const JOBSET_DESTINATION& rhs ) const
{
return rhs.m_type == m_type;
}
JOBSET::JOBSET( const wxString& aFilename ) :
JSON_SETTINGS( aFilename, SETTINGS_LOC::NONE, jobsFileSchemaVersion ),
m_dirty( false )
{
m_params.emplace_back( new PARAM_LIST<JOBSET_JOB>( "jobs", &m_jobs, {} ) );
2025-03-03 18:34:20 +00:00
m_params.emplace_back( new PARAM_LIST<JOBSET_DESTINATION>( "outputs", &m_destinations, {} ) );
m_fileNameWithoutPath = wxFileName( aFilename ).GetFullName();
}
wxString JOBSET::getFileExt() const
{
return FILEEXT::KiCadJobSetFileExtension;
}
void JOBSET::AddNewJob( wxString aType, JOB* aJob )
{
m_jobs.emplace_back( KIID().AsString(), aType, aJob );
SetDirty();
}
2025-03-03 18:34:20 +00:00
JOBSET_DESTINATION* JOBSET::AddNewDestination( JOBSET_DESTINATION_T aType )
{
2025-03-03 18:34:20 +00:00
m_destinations.emplace_back( KIID().AsString(), aType );
SetDirty();
2025-03-03 18:34:20 +00:00
return &m_destinations.back();
}
2025-03-03 18:34:20 +00:00
void JOBSET::RemoveDestination( JOBSET_DESTINATION* aDestination )
{
2025-03-03 18:34:20 +00:00
std::erase_if( m_destinations,
[&]( JOBSET_DESTINATION const& destination )
{
2025-03-03 18:34:20 +00:00
return destination.m_id == aDestination->m_id;
} );
}
2024-10-02 22:53:43 -04:00
void JOBSET::MoveJobUp( size_t aJobIdx )
{
if( aJobIdx > 0 )
{
std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx - 1] );
SetDirty();
}
}
void JOBSET::MoveJobDown( size_t aJobIdx )
{
if( aJobIdx < m_jobs.size() - 1 )
{
std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx + 1] );
SetDirty();
}
}
2024-10-03 08:02:21 -04:00
void JOBSET::RemoveJob( size_t aJobIdx )
{
m_jobs.erase( m_jobs.begin() + aJobIdx );
SetDirty();
}
bool JOBSET::SaveToFile( const wxString& aDirectory, bool aForce )
{
bool success = JSON_SETTINGS::SaveToFile( aDirectory, aForce );
if( success )
{
m_dirty = false;
}
return success;
}
JOBSET_DESTINATION* JOBSET::FindDestination( wxString& aDestinationStr )
{
auto is_matching_dest = [&]( const JOBSET_DESTINATION& destination )
{
if( destination.m_id == aDestinationStr || destination.m_description == aDestinationStr )
return true;
return false;
};
auto count = std::count_if( m_destinations.begin(), m_destinations.end(), is_matching_dest );
// we want to intentionally fail if more than one matching dest exists
// as theres no good way to handle it
if( count != 1 )
return nullptr;
auto it = std::find_if( m_destinations.begin(), m_destinations.end(), is_matching_dest );
2025-03-03 18:34:20 +00:00
if( it != m_destinations.end() )
return &(*it);
return nullptr;
}
2025-03-03 18:34:20 +00:00
std::vector<JOBSET_JOB> JOBSET::GetJobsForDestination( JOBSET_DESTINATION* aDestination )
{
2025-03-03 18:34:20 +00:00
wxASSERT( aDestination != nullptr );
2025-03-03 18:34:20 +00:00
if( aDestination->m_only.size() == 0 )
{
return m_jobs;
}
std::vector<JOBSET_JOB> result;
2025-03-03 18:34:20 +00:00
for( wxString& onlyId : aDestination->m_only )
{
auto it = std::find_if( m_jobs.begin(), m_jobs.end(),
[&]( const JOBSET_JOB& job )
{
if( job.m_id == onlyId )
return true;
return false;
} );
if( it != m_jobs.end() )
result.push_back( *it );
}
return result;
}
#if !defined( __MINGW32__ )
template class KICOMMON_API PARAM_LIST<JOBSET_JOB>;
2025-03-03 18:34:20 +00:00
template class KICOMMON_API PARAM_LIST<JOBSET_DESTINATION>;
#endif