11#include <system_error>
13#include <wx/filename.h>
23 return std::filesystem::path( aPath.ToStdWstring() );
25 return std::filesystem::path( aPath.utf8_string() );
32 return wxString( aPath.wstring() );
34 return wxString::FromUTF8( aPath.string() );
44 aError =
_(
"The report file must not be a design or library file." );
67 aError = wxString::Format(
_(
"Could not write the report to '%s'." ), aPath );
77 if( aOutput.IsEmpty() )
79 aError =
_(
"An output path is required. Use --output." );
83 wxFileName output( aOutput );
86 if( !output.MakeAbsolute() )
88 aError =
_(
"The output path is not accessible." );
92 std::error_code error;
93 auto status = std::filesystem::symlink_status(
DowngradeNativePath( output.GetFullPath() ), error );
95 if( ( error && error != std::errc::no_such_file_or_directory )
96 || status.type() != std::filesystem::file_type::not_found )
98 aError =
_(
"The output path already exists or is inaccessible. Choose a new destination." );
107 wxFileName file( aPath );
109 || file.GetFullName().Contains( wxT(
".downgrade_tmp." ) )
110 || file.GetFullName().Contains( wxT(
".downgrade_backup." ) );
115 std::error_code error;
118 if( error || !std::filesystem::is_directory(
source, error ) || error )
120 aError =
_(
"The library folder is not accessible." );
124 std::filesystem::recursive_directory_iterator iterator(
source, error );
125 const std::filesystem::recursive_directory_iterator
end;
127 while( !error && iterator !=
end )
129 const auto& entry = *iterator;
134 iterator.disable_recursion_pending();
136 else if( entry.is_symlink( error ) && entry.is_directory( error ) )
138 aError =
_(
"Library folders containing directory links cannot be copied safely." );
141 else if( !error && !entry.is_directory( error ) && !entry.is_regular_file( error ) )
143 aError =
_(
"The library folder contains an inaccessible or unsupported file." );
148 iterator.increment( error );
153 aError =
_(
"The library folder could not be read completely." );
167 std::error_code error;
169 auto destination = std::filesystem::weakly_canonical(
DowngradeNativePath( aDestination ), error );
173 aError =
_(
"The output path is not accessible." );
177 auto relative = destination.lexically_relative(
source );
179 if( destination ==
source || ( !relative.empty() && *relative.begin() !=
".." ) )
181 aError =
_(
"The library output must be outside the source library folder." );
202 std::error_code error;
205 std::filesystem::remove_all( it->first, error );
207 std::filesystem::remove( it->first, error );
211 bool Create(
const wxString& aDirectory,
bool aOwnContents =
false )
213 std::error_code error;
219 std::vector<std::filesystem::path> missing;
223 if( error ||
path.empty() ||
path ==
path.parent_path() )
226 missing.push_back(
path );
229 if( error || ( aOwnContents && missing.empty() ) )
232 for(
auto it = missing.rbegin(); it != missing.rend(); ++it )
234 if( !std::filesystem::create_directory( *it, error ) || error )
240 return std::filesystem::is_directory(
directory, error ) && !error;
246 std::vector<std::pair<std::filesystem::path, bool>>
m_created;
252 std::error_code error;
256 return wxEmptyString;
267 if( !aDirectories.
Create( aDestination,
true ) )
269 aError =
_(
"Could not create the library output folder." );
273 std::error_code error;
275 std::filesystem::recursive_directory_iterator iterator(
source, error );
276 const std::filesystem::recursive_directory_iterator
end;
278 while( !error && iterator !=
end )
280 const auto& entry = *iterator;
284 iterator.disable_recursion_pending();
290 if( entry.is_directory( error ) )
291 std::filesystem::create_directory( destination, error );
293 std::filesystem::copy_file( entry.path(), destination, error );
297 iterator.increment( error );
302 aError =
_(
"Could not copy the library folder." );
What an export to a given target will keep, approximate, drop, or block.
wxString ToText(const wxString &aTargetName) const
A readable preview of the classified export, including counts and feature details.
std::vector< std::pair< std::filesystem::path, bool > > m_created
DOWNGRADE_OUTPUT_DIRECTORIES(const DOWNGRADE_OUTPUT_DIRECTORIES &)=delete
DOWNGRADE_OUTPUT_DIRECTORIES()=default
~DOWNGRADE_OUTPUT_DIRECTORIES()
DOWNGRADE_OUTPUT_DIRECTORIES & operator=(const DOWNGRADE_OUTPUT_DIRECTORIES &)=delete
bool Create(const wxString &aDirectory, bool aOwnContents=false)
wxString DowngradeWxPath(const std::filesystem::path &aPath)
wxString DowngradeLibraryCopyPath(const wxString &aLibrary, const wxString &aOutputDirectory)
bool IsDowngradeLibraryWorkingArtifact(const wxString &aPath)
bool ValidateDowngradeReportPath(const wxString &aPath, wxString &aError)
A report destination that is a design or library file is a mistyped path, not a report.
bool ValidateDowngradeLibrarySource(const wxString &aSource, wxString &aError)
bool ValidateDowngradeLibraryCopy(const wxString &aSource, const wxString &aDestination, wxString &aError)
bool ValidateFreshDowngradeOutput(const wxString &aOutput, wxString &aError)
bool CopyDowngradeLibraryDirectory(const wxString &aSource, const wxString &aDestination, DOWNGRADE_OUTPUT_DIRECTORIES &aDirectories, wxString &aError)
bool WriteDowngradeReport(const COMPATIBILITY_REPORT &aReport, const wxString &aTarget, const wxString &aPath, wxString &aError)
Write the readable compatibility report, the same text the export dialog shows.
std::filesystem::path DowngradeNativePath(const wxString &aPath)
bool IsDowngradeWorkingArtifact(const wxString &aPath)
Editor working state (autosaves, local history, backups) holds current-format copies nothing downgrad...
bool IsDowngradeDesignFile(const wxString &aPath)
Files the project export carries over.
const std::vector< DOWNGRADE_TARGET > & GetDowngradeTargets()
Supported downgrade targets, newest first.
const DOWNGRADE_TARGET * FindDowngradeTarget(const wxString &aNameOrId)
Match a user-supplied target string against name or stable id, case-insensitively.
An older KiCad release we can export a project back to.
wxString m_name
User-facing name, e.g. "KiCad 10.0".