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
28#include <QDir>
29#include <QDirIterator>
30#include <QFileInfo>
31
32#include <IceUtil/UUID.h>
33
35
36using namespace armarx;
37using namespace armarx::statechartmodel;
38
39GroupRenamer::GroupRenamer(const GroupClonerPtr& groupCloner) : groupCloner(groupCloner)
40{
41}
42
43std::optional<QString>
45 const QString& newName,
46 const QVector<StatechartGroupPtr>& affectedGroups)
47{
48 if (!groupCloner->cloneGroupsTo(
49 {{group, newName}}, group->getPackagePath(), StatechartGroupMapping(), false))
50 {
51 ARMARX_ERROR_S << "Renaming group '" << group->getName() << "' to '" << newName
52 << "' failed";
53 return std::nullopt;
54 }
55 GroupCloner::RemoveDir(group->getGroupPath());
56 const QString oldRSOName = group->getName() + "RemoteStateOfferer";
57 const QString newRSOName = newName + "RemoteStateOfferer";
58
59 auto xmlProcessor = [&](const std::string&,
60 const std::string& attribName,
61 const std::string& attribValue) -> std::string
62 {
63 QString value = QString::fromUtf8(attribValue.c_str());
64
65 if (attribName == "proxyName" && value == oldRSOName)
66 {
67 return newRSOName.toUtf8().data();
68 }
69
70 return attribValue;
71 };
72
73 for (const StatechartGroupPtr& g : affectedGroups)
74 {
75 QDirIterator it(g->getGroupPath(), QDir::Files, QDirIterator::Subdirectories);
76
77 while (it.hasNext())
78 {
79 const auto f = QFileInfo(it.next());
80
81 if (f.isDir())
82 {
83 continue;
84 }
85
86 if (f.suffix() == "xml")
87 {
88 ARMARX_VERBOSE_S << "processing xml file: " << it.fileName();
89 auto readerPtr = RapidXmlReader::FromFile(it.filePath().toUtf8().data());
90 RapidXmlReaderNode rootNode = readerPtr->getRoot();
91 RapidXmlWriter writer;
92 auto writerNode = writer.createRootNode(rootNode.name());
93
94 GroupCloner::ProcessXMLFile(rootNode, writerNode, xmlProcessor);
95
96 writer.saveToFile(f.absoluteFilePath().toUtf8().data(), true);
97 }
98 }
99 }
100
101 ARMARX_VERBOSE_S << "Adjusting statecharts/CMakeLists.txt";
102 QDir oldDir(group->getGroupPath());
103 oldDir.cdUp();
104 QString newGroupPath = oldDir.path() + QDir::separator() + newName;
105 QString cmakeListsPath = oldDir.path() + QDir::separator() + "CMakeLists.txt";
106
107 QString cmakeLists = GuiStatechartGroupXmlReader::ReadFileContents(cmakeListsPath);
108 cmakeLists =
109 cmakeLists.replace(QRegExp("add_subdirectory\\(" + group->getName() + "\\)\\n"), "");
110 GroupXmlWriter::WriteFileContents(cmakeListsPath, cmakeLists);
111
112 if (auto mappingOpt =
114 {
115 ARMARX_INFO_S << "Package '" << group->getPackagePath()
116 << "' contains a mapping file; updating...";
117 StatechartGroupMapping mapping = *mappingOpt;
118 auto gmIt = std::find_if(mapping.groupMappings.begin(),
119 mapping.groupMappings.end(),
120 [&](const StatechartGroupMapping::GroupMapping& groupMapping)
121 { return groupMapping.newGroupName == group->getName(); });
122
123 if (gmIt != mapping.groupMappings.end())
124 {
126 gmIt->groupName, newName, gmIt->groupPackage, gmIt->stateMappings};
127 mapping.groupMappings.erase(gmIt);
128 mapping.groupMappings.insert(newGroupMapping);
129 mapping.writeToFile(group->getPackagePath());
130 }
131 }
132
133 return newGroupPath;
134}
static void ProcessXMLFile(RapidXmlReaderNode source, RapidXmlWriterNode target, const Function &processorFunction)
Definition GroupCloner.h:66
static bool RemoveDir(const QString &dirName)
std::optional< QString > renameGroup(const StatechartGroupPtr &group, const QString &newName, const QVector< StatechartGroupPtr > &affectedGroups)
GroupRenamer(const GroupClonerPtr &groupCloner)
static void WriteFileContents(QString path, QString contents)
static QString ReadFileContents(QString path)
std::string name() const
static RapidXmlReaderPtr FromFile(const std::string &path)
void saveToFile(const std::string &path, bool indent)
RapidXmlWriterNode createRootNode(const std::string &name)
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
#define ARMARX_INFO_S
Definition Logging.h:202
#define ARMARX_VERBOSE_S
Definition Logging.h:207
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< StatechartGroup > StatechartGroupPtr
std::shared_ptr< GroupCloner > GroupClonerPtr
Definition GroupCloner.h:46
std::set< GroupMapping > groupMappings
void writeToFile(const QString &packageDir) const
static std::optional< StatechartGroupMapping > ReadStatechartGroupMappingFile(const QString &packageDir)