]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMProject.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMProject.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Sant�)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ 
26  */
27
28 /*
29  * modelCDMProject.cpp
30  *
31  *  Created on: 13/11/2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMProject.h"
36
37 #include <iostream>
38 #include <vector>
39 #include <algorithm>
40 #include <fstream>
41
42 #include "CDMUtilities.h"
43 #include "creaWx.h"
44 #include "wx/dir.h"
45
46 modelCDMProject::modelCDMProject()
47 {
48   std::cout << "in constructor1" << std::endl;
49   this->appli = NULL;
50   this->lib = NULL;
51   this->CMakeLists = NULL;
52 }
53
54 modelCDMProject::modelCDMProject(
55     const std::string& path,
56     const std::string& buildPath
57 )
58 {
59   this->path = CDMUtilities::fixPath(path);
60   //open makelists file
61   std::string pathFixed(CDMUtilities::fixPath(path));
62
63   //TODO: set pathMakeLists for windows
64   std::string pathMakeLists = pathFixed + "/CMakeLists.txt";
65
66   std::ifstream confFile;
67   confFile.open((pathMakeLists).c_str());
68
69   std::string word;
70   while(confFile.is_open() && !confFile.eof())
71     {
72       //std::cout << "leyendo " << word << std::endl;
73       //get project name
74       std::getline(confFile,word,'(');
75       std::vector<std::string> wordBits;
76       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
77
78       if(wordBits[wordBits.size()-1] == "PROJECT")
79         {
80           std::getline(confFile,word,')');
81           std::vector<std::string> nameBits;
82           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
83
84           this->name = this->nameProject = "";
85           for (int i = 0; i < nameBits.size(); i++)
86             {
87               if(i != 0)
88                 this->name += " ";
89               this->name += nameBits[i];
90             }
91           this->nameProject = this->name;
92
93         }
94
95
96       if(wordBits[wordBits.size()-1] == "SET")
97         {
98           //get project version
99           std::getline(confFile,word,')');
100           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
101           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
102             {
103               version = wordBits[1];
104             }
105           if(wordBits[0] == "PROJECT_MINOR_VERSION")
106             {
107               version += "." + wordBits[1];
108             }
109           if(wordBits[0] == "PROJECT_BUILD_VERSION")
110             {
111               version += "." + wordBits[1];
112             }
113
114           //get project versionDate
115           if(wordBits[0] == "PROJECT_VERSION_DATE")
116             {
117               std::vector<std::string> versionBits;
118               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
119               versionDate = versionBits[0];
120             }
121           //get project buildPath
122
123           if (buildPath != "")
124             {
125               this->buildPath = buildPath;
126             }
127           else
128             {
129               this->buildPath = this->path + "Bin";
130             }
131         }
132     }
133   confFile.close();
134
135   this->type = wxDIR_DIRS;
136   this->level = 0;
137
138   this->children.clear();
139   this->appli = NULL;
140   this->lib = NULL;
141   this->packages.clear();
142
143
144   //check all folders
145   wxDir dir(crea::std2wx((pathFixed).c_str()));
146   if (dir.IsOpened())
147     {
148       wxString fileName;
149       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
150       while (cont)
151         {
152           std::string stdfileName = crea::wx2std(fileName);
153
154           //if appli, create appli
155           if(stdfileName == "appli")
156             {
157               this->appli = new modelCDMAppli(pathFixed + "/appli", this->level + 1);
158               this->children.push_back(this->appli);
159             }
160           //if lib, create lib
161           else if(stdfileName == "lib")
162             {
163               this->lib = new modelCDMLib(pathFixed + "/lib", this->level + 1);
164               this->children.push_back(this->lib);
165             }
166           //if package , create package
167           else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
168             {
169               modelCDMPackage* package = new modelCDMPackage(pathFixed + "/" + stdfileName, this->level + 1);
170               this->packages.push_back(package);
171               this->children.push_back(package);
172             }
173           //if is an unknown folder, create folder
174           else
175             {
176               this->children.push_back(new modelCDMFolder(pathFixed + "/" + stdfileName, this->level + 1));
177             }
178
179           cont = dir.GetNext(&fileName);
180         }
181
182       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
183       while (cont)
184         {
185           std::string stdfileName = crea::wx2std(fileName);
186
187           //if CMakeLists, create CMakeLists
188           if(stdfileName == "CMakeLists.txt")
189             {
190               this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + "/" + stdfileName, this->level + 1);
191               this->children.push_back(this->CMakeLists);
192             }
193           else
194             {
195               this->children.push_back(new modelCDMFile(pathFixed + "/" + stdfileName, this->level + 1));
196             }
197           //if is an unknown file, create file
198           cont = dir.GetNext(&fileName);
199         }
200     }
201
202   this->SortChildren();
203
204 }
205
206 modelCDMProject::~modelCDMProject()
207 {
208 }
209
210 const std::string& modelCDMProject::GetNameProject() const
211 {
212   return this->nameProject;
213 }
214
215 const std::string& modelCDMProject::GetVersion() const
216 {
217   return this->version;
218 }
219
220 const std::string& modelCDMProject::GetVersionDate() const
221 {
222   return this->versionDate;
223 }
224
225 const std::string& modelCDMProject::GetBuildPath() const
226 {
227   return this->buildPath;
228 }
229
230 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
231 {
232   //TODO: implement method
233   return true;
234 }
235
236 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
237 {
238   //TODO: implement method
239   return true;
240 }
241
242 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
243     const std::string& name,
244     std::string*& result,
245     const std::string& authors,
246     const std::string& authorsEmail,
247     const std::string& version,
248     const std::string& description
249 )
250 {
251   //TODO: implement method
252   return NULL;
253 }
254
255 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
256     const std::string& name,
257     std::string*& result,
258     const std::string& path
259 )
260 {
261   if(this->lib != NULL)
262     {
263       return this->lib->CreateLibrary(name, result);
264     }
265   result = new std::string("there is no lib folder in this project.");
266   return NULL;
267 }
268
269 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
270     const std::string& name,
271     std::string*& result,
272     const std::string& path
273 )
274 {
275   //TODO: implement method
276   return NULL;
277 }
278
279 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
280     const std::string& name,
281     const std::string& package,
282     const std::string& authors,
283     const std::string& authorsEmail,
284     const std::string& categories,
285     const std::string& description
286 )
287 {
288   //TODO: implement method
289   return NULL;
290 }
291
292 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
293 {
294   //TODO: implement method
295   return true;
296 }
297
298 const bool modelCDMProject::Refresh(std::string*& result)
299 {
300
301   //open makelists file
302   //TODO: set pathMakeLists for windows
303   std::string pathMakeLists = this->path + "/CMakeLists.txt";
304
305   std::ifstream confFile;
306   confFile.open((pathMakeLists).c_str());
307
308   std::string word;
309   while(confFile.is_open() && !confFile.eof())
310     {
311       //std::cout << "leyendo " << word << std::endl;
312       //get project name
313       std::getline(confFile,word,'(');
314       std::vector<std::string> wordBits;
315       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
316
317       if(wordBits[wordBits.size()-1] == "PROJECT")
318         {
319           std::getline(confFile,word,')');
320           std::vector<std::string> nameBits;
321           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
322
323           this->name = this->nameProject = "";
324           for (int i = 0; i < nameBits.size(); i++)
325             {
326               if(i != 0)
327                 this->name += " ";
328               this->name += nameBits[i];
329             }
330           this->nameProject = this->name;
331
332         }
333
334
335       if(wordBits[wordBits.size()-1] == "SET")
336         {
337           //get project version
338           std::getline(confFile,word,')');
339           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
340           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
341             {
342               version = wordBits[1];
343             }
344           if(wordBits[0] == "PROJECT_MINOR_VERSION")
345             {
346               version += "." + wordBits[1];
347             }
348           if(wordBits[0] == "PROJECT_BUILD_VERSION")
349             {
350               version += "." + wordBits[1];
351             }
352
353           //get project versionDate
354           if(wordBits[0] == "PROJECT_VERSION_DATE")
355             {
356               std::vector<std::string> versionBits;
357               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
358               versionDate = versionBits[0];
359             }
360         }
361     }
362   confFile.close();
363
364   this->type = wxDIR_DIRS;
365   this->level = 0;
366
367   std::vector<bool> checked(this->children.size(), false);
368   std::vector<bool> checkedPackages(this->packages.size(), false);
369
370   //check all folders
371   wxDir dir(crea::std2wx((this->path).c_str()));
372   if (dir.IsOpened())
373     {
374       wxString fileName;
375       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
376       while (cont)
377         {
378           std::string stdfileName = crea::wx2std(fileName);
379
380           //if appli, create appli
381           if(stdfileName == "appli")
382             {
383               if (this->appli == NULL)
384                 {
385                   this->appli = new modelCDMAppli(this->path + "/appli", this->level + 1);
386                   this->children.push_back(this->appli);
387                 }
388               else
389                 {
390                   int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
391                   checked[pos] = true;
392                   if(!this->appli->Refresh(result))
393                     return false;
394                 }
395             }
396           //if lib, create lib
397           else if(stdfileName == "lib")
398             {
399               if (this->lib == NULL)
400                 {
401                   this->lib = new modelCDMLib(this->path + "/lib", this->level + 1);
402                   this->children.push_back(this->lib);
403                 }
404               else
405                 {
406                   int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
407                   checked[pos] = true;
408                   if(!this->lib->Refresh(result))
409                     return false;
410                 }
411
412             }
413           //if package , create package
414           else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
415             {
416               std::string packageName = stdfileName.substr(5, stdfileName.size()-9);
417               bool found = false;
418               for (int i = 0;!found && i < this->packages.size(); i++)
419                 {
420                   if (this->packages[i]->GetName() == packageName)
421                     {
422                       found = true;
423                       int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
424                       checked[pos] = true;
425                       checkedPackages[i] = true;
426                       if(!this->packages[i]->Refresh(result))
427                         return false;
428                     }
429                 }
430               if(!found)
431                 {
432                   modelCDMPackage* package = new modelCDMPackage(this->path + "/" + stdfileName, this->level + 1);
433                   this->packages.push_back(package);
434                   this->children.push_back(package);
435                 }
436
437             }
438           //if is an unknown folder, create folder
439           else
440             {
441               bool found = false;
442               for (int i = 0; !found && i < this->children.size(); i++)
443                 {
444                   if (this->children[i]->GetName() == stdfileName)
445                     {
446                       found = true;
447                       checked[i] = true;
448                       if(!this->children[i]->Refresh(result))
449                         return false;
450                     }
451                 }
452
453               if(!found)
454                 {
455                   modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
456                   this->children.push_back(folder);
457                 }
458             }
459
460           cont = dir.GetNext(&fileName);
461         }
462
463       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
464       while (cont)
465         {
466           std::string stdfileName = crea::wx2std(fileName);
467
468           //if CMakeLists, create CMakeLists
469           if(stdfileName == "CMakeLists.txt")
470             {
471               if (this->CMakeLists == NULL)
472                 {
473                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
474                   this->children.push_back(this->CMakeLists);
475                 }
476               else
477                 {
478                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
479                   checked[pos] = true;
480                   if(!this->CMakeLists->Refresh(result))
481                     return false;
482                 }
483             }
484           //if is an unknown file, create file
485           else
486             {
487               bool found = false;
488               for (int i = 0; i <!found && this->children.size(); i++)
489                 {
490                   if (this->children[i]->GetName() == stdfileName)
491                     {
492                       found = true;
493                       checked[i] = true;
494                       if(!this->children[i]->Refresh(result))
495                         return false;
496                     }
497                 }
498
499               if(!found)
500                 {
501                   modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
502                   this->children.push_back(file);
503                 }
504             }
505
506           cont = dir.GetNext(&fileName);
507         }
508     }
509
510   for (int i = 0; i < checkedPackages.size(); i++)
511     {
512       if(!checkedPackages[i])
513         {
514           this->packages.erase(this->packages.begin()+i);
515                     checkedPackages.erase(checkedPackages.begin()+i);
516                     i--;
517         }
518     }
519   for (int i = 0; i < checked.size(); i++)
520     {
521       if(!checked[i])
522         {
523           delete this->children[i];
524           this->children.erase(this->children.begin()+i);
525           checked.erase(checked.begin()+i);
526           i--;
527         }
528     }
529   this->SortChildren();
530   return true;
531 }
532
533 bool modelCDMProject::ConfigureBuild(std::string*& result)
534 {
535   //TODO: implement method
536   return true;
537 }
538
539 bool modelCDMProject::Build(std::string*& result)
540 {
541   //TODO: implement method
542   return true;
543 }
544
545 bool modelCDMProject::Connect(std::string*& result)
546 {
547   //TODO: implement method
548   return true;
549 }