]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMMain.cpp
e856fd2bae2f847f2d318a55bee08a3679283cfc
[crea.git] / lib / creaDevManagerLib / modelCDMMain.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  * modelCDMMain.cpp
30  *
31  *  Created on: 13/11/2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMMain.h"
36
37
38 #include <cstdlib>
39 #include <iostream>
40 #include <string>
41 #include <cstdio>
42 #include <fstream>
43
44 #include "CDMUtilities.h"
45 #include "modelCDMProject.h"
46
47 modelCDMMain::modelCDMMain()
48 {
49   this->project = NULL;
50 }
51
52 modelCDMMain::~modelCDMMain()
53 {
54   if(project != NULL)
55     {
56       delete this->project;
57       this->project = NULL;
58     }
59 }
60
61 modelCDMProject* modelCDMMain::GetProject() const
62 {
63   return this->project;
64 }
65
66 bool modelCDMMain::CreateProject(
67     const std::string& name,
68     const std::string& location,
69     std::string*& result,
70     const std::string& author,
71     const std::string& description)
72 {
73   std::cout << "Open selection path: "<< location << std::endl;
74   //get fixed location
75   std::string locationFixed = CDMUtilities::fixPath(location);
76   std::cout << "Opening path: "<< locationFixed << std::endl;
77
78   //creates project in disk
79 #if(_WIN32)
80
81   std::string command("creaNewProject.bat ");
82   std::string command1("creaSed.exe ");
83   std::string command2("del ");
84
85   command  += "\"" + locationFixed + "\" \"" + name + "\"";
86   command1 += "\"" + locationFixed +CDMUtilities::SLASH+name+CDMUtilities::SLASH+"CMakeLists.txt.in\" " + "NameOfTheProject " + name + "> \"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
87   command2 += "\"" + locationFixed +CDMUtilities::SLASH+name+CDMUtilities::SLASH+"CMakeLists.txt.in\"";
88
89
90   if (system (command.c_str()))
91     {
92       result = new std::string("An error occurred while running '" + command + "'.");
93       return false;
94     }
95
96   system ( command1.c_str() );
97   system ( command2.c_str() );
98
99   char *author = author.c_str();
100   std::string nomDirectory = locationFixed + CDMUtilities::SLASH + name;
101   std::string nomPackageDirectory = nomDirectory + CDMUtilities::SLASH + "bbtk_" + name + "_PKG";
102   std::string bbCreatePackage("bbCreatePackage ");
103   bbCreatePackage += "\"" + nomDirectory + "\" \"" + name + "\" \"" + author + "\" \"" + description + "\"";
104   system (bbCreatePackage.c_str());
105   std::string add;
106   add = "echo ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG) >> \"" + nomDirectory + CDMUtilities::SLASH + "CMakeLists.txt\"";
107   system(add.c_str());
108
109   this->project = new modelCDMProject(nomDirectory, name);
110
111 #else
112   // ------ LINUX / MacOS
113   std::string command("creaNewProject.sh ");
114   command += "\"" + locationFixed + "\"" +" \"" + name + "\"";
115   //std::cout << "executing " << command << std::endl;
116   if (system ( command.c_str() ) )
117     {
118       result = new std::string("An error occured while running '" + command + "'.");
119       return false;
120     }
121
122   std::string nomDirectory = locationFixed + CDMUtilities::SLASH + name;
123   std::string nomPackageDirectory = nomDirectory + CDMUtilities::SLASH + "bbtk_" + name + "_PKG";
124
125   std::string bbCreatePackage("bbCreatePackage ");
126   bbCreatePackage += "\"" + nomDirectory + "\" \"" + name + "\" \"" + author + "\" \"" + description + "\"";
127   //std::cout << "executing " << bbCreatePackage << std::endl;
128   system (bbCreatePackage.c_str());
129
130   std::string add;
131   add = "echo 'ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG)' >> \"" + nomDirectory + CDMUtilities::SLASH + "CMakeLists.txt\"";
132
133   //std::cout << "executing " << add << std::endl;
134   system(add.c_str());
135
136   if(this->project != NULL)
137     {
138       if (!CloseProject(result))
139         return false;
140     }
141
142   this->project = new modelCDMProject(NULL, nomDirectory, name);
143
144 #endif
145
146   return true;
147 }
148
149 bool modelCDMMain::OpenProject(
150     const std::string& path,
151     std::string*& result
152 )
153 {
154   //std::cout << "Open selection path: "<< path << std::endl;
155   //get fixed path
156   std::string pathFixed = CDMUtilities::fixPath(path);
157   std::cout << "Opening path: "<< pathFixed << std::endl;
158
159   //check if its binaries' folder
160   bool isBinary = false;
161   std::string pathBuild = "";
162
163   //check if Makefile file exists
164   std::string pathMakefile = pathFixed + CDMUtilities::SLASH + "Makefile";
165   FILE* pFile = fopen(pathMakefile.c_str(), "r");
166
167   //is the binary folder
168   if (pFile != NULL)
169     {
170       fclose(pFile);
171       std::ifstream readFile;
172       readFile.open(pathMakefile.c_str());
173       std::string word;
174
175       while(!isBinary && readFile >> word)
176         {
177           if(word == "CMAKE_SOURCE_DIR")
178             {
179               readFile >> word;
180               readFile.ignore();
181               getline(readFile, word, '\n');
182               pathBuild = pathFixed;
183               pathFixed = CDMUtilities::fixPath(word);
184               isBinary = true;
185             }
186         }
187       readFile.close();
188     }
189
190   //check if its source's folder
191   bool isSource = false;
192   std::string pathSource = "";
193   //check if CMakeLists file exists
194   std::string pathCMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
195   pFile = fopen(pathCMakeLists.c_str(), "r");
196
197   //is the source folder
198   if (pFile != NULL)
199     {
200       fclose(pFile);
201       std::ifstream readFile;
202       readFile.open(pathCMakeLists.c_str());
203       std::string word;
204
205
206       while(!isSource && !readFile.eof())
207         {
208           std::getline(readFile,word,'(');
209           std::vector<std::string> wordBits;
210           CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
211
212           if(wordBits[wordBits.size()-1] == "PROJECT")
213             {
214               isSource = true;
215               pathSource = pathFixed;
216             }
217         }
218       readFile.close();
219     }
220
221   //if is source folder
222   if(isSource)
223     {
224       if(this->project != NULL)
225         {
226           if (!CloseProject(result))
227             return false;
228         }
229       std::vector<std::string> words;
230       CDMUtilities::splitter::split(words, pathSource, CDMUtilities::SLASH, CDMUtilities::splitter::no_empties);
231
232       std::cout << "Project sources at: " << pathSource << std::endl;
233       if(isBinary)
234         {
235           std::cout << ", and built in: " << pathBuild << std::endl;
236
237           this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1], pathBuild);
238         }
239       else
240         {
241           this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1]);
242         }
243     }
244   else
245     {
246       result = new std::string("No source folder found. Please make sure to select either the project's build or source folder.");
247       return false;
248     }
249
250   return true;
251 }
252
253 bool modelCDMMain::RefreshProject(
254     std::string*& result
255 )
256 {
257   //recreate the project using the project's path
258   if (this->project != NULL)
259     {
260       return this->project->Refresh(result);
261     }
262   else
263     {
264       result = new std::string("There is no open project.");
265       return false;
266     }
267 }
268
269 std::map<wxTreeItemId, modelCDMIProjectTreeNode*>& modelCDMMain::GetModelElements()
270 {
271   return this->modelElements;
272 }
273
274 bool modelCDMMain::CloseProject(
275     std::string*& result
276 )
277 {
278   //delete the project tree and leave it as NULL
279   if (this->project != NULL)
280     {
281       delete this->project;
282       this->project = NULL;
283       return true;
284     }
285   else
286     {
287       result = new std::string("There is no open project.");
288       return false;
289     }
290 }