25#include <wx/txtstrm.h>
26#include <wx/wfstream.h>
28#include <wx/textfile.h>
29#include <unordered_map>
36#define SEP wxFileName::GetPathSeparator()
49 if( !wxFileName::DirExists(
m_basePath.GetPath() ) )
52 m_error.Printf(
_(
"Could not open the template path '%s'" ), aPath );
54 else if( !wxFileName::DirExists(
m_metaPath.GetPath() ) )
57 m_error.Printf(
_(
"Could not find the expected 'meta' directory at '%s'" ),
m_metaPath.GetPath() );
85 virtual wxDirTraverseResult
OnFile(
const wxString& filename )
override
87 wxFileName fn( filename );
88 wxString
path( fn.GetPathWithSep() );
93 return wxDIR_CONTINUE;
95 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 );
256 wxString
path = destFile.GetPathWithSep();
257 path.Replace(
m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
258 path.Replace(
SEP + basename +
SEP,
SEP + aNewProjectPath.GetName() +
SEP );
259 path.Replace(
SEP + basename + wxS(
"-" ),
SEP + aNewProjectPath.GetName() + wxS(
"-" ) );
260 destFile.SetPath(
path );
262 aDestFiles.push_back( destFile );
265 return aDestFiles.size();
280 bool multipleProjectFilesFound =
false;
282 for( wxFileName& file : srcFiles )
286 if( !basename.IsEmpty() && basename != file.GetName() )
287 multipleProjectFilesFound =
true;
289 basename = file.GetName();
293 if( multipleProjectFilesFound )
296 for( wxFileName& srcFile : srcFiles )
299 wxFileName destFile = srcFile;
302 wxString currname = destFile.GetName();
308 else if( destFile.GetName().EndsWith(
"-cache" ) || destFile.GetName().EndsWith(
"-rescue" ) )
310 currname.Replace( basename, aNewProjectPath.GetName() );
322 currname.Replace( basename, aNewProjectPath.GetName() );
325 destFile.SetName( currname );
329 wxString destpath = destFile.GetPathWithSep();
330 destpath.Replace(
m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
331 destpath.Replace(
SEP + basename +
SEP,
SEP + aNewProjectPath.GetName() +
SEP );
332 destpath.Replace(
SEP + basename + wxS(
"-" ),
SEP + aNewProjectPath.GetName() + wxS(
"-" ) );
335 if( !wxFileName::DirExists( destpath ) )
337 if( !wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL ) )
341 if( !aErrorMsg->empty() )
346 msg.Printf(
_(
"Cannot create folder '%s'." ), destpath );
354 destFile.SetPath( destpath );
356 if( srcFile.FileExists() && !wxCopyFile( srcFile.GetFullPath(), destFile.GetFullPath() ) )
360 if( !aErrorMsg->empty() )
365 msg.Printf(
_(
"Cannot copy file '%s'." ), destFile.GetFullPath() );
384 wxFFileInputStream input(
GetHtmlFile().GetFullPath() );
385 wxString separator( wxT(
"\x9" ) );
386 wxTextInputStream
text( input, separator, wxConvUTF8 );
391 bool hasStart =
false;
393 while( input.IsOk() && !input.Eof() && !done )
395 wxString line =
text.ReadLine();
396 wxString upperline = line.Clone().Upper();
398 start = upperline.Find( wxT(
"<TITLE>" ) );
399 finish = upperline.Find( wxT(
"</TITLE>" ) );
400 int length = finish - start - 7;
403 if( start != wxNOT_FOUND )
405 if( finish != wxNOT_FOUND )
407 m_title = line( start + 7, length );
412 m_title = line.Mid( start + 7 );
418 if( finish != wxNOT_FOUND )
420 m_title += line.SubString( 0, finish - 1 );
429 m_title.Replace( wxT(
"\r" ), wxT(
"" ) );
430 m_title.Replace( wxT(
"\n" ), wxT(
"" ) );
442 if( aBaseDir.IsEmpty() )
445 wxFileName templatePath;
446 templatePath.AssignDir( aBaseDir );
448 templatePath.AppendDir( wxT(
"default" ) );
450 if( !templatePath.DirExists() && !templatePath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
453 wxFileName metaDir = templatePath;
456 if( !metaDir.DirExists() && !metaDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
459 wxFileName infoFile = metaDir;
462 if( !infoFile.FileExists() )
464 wxFFile
info( infoFile.GetFullPath(), wxT(
"w" ) );
466 if( !
info.IsOpened() )
469 info.Write( wxT(
"<html><head><title>Default</title></head><body>"
470 "<h3>Default KiCad project template.</h3></body></html>" ) );
474 wxFileName proFile = templatePath;
475 proFile.SetFullName( wxT(
"default.kicad_pro" ) );
477 if( !proFile.FileExists() )
479 wxFFile proj( proFile.GetFullPath(), wxT(
"w" ) );
481 if( !proj.IsOpened() )
484 proj.Write( wxT(
"{}" ) );
488 if( infoFile.FileExists() && proFile.FileExists() )
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
wxFileName EnsureDefaultProjectTemplate(const wxString &aBaseDir)
Seed the built-in "default" project template under aBaseDir, creating the directory tree and minimal ...
#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.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().