28#include <wx/txtstrm.h>
29#include <wx/wfstream.h>
36#define SEP wxFileName::GetPathSeparator()
49 if( !wxFileName::DirExists(
m_basePath.GetPath() ) )
52 m_title =
_(
"Could open the template path!" ) + wxS(
" " ) + aPath;
54 else if( !wxFileName::DirExists(
m_metaPath.GetPath() ) )
57 m_title =
_(
"Couldn't open the meta information directory for this template!" ) +
63 m_title =
_(
"Couldn't find the meta HTML information file for this template!" );
82 virtual wxDirTraverseResult
OnFile(
const wxString& filename )
override
84 wxFileName fn( filename );
85 wxString
path( fn.GetPathWithSep() );
87 bool exclude = fn.GetName().Contains(
"fp-info-cache" )
89 || fn.GetExt().Contains(
"lck" );
92 m_files.emplace_back( wxFileName( filename ) );
96 const wxString gitfiles[] = { wxT(
".gitignore" ), wxT(
".gitattributes" ) };
98 for(
const wxString& file : gitfiles )
100 if( wxFileExists(
path + file ) )
107 return wxDIR_CONTINUE;
110 virtual wxDirTraverseResult
OnDir(
const wxString& dirname )
override
112 wxDirTraverseResult result = wxDIR_IGNORE;
114 bool exclude = dirname.StartsWith(
m_exclude ) || dirname.EndsWith(
"-backups" );
118 m_files.emplace_back( wxFileName::DirName( dirname ) );
119 result = wxDIR_CONTINUE;
134 std::vector<wxFileName> files;
138 dir.Traverse( sink, wxEmptyString, ( wxDIR_FILES | wxDIR_DIRS ) );
168 std::vector< wxFileName >& aDestFiles )
170 std::vector< wxFileName > srcFiles =
GetFileList();
174 bool multipleProjectFilesFound =
false;
176 for( wxFileName& file : srcFiles )
181 if( !basename.IsEmpty() && basename != file.GetName() )
182 multipleProjectFilesFound =
true;
184 basename = file.GetName();
188 if( multipleProjectFilesFound )
191 for( wxFileName& srcFile : srcFiles )
194 wxFileName destFile = srcFile;
197 wxString
name = destFile.GetName();
198 name.Replace( basename, aNewProjectPath.GetName() );
199 destFile.SetName(
name );
202 wxString
path = destFile.GetPathWithSep();
203 path.Replace(
m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
204 destFile.SetPath(
path );
206 aDestFiles.push_back( destFile );
209 return aDestFiles.size();
224 bool multipleProjectFilesFound =
false;
226 for( wxFileName& file : srcFiles )
231 if( !basename.IsEmpty() && basename != file.GetName() )
232 multipleProjectFilesFound =
true;
234 basename = file.GetName();
238 if( multipleProjectFilesFound )
241 for( wxFileName& srcFile : srcFiles )
244 wxFileName destFile = srcFile;
247 wxString currname = destFile.GetName();
253 else if( destFile.GetName().EndsWith(
"-cache" )
254 || destFile.GetName().EndsWith(
"-rescue" ) )
256 currname.Replace( basename, aNewProjectPath.GetName() );
258 else if( destFile.GetExt() ==
"dcm"
259 || destFile.GetExt() ==
"lib"
261 || destFile.GetPath().EndsWith(
".pretty" ) )
268 currname.Replace( basename, aNewProjectPath.GetName() );
271 destFile.SetName( currname );
275 wxString destpath = destFile.GetPathWithSep();
276 destpath.Replace(
m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
279 if( !wxFileName::DirExists( destpath ) )
281 if( !wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL ) )
285 if( !aErrorMsg->empty() )
290 msg.Printf(
_(
"Cannot create folder '%s'." ), destpath );
298 destFile.SetPath( destpath );
300 if( srcFile.FileExists() && !wxCopyFile( srcFile.GetFullPath(), destFile.GetFullPath() ) )
304 if( !aErrorMsg->empty() )
309 msg.Printf(
_(
"Cannot copy file '%s'." ), destFile.GetFullPath() );
323 wxFFileInputStream input(
GetHtmlFile().GetFullPath() );
324 wxString separator( wxT(
"\x9" ) );
325 wxTextInputStream
text( input, separator, wxConvUTF8 );
334 while( input.IsOk() && !input.Eof() && !done )
336 wxString line =
text.ReadLine();
337 wxString upperline = line.Clone().Upper();
339 start = upperline.Find( wxT(
"<TITLE>" ) );
340 finish = upperline.Find( wxT(
"</TITLE>" ) );
341 int length = finish - start - 7;
344 if( start != wxNOT_FOUND )
346 if( finish != wxNOT_FOUND )
348 m_title = line( start + 7, length );
352 m_title = line.Mid( start + 7 );
359 if( finish != wxNOT_FOUND )
361 m_title += line.SubString( 0, finish );
367 m_title.Replace( wxT(
"\r" ), wxT(
" " ) );
368 m_title.Replace( wxT(
"\n" ), wxT(
" " ) );
virtual wxDirTraverseResult OnDir(const wxString &dirname) override
FILE_TRAVERSER(std::vector< wxFileName > &files, const wxString exclude)
virtual wxDirTraverseResult OnFile(const wxString &filename) override
std::vector< wxFileName > & m_files
wxBitmap * GetIcon()
Get the 64px^2 icon for the project template.
size_t GetDestinationFiles(const wxFileName &aNewProjectPath, std::vector< wxFileName > &aDestFiles)
Fetch the list of destination files to be copied when the new project is created.
wxFileName m_metaHtmlFile
PROJECT_TEMPLATE(const wxString &aPath)
Create a new project instance from aPath.
std::vector< wxFileName > GetFileList()
Get a vector list of filenames for the template.
~PROJECT_TEMPLATE()
Non-virtual destructor (so no derived classes)
wxFileName GetHtmlFile()
Get the full Html filename for the project template.
wxString * GetTitle()
Get the title of the project (extracted from the html title tag)
bool CreateProject(wxFileName &aNewProjectPath, wxString *aErrorMsg=nullptr)
Copies and renames all template files to create a new project.
wxFileName m_metaIconFile
wxString GetPrjDirName()
Get the dir name of the project template (i.e.
static const std::string ProjectFileExtension
static const std::string LegacyProjectFileExtension
static const std::string DrawingSheetFileExtension
static const std::string AutoSaveFilePrefix
#define METAFILE_ICON
An optional png icon, exactly 64px x 64px which is used in the template selector if present.
#define METADIR
A directory which contains information about the project template and does not get copied.
#define METAFILE_INFO_HTML
A required html formatted file which contains information about the project template.
Definition of file extensions used in Kicad.