KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project_template.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2012 Brian Sidebotham <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25
26#include <wx/bitmap.h>
27#include <wx/dir.h>
28#include <wx/txtstrm.h>
29#include <wx/wfstream.h>
30#include <wx/log.h>
31#include <wx/textfile.h>
32#include <unordered_map>
33
35#include "project_template.h"
36
37
38#define SEP wxFileName::GetPathSeparator()
39
40
41PROJECT_TEMPLATE::PROJECT_TEMPLATE( const wxString& aPath )
42{
43 m_basePath = wxFileName::DirName( aPath );
44 m_metaPath = wxFileName::DirName( aPath + SEP + METADIR );
45 m_metaHtmlFile = wxFileName::FileName( aPath + SEP + METADIR + SEP + METAFILE_INFO_HTML );
46 m_metaIconFile = wxFileName::FileName( aPath + SEP + METADIR + SEP + METAFILE_ICON );
47
48 m_title = wxEmptyString;
49
50 // Test the project template requirements to make sure aPath is a valid template structure.
51 if( !wxFileName::DirExists( m_basePath.GetPath() ) )
52 {
53 // Error, the path doesn't exist!
54 m_error.Printf( _( "Could not open the template path '%s'" ), aPath );
55 }
56 else if( !wxFileName::DirExists( m_metaPath.GetPath() ) )
57 {
58 // Error, the meta information directory doesn't exist!
59 m_error.Printf( _( "Could not find the expected 'meta' directory at '%s'" ), m_metaPath.GetPath() );
60 }
61 else if( !wxFileName::FileExists( m_metaHtmlFile.GetFullPath() ) )
62 {
63 // Error, the meta information directory doesn't contain the informational html file!
64 m_error.Printf( _( "Could not find the expected meta HTML file at '%s'" ), m_metaHtmlFile.GetFullPath() );
65 }
66
67 if( m_title.IsEmpty() )
69
70 // Try to load an icon
71 if( !wxFileName::FileExists( m_metaIconFile.GetFullPath() ) )
72 m_metaIcon = &wxNullBitmap;
73 else
74 m_metaIcon = new wxBitmap( m_metaIconFile.GetFullPath(), wxBITMAP_TYPE_PNG );
75}
76
77
78class FILE_TRAVERSER : public wxDirTraverser
79{
80public:
81 FILE_TRAVERSER( std::vector<wxFileName>& files, const wxString& exclude ) :
82 m_files( files ),
83 m_exclude( exclude )
84 {
85 }
86
87 virtual wxDirTraverseResult OnFile( const wxString& filename ) override
88 {
89 wxFileName fn( filename );
90 wxString path( fn.GetPathWithSep() );
91
93
94 if( IsIgnored( path, fn.GetFullName(), false ) )
95 return wxDIR_CONTINUE;
96
97 bool exclude = fn.GetName().Contains( "fp-info-cache" )
98 || fn.GetName().StartsWith( FILEEXT::LockFilePrefix );
99
100 if( !exclude )
101 m_files.emplace_back( wxFileName( filename ) );
102
103 return wxDIR_CONTINUE;
104 }
105
106 virtual wxDirTraverseResult OnDir( const wxString& dirname ) override
107 {
108 wxFileName dir( dirname );
109 wxString parent = dir.GetPathWithSep();
110
111 EnsureGitFiles( parent );
112
113 if( dir.GetFullName() == wxT( ".git" ) || IsIgnored( parent, dir.GetFullName(), true )
114 || dirname.StartsWith( m_exclude ) || dirname.EndsWith( "-backups" ) )
115 {
116 return wxDIR_IGNORE;
117 }
118
119 m_files.emplace_back( wxFileName::DirName( dirname ) );
120 EnsureGitFiles( dirname + wxFileName::GetPathSeparator() );
121 return wxDIR_CONTINUE;
122 }
123
124private:
125 void EnsureGitFiles( const wxString& path )
126 {
127 if( m_gitIgnores.find( path ) != m_gitIgnores.end() )
128 return;
129
130 wxString gitignore = path + wxT( ".gitignore" );
131
132 if( wxFileExists( gitignore ) )
133 {
134 wxFileInputStream input( gitignore );
135 wxTextInputStream text( input, wxT( "\x9" ), wxConvUTF8 );
136
137 while( input.IsOk() && !input.Eof() )
138 {
139 wxString line = text.ReadLine();
140
141 line.Trim().Trim( false );
142
143 if( line.IsEmpty() || line.StartsWith( wxT( "#" ) ) )
144 continue;
145
146 m_gitIgnores[path].push_back( line );
147 }
148
149 m_files.emplace_back( wxFileName( gitignore ) );
150 }
151 else
152 {
153 m_gitIgnores[path] = {};
154 }
155
156 wxString gitattributes = path + wxT( ".gitattributes" );
157
158 if( wxFileExists( gitattributes ) )
159 m_files.emplace_back( wxFileName( gitattributes ) );
160 }
161
162 bool IsIgnored( const wxString& path, const wxString& name, bool isDir )
163 {
164 auto it = m_gitIgnores.find( path );
165
166 if( it == m_gitIgnores.end() )
167 return false;
168
169 for( const wxString& pattern : it->second )
170 {
171 bool dirOnly = pattern.EndsWith( wxT( "/" ) );
172 wxString pat = dirOnly ? pattern.substr( 0, pattern.length() - 1 ) : pattern;
173
174 if( dirOnly && !isDir )
175 continue;
176
177 if( wxMatchWild( pat, name ) )
178 return true;
179 }
180
181 return false;
182 }
183
184 std::vector<wxFileName>& m_files;
185 wxString m_exclude;
186 std::unordered_map<wxString, std::vector<wxString>> m_gitIgnores;
187};
188
189
190std::vector<wxFileName> PROJECT_TEMPLATE::GetFileList()
191{
192 std::vector<wxFileName> files;
193 FILE_TRAVERSER sink( files, m_metaPath.GetPath() );
194 wxDir dir( m_basePath.GetPath() );
195
196 dir.Traverse( sink, wxEmptyString, ( wxDIR_FILES | wxDIR_DIRS ) );
197 return files;
198}
199
200
202{
203 return m_basePath.GetDirs()[m_basePath.GetDirCount() - 1];
204}
205
206
210
211
213{
214 return m_metaHtmlFile;
215}
216
217
219{
220 return m_metaIcon;
221}
222
223
224size_t PROJECT_TEMPLATE::GetDestinationFiles( const wxFileName& aNewProjectPath, std::vector<wxFileName>& aDestFiles )
225{
226 std::vector<wxFileName> srcFiles = GetFileList();
227
228 // Find the template file name base. this is the name of the .pro template file
229 wxString basename;
230 bool multipleProjectFilesFound = false;
231
232 for( wxFileName& file : srcFiles )
233 {
234 if( file.GetExt() == FILEEXT::ProjectFileExtension || file.GetExt() == FILEEXT::LegacyProjectFileExtension )
235 {
236 if( !basename.IsEmpty() && basename != file.GetName() )
237 multipleProjectFilesFound = true;
238
239 basename = file.GetName();
240 }
241 }
242
243 if( multipleProjectFilesFound )
244 basename = GetPrjDirName();
245
246 for( wxFileName& srcFile : srcFiles )
247 {
248 // Replace the template path
249 wxFileName destFile = srcFile;
250
251 // Replace the template filename with the project filename for the new project creation
252 wxString name = destFile.GetName();
253 name.Replace( basename, aNewProjectPath.GetName() );
254 destFile.SetName( name );
255
256 // Replace the template path with the project path, also renaming any subdirectories
257 // that contain the template basename.
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 );
263
264 aDestFiles.push_back( destFile );
265 }
266
267 return aDestFiles.size();
268}
269
270
271bool PROJECT_TEMPLATE::CreateProject( wxFileName& aNewProjectPath, wxString* aErrorMsg )
272{
273 // CreateProject copy the files from template to the new project folder and renames files
274 // which have the same name as the template .kicad_pro file
275 bool result = true;
276
277 std::vector<wxFileName> srcFiles = GetFileList();
278
279 // Find the template file name base. this is the name of the .kicad_pro (or .pro) template
280 // file
281 wxString basename;
282 bool multipleProjectFilesFound = false;
283
284 for( wxFileName& file : srcFiles )
285 {
286 if( file.GetExt() == FILEEXT::ProjectFileExtension || file.GetExt() == FILEEXT::LegacyProjectFileExtension )
287 {
288 if( !basename.IsEmpty() && basename != file.GetName() )
289 multipleProjectFilesFound = true;
290
291 basename = file.GetName();
292 }
293 }
294
295 if( multipleProjectFilesFound )
296 basename = GetPrjDirName();
297
298 for( wxFileName& srcFile : srcFiles )
299 {
300 // Replace the template path
301 wxFileName destFile = srcFile;
302
303 // Replace the template filename with the project filename for the new project creation
304 wxString currname = destFile.GetName();
305
306 if( destFile.GetExt() == FILEEXT::DrawingSheetFileExtension )
307 {
308 // Don't rename drawing sheet definitions; they're often shared
309 }
310 else if( destFile.GetName().EndsWith( "-cache" ) || destFile.GetName().EndsWith( "-rescue" ) )
311 {
312 currname.Replace( basename, aNewProjectPath.GetName() );
313 }
314 else if( destFile.GetExt() == FILEEXT::LegacySymbolDocumentFileExtension
315 || destFile.GetExt() == FILEEXT::LegacySymbolLibFileExtension
316 // Footprint libraries are directories not files, so GetExt() won't work
317 || destFile.GetPath().EndsWith( '.' + FILEEXT::KiCadFootprintLibPathExtension ) )
318 {
319 // Don't rename project-specific libraries. This will break the library tables and
320 // cause broken links in the schematic/pcb.
321 }
322 else
323 {
324 currname.Replace( basename, aNewProjectPath.GetName() );
325 }
326
327 destFile.SetName( currname );
328
329 // Replace the template path with the project path for the new project creation,
330 // also renaming any subdirectories that contain the template basename.
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( "-" ) );
335
336 // Check to see if the path already exists, if not attempt to create it here.
337 if( !wxFileName::DirExists( destpath ) )
338 {
339 if( !wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL ) )
340 {
341 if( aErrorMsg )
342 {
343 if( !aErrorMsg->empty() )
344 *aErrorMsg += "\n";
345
346 wxString msg;
347
348 msg.Printf( _( "Cannot create folder '%s'." ), destpath );
349 *aErrorMsg += msg;
350 }
351
352 continue;
353 }
354 }
355
356 destFile.SetPath( destpath );
357
358 if( srcFile.FileExists() && !wxCopyFile( srcFile.GetFullPath(), destFile.GetFullPath() ) )
359 {
360 if( aErrorMsg )
361 {
362 if( !aErrorMsg->empty() )
363 *aErrorMsg += "\n";
364
365 wxString msg;
366
367 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
368 *aErrorMsg += msg;
369 }
370
371 result = false;
372 }
373 }
374
375 return result;
376}
377
378
380{
381 if( !GetHtmlFile().IsFileReadable() )
382 return &m_title;
383
384 if( m_title == wxEmptyString )
385 {
386 wxFFileInputStream input( GetHtmlFile().GetFullPath() );
387 wxString separator( wxT( "\x9" ) );
388 wxTextInputStream text( input, separator, wxConvUTF8 );
389
390 int start = 0;
391 int finish = 0;
392 bool done = false;
393 bool hasStart = false;
394
395 while( input.IsOk() && !input.Eof() && !done )
396 {
397 wxString line = text.ReadLine();
398 wxString upperline = line.Clone().Upper();
399
400 start = upperline.Find( wxT( "<TITLE>" ) );
401 finish = upperline.Find( wxT( "</TITLE>" ) );
402 int length = finish - start - 7;
403
404 // find the opening tag
405 if( start != wxNOT_FOUND )
406 {
407 if( finish != wxNOT_FOUND )
408 {
409 m_title = line( start + 7, length );
410 done = true;
411 }
412 else
413 {
414 m_title = line.Mid( start + 7 );
415 hasStart = true;
416 }
417 }
418 else
419 {
420 if( finish != wxNOT_FOUND )
421 {
422 m_title += line.SubString( 0, finish - 1 );
423 done = true;
424 }
425 else if( hasStart )
426 m_title += line;
427 }
428 }
429
430 // Remove line endings
431 m_title.Replace( wxT( "\r" ), wxT( "" ) );
432 m_title.Replace( wxT( "\n" ), wxT( "" ) );
433
434 m_title.Trim( false ); // Trim from left
435 m_title.Trim(); // Trim from right
436 }
437
438 return &m_title;
439}
const char * name
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.
#define _(s)
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 SEP()
#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.
std::string path
wxString result
Test unit parsing edge cases and error handling.
Definition of file extensions used in Kicad.