]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMProject.cpp
23dd1666d041bbc0a186c7628e0adebcb00d9914
[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& description,
248     const std::string& version
249 )
250 {
251   //fixing input parameters
252   std::vector<std::string> words;
253
254   CDMUtilities::splitter::split(words,name," ",CDMUtilities::splitter::no_empties);
255   std::string nameFixed = "";
256   for (int i = 0; i < words.size(); i++)
257     {
258       nameFixed += words[i];
259     }
260
261   words.clear();
262   CDMUtilities::splitter::split(words,authors," ",CDMUtilities::splitter::no_empties);
263   std::string authorFixed;
264   for (int i = 0; i < words.size(); i++)
265     {
266       authorFixed += words[i];
267     }
268
269   words.clear();
270   std::string descriptionFixed;
271   CDMUtilities::splitter::split(words,authorsEmail," ",CDMUtilities::splitter::no_empties);
272   for (int i = 0; i < words.size(); i++)
273     {
274       descriptionFixed += words[i];
275     }
276   words.clear();
277   CDMUtilities::splitter::split(words,description," ",CDMUtilities::splitter::no_empties);
278   for (int i = 0; i < words.size(); i++)
279     {
280       descriptionFixed += "_" + words[i];
281     }
282
283   //call project to create package : use bbCreatePackage <path> <name> [author] [description]
284   std::string creationCommand = "bbCreatePackage \"" + this->path + "\" " + nameFixed + " " + authorFixed + " " + descriptionFixed;
285   //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
286   if(system(creationCommand.c_str()))
287     {
288       result = new std::string("An error occurred while running '" + creationCommand + "'.");
289       return NULL;
290     }
291
292   //add library to model
293   //TODO: fix for windows
294   modelCDMPackage* package = new modelCDMPackage(this->path + "/bbtk_" + nameFixed + "_PKG", this->level + 1);
295   this->packages.push_back(package);
296   this->children.push_back(package);
297
298   //TODO: set package version
299
300   this->SortChildren();
301
302   result = new std::string(this->path + "/" + name);
303   return package;
304 }
305
306 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
307     const std::string& name,
308     std::string*& result,
309     const std::string& path
310 )
311 {
312   if(this->lib != NULL)
313     {
314       return this->lib->CreateLibrary(name, result);
315     }
316   result = new std::string("there is no lib folder in this project.");
317   return NULL;
318 }
319
320 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
321     const std::string& name,
322     std::string*& result,
323     const std::string& path
324 )
325 {
326   if(this->appli != NULL)
327       {
328         return this->appli->CreateApplication(name, result);
329       }
330     result = new std::string("there is no appli folder in this project.");
331     return NULL;
332 }
333
334 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
335     const std::string& name,
336     const std::string& package,
337     const std::string& authors,
338     const std::string& authorsEmail,
339     const std::string& categories,
340     const std::string& description
341 )
342 {
343   //TODO: implement method
344   return NULL;
345 }
346
347 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
348 {
349   //TODO: implement method
350   return true;
351 }
352
353 const bool modelCDMProject::Refresh(std::string*& result)
354 {
355   std::cout << "refreshing project" << std::endl;
356   //open makelists file
357   //TODO: set pathMakeLists for windows
358   std::string pathMakeLists = this->path + "/CMakeLists.txt";
359
360   std::ifstream confFile;
361   confFile.open((pathMakeLists).c_str());
362
363   std::string word;
364   while(confFile.is_open() && !confFile.eof())
365     {
366       //std::cout << "leyendo " << word << std::endl;
367       //get project name
368       std::getline(confFile,word,'(');
369       std::vector<std::string> wordBits;
370       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
371
372       if(wordBits[wordBits.size()-1] == "PROJECT")
373         {
374           std::getline(confFile,word,')');
375           std::vector<std::string> nameBits;
376           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
377
378           this->name = this->nameProject = "";
379           for (int i = 0; i < nameBits.size(); i++)
380             {
381               if(i != 0)
382                 this->name += " ";
383               this->name += nameBits[i];
384             }
385           this->nameProject = this->name;
386
387         }
388
389
390       if(wordBits[wordBits.size()-1] == "SET")
391         {
392           //get project version
393           std::getline(confFile,word,')');
394           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
395           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
396             {
397               version = wordBits[1];
398             }
399           if(wordBits[0] == "PROJECT_MINOR_VERSION")
400             {
401               version += "." + wordBits[1];
402             }
403           if(wordBits[0] == "PROJECT_BUILD_VERSION")
404             {
405               version += "." + wordBits[1];
406             }
407
408           //get project versionDate
409           if(wordBits[0] == "PROJECT_VERSION_DATE")
410             {
411               std::vector<std::string> versionBits;
412               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
413               versionDate = versionBits[0];
414             }
415         }
416     }
417   confFile.close();
418
419   this->type = wxDIR_DIRS;
420   this->level = 0;
421
422   std::vector<bool> checked(this->children.size(), false);
423   std::vector<bool> checkedPackages(this->packages.size(), false);
424
425   //check all folders
426   wxDir dir(crea::std2wx((this->path).c_str()));
427   if (dir.IsOpened())
428     {
429       wxString fileName;
430       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
431       while (cont)
432         {
433           std::string stdfileName = crea::wx2std(fileName);
434
435           //if appli, create appli
436           if(stdfileName == "appli")
437             {
438               if (this->appli == NULL)
439                 {
440                   this->appli = new modelCDMAppli(this->path + "/appli", this->level + 1);
441                   this->children.push_back(this->appli);
442                 }
443               else
444                 {
445                   int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
446                   checked[pos] = true;
447                   if(!this->appli->Refresh(result))
448                     return false;
449                 }
450             }
451           //if lib, create lib
452           else if(stdfileName == "lib")
453             {
454               if (this->lib == NULL)
455                 {
456                   this->lib = new modelCDMLib(this->path + "/lib", this->level + 1);
457                   this->children.push_back(this->lib);
458                 }
459               else
460                 {
461                   int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
462                   checked[pos] = true;
463                   if(!this->lib->Refresh(result))
464                     return false;
465                 }
466
467             }
468           //if package , create package
469           else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
470             {
471               std::string packageName = stdfileName.substr(5, stdfileName.size()-9);
472               bool found = false;
473               for (int i = 0;!found && i < this->packages.size(); i++)
474                 {
475                   if (this->packages[i]->GetName() == packageName)
476                     {
477                       found = true;
478                       int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
479                       checked[pos] = true;
480                       checkedPackages[i] = true;
481                       if(!this->packages[i]->Refresh(result))
482                         return false;
483                     }
484                 }
485               if(!found)
486                 {
487                   modelCDMPackage* package = new modelCDMPackage(this->path + "/" + stdfileName, this->level + 1);
488                   this->packages.push_back(package);
489                   this->children.push_back(package);
490                 }
491
492             }
493           //if is an unknown folder, create folder
494           else
495             {
496               bool found = false;
497               for (int i = 0; !found && i < this->children.size(); i++)
498                 {
499                   if (this->children[i]->GetName() == stdfileName)
500                     {
501                       found = true;
502                       checked[i] = true;
503                       if(!this->children[i]->Refresh(result))
504                         return false;
505                     }
506                 }
507
508               if(!found)
509                 {
510                   modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
511                   this->children.push_back(folder);
512                 }
513             }
514
515           cont = dir.GetNext(&fileName);
516         }
517
518       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
519       while (cont)
520         {
521           std::string stdfileName = crea::wx2std(fileName);
522
523           //if CMakeLists, create CMakeLists
524           if(stdfileName == "CMakeLists.txt")
525             {
526               if (this->CMakeLists == NULL)
527                 {
528                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
529                   this->children.push_back(this->CMakeLists);
530                 }
531               else
532                 {
533                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
534                   checked[pos] = true;
535                   if(!this->CMakeLists->Refresh(result))
536                     return false;
537                 }
538             }
539           //if is an unknown file, create file
540           else
541             {
542               bool found = false;
543               for (int i = 0; i <!found && this->children.size(); i++)
544                 {
545                   if (this->children[i]->GetName() == stdfileName)
546                     {
547                       found = true;
548                       checked[i] = true;
549                       if(!this->children[i]->Refresh(result))
550                         return false;
551                     }
552                 }
553
554               if(!found)
555                 {
556                   modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
557                   this->children.push_back(file);
558                 }
559             }
560
561           cont = dir.GetNext(&fileName);
562         }
563     }
564
565   for (int i = 0; i < checkedPackages.size(); i++)
566     {
567       if(!checkedPackages[i])
568         {
569           this->packages.erase(this->packages.begin()+i);
570           checkedPackages.erase(checkedPackages.begin()+i);
571           i--;
572         }
573     }
574   for (int i = 0; i < checked.size(); i++)
575     {
576       if(!checked[i])
577         {
578           delete this->children[i];
579           this->children.erase(this->children.begin()+i);
580           checked.erase(checked.begin()+i);
581           i--;
582         }
583     }
584   this->SortChildren();
585   return true;
586 }
587
588 bool modelCDMProject::ConfigureBuild(std::string*& result)
589 {
590   //TODO: implement method
591   return true;
592 }
593
594 bool modelCDMProject::Build(std::string*& result)
595 {
596   //TODO: implement method
597   return true;
598 }
599
600 bool modelCDMProject::Connect(std::string*& result)
601 {
602   //TODO: implement method
603   return true;
604 }