]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMMain.cpp
Bug #1912
[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
46 modelCDMMain::modelCDMMain()
47 {
48   //TODO: implement method
49 }
50
51 modelCDMMain::~modelCDMMain()
52 {
53   //TODO: implement method
54 }
55
56 const modelCDMProject* modelCDMMain::GetProject() const
57 {
58   //TODO: implement method
59   return this->project;
60 }
61
62 bool modelCDMMain::CreateProject(
63     const std::string& name,
64     const std::string& location,
65     std::string*& result,
66     const std::string& author,
67     const std::string& description)
68 {
69   std::cout << "Open selection path: "<< location << std::endl;
70   //get fixed location
71   std::string locationFixed = CDMUtilities::fixPath(location);
72   std::cout << "Opening path: "<< locationFixed << std::endl;
73
74   //TODO: create Project given the source folder
75
76 #if(_WIN32)
77
78   std::string command("creaNewProject.bat ");
79   std::string command1("creaSed.exe ");
80   std::string command2("del ");
81
82   command  += "\"" + locationFixed + "\" \"" + name + "\"";
83   command1 += "\"" + locationFixed +"\\"+name+"\\CMakeLists.txt.in\" " + "NameOfTheProject " + name + "> \"" + locationFixed + "\\" + name + "\\CMakeLists.txt\"";
84   command2 += "\"" + locationFixed +"\\"+name+"\\CMakeLists.txt.in\"";
85   if ( ! system ( command.c_str() ) )
86     {
87       system ( command1.c_str() );
88       system ( command2.c_str() );
89
90       // Create a Package at the same time.   JPR
91       char *author = author.c_str();
92       std::string nomDirectory = locationFixed + "\\" + name;
93       std::string nomPackageDirectory = nomDirectory + "\\" + "bbtk_" + name + "_PKG";
94       std::string bbCreatePackage("bbCreatePackage ");
95       bbCreatePackage += nomDirectory + " " + name + " " + author + " " + description;
96       system (bbCreatePackage.c_str());
97       std::string add;
98       add = "echo ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG) >> " + nomDirectory + "/CMakeLists.txt";
99       system(add.c_str());
100     }
101   else
102     {
103       result = new std::string("An error occured while running '" + command + "'.");
104       return false;
105     }
106
107 #else
108   // ------ LINUX / MacOS
109   std::string command("creaNewProject.sh ");
110   command += "\"" + locationFixed + "\"" +" " + name;
111   std::cout << "executing " << command << std::endl;
112   if ( ! system ( command.c_str() ) )
113     {
114       std::string nomDirectory = locationFixed + "/" + name;
115       std::string nomPackageDirectory = nomDirectory + "/" + "bbtk_" + name + "_PKG";
116
117       std::string bbCreatePackage("bbCreatePackage ");
118       bbCreatePackage += nomDirectory + " " + name + " " + author + " " + description;
119       std::cout << "executing " << bbCreatePackage << std::endl;
120       system (bbCreatePackage.c_str());
121
122       std::string add;
123       add = "echo 'ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG)' >> " + nomDirectory + "/CMakeLists.txt";
124       //std::cout << add << std::endl;
125       std::cout << "executing " << add << std::endl;
126       system(add.c_str());
127     }
128   else
129     {
130       result = new std::string("An error occured while running '" + command + "'.");
131       return false;
132     }
133
134 #endif
135
136
137
138   return true;
139 }
140
141 bool modelCDMMain::OpenProject(
142     const std::string& path,
143     std::string*& result
144 )
145 {
146   std::cout << "Open selection path: "<< path << std::endl;
147   //get fixed path
148   std::string pathFixed = CDMUtilities::fixPath(path);
149   std::cout << "Opening path: "<< pathFixed << std::endl;
150
151   //check if its binaries' folder
152   bool isBinary = false;
153   std::string pathBuild = "";
154
155   //check if Makefile file exists
156   std::string pathMakefile = pathFixed + "/Makefile";
157   FILE* pFile = fopen(pathMakefile.c_str(), "r");
158
159   //is the binary folder
160   if (pFile != NULL)
161     {
162       fclose(pFile);
163       std::ifstream readFile;
164       readFile.open(pathMakefile.c_str());
165       std::string word;
166
167       while(!isBinary && readFile >> word)
168         {
169           if(word == "CMAKE_SOURCE_DIR")
170             {
171               readFile >> word;
172               readFile.ignore();
173               getline(readFile, word, '\n');
174               pathBuild = pathFixed;
175               pathFixed = word;
176               isBinary = true;
177             }
178         }
179       readFile.close();
180     }
181
182   //check if its source's folder
183   bool isSource = false;
184   std::string pathSource = "";
185   //check if CMakeLists file exists
186   std::string pathCMakeLists = pathFixed + "/CMakeLists.txt";
187   pFile = fopen(pathCMakeLists.c_str(), "r");
188
189   //is the source folder
190   if (pFile != NULL)
191     {
192       fclose(pFile);
193       std::ifstream readFile;
194       readFile.open(pathCMakeLists.c_str());
195       std::string word;
196
197
198       while(!isSource && !readFile.eof())
199         {
200           std::getline(readFile,word,'\n');
201           int pos = word.find("PROJECT");
202           if(pos != std::string::npos)
203             {
204               pathSource = pathFixed;
205               isSource = true;
206             }
207         }
208       readFile.close();
209     }
210
211   //if is source folder
212   if(isSource)
213     {
214       std::cout << "Project sources at: " << pathSource;
215       if(isBinary)
216         {
217           std::cout << ", and built in: " << pathBuild;
218         }
219       std::cout << std::endl;
220     }
221   else
222     {
223       result = new std::string("No source folder found. Please make sure to select either the project's build or source folder.");
224       return false;
225     }
226
227   //TODO: create Project given the source folder
228   return true;
229 }
230
231 bool modelCDMMain::RefreshProject(
232     std::string*& result
233 )
234 {
235   //TODO: recreate the project using the project's path
236   return true;
237 }
238
239 bool modelCDMMain::CloseProject(
240     std::string*& result
241 )
242 {
243   //TODO: delete the project tree and leave it as NULL
244   return true;
245 }