]> 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 +"\\"+name+"\\CMakeLists.txt.in\" " + "NameOfTheProject " + name + "> \"" + locationFixed + "\\" + name + "\\CMakeLists.txt\"";
87   command2 += "\"" + locationFixed +"\\"+name+"\\CMakeLists.txt.in\"";
88
89
90   if (system (command.c_str()))
91     {
92       result = new std::string("An error occured 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 + "\\" + name;
101   std::string nomPackageDirectory = nomDirectory + "\\" + "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 + "/CMakeLists.txt";
107   system(add.c_str());
108
109   this->project = new modelCDMProject(nomDirectory);
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 + "/" + name;
123   std::string nomPackageDirectory = nomDirectory + "/" + "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 + "/CMakeLists.txt";
132
133   //std::cout << "executing " << add << std::endl;
134   system(add.c_str());
135
136
137   this->project = new modelCDMProject(nomDirectory);
138
139 #endif
140
141   return true;
142 }
143
144 bool modelCDMMain::OpenProject(
145     const std::string& path,
146     std::string*& result
147 )
148 {
149   //std::cout << "Open selection path: "<< path << std::endl;
150   //get fixed path
151   std::string pathFixed = CDMUtilities::fixPath(path);
152   std::cout << "Opening path: "<< pathFixed << std::endl;
153
154   //check if its binaries' folder
155   bool isBinary = false;
156   std::string pathBuild = "";
157
158   //check if Makefile file exists
159   std::string pathMakefile = pathFixed + "/Makefile";
160   FILE* pFile = fopen(pathMakefile.c_str(), "r");
161
162   //is the binary folder
163   if (pFile != NULL)
164     {
165       fclose(pFile);
166       std::ifstream readFile;
167       readFile.open(pathMakefile.c_str());
168       std::string word;
169
170       while(!isBinary && readFile >> word)
171         {
172           if(word == "CMAKE_SOURCE_DIR")
173             {
174               readFile >> word;
175               readFile.ignore();
176               getline(readFile, word, '\n');
177               pathBuild = pathFixed;
178               pathFixed = CDMUtilities::fixPath(word);
179               isBinary = true;
180             }
181         }
182       readFile.close();
183     }
184
185   //check if its source's folder
186   bool isSource = false;
187   std::string pathSource = "";
188   //check if CMakeLists file exists
189   std::string pathCMakeLists = pathFixed + "/CMakeLists.txt";
190   pFile = fopen(pathCMakeLists.c_str(), "r");
191
192   //is the source folder
193   if (pFile != NULL)
194     {
195       fclose(pFile);
196       std::ifstream readFile;
197       readFile.open(pathCMakeLists.c_str());
198       std::string word;
199
200
201       while(!isSource && !readFile.eof())
202         {
203           std::getline(readFile,word,'(');
204           std::vector<std::string> wordBits;
205           CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
206
207           if(wordBits[wordBits.size()-1] == "PROJECT")
208             {
209               isSource = true;
210               pathSource = pathFixed;
211             }
212         }
213       readFile.close();
214     }
215
216   //if is source folder
217   if(isSource)
218     {
219       std::cout << "Project sources at: " << pathSource << std::endl;
220       if(isBinary)
221         {
222           std::cout << ", and built in: " << pathBuild << std::endl;
223           this->project = new modelCDMProject(pathSource, pathBuild);
224         }
225       else
226         {
227           this->project = new modelCDMProject(pathSource);
228         }
229     }
230   else
231     {
232       result = new std::string("No source folder found. Please make sure to select either the project's build or source folder.");
233       return false;
234     }
235
236   return true;
237 }
238
239 bool modelCDMMain::RefreshProject(
240     std::string*& result
241 )
242 {
243   //TODO: recreate the project using the project's path
244   return true;
245 }
246
247 bool modelCDMMain::CloseProject(
248     std::string*& result
249 )
250 {
251   //TODO: delete the project tree and leave it as NULL
252   return true;
253 }