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