1 /*
2  * RSourceManager.hpp
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 
16 #ifndef R_SOURCE_MANAGER_HPP
17 #define R_SOURCE_MANAGER_HPP
18 
19 #include <time.h>
20 
21 #include <string>
22 #include <vector>
23 
24 #include <boost/utility.hpp>
25 #include <boost/unordered_map.hpp>
26 
27 #include <shared_core/FilePath.hpp>
28 
29 namespace rstudio {
30 namespace core {
31    class Error;
32 }
33 }
34 
35 namespace rstudio {
36 namespace r {
37 
38 // singleton
39 class SourceManager;
40 SourceManager& sourceManager();
41 
42 class SourceManager : boost::noncopyable
43 {
44 private:
SourceManager()45    SourceManager() : autoReload_(false) {}
46    friend SourceManager& sourceManager();
47    // COPYING: boost::noncopyable
48 
49 public:
50 
autoReload() const51    bool autoReload() const { return autoReload_; }
setAutoReload(bool autoReload)52    void setAutoReload(bool autoReload) { autoReload_ = autoReload; }
53 
54    core::Error sourceTools(const core::FilePath& filePath);
55    void ensureToolsLoaded();
56 
57    core::Error sourceLocal(const core::FilePath& filePath);
58 
59    void reloadIfNecessary();
60 
61 private:
62    // data types
63    struct SourcedFileInfo
64    {
SourcedFileInforstudio::r::SourceManager::SourcedFileInfo65       SourcedFileInfo() : lastWriteTime((time_t)-1), local(true) {}
SourcedFileInforstudio::r::SourceManager::SourcedFileInfo66       SourcedFileInfo(time_t lastWriteTime, bool local)
67       :  lastWriteTime(lastWriteTime),
68       local(local)
69       {
70       }
71       time_t lastWriteTime;
72       bool local;
73    };
74    typedef boost::unordered_map<std::string, SourcedFileInfo> SourcedFileMap;
75 
76    // helper functions
77    core::Error source(const core::FilePath& filePath, bool local);
78    void reSourceTools(const core::FilePath& filePath);
79    void recordSourcedFile(const core::FilePath& filePath, bool local);
80    void reloadSourceIfNecessary(const SourcedFileMap::value_type& value);
81 
82    // members
83    bool autoReload_;
84    SourcedFileMap sourcedFiles_;
85    std::vector<core::FilePath> toolsFilePaths_;
86 };
87 
88 } // namespace r
89 } // namespace rstudio
90 
91 
92 #endif // R_SOURCE_MANAGER_HPP
93 
94