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_error.Printf(
_(
"Could not open the template path '%s'" ), aPath );
56 else if( !wxFileName::DirExists(
m_metaPath.GetPath() ) )
59 m_error.Printf(
_(
"Could not find the expected 'meta' directory at '%s'" ),
m_metaPath.GetPath() );
87 virtual wxDirTraverseResult
OnFile(
const wxString& filename )
override
89 wxFileName fn( filename );
90 wxString
path( fn.GetPathWithSep() );
95 return wxDIR_CONTINUE;
97 bool exclude = fn.GetName().Contains(
"fp-info-cache" )
101 m_files.emplace_back( wxFileName( filename ) );
103 return wxDIR_CONTINUE;
106 virtual wxDirTraverseResult
OnDir(
const wxString& dirname )
override
108 wxFileName dir( dirname );
109 wxString parent = dir.GetPathWithSep();
113 if( dir.GetFullName() == wxT(
".git" ) ||
IsIgnored( parent, dir.GetFullName(),
true )
114 || dirname.StartsWith(
m_exclude ) || dirname.EndsWith(
"-backups" ) )
119 m_files.emplace_back( wxFileName::DirName( dirname ) );
121 return wxDIR_CONTINUE;
130 wxString gitignore =
path + wxT(
".gitignore" );
132 if( wxFileExists( gitignore ) )
134 wxFileInputStream input( gitignore );
135 wxTextInputStream
text( input, wxT(
"\x9" ), wxConvUTF8 );
137 while( input.IsOk() && !input.Eof() )
139 wxString line =
text.ReadLine();
141 line.Trim().Trim(
false );
143 if( line.IsEmpty() || line.StartsWith( wxT(
"#" ) ) )
149 m_files.emplace_back( wxFileName( gitignore ) );
156 wxString gitattributes =
path + wxT(
".gitattributes" );
158 if( wxFileExists( gitattributes ) )
159 m_files.emplace_back( wxFileName( gitattributes ) );
169 for(
const wxString& pattern : it->second )
171 bool dirOnly = pattern.EndsWith( wxT(
"/" ) );
172 wxString pat = dirOnly ? pattern.substr( 0, pattern.length() - 1 ) : pattern;
174 if( dirOnly && !isDir )
177 if( wxMatchWild( pat,
name ) )
192 std::vector<wxFileName> files;
196 dir.Traverse( sink, wxEmptyString, ( wxDIR_FILES | wxDIR_DIRS ) );
230 bool multipleProjectFilesFound =
false;
232 for( wxFileName& file : srcFiles )
236 if( !basename.IsEmpty() && basename != file.GetName() )
237 multipleProjectFilesFound =
true;
239 basename = file.GetName();
243 if( multipleProjectFilesFound )
246 for( wxFileName& srcFile : srcFiles )
249 wxFileName destFile = srcFile;
252 wxString
name = destFile.GetName();
253 name.Replace( basename, aNewProjectPath.GetName() );
254 destFile.SetName(
name );
258 wxString
path = destFile.GetPathWithSep();
259 path.Replace(
m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
260 path.Replace(
SEP + basename +
SEP,
SEP + aNewProjectPath.GetName() +
SEP );
261 path.Replace(
SEP + basename + wxS(
"-" ),
SEP + aNewProjectPath.GetName() + wxS(
"-" ) );
262 destFile.SetPath(
path );
264 aDestFiles.push_back( destFile );
267 return aDestFiles.size();
282 bool multipleProjectFilesFound =
false;
284 for( wxFileName& file : srcFiles )
288 if( !basename.IsEmpty() && basename != file.GetName() )
289 multipleProjectFilesFound =
true;
291 basename = file.GetName();
295 if( multipleProjectFilesFound )
298 for( wxFileName& srcFile : srcFiles )
301 wxFileName destFile = srcFile;
304 wxString currname = destFile.GetName();
310 else if( destFile.GetName().EndsWith(
"-cache" ) || destFile.GetName().EndsWith(
"-rescue" ) )
312 currname.Replace( basename, aNewProjectPath.GetName() );
324 currname.Replace( basename, aNewProjectPath.GetName() );
327 destFile.SetName( currname );
331 wxString destpath = destFile.GetPathWithSep();
332 destpath.Replace(
m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
333 destpath.Replace(
SEP + basename +
SEP,
SEP + aNewProjectPath.GetName() +
SEP );
334 destpath.Replace(
SEP + basename + wxS(
"-" ),
SEP + aNewProjectPath.GetName() + wxS(
"-" ) );
337 if( !wxFileName::DirExists( destpath ) )
339 if( !wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL ) )
343 if( !aErrorMsg->empty() )
348 msg.Printf(
_(
"Cannot create folder '%s'." ), destpath );
356 destFile.SetPath( destpath );
358 if( srcFile.FileExists() && !wxCopyFile( srcFile.GetFullPath(), destFile.GetFullPath() ) )
362 if( !aErrorMsg->empty() )
367 msg.Printf(
_(
"Cannot copy file '%s'." ), destFile.GetFullPath() );
386 wxFFileInputStream input(
GetHtmlFile().GetFullPath() );
387 wxString separator( wxT(
"\x9" ) );
388 wxTextInputStream
text( input, separator, wxConvUTF8 );
393 bool hasStart =
false;
395 while( input.IsOk() && !input.Eof() && !done )
397 wxString line =
text.ReadLine();
398 wxString upperline = line.Clone().Upper();
400 start = upperline.Find( wxT(
"<TITLE>" ) );
401 finish = upperline.Find( wxT(
"</TITLE>" ) );
402 int length = finish - start - 7;
405 if( start != wxNOT_FOUND )
407 if( finish != wxNOT_FOUND )
409 m_title = line( start + 7, length );
414 m_title = line.Mid( start + 7 );
420 if( finish != wxNOT_FOUND )
422 m_title += line.SubString( 0, finish - 1 );
431 m_title.Replace( wxT(
"\r" ), wxT(
"" ) );
432 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 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.
wxString result
Test unit parsing edge cases and error handling.
Definition of file extensions used in Kicad.