Browse Source

cmWorkingDirectory: add class for changing the workdir

Ben Boeckel 8 years ago
parent
commit
047a5e4d66
4 changed files with 52 additions and 0 deletions
  1. 2 0
      Source/CMakeLists.txt
  2. 24 0
      Source/cmWorkingDirectory.cxx
  3. 25 0
      Source/cmWorkingDirectory.h
  4. 1 0
      bootstrap

+ 2 - 0
Source/CMakeLists.txt

@@ -381,6 +381,8 @@ set(SRCS
   cmVariableWatch.h
   cmVariableWatch.h
   cmVersion.cxx
   cmVersion.cxx
   cmVersion.h
   cmVersion.h
+  cmWorkingDirectory.cxx
+  cmWorkingDirectory.h
   cmXMLParser.cxx
   cmXMLParser.cxx
   cmXMLParser.h
   cmXMLParser.h
   cmXMLSafe.cxx
   cmXMLSafe.cxx

+ 24 - 0
Source/cmWorkingDirectory.cxx

@@ -0,0 +1,24 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+   file Copyright.txt or https://cmake.org/licensing for details.  */
+#include "cmWorkingDirectory.h"
+
+#include "cmSystemTools.h"
+
+cmWorkingDirectory::cmWorkingDirectory(std::string const& newdir)
+{
+  this->OldDir = cmSystemTools::GetCurrentWorkingDirectory();
+  cmSystemTools::ChangeDirectory(newdir);
+}
+
+cmWorkingDirectory::~cmWorkingDirectory()
+{
+  this->Pop();
+}
+
+void cmWorkingDirectory::Pop()
+{
+  if (!this->OldDir.empty()) {
+    cmSystemTools::ChangeDirectory(this->OldDir);
+    this->OldDir.clear();
+  }
+}

+ 25 - 0
Source/cmWorkingDirectory.h

@@ -0,0 +1,25 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+   file Copyright.txt or https://cmake.org/licensing for details.  */
+#ifndef cmWorkingDirectory_h
+#define cmWorkingDirectory_h
+
+#include <cmConfigure.h> // IWYU pragma: keep
+
+#include <string>
+
+/** \class cmWorkingDirectory
+ * \brief An RAII class to manipulate the working directory.
+ */
+class cmWorkingDirectory
+{
+public:
+  cmWorkingDirectory(std::string const& newdir);
+  ~cmWorkingDirectory();
+
+  void Pop();
+
+private:
+  std::string OldDir;
+};
+
+#endif

+ 1 - 0
bootstrap

@@ -404,6 +404,7 @@ CMAKE_CXX_SOURCES="\
   cmUnsetCommand \
   cmUnsetCommand \
   cmVersion \
   cmVersion \
   cmWhileCommand \
   cmWhileCommand \
+  cmWorkingDirectory \
   cmake  \
   cmake  \
   cmakemain \
   cmakemain \
   cmcmd  \
   cmcmd  \