]> 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 <vector>
42 #include <cstdio>
43 #include <fstream>
44
45 #include "CDMUtilities.h"
46 #include "modelCDMProject.h"
47
48 modelCDMMain::modelCDMMain()
49 {
50   this->project = NULL;
51 }
52
53 modelCDMMain::~modelCDMMain()
54 {
55   if(project != NULL)
56     {
57       delete this->project;
58       this->project = NULL;
59     }
60 }
61
62 modelCDMProject* modelCDMMain::GetProject() const
63 {
64   return this->project;
65 }
66
67 bool modelCDMMain::CreateProject(
68     const std::string& name,
69     const std::string& location,
70     std::string*& result,
71     const std::string& author,
72     const std::string& description)
73 {
74   std::cout << "Open selection path: "<< location << std::endl;
75   //get fixed location
76   std::string locationFixed = CDMUtilities::fixPath(location);
77   std::cout << "Opening path: "<< locationFixed << std::endl;
78
79   //creates project in disk
80 #if(_WIN32)
81
82   std::string command("creaNewProject.bat ");
83   std::string command1("creaSed.exe ");
84   std::string command2("del ");
85
86   command  += "\"" + locationFixed + "\" \"" + name + "\"";
87   command1 += "\"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.in\" " + "PROJECT_NAME " + name + "> \"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
88   command2 += "\"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.in\"";
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   if (system (command1.c_str()))
97     {
98       result = new std::string("An error occurred while running '" + command1 + "'.");
99       return false;
100     }
101   if (system (command2.c_str()))
102     {
103       result = new std::string("An error occurred while running '" + command2 + "'.");
104       return false;
105     }
106
107   std::string nomDirectory = locationFixed + CDMUtilities::SLASH + name;
108   std::string nomPackageDirectory = nomDirectory + CDMUtilities::SLASH + "bbtk_" + name + "_PKG";
109   std::string bbCreatePackage("bbCreatePackage ");
110   bbCreatePackage += "\"" + nomDirectory + "\" \"" + name + "\" \"" + author + "\" \"" + description + "\"";
111   if (!system (bbCreatePackage.c_str()))
112     {
113       result = new std::string("An error occurred while running '" + bbCreatePackage + "'.");
114           return false;
115     }
116   std::string add;
117   add = "echo ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG) >> \"" + nomDirectory + CDMUtilities::SLASH + "CMakeLists.txt\"";
118   if (system (add.c_str()))
119     {
120       result = new std::string("An error occurred while running '" + add + "'.");
121           return false;
122     }
123
124   this->project = new modelCDMProject(NULL, nomDirectory, name);
125
126 #else
127   // ------ LINUX / MacOS
128   std::string command("creaNewProject.sh ");
129   command += "\"" + locationFixed + "\"" +" \"" + name + "\"";
130   //std::cout << "executing " << command << std::endl;
131   if (system ( command.c_str() ) )
132     {
133       result = new std::string("An error occured while running '" + command + "'.");
134       return false;
135     }
136
137   std::string nomDirectory = locationFixed + CDMUtilities::SLASH + name;
138   std::string nomPackageDirectory = nomDirectory + CDMUtilities::SLASH + "bbtk_" + name + "_PKG";
139
140   std::string bbCreatePackage("bbCreatePackage ");
141   bbCreatePackage += "\"" + nomDirectory + "\" \"" + name + "\" \"" + author + "\" \"" + description + "\"";
142   //std::cout << "executing " << bbCreatePackage << std::endl;
143   system (bbCreatePackage.c_str());
144
145   std::string add;
146   add = "echo 'ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG)' >> \"" + nomDirectory + CDMUtilities::SLASH + "CMakeLists.txt\"";
147
148   //std::cout << "executing " << add << std::endl;
149   system(add.c_str());
150
151   if(this->project != NULL)
152     {
153       if (!CloseProject(result))
154         return false;
155     }
156
157   this->project = new modelCDMProject(NULL, nomDirectory, name);
158
159 #endif
160
161   return true;
162 }
163
164 bool modelCDMMain::OpenProject(
165     const std::string& path,
166     std::string*& result
167 )
168 {
169   //std::cout << "Open selection path: "<< path << std::endl;
170   //get fixed path
171   std::string pathFixed = CDMUtilities::fixPath(path);
172   std::cout << "Opening path: "<< pathFixed << std::endl;
173
174   //check if its binaries' folder
175   bool isBinary = false;
176   std::string pathBuild = "";
177
178   //check if Makefile file exists
179 #ifdef _WIN32
180   std::string pathMakefile = pathFixed + CDMUtilities::SLASH + "CMakeCache.txt";
181   FILE* pFile;
182   errno_t errorOpen = fopen_s(&pFile, pathMakefile.c_str(), "r");
183 #else
184   std::string pathMakefile = pathFixed + CDMUtilities::SLASH + "Makefile";
185   FILE* pFile;
186   pFile = fopen(pathMakefile.c_str(), "r");
187   bool errorOpen = (pFile == NULL);
188 #endif
189   //is the binary folder
190   if (!errorOpen && pFile)
191     {
192       fclose(pFile);
193       std::ifstream readFile;
194       readFile.open(pathMakefile.c_str());
195       std::string word;
196
197       
198 #ifdef _WIN32
199           while(!isBinary && !readFile.eof())
200         {
201             getline(readFile, word, '\n');
202                         std::vector<std::string> words;
203                         CDMUtilities::splitter::split(words,word,"=",CDMUtilities::splitter::no_empties);
204                         if(words.size() && words[0] == "CMAKE_HOME_DIRECTORY:INTERNAL")
205             {
206                           pathBuild = pathFixed;
207               pathFixed = CDMUtilities::fixPath(words[1]);
208                           for (int i = 0; i < (int)(pathFixed.size()); i++)
209                 {
210                   if (pathFixed[i]=='/')
211                                           pathFixed[i]='\\';
212                 }
213               isBinary = true;
214             }
215         }
216 #else
217       while(!isBinary && readFile >> word)
218         {
219             if(word == "CMAKE_SOURCE_DIR")
220             {
221               readFile >> word;
222               readFile.ignore();
223               getline(readFile, word, '\n');
224               pathBuild = pathFixed;
225               pathFixed = CDMUtilities::fixPath(word);
226               isBinary = true;
227             }
228             }
229 #endif
230       readFile.close();
231     }
232
233   //check if its source's folder
234   bool isSource = false;
235   std::string pathSource = "";
236   //check if CMakeLists file exists
237   std::string pathCMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
238 #ifdef _WIN32
239   errorOpen = fopen_s(&pFile, pathCMakeLists.c_str(), "r");
240 #else
241   pFile = fopen(pathCMakeLists.c_str(), "r");
242   errorOpen = (pFile == NULL);
243 #endif
244
245   //is the source folder
246   if (!errorOpen && pFile)
247     {
248       fclose(pFile);
249       std::ifstream readFile;
250       readFile.open(pathCMakeLists.c_str());
251       std::string word;
252
253
254       while(!isSource && !readFile.eof())
255         {
256           std::getline(readFile,word,'(');
257           std::vector<std::string> wordBits;
258           CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
259
260           if(wordBits[wordBits.size()-1] == "PROJECT")
261             {
262               isSource = true;
263               pathSource = pathFixed;
264             }
265         }
266       readFile.close();
267     }
268
269   //if is source folder
270   if(isSource)
271     {
272       if(this->project != NULL)
273         {
274           if (!CloseProject(result))
275             return false;
276         }
277       std::vector<std::string> words;
278       CDMUtilities::splitter::split(words, pathSource, CDMUtilities::SLASH, CDMUtilities::splitter::no_empties);
279
280       std::cout << "Project sources at: " << pathSource << std::endl;
281       if(isBinary)
282         {
283           std::cout << ", and built in: " << pathBuild << std::endl;
284
285           this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1], pathBuild);
286         }
287       else
288         {
289           this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1]);
290         }
291     }
292   else
293     {
294       result = new std::string("No source folder found. Please make sure to select either the project's build or source folder. " + pathBuild + pathFixed);
295       return false;
296     }
297
298   return true;
299 }
300
301 bool modelCDMMain::RefreshProject(
302     std::string*& result
303 )
304 {
305   //recreate the project using the project's path
306   if (this->project != NULL)
307     {
308       return this->project->Refresh(result);
309     }
310   else
311     {
312       result = new std::string("There is no open project.");
313       return false;
314     }
315 }
316
317 std::map<wxCDMTreeItemId, modelCDMIProjectTreeNode*>& modelCDMMain::GetModelElements()
318 {
319   return this->modelElements;
320 }
321
322 bool modelCDMMain::CloseProject(
323     std::string*& result
324 )
325 {
326   //delete the project tree and leave it as NULL
327   if (this->project != NULL)
328     {
329       delete this->project;
330       this->project = NULL;
331       return true;
332     }
333   else
334     {
335       result = new std::string("There is no open project.");
336       return false;
337     }
338 }