27 #include <IceUtil/UUID.h>
29 #include <QDirIterator>
43 const QString& newStateName,
46 RenameStateInstances(instanceRenameInfos);
48 if (!RenameStateClass(state, newStateName))
53 if (!AdjustStatechartGroupFile(group, state, newStateName))
58 auto cmakeListPath = QString::fromUtf8((group->getBoostDefinitionFilePath().remove_filename() /
"CMakeLists.txt").c_str());
60 fileContents = fileContents.replace(state->getState()->getStateName() +
".xml", newStateName +
".xml");
61 fileContents = fileContents.replace(state->getState()->getStateName() +
".cpp", newStateName +
".cpp");
62 fileContents = fileContents.replace(state->getState()->getStateName() +
".h", newStateName +
".h");
63 fileContents = fileContents.replace(state->getState()->getStateName() +
".generated.h", newStateName +
".generated.h");
69 void StateRenamer::RenameStateInstances(
const QVector<InstanceRenameInfo>& instanceRename)
71 for (
const auto& entry : instanceRename)
73 if (entry.fromName == entry.toName)
78 auto xmlProcessor = [&](
const std::string & nodeName,
const std::string & attribName,
const std::string & attribValue) -> std::string
80 QString
value = QString::fromUtf8(attribValue.c_str());
81 const bool nameRef = attribName ==
"name" &&
value == entry.fromName;
82 const bool transitionRef = nodeName ==
"Transition" && (attribName ==
"from" || attribName ==
"to") &&
value == entry.fromName;
83 const bool startRef = nodeName ==
"StartState" && attribName ==
"substateName" &&
value == entry.fromName;
85 if (nameRef || transitionRef || startRef)
87 return entry.toName.toUtf8().data();
107 bool StateRenamer::RenameStateClass(
const StateTreeNodePtr& state,
const QString& newStateName)
109 const std::string newStateNameStd = newStateName.toUtf8().data();
110 auto oldStateName = state->getState()->getStateName();
118 auto xmlProcessor = [&](
const std::string & nodeName,
const std::string & attribName,
const std::string & attribValue) -> std::string
120 QString
value = QString::fromUtf8(attribValue.c_str());
122 if (nodeName ==
"State" && attribName ==
"name" &&
value == state->getState()->getStateName())
125 <<
"expected state file of '"
126 << state->getState()->getStateName()
127 <<
"', but got '" <<
value <<
"'";
128 return newStateName.toUtf8().data();
135 writer.
saveToFile(newStatePath.string(),
true);
137 QVector<QPair<QRegExp, QString>> codeFileReplaceList;
138 codeFileReplaceList.push_back({QRegExp{
"\\b(" + oldStateName +
")\\b"}, newStateName});
139 codeFileReplaceList.push_back({QRegExp{
"_" + oldStateName +
"_"},
"_" + newStateName +
"_"});
140 codeFileReplaceList.push_back({QRegExp{oldStateName +
"GeneratedBase"}, newStateName +
"GeneratedBase"});
144 if (state->checkCppExists())
146 auto oldCppPath = state->getBoostCppFilePath();
147 auto newCppPath = state->getBoostCppFilePath().parent_path() / (newStateNameStd +
".cpp");
148 auto oldHPath = state->getBoostHFilePath();
149 auto newHPath = state->getBoostHFilePath().parent_path() / (newStateNameStd +
".h");
150 auto genPath = state->getBoostGeneratedHFilePath();
154 success = !std::filesystem::exists(oldCppPath) || std::filesystem::remove(oldCppPath);
155 success &= !std::filesystem::exists(oldHPath) || std::filesystem::remove(oldHPath);
156 success &= !std::filesystem::exists(genPath) || std::filesystem::remove(genPath);
165 std::string path = group->getDefinitionFilePath().toUtf8().data();
171 auto scgxmlProcessor = [&](
const std::string & nodeName,
const std::string & attribName,
const std::string & attribValue) -> std::string
173 QString
value = QString::fromUtf8(attribValue.c_str());
175 if (nodeName ==
"State" && attribName ==
"filename" &&
value == state->getBasename())
177 const QString newStateFile = newName +
".xml";
178 return newStateFile.toUtf8().data();
189 bool StateRenamer::AttemptMoveTo(
const QString&
source,
const QString&
target)