]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMMain.cpp
Feature #1711
[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;
180   errno_t errorOpen = fopen_s(&pFile, pathMakefile.c_str(), "r");
181
182   //is the binary folder
183   if (!errorOpen && pFile)
184     {
185       fclose(pFile);
186       std::ifstream readFile;
187       readFile.open(pathMakefile.c_str());
188       std::string word;
189
190       while(!isBinary && readFile >> word)
191         {
192           if(word == "CMAKE_SOURCE_DIR")
193             {
194               readFile >> word;
195               readFile.ignore();
196               getline(readFile, word, '\n');
197               pathBuild = pathFixed;
198               pathFixed = CDMUtilities::fixPath(word);
199               isBinary = true;
200             }
201         }
202       readFile.close();
203     }
204
205   //check if its source's folder
206   bool isSource = false;
207   std::string pathSource = "";
208   //check if CMakeLists file exists
209   std::string pathCMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
210   errorOpen = fopen_s(&pFile, pathCMakeLists.c_str(), "r");
211
212   //is the source folder
213   if (!errorOpen && pFile)
214     {
215       fclose(pFile);
216       std::ifstream readFile;
217       readFile.open(pathCMakeLists.c_str());
218       std::string word;
219
220
221       while(!isSource && !readFile.eof())
222         {
223           std::getline(readFile,word,'(');
224           std::vector<std::string> wordBits;
225           CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
226
227           if(wordBits[wordBits.size()-1] == "PROJECT")
228             {
229               isSource = true;
230               pathSource = pathFixed;
231             }
232         }
233       readFile.close();
234     }
235
236   //if is source folder
237   if(isSource)
238     {
239       if(this->project != NULL)
240         {
241           if (!CloseProject(result))
242             return false;
243         }
244       std::vector<std::string> words;
245       CDMUtilities::splitter::split(words, pathSource, CDMUtilities::SLASH, CDMUtilities::splitter::no_empties);
246
247       std::cout << "Project sources at: " << pathSource << std::endl;
248       if(isBinary)
249         {
250           std::cout << ", and built in: " << pathBuild << std::endl;
251
252           this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1], pathBuild);
253         }
254       else
255         {
256           this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1]);
257         }
258     }
259   else
260     {
261       result = new std::string("No source folder found. Please make sure to select either the project's build or source folder.");
262       return false;
263     }
264
265   return true;
266 }
267
268 bool modelCDMMain::RefreshProject(
269     std::string*& result
270 )
271 {
272   //recreate the project using the project's path
273   if (this->project != NULL)
274     {
275       return this->project->Refresh(result);
276     }
277   else
278     {
279       result = new std::string("There is no open project.");
280       return false;
281     }
282 }
283
284 std::map<wxTreeItemId, modelCDMIProjectTreeNode*>& modelCDMMain::GetModelElements()
285 {
286   return this->modelElements;
287 }
288
289 bool modelCDMMain::CloseProject(
290     std::string*& result
291 )
292 {
293   //delete the project tree and leave it as NULL
294   if (this->project != NULL)
295     {
296       delete this->project;
297       this->project = NULL;
298       return true;
299     }
300   else
301     {
302       result = new std::string("There is no open project.");
303       return false;
304     }
305 }