GroupRenamer.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "GroupRenamer.h"
25 
26 #include <filesystem>
27 #include <IceUtil/UUID.h>
28 #include <QDir>
29 #include <QDirIterator>
30 #include <QFileInfo>
32 #include <filesystem>
33 
34 using namespace armarx;
35 using namespace armarx::statechartmodel;
36 
37 GroupRenamer::GroupRenamer(const GroupClonerPtr& groupCloner) : groupCloner(groupCloner)
38 {
39 }
40 
41 std::optional<QString> GroupRenamer::renameGroup(const StatechartGroupPtr& group, const QString& newName, const QVector<StatechartGroupPtr>& affectedGroups)
42 {
43  if (!groupCloner->cloneGroupsTo({{group, newName}}, group->getPackagePath(), StatechartGroupMapping(), false))
44  {
45  ARMARX_ERROR_S << "Renaming group '" << group->getName() << "' to '" << newName << "' failed";
46  return std::nullopt;
47  }
48  GroupCloner::RemoveDir(group->getGroupPath());
49  const QString oldRSOName = group->getName() + "RemoteStateOfferer";
50  const QString newRSOName = newName + "RemoteStateOfferer";
51 
52  auto xmlProcessor = [&](const std::string&, const std::string & attribName, const std::string & attribValue) -> std::string
53  {
54  QString value = QString::fromUtf8(attribValue.c_str());
55 
56  if (attribName == "proxyName" && value == oldRSOName)
57  {
58  return newRSOName.toUtf8().data();
59  }
60 
61  return attribValue;
62  };
63 
64  for (const StatechartGroupPtr& g : affectedGroups)
65  {
66  QDirIterator it(g->getGroupPath(), QDir::Files, QDirIterator::Subdirectories);
67 
68  while (it.hasNext())
69  {
70  const auto f = QFileInfo(it.next());
71 
72  if (f.isDir())
73  {
74  continue;
75  }
76 
77  if (f.suffix() == "xml")
78  {
79  ARMARX_VERBOSE_S << "processing xml file: " << it.fileName();
80  auto readerPtr = RapidXmlReader::FromFile(it.filePath().toUtf8().data());
81  RapidXmlReaderNode rootNode = readerPtr->getRoot();
82  RapidXmlWriter writer;
83  auto writerNode = writer.createRootNode(rootNode.name());
84 
85  GroupCloner::ProcessXMLFile(rootNode, writerNode, xmlProcessor);
86 
87  writer.saveToFile(f.absoluteFilePath().toUtf8().data(), true);
88  }
89  }
90  }
91 
92  ARMARX_VERBOSE_S << "Adjusting statecharts/CMakeLists.txt";
93  QDir oldDir(group->getGroupPath());
94  oldDir.cdUp();
95  QString newGroupPath = oldDir.path() + QDir::separator() + newName;
96  QString cmakeListsPath = oldDir.path() + QDir::separator() + "CMakeLists.txt";
97 
98  QString cmakeLists = GuiStatechartGroupXmlReader::ReadFileContents(cmakeListsPath);
99  cmakeLists = cmakeLists.replace(QRegExp("add_subdirectory\\(" + group->getName() + "\\)\\n"), "");
100  GroupXmlWriter::WriteFileContents(cmakeListsPath, cmakeLists);
101 
102  if (auto mappingOpt = StatechartGroupMapping::ReadStatechartGroupMappingFile(group->getPackagePath()))
103  {
104  ARMARX_INFO_S << "Package '" << group->getPackagePath() << "' contains a mapping file; updating...";
105  StatechartGroupMapping mapping = *mappingOpt;
106  auto gmIt = std::find_if(mapping.groupMappings.begin(), mapping.groupMappings.end(), [&](const StatechartGroupMapping::GroupMapping & groupMapping)
107  {
108  return groupMapping.newGroupName == group->getName();
109  });
110 
111  if (gmIt != mapping.groupMappings.end())
112  {
113  StatechartGroupMapping::GroupMapping newGroupMapping {gmIt->groupName, newName, gmIt->groupPackage, gmIt->stateMappings};
114  mapping.groupMappings.erase(gmIt);
115  mapping.groupMappings.insert(newGroupMapping);
116  mapping.writeToFile(group->getPackagePath());
117  }
118  }
119 
120  return newGroupPath;
121 }
armarx::RapidXmlReader::FromFile
static RapidXmlReaderPtr FromFile(const std::string &path)
Definition: RapidXmlReader.h:497
armarx::RapidXmlWriter
Definition: RapidXmlWriter.h:99
XmlReader.h
armarx::GuiStatechartGroupXmlReader::ReadFileContents
static QString ReadFileContents(QString path)
Definition: GroupXmlReader.cpp:109
armarx::GroupCloner::ProcessXMLFile
static void ProcessXMLFile(RapidXmlReaderNode source, RapidXmlWriterNode target, const Function &processorFunction)
Definition: GroupCloner.h:64
armarx::GroupClonerPtr
std::shared_ptr< GroupCloner > GroupClonerPtr
Definition: GroupCloner.h:46
armarx::StatechartGroupMapping::writeToFile
void writeToFile(const QString &packageDir) const
Definition: StatechartGroupMapping.cpp:117
armarx::StatechartGroupMapping
Definition: StatechartGroupMapping.h:34
armarx::RapidXmlReaderNode::name
std::string name() const
Definition: RapidXmlReader.h:349
armarx::GroupRenamer::renameGroup
std::optional< QString > renameGroup(const StatechartGroupPtr &group, const QString &newName, const QVector< StatechartGroupPtr > &affectedGroups)
Definition: GroupRenamer.cpp:41
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::GroupXmlWriter::WriteFileContents
static void WriteFileContents(QString path, QString contents)
Definition: GroupXmlWriter.cpp:90
armarx::GroupCloner::RemoveDir
static bool RemoveDir(const QString &dirName)
Definition: GroupCloner.cpp:511
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
armarx::statechartmodel
Definition: XmlWriter.h:36
armarx::StatechartGroupMapping::GroupMapping
Definition: StatechartGroupMapping.h:48
armarx::StatechartGroupMapping::groupMappings
std::set< GroupMapping > groupMappings
Definition: StatechartGroupMapping.h:60
armarx::StatechartGroupMapping::ReadStatechartGroupMappingFile
static std::optional< StatechartGroupMapping > ReadStatechartGroupMappingFile(const QString &packageDir)
Definition: StatechartGroupMapping.cpp:38
armarx::RapidXmlReaderNode
Definition: RapidXmlReader.h:68
armarx::StatechartGroupPtr
std::shared_ptr< StatechartGroup > StatechartGroupPtr
Definition: StatechartGroupDefs.h:34
armarx::RapidXmlWriter::saveToFile
void saveToFile(const std::string &path, bool indent)
Definition: RapidXmlWriter.h:126
GroupRenamer.h
armarx::StatechartGroupMapping::GroupMapping::groupName
QString groupName
Definition: StatechartGroupMapping.h:50
armarx::RapidXmlWriter::createRootNode
RapidXmlWriterNode createRootNode(const std::string &name)
Definition: RapidXmlWriter.h:113
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:200
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::GroupRenamer::GroupRenamer
GroupRenamer(const GroupClonerPtr &groupCloner)
Definition: GroupRenamer.cpp:37