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