]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMPackage.cpp
9ec218a6817fd7fa3e59fc144d1bc8d71059d61e
[crea.git] / lib / creaDevManagerLib / modelCDMPackage.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  * modelCDMPackage.cpp
30  *
31  *  Created on: Nov 23, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMPackage.h"
36
37 #include <fstream>
38
39 #include "creaWx.h"
40 #include "wx/dir.h"
41 #include "CDMUtilities.h"
42
43 modelCDMPackage::modelCDMPackage()
44 {
45 }
46
47 modelCDMPackage::modelCDMPackage(const std::string& path, const int& level)
48 {
49   this->type = wxDIR_DIRS;
50   //Get Package Name
51
52   //TODO: set pathMakeLists for windows
53   std::string pathMakeLists = path + "/CMakeLists.txt";
54
55   std::ifstream confFile;
56   confFile.open((pathMakeLists).c_str());
57
58   std::string word;
59   while(confFile.is_open() && !confFile.eof())
60     {
61       //get sets
62       std::getline(confFile,word,'(');
63       std::vector<std::string> wordBits;
64       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
65
66       if(wordBits[wordBits.size()-1] == "SET")
67         {
68           //get package name
69           std::getline(confFile,word,')');
70           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
71           if(wordBits[0] == "BBTK_PACKAGE_NAME")
72             {
73               word = wordBits[1];
74               for (int i = 2; i < wordBits.size(); i++)
75                 {
76                   word += " " + wordBits[i];
77                 }
78               wordBits.clear();
79               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
80
81               this->namePackage = wordBits[0];
82             }
83           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
84             {
85               word = wordBits[1];
86               for (int i = 2; i < wordBits.size(); i++)
87                 {
88                   word += " " + wordBits[i];
89                 }
90               wordBits.clear();
91               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
92
93               this->authors = wordBits[0];
94             }
95           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
96             {
97               word = wordBits[1];
98               for (int i = 2; i < wordBits.size(); i++)
99                 {
100                   word += " " + wordBits[i];
101                 }
102               wordBits.clear();
103               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
104
105               this->description = wordBits[0];
106             }
107           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
108             {
109               this->version = wordBits[1];
110             }
111           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
112             {
113               this->version += "." + wordBits[1];
114             }
115           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
116             {
117               this->version += "." + wordBits[1];
118             }
119         }
120     }
121
122   std::vector<std::string> words;
123   std::string delimiters;
124   //TODO::fix for windows
125   delimiters = "/";
126   CDMUtilities::splitter::split(words, path, delimiters, CDMUtilities::splitter::no_empties);
127   this->name = words[words.size()-1];
128   this->level = level;
129   this->path = path;
130
131   //check all folders and files
132   wxDir dir(crea::std2wx((path).c_str()));
133   if (dir.IsOpened())
134     {
135       wxString fileName;
136
137       //folders
138       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
139       while (cont)
140         {
141           std::string stdfileName = crea::wx2std(fileName);
142           //if src, check for black boxes
143           if(stdfileName == "src")
144             {
145               wxDir srcF(crea::std2wx((path + delimiters + "src").c_str()));
146               if (srcF.IsOpened())
147                 {
148                   wxString srcName;
149                   bool innerCont = srcF.GetFirst(&srcName, wxT("*.h"), wxDIR_FILES);
150                   while (innerCont)
151                     {
152                       if(crea::wx2std(srcName.substr(0,2)) == "bb")
153                         {
154                           modelCDMBlackBox* blackbox = new modelCDMBlackBox(crea::wx2std(srcName), path + delimiters + "src", this->level + 1);
155                           this->blackBoxes.push_back(blackbox);
156                           this->children.push_back(blackbox);
157                         }
158                       innerCont = srcF.GetNext(&srcName);
159                     }
160                 }
161             }
162           //if is an unknown folder, create folder
163           else
164             {
165               this->children.push_back(new modelCDMFolder(path + delimiters + stdfileName, this->level + 1));
166             }
167           cont = dir.GetNext(&fileName);
168         }
169
170       //files
171       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
172       while (cont)
173         {
174           std::string stdfileName = crea::wx2std(fileName);
175
176           //if CMakeLists, create CMakeLists
177           if(stdfileName == "CMakeLists.txt")
178             {
179               this->CMakeLists = new modelCDMCMakeListsFile(path + delimiters + stdfileName, this->level + 1);
180               this->children.push_back(this->CMakeLists);
181             }
182           else
183             {
184               this->children.push_back(new modelCDMFile(path + delimiters + stdfileName, this->level + 1));
185             }
186           //if is an unknown file, create file
187           cont = dir.GetNext(&fileName);
188         }
189     }
190   this->SortChildren();
191 }
192
193 modelCDMPackage::~modelCDMPackage()
194 {
195 }
196
197 const std::string& modelCDMPackage::GetNamePackage() const
198 {
199   return this->namePackage;
200 }
201
202 const std::string& modelCDMPackage::GetAuthors() const
203 {
204   return this->authors;
205 }
206
207 const std::string& modelCDMPackage::GetAuthorsEmail() const
208 {
209   return this->authorsEmail;
210 }
211
212 const std::string& modelCDMPackage::GetVersion() const
213 {
214   return this->version;
215 }
216
217 const std::string& modelCDMPackage::GetDescription() const
218 {
219   return this->description;
220 }
221
222 const std::vector<modelCDMBlackBox*>& modelCDMPackage::GetBlackBoxes() const
223 {
224   return this->blackBoxes;
225 }
226
227 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
228 {
229   std::vector<std::string> words;
230   CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
231   std::string authorsReal = words[0];
232   for (int i = 1; i < words.size(); i++)
233     {
234       authorsReal += "/" + words[i];
235     }
236
237   std::string line;
238   //opening original cmakelists
239   std::ifstream in((this->path + "/CMakeLists.txt").c_str());
240   if( !in.is_open())
241     {
242       result = new std::string("CMakeLists.txt file failed to open.");
243       return false;
244     }
245   //opening temporal cmakelists
246   std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
247   if( !out.is_open())
248     {
249       result = new std::string("CMakeLists.txt.tmp file failed to open.");
250       return false;
251     }
252   //copying contents from original to temporal and making changes
253   while (getline(in, line))
254     {
255       if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
256         line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
257       out << line << std::endl;
258     }
259   in.close();
260   out.close();
261   //delete old file and rename new file
262   std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
263   if(system(renameCommand.c_str()))
264     {
265       result = new std::string("An error occurred while running '" + renameCommand + "'.");
266       return false;
267     }
268
269   this->authors = authorsReal;
270   return true;
271 }
272
273 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
274 {
275   //TODO: implement method
276   return true;
277 }
278
279 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
280 {
281   std::vector<std::string> vers;
282   CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
283
284
285   std::string line;
286   //opening original cmakelists
287   std::ifstream in((this->path + "/CMakeLists.txt").c_str());
288   if( !in.is_open())
289     {
290       result = new std::string("CMakeLists.txt file failed to open.");
291       return false;
292     }
293   //opening temporal cmakelists
294   std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
295   if( !out.is_open())
296     {
297       result = new std::string("CMakeLists.txt.tmp file failed to open.");
298       return false;
299     }
300   //copying contents from original to temporal and making changes
301   while (getline(in, line))
302     {
303       if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
304         line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
305       else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
306         line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
307       else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
308         line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
309       out << line << std::endl;
310     }
311   in.close();
312   out.close();
313   //delete old file and rename new file
314   std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
315   if(system(renameCommand.c_str()))
316     {
317       result = new std::string("An error occurred while running '" + renameCommand + "'.");
318       return false;
319     }
320
321   this->version = vers[0] + "." + vers[1] + "." + vers[2];
322   return true;
323 }
324
325 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
326 {
327   std::vector<std::string> words;
328   CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
329   std::string descriptionReal = words[0];
330   for (int i = 1; i < words.size(); i++)
331     {
332       descriptionReal += " " + words[i];
333     }
334
335   std::string line;
336   //opening original cmakelists
337   std::ifstream in((this->path + "/CMakeLists.txt").c_str());
338   if( !in.is_open())
339     {
340       result = new std::string("CMakeLists.txt file failed to open.");
341       return false;
342     }
343   //opening temporal cmakelists
344   std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
345   if( !out.is_open())
346     {
347       result = new std::string("CMakeLists.txt.tmp file failed to open.");
348       return false;
349     }
350   //copying contents from original to temporal and making changes
351   while (getline(in, line))
352     {
353       if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
354         line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
355       out << line << std::endl;
356     }
357   in.close();
358   out.close();
359   //delete old file and rename new file
360   std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
361   if(system(renameCommand.c_str()))
362     {
363       result = new std::string("An error occurred while running '" + renameCommand + "'.");
364       return false;
365     }
366
367   this->description = descriptionReal;
368   return true;
369 }
370
371 bool modelCDMPackage::CreateBlackBox(
372     const std::string& name,
373     const std::string& authors,
374     const std::string& authorsEmail,
375     const std::string& categories,
376     const std::string& description
377 )
378 {
379   //TODO: implement method
380   return true;
381 }
382
383 const bool modelCDMPackage::Refresh(std::string*& result)
384 {
385   //TODO: implement method
386   return true;
387 }