]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMMain.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMMain.cpp
index bb37b3d93acb0a7b94a02c0a6b84779f09d5ac43..8d69741da2b3d46229e93248e4ae5943355ad256 100644 (file)
@@ -38,6 +38,7 @@
 #include <cstdlib>
 #include <iostream>
 #include <string>
+#include <vector>
 #include <cstdio>
 #include <fstream>
 
@@ -83,9 +84,8 @@ bool modelCDMMain::CreateProject(
   std::string command2("del ");
 
   command  += "\"" + locationFixed + "\" \"" + name + "\"";
-  command1 += "\"" + locationFixed +"\\"+name+"\\CMakeLists.txt.in\" " + "NameOfTheProject " + name + "> \"" + locationFixed + "\\" + name + "\\CMakeLists.txt\"";
-  command2 += "\"" + locationFixed +"\\"+name+"\\CMakeLists.txt.in\"";
-
+  command1 += "\"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.in\" " + "PROJECT_NAME " + name + "> \"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
+  command2 += "\"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.in\"";
 
   if (system (command.c_str()))
     {
@@ -93,25 +93,40 @@ bool modelCDMMain::CreateProject(
       return false;
     }
 
-  system ( command1.c_str() );
-  system ( command2.c_str() );
+  if (system (command1.c_str()))
+    {
+      result = new std::string("An error occurred while running '" + command1 + "'.");
+      return false;
+    }
+  if (system (command2.c_str()))
+    {
+      result = new std::string("An error occurred while running '" + command2 + "'.");
+      return false;
+    }
 
-  char *author = author.c_str();
-  std::string nomDirectory = locationFixed + "\\" + name;
-  std::string nomPackageDirectory = nomDirectory + "\\" + "bbtk_" + name + "_PKG";
+  std::string nomDirectory = locationFixed + CDMUtilities::SLASH + name;
+  std::string nomPackageDirectory = nomDirectory + CDMUtilities::SLASH + "bbtk_" + name + "_PKG";
   std::string bbCreatePackage("bbCreatePackage ");
-  bbCreatePackage += nomDirectory + " " + name + " " + author + " " + description;
-  system (bbCreatePackage.c_str());
+  bbCreatePackage += "\"" + nomDirectory + "\" \"" + name + "\" \"" + author + "\" \"" + description + "\"";
+  if (!system (bbCreatePackage.c_str()))
+    {
+      result = new std::string("An error occurred while running '" + bbCreatePackage + "'.");
+         return false;
+    }
   std::string add;
-  add = "echo ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG) >> " + nomDirectory + "/CMakeLists.txt";
-  system(add.c_str());
+  add = "echo ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG) >> \"" + nomDirectory + CDMUtilities::SLASH + "CMakeLists.txt\"";
+  if (system (add.c_str()))
+    {
+      result = new std::string("An error occurred while running '" + add + "'.");
+         return false;
+    }
 
-  this->project = new modelCDMProject(nomDirectory);
+  this->project = new modelCDMProject(NULL, nomDirectory, name);
 
 #else
   // ------ LINUX / MacOS
   std::string command("creaNewProject.sh ");
-  command += "\"" + locationFixed + "\"" +" " + name;
+  command += "\"" + locationFixed + "\"" +" \"" + name + "\"";
   //std::cout << "executing " << command << std::endl;
   if (system ( command.c_str() ) )
     {
@@ -119,16 +134,16 @@ bool modelCDMMain::CreateProject(
       return false;
     }
 
-  std::string nomDirectory = locationFixed + "/" + name;
-  std::string nomPackageDirectory = nomDirectory + "/" + "bbtk_" + name + "_PKG";
+  std::string nomDirectory = locationFixed + CDMUtilities::SLASH + name;
+  std::string nomPackageDirectory = nomDirectory + CDMUtilities::SLASH + "bbtk_" + name + "_PKG";
 
   std::string bbCreatePackage("bbCreatePackage ");
-  bbCreatePackage += nomDirectory + " " + name + " " + author + " " + description;
+  bbCreatePackage += "\"" + nomDirectory + "\" \"" + name + "\" \"" + author + "\" \"" + description + "\"";
   //std::cout << "executing " << bbCreatePackage << std::endl;
   system (bbCreatePackage.c_str());
 
   std::string add;
-  add = "echo 'ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG)' >> " + nomDirectory + "/CMakeLists.txt";
+  add = "echo 'ADD_SUBDIRECTORY(bbtk_" + name  + "_PKG)' >> \"" + nomDirectory + CDMUtilities::SLASH + "CMakeLists.txt\"";
 
   //std::cout << "executing " << add << std::endl;
   system(add.c_str());
@@ -139,7 +154,7 @@ bool modelCDMMain::CreateProject(
         return false;
     }
 
-  this->project = new modelCDMProject(nomDirectory);
+  this->project = new modelCDMProject(NULL, nomDirectory, name);
 
 #endif
 
@@ -161,20 +176,47 @@ bool modelCDMMain::OpenProject(
   std::string pathBuild = "";
 
   //check if Makefile file exists
-  std::string pathMakefile = pathFixed + "/Makefile";
-  FILE* pFile = fopen(pathMakefile.c_str(), "r");
-
+#ifdef _WIN32
+  std::string pathMakefile = pathFixed + CDMUtilities::SLASH + "CMakeCache.txt";
+  FILE* pFile;
+  errno_t errorOpen = fopen_s(&pFile, pathMakefile.c_str(), "r");
+#else
+  std::string pathMakefile = pathFixed + CDMUtilities::SLASH + "Makefile";
+  FILE* pFile;
+  pFile = fopen(pathMakefile.c_str(), "r");
+  bool errorOpen = (pFile == NULL);
+#endif
   //is the binary folder
-  if (pFile != NULL)
+  if (!errorOpen && pFile)
     {
       fclose(pFile);
       std::ifstream readFile;
       readFile.open(pathMakefile.c_str());
       std::string word;
 
+      
+#ifdef _WIN32
+         while(!isBinary && !readFile.eof())
+        {
+            getline(readFile, word, '\n');
+                       std::vector<std::string> words;
+                       CDMUtilities::splitter::split(words,word,"=",CDMUtilities::splitter::no_empties);
+                       if(words.size() && words[0] == "CMAKE_HOME_DIRECTORY:INTERNAL")
+            {
+                         pathBuild = pathFixed;
+              pathFixed = CDMUtilities::fixPath(words[1]);
+                         for (int i = 0; i < (int)(pathFixed.size()); i++)
+                {
+                  if (pathFixed[i]=='/')
+                                         pathFixed[i]='\\';
+                }
+              isBinary = true;
+            }
+        }
+#else
       while(!isBinary && readFile >> word)
         {
-          if(word == "CMAKE_SOURCE_DIR")
+            if(word == "CMAKE_SOURCE_DIR")
             {
               readFile >> word;
               readFile.ignore();
@@ -183,7 +225,8 @@ bool modelCDMMain::OpenProject(
               pathFixed = CDMUtilities::fixPath(word);
               isBinary = true;
             }
-        }
+           }
+#endif
       readFile.close();
     }
 
@@ -191,11 +234,16 @@ bool modelCDMMain::OpenProject(
   bool isSource = false;
   std::string pathSource = "";
   //check if CMakeLists file exists
-  std::string pathCMakeLists = pathFixed + "/CMakeLists.txt";
+  std::string pathCMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
+#ifdef _WIN32
+  errorOpen = fopen_s(&pFile, pathCMakeLists.c_str(), "r");
+#else
   pFile = fopen(pathCMakeLists.c_str(), "r");
+  errorOpen = (pFile == NULL);
+#endif
 
   //is the source folder
-  if (pFile != NULL)
+  if (!errorOpen && pFile)
     {
       fclose(pFile);
       std::ifstream readFile;
@@ -226,21 +274,24 @@ bool modelCDMMain::OpenProject(
           if (!CloseProject(result))
             return false;
         }
+      std::vector<std::string> words;
+      CDMUtilities::splitter::split(words, pathSource, CDMUtilities::SLASH, CDMUtilities::splitter::no_empties);
 
       std::cout << "Project sources at: " << pathSource << std::endl;
       if(isBinary)
         {
           std::cout << ", and built in: " << pathBuild << std::endl;
-          this->project = new modelCDMProject(pathSource, pathBuild);
+
+          this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1], pathBuild);
         }
       else
         {
-          this->project = new modelCDMProject(pathSource);
+          this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1]);
         }
     }
   else
     {
-      result = new std::string("No source folder found. Please make sure to select either the project's build or source folder.");
+      result = new std::string("No source folder found. Please make sure to select either the project's build or source folder. " + pathBuild + pathFixed);
       return false;
     }
 
@@ -263,7 +314,7 @@ bool modelCDMMain::RefreshProject(
     }
 }
 
-std::map<wxTreeItemId, modelCDMIProjectTreeNode*>& modelCDMMain::GetModelElements()
+std::map<wxCDMTreeItemId, modelCDMIProjectTreeNode*>& modelCDMMain::GetModelElements()
 {
   return this->modelElements;
 }