28#include <wx/txtstrm.h>
29#include <wx/wfstream.h>
31#include <wx/textfile.h>
32#include <unordered_map>
38#define SEP wxFileName::GetPathSeparator()
51 if( !wxFileName::DirExists(
m_basePath.GetPath() ) )
54 m_title.Printf(
_(
"Could not open the template path '%s'" ), aPath );
56 else if( !wxFileName::DirExists(
m_metaPath.GetPath() ) )
59 m_title.Printf(
_(
"Could not find the expected 'meta' directory at '%s'" ),
m_metaPath.GetPath() );
84 virtual wxDirTraverseResult
OnFile(
const wxString& filename )
override
86 wxFileName fn( filename );
87 wxString
path( fn.GetPathWithSep() );
92 return wxDIR_CONTINUE;
94 bool exclude = fn.GetName().Contains(
"fp-info-cache" )
99 m_files.emplace_back( wxFileName( filename ) );
101 return wxDIR_CONTINUE;
104 virtual wxDirTraverseResult
OnDir(
const wxString& dirname )
override
106 wxFileName dir( dirname );
107 wxString parent = dir.GetPathWithSep();
111 if( dir.GetFullName() == wxT(
".git" ) ||
IsIgnored( parent, dir.GetFullName(),
true )
112 || dirname.StartsWith(
m_exclude ) || dirname.EndsWith(
"-backups" ) )
117 m_files.emplace_back( wxFileName::DirName( dirname ) );
119 return wxDIR_CONTINUE;
128 wxString gitignore =
path + wxT(
".gitignore" );
130 if( wxFileExists( gitignore ) )
132 wxFileInputStream input( gitignore );
133 wxTextInputStream
text( input, wxT(
"\x9" ), wxConvUTF8 );
135 while( input.IsOk() && !input.Eof() )
137 wxString line =
text.ReadLine();
139 line.Trim().Trim(
false );
141 if( line.IsEmpty() || line.StartsWith( wxT(
"#" ) ) )
147 m_files.emplace_back( wxFileName( gitignore ) );
154 wxString gitattributes =
path + wxT(
".gitattributes" );
156 if( wxFileExists( gitattributes ) )
157 m_files.emplace_back( wxFileName( gitattributes ) );
167 for(
const wxString& pattern : it->second )
169 bool dirOnly = pattern.EndsWith( wxT(
"/" ) );
170 wxString pat = dirOnly ? pattern.substr( 0, pattern.length() - 1 ) : pattern;
172 if( dirOnly && !isDir )
175 if( wxMatchWild( pat,
name ) )
190 std::vector<wxFileName> files;
194 dir.Traverse( sink, wxEmptyString, ( wxDIR_FILES | wxDIR_DIRS ) );
228 bool multipleProjectFilesFound =
false;
230 for( wxFileName& file : srcFiles )
234 if( !basename.IsEmpty() && basename != file.GetName() )
235 multipleProjectFilesFound =
true;
237 basename = file.GetName();
241 if( multipleProjectFilesFound )
244 for( wxFileName& srcFile : srcFiles )
247 wxFileName destFile = srcFile;
250 wxString
name = destFile.GetName();
251 name.Replace( basename, aNewProjectPath.GetName() );
252 destFile.SetName(
name );
255 wxString
path = destFile.GetPathWithSep();
256 path.Replace(
m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
257 destFile.SetPath(
path );
259 aDestFiles.push_back( destFile );
262 return aDestFiles.size();
277 bool multipleProjectFilesFound =
false;
279 for( wxFileName& file : srcFiles )
283 if( !basename.IsEmpty() && basename != file.GetName() )
284 multipleProjectFilesFound =
true;
286 basename = file.GetName();
290 if( multipleProjectFilesFound )
293 for( wxFileName& srcFile : srcFiles )
296 wxFileName destFile = srcFile;
299 wxString currname = destFile.GetName();
305 else if( destFile.GetName().EndsWith(
"-cache" ) || destFile.GetName().EndsWith(
"-rescue" ) )
307 currname.Replace( basename, aNewProjectPath.GetName() );
319 currname.Replace( basename, aNewProjectPath.GetName() );
322 destFile.SetName( currname );
326 wxString destpath = destFile.GetPathWithSep();
327 destpath.Replace(
m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
330 if( !wxFileName::DirExists( destpath ) )
332 if( !wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL ) )
336 if( !aErrorMsg->empty() )
341 msg.Printf(
_(
"Cannot create folder '%s'." ), destpath );
349 destFile.SetPath( destpath );
351 if( srcFile.FileExists() && !wxCopyFile( srcFile.GetFullPath(), destFile.GetFullPath() ) )
355 if( !aErrorMsg->empty() )
360 msg.Printf(
_(
"Cannot copy file '%s'." ), destFile.GetFullPath() );
374 wxFFileInputStream input(
GetHtmlFile().GetFullPath() );
375 wxString separator( wxT(
"\x9" ) );
376 wxTextInputStream
text( input, separator, wxConvUTF8 );
384 bool hasStart =
false;
386 while( input.IsOk() && !input.Eof() && !done )
388 wxString line =
text.ReadLine();
389 wxString upperline = line.Clone().Upper();
391 start = upperline.Find( wxT(
"<TITLE>" ) );
392 finish = upperline.Find( wxT(
"</TITLE>" ) );
393 int length = finish - start - 7;
396 if( start != wxNOT_FOUND )
398 if( finish != wxNOT_FOUND )
400 m_title = line( start + 7, length );
405 m_title = line.Mid( start + 7 );
411 if( finish != wxNOT_FOUND )
413 m_title += line.SubString( 0, finish - 1 );
422 m_title.Replace( wxT(
"\r" ), wxT(
"" ) );
423 m_title.Replace( wxT(
"\n" ), wxT(
"" ) );
virtual wxDirTraverseResult OnDir(const wxString &dirname) override
FILE_TRAVERSER(std::vector< wxFileName > &files, const wxString &exclude)
void EnsureGitFiles(const wxString &path)
virtual wxDirTraverseResult OnFile(const wxString &filename) override
std::unordered_map< wxString, std::vector< wxString > > m_gitIgnores
std::vector< wxFileName > & m_files
bool IsIgnored(const wxString &path, const wxString &name, bool isDir)
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 LegacySymbolLibFileExtension
static const std::string LockFilePrefix
static const std::string DrawingSheetFileExtension
static const std::string AutoSaveFilePrefix
static const std::string LegacySymbolDocumentFileExtension
static const std::string KiCadFootprintLibPathExtension
#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.