]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMMain.cpp
7b47e90035841b8cafb42d19a26113b6246ba121
[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\" " + "PROJECT_NAME " + name + "> \"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
87   command2 += "\"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.in\"";
88
89   if (system (command.c_str()))
90     {
91       result = new std::string("An error occurred while running '" + command + "'.");
92       return false;
93     }
94
95   if (system (command1.c_str()))
96     {
97       result = new std::string("An error occurred while running '" + command1 + "'.");
98       return false;
99     }
100   if (system (command2.c_str()))
101     {
102       result = new std::string("An error occurred while running '" + command2 + "'.");
103       return false;
104     }
105
106   std::string nomDirectory = locationFixed + CDMUtilities::SLASH + name;
107   std::string nomPackageDirectory = nomDirectory + CDMUtilities::SLASH + "bbtk_" + name + "_PKG";
108   std::string bbCreatePackage("bbCreatePackage ");
109   bbCreatePackage += "\"" + nomDirectory + "\" \"" + name + "\" \"" + author + "\" \"" + description + "\"";
110   if (!system (bbCreatePackage.c_str()))
111     {
112       result = new std::string("An error occurred while running '" + bbCreatePackage + "'.");
113           return false;
114     }
115   std::string add;
116   add = "echo ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG) >> \"" + nomDirectory + CDMUtilities::SLASH + "CMakeLists.txt\"";
117   if (system (add.c_str()))
118     {
119       result = new std::string("An error occurred while running '" + add + "'.");
120           return false;
121     }
122
123   this->project = new modelCDMProject(NULL, nomDirectory, name);
124
125 #else
126   // ------ LINUX / MacOS
127   std::string command("creaNewProject.sh ");
128   command += "\"" + locationFixed + "\"" +" \"" + name + "\"";
129   //std::cout << "executing " << command << std::endl;
130   if (system ( command.c_str() ) )
131     {
132       result = new std::string("An error occured while running '" + command + "'.");
133       return false;
134     }
135
136   std::string nomDirectory = locationFixed + CDMUtilities::SLASH + name;
137   std::string nomPackageDirectory = nomDirectory + CDMUtilities::SLASH + "bbtk_" + name + "_PKG";
138
139   std::string bbCreatePackage("bbCreatePackage ");
140   bbCreatePackage += "\"" + nomDirectory + "\" \"" + name + "\" \"" + author + "\" \"" + description + "\"";
141   //std::cout << "executing " << bbCreatePackage << std::endl;
142   system (bbCreatePackage.c_str());
143
144   std::string add;
145   add = "echo 'ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG)' >> \"" + nomDirectory + CDMUtilities::SLASH + "CMakeLists.txt\"";
146
147   //std::cout << "executing " << add << std::endl;
148   system(add.c_str());
149
150   if(this->project != NULL)
151     {
152       if (!CloseProject(result))
153         return false;
154     }
155
156   this->project = new modelCDMProject(NULL, nomDirectory, name);
157
158 #endif
159
160   return true;
161 }
162
163 bool modelCDMMain::OpenProject(
164     const std::string& path,
165     std::string*& result
166 )
167 {
168   //std::cout << "Open selection path: "<< path << std::endl;
169   //get fixed path
170   std::string pathFixed = CDMUtilities::fixPath(path);
171   std::cout << "Opening path: "<< pathFixed << std::endl;
172
173   //check if its binaries' folder
174   bool isBinary = false;
175   std::string pathBuild = "";
176
177   //check if Makefile file exists
178   std::string pathMakefile = pathFixed + CDMUtilities::SLASH + "Makefile";
179   FILE* pFile = fopen(pathMakefile.c_str(), "r");
180
181   //is the binary folder
182   if (pFile != NULL)
183     {
184       fclose(pFile);
185       std::ifstream readFile;
186       readFile.open(pathMakefile.c_str());
187       std::string word;
188
189       while(!isBinary && readFile >> word)
190         {
191           if(word == "CMAKE_SOURCE_DIR")
192             {
193               readFile >> word;
194               readFile.ignore();
195               getline(readFile, word, '\n');
196               pathBuild = pathFixed;
197               pathFixed = CDMUtilities::fixPath(word);
198               isBinary = true;
199             }
200         }
201       readFile.close();
202     }
203
204   //check if its source's folder
205   bool isSource = false;
206   std::string pathSource = "";
207   //check if CMakeLists file exists
208   std::string pathCMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
209   pFile = fopen(pathCMakeLists.c_str(), "r");
210
211   //is the source folder
212   if (pFile != NULL)
213     {
214       fclose(pFile);
215       std::ifstream readFile;
216       readFile.open(pathCMakeLists.c_str());
217       std::string word;
218
219
220       while(!isSource && !readFile.eof())
221         {
222           std::getline(readFile,word,'(');
223           std::vector<std::string> wordBits;
224           CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
225
226           if(wordBits[wordBits.size()-1] == "PROJECT")
227             {
228               isSource = true;
229               pathSource = pathFixed;
230             }
231         }
232       readFile.close();
233     }
234
235   //if is source folder
236   if(isSource)
237     {
238       if(this->project != NULL)
239         {
240           if (!CloseProject(result))
241             return false;
242         }
243       std::vector<std::string> words;
244       CDMUtilities::splitter::split(words, pathSource, CDMUtilities::SLASH, CDMUtilities::splitter::no_empties);
245
246       std::cout << "Project sources at: " << pathSource << std::endl;
247       if(isBinary)
248         {
249           std::cout << ", and built in: " << pathBuild << std::endl;
250
251           this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1], pathBuild);
252         }
253       else
254         {
255           this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1]);
256         }
257     }
258   else
259     {
260       result = new std::string("No source folder found. Please make sure to select either the project's build or source folder.");
261       return false;
262     }
263
264   return true;
265 }
266
267 bool modelCDMMain::RefreshProject(
268     std::string*& result
269 )
270 {
271   //recreate the project using the project's path
272   if (this->project != NULL)
273     {
274       return this->project->Refresh(result);
275     }
276   else
277     {
278       result = new std::string("There is no open project.");
279       return false;
280     }
281 }
282
283 std::map<wxTreeItemId, modelCDMIProjectTreeNode*>& modelCDMMain::GetModelElements()
284 {
285   return this->modelElements;
286 }
287
288 bool modelCDMMain::CloseProject(
289     std::string*& result
290 )
291 {
292   //delete the project tree and leave it as NULL
293   if (this->project != NULL)
294     {
295       delete this->project;
296       this->project = NULL;
297       return true;
298     }
299   else
300     {
301       result = new std::string("There is no open project.");
302       return false;
303     }
304 }