]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMProject.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / modelCDMProject.cpp
index 8b93e189a2f3decab2cad2de0d9906fe3332d5bb..7edde9ad3738891b6aba77479c0084c5c7068be6 100644 (file)
@@ -40,6 +40,7 @@
 #include <algorithm>
 #include <fstream>
 #include <ctime>
+#include <boost/regex.hpp>
 
 #include "CDMUtilities.h"
 #include "creaWx.h"
@@ -345,7 +346,7 @@ modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
   //fixing input parameters
   std::vector<std::string> words;
 
-  CDMUtilities::splitter::split(words,name," ",CDMUtilities::splitter::no_empties);
+  CDMUtilities::splitter::split(words,name," '/\"\\,.",CDMUtilities::splitter::no_empties);
   std::string nameFixed = "";
   for (int i = 0; i < (int)(words.size()); i++)
     {
@@ -353,7 +354,7 @@ modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
     }
 
   words.clear();
-  CDMUtilities::splitter::split(words,authors," ",CDMUtilities::splitter::no_empties);
+  CDMUtilities::splitter::split(words,authors," '/\"\\,.",CDMUtilities::splitter::no_empties);
   std::string authorFixed;
   for (int i = 0; i < (int)(words.size()); i++)
     {
@@ -362,13 +363,13 @@ modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
 
   words.clear();
   std::string descriptionFixed;
-  CDMUtilities::splitter::split(words,authorsEmail," ",CDMUtilities::splitter::no_empties);
+  CDMUtilities::splitter::split(words,authorsEmail," '/\"\\,",CDMUtilities::splitter::no_empties);
   for (int i = 0; i < (int)(words.size()); i++)
     {
-      descriptionFixed += words[i];
+      descriptionFixed += words[i] + "/";
     }
   words.clear();
-  CDMUtilities::splitter::split(words,description," ",CDMUtilities::splitter::no_empties);
+  CDMUtilities::splitter::split(words,description," '\"",CDMUtilities::splitter::no_empties);
   for (int i = 0; i < (int)(words.size()); i++)
     {
       descriptionFixed += "_" + words[i];
@@ -387,6 +388,14 @@ modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
       return NULL;
     }
 
+  //add library to project CMakeLists
+  std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
+  if (out1.is_open())
+    {
+      out1 << "ADD_SUBDIRECTORY(bbtk_" << nameFixed << "_PKG)" << std::endl;
+      out1.close();
+    }
+
   //add library to model
   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
   this->packages.push_back(package);
@@ -416,13 +425,14 @@ modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
 
 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
     const std::string& name,
+    const int& type,
     std::string*& result,
     const std::string& path
 )
 {
   if(this->appli != NULL)
     {
-      return this->appli->CreateApplication(name, result);
+      return this->appli->CreateApplication(name, type, result);
     }
   result = new std::string("there is no appli folder in this project.");
   return NULL;
@@ -738,15 +748,13 @@ bool modelCDMProject::Build(std::string*& result, const std::string& line)
   //TODO: adjust for windows and mac
 #ifdef _WIN32
   // ------ Windows
-       //\\..\\IDE\\VCExpress.exe \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\"
-//     std::string command = "\"" + std::string(getenv("VS90COMNTOOLS")) + "..\\IDE\\VCExpress.exe\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\" &";
-       std::string command = "\"\"%VS90COMNTOOLS%..\\IDE\\VCExpress.exe\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\" &\"";
-       command = "start cmd.exe /k " + command + " &";
-  if(0 == system(command.c_str()))
+       std::string command = "start \"\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\"";
+  //wxMessageBox(crea::std2wx(command), wxT("Project Compilation - Check!"));
+       if(0 == system(command.c_str()))
     return true;
   else
     {
-      result = new std::string("An error has happened running: \"" + command + "\". Please make sure to have visual c++ express installed and to have the VS90COMNTOOLS environment variable set.");
+      result = new std::string("An error has happened running: \"" + command + "\". Please make sure to have visual c++ installed.");
          return false;
     }
 #elif __APPLE__
@@ -832,13 +840,34 @@ bool modelCDMProject::Connect(std::string*& result, const std::string& folder)
       return false;
     }
   //create plug command
-  std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\" > \"" + this->buildPath + CDMUtilities::SLASH + "plugging.log\" 2>&1";
-  std::cout << "executing '" << plugComm << "'" << std::endl;
+  std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\"";
+
+  std::string Comm = "gnome-terminal -e \"bash -c \\\"";
+    for (int i = 0; i < plugComm.size(); i++)
+      {
+        if(plugComm[i] == '"')
+          {
+            Comm+="\\\\\\\"";
+          }
+        else if(plugComm[i] == '\\')
+          {
+            Comm+="\\\\\\\\";
+          }
+        else
+          {
+            Comm.push_back(plugComm[i]);
+          }
+      }
+    Comm += "; echo -e '\\a'; bash";
+    Comm += "\\\"\"";
+
+
+  std::cout << "executing '" << Comm << "'" << std::endl;
   //execute plug command
-  if(system(plugComm.c_str()))
+  if(system(Comm.c_str()))
     {
       //if there was an error then report it
-      result = new std::string("There was an error plugging the packages of the project, please check the plugging.log in the build folder file to read more about the problem.");
+      result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
       return false;
     }
 #endif
@@ -979,3 +1008,299 @@ void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
       this->packages[i]->CheckStructure(properties);
     }
 }
+
+bool modelCDMProject::IsPackageIncluded(const std::string& package_name)
+{
+  if (this->HasCMakeLists())
+    {
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+      for (int i = 0; i < cmlFile.size(); ++i)
+        {
+          if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
+            {
+              int pos = 1;
+              while (pos < cmlFile[i].second.size())
+                {
+                  if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
+                    {
+                      if (package_name == cmlFile[i].second[pos])
+                        return true;
+                      break;
+                    }
+                  pos++;
+                }
+            }
+        }
+    }
+  return false;
+
+}
+
+bool modelCDMProject::SetPackageInclude(const std::string& package_name, const bool& toInclude)
+{
+  if (this->HasCMakeLists())
+    {
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+
+      bool found = false;
+
+      for (int i = 0; i < cmlFile.size(); ++i)
+        {
+          if(toInclude && cmlFile[i].first == "comment")
+            {
+              std::vector<std::string> segments;
+              std::string line = cmlFile[i].second[0];
+              while(line[0] == '#')
+                line.erase(0,1);
+
+              CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
+              if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == package_name)
+                {
+                  found = true;
+                  while(cmlFile[i].second[0][0] == '#')
+                    cmlFile[i].second[0].erase(0,1);
+                }
+            }
+          else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
+            {
+              int pos = 1;
+              while (pos < cmlFile[i].second.size())
+                {
+                  if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
+                    {
+                      if (package_name == cmlFile[i].second[pos])
+                        {
+                          found = true;
+                          if (!toInclude)
+                            {
+                              cmlFile[i].first = "comment";
+                              cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
+                              while (cmlFile[i].second.size() > 1)
+                                {
+                                  cmlFile[i].second[0] += cmlFile[i].second[1];
+                                  cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
+                                }
+
+                            }
+                        }
+                      break;
+                    }
+                  pos++;
+                }
+            }
+        }
+      if (!found && toInclude)
+        {
+          CDMUtilities::syntaxElement element;
+          element.first = "command";
+          element.second.push_back("ADD_SUBDIRECTORY(" + package_name + ")");
+          cmlFile.push_back(element);
+        }
+
+      return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);
+    }
+  return false;
+}
+
+std::map<std::string, bool> modelCDMProject::Get3rdPartyLibraries()
+{
+  std::map<std::string, std::string> correspondence;
+  correspondence["USE_CREA"] = "Crea";
+  correspondence["USE_GDCM"] = "GDCM";
+  correspondence["USE_GDCM_VTK"] = "GDCM_VTK";
+  correspondence["USE_GDCM2"] = "GDCM2";
+  correspondence["USE_WXWIDGETS"] = "WxWidgets";
+  correspondence["USE_KWWIDGETS"] = "KWWidgets";
+  correspondence["USE_VTK"] = "VTK";
+  correspondence["USE_ITK"] = "ITK";
+  correspondence["USE_BOOST"] = "Boost";
+
+  std::map<std::string, bool> res;
+  res["Crea"] = false;
+  res["GDCM"] = false;
+  res["GDCM_VTK"] = false;
+  res["GDCM2"] = false;
+  res["WxWidgets"] = false;
+  res["KWWidgets"] = false;
+  res["VTK"] = false;
+  res["ITK"] = false;
+  res["Boost"] = false;
+
+  if (this->HasCMakeLists())
+    {
+      std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
+
+      boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*USE_\\w+\\s+ON");
+      std::string::const_iterator start, end;
+      start = CMfile.begin();
+      end = CMfile.end();
+      boost::match_results<std::string::const_iterator> what;
+      boost::match_flag_type flags = boost::match_default;
+      while(boost::regex_search(start, end, what, expression, flags))
+        {
+          //std::cout << what[0].str() << std::endl;
+          boost::regex expression1 = boost::regex("USE_\\w+");
+          std::string::const_iterator start1, end1;
+          start1 = what[0].first;
+          end1 = what[0].second;
+          boost::match_results<std::string::const_iterator> what1;
+          if(boost::regex_search(start1, end1, what1, expression1, flags))
+            {
+              std::string dete = what1.str();
+              CDMUtilities::normalizeStr(dete);
+              //std::cout << dete << std::endl;
+              if(correspondence.find(dete) != correspondence.end())
+                res[correspondence[dete]] = true;
+            }
+          start = what[0].second;
+        }
+    }
+
+  return res;
+}
+
+bool modelCDMProject::Set3rdPartyLibrary(const std::string& library_name,
+    const bool& toInclude)
+{
+  std::map<std::string, std::string> correspondence;
+  correspondence["Crea"] = "USE_CREA";
+  correspondence["GDCM"] = "USE_GDCM";
+  correspondence["GDCM_VTK"] = "USE_GDCM_VTK";
+  correspondence["GDCM2"] = "USE_GDCM2";
+  correspondence["WxWidgets"] = "USE_WXWIDGETS";
+  correspondence["KWWidgets"] = "USE_KWWIDGETS";
+  correspondence["VTK"] = "USE_VTK";
+  correspondence["ITK"] = "USE_ITK";
+  correspondence["Boost"] = "USE_BOOST";
+
+  if (correspondence.find(library_name) != correspondence.end())
+    {
+      std::string library_command = correspondence[library_name];
+
+      if (this->HasCMakeLists())
+        {
+          std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
+          std::string resCMfile = "";
+
+          std::string::const_iterator start, end;
+          boost::match_results<std::string::const_iterator> what, tmp;
+          boost::match_flag_type flags = boost::match_default;
+          bool found = false;
+
+          start = CMfile.begin();
+          end = CMfile.end();
+
+          //search for existing inclusions
+          boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
+          while(boost::regex_search(start, end, what, expression, flags))
+            {
+              found = true;
+              resCMfile += what.prefix().str();
+              if(!toInclude)
+                {
+                  std::string dete = what.str();
+                  int pos = dete.find("ON",0);
+                  dete.replace(pos, 2, "OFF");
+                  resCMfile += dete;
+                }
+              else
+                resCMfile += what.str();
+              tmp = what;
+              start = what[0].second;
+            }
+
+          if (found)
+            resCMfile += tmp.suffix().str();
+          else
+            {
+              start = CMfile.begin();
+              end = CMfile.end();
+
+              //search for existing exclusions
+              boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+OFF\\s*\\)");
+              while(boost::regex_search(start, end, what, expression, flags))
+                {
+                  found = true;
+                  resCMfile += what.prefix().str();
+                  if(toInclude)
+                    {
+                      std::string dete = what.str();
+                      int pos = dete.find("OFF",0);
+                      dete.replace(pos, 3, "ON");
+                      resCMfile += dete;
+                    }
+                  else
+                    resCMfile += what.str();
+                  tmp = what;
+                  start = what[0].second;
+                }
+
+              if (found)
+                resCMfile += tmp.suffix().str();
+              else
+                {
+                  start = CMfile.begin();
+                  end = CMfile.end();
+
+                  //search for existing commented inclusions
+                  expression = boost::regex("^\\h*#+\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
+                  while(boost::regex_search(start, end, what, expression, flags))
+                    {
+                      found = true;
+                      resCMfile += what.prefix().str();
+                      if(toInclude)
+                        {
+                          std::string dete = what.str();
+                          for (int i = 0; i < dete.size(); ++i) {
+                            if(dete[i] == '#')
+                              {
+                                dete.erase(i,1);
+                                i--;
+                              }
+                          }
+                          resCMfile += dete;
+                        }
+                      else
+                        resCMfile += what.str();
+
+                      tmp = what;
+                      start = what[0].second;
+                    }
+
+                  if (found)
+                    resCMfile += tmp.suffix().str();
+                  else
+                    {
+                      if(toInclude)
+                        {
+                          start = CMfile.begin();
+                          end = CMfile.end();
+
+                          //search for position to insert
+                          expression = boost::regex("^\\h*#\\h*Libraries\\/tools used\\h*$");
+                          if(boost::regex_search(start, end, what, expression, flags))
+                            {
+                              found = true;
+                              resCMfile += what.prefix().str();
+                              resCMfile += what.str();
+                              resCMfile += "\nSET(" + library_command + " ON)";
+                              resCMfile += what.suffix().str();
+                            }
+                        }
+                      else
+                        {
+                          found = true;
+                        }
+                    }
+                }
+            }
+          if (!found) {
+            return false;
+          }
+          else
+            return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
+        }
+    }
+
+  return false;
+}