PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
WorkspaceManager Class Reference

#include <WorkspaceManager.h>

Public Member Functions

 WorkspaceManager ()
 
 ~WorkspaceManager ()
 
bool IsPortableMode () const
 
bool IsFirstRun () const
 
bool Initialize ()
 
bool CopyResources (const std::string &installationPath)
 
bool IsInitialized () const
 
void SetWorkspacePath (const std::string &path)
 
std::string GetWorkspacePath () const
 
std::string GetStudiesPath () const
 
std::string GetSnapshotsPath () const
 
std::string GetChainsPath () const
 
std::string GetScalesPath () const
 
std::string GetDocsPath () const
 
std::string GetDemoPath () const
 
std::string GetTutorialPath () const
 
std::vector< std::string > GetStudyDirectories () const
 
std::vector< std::string > GetSnapshotDirectories () const
 
bool CreateStudyDirectory (const std::string &studyName)
 
bool CreateSnapshot (const std::string &studyPath, const std::string &snapshotName)
 
bool ImportSnapshot (const std::string &snapshotPath, const std::string &newStudyName)
 

Detailed Description

Definition at line 17 of file WorkspaceManager.h.

Constructor & Destructor Documentation

◆ WorkspaceManager()

WorkspaceManager::WorkspaceManager ( )

Definition at line 37 of file WorkspaceManager.cpp.

38 : mInitialized(false)
39{
40 // In portable mode, use current directory; otherwise use Documents
41 if (IsPortableMode()) {
42 mWorkspacePath = GetPortableWorkspacePath();
43 } else {
44 mWorkspacePath = GetDocumentsPath() + "/pebl-exp." + PEBL_VERSION;
45 }
46}
#define PEBL_VERSION
bool IsPortableMode() const

References IsPortableMode(), and PEBL_VERSION.

◆ ~WorkspaceManager()

WorkspaceManager::~WorkspaceManager ( )

Definition at line 48 of file WorkspaceManager.cpp.

48 {
49}

Member Function Documentation

◆ CopyResources()

bool WorkspaceManager::CopyResources ( const std::string &  installationPath)

Definition at line 176 of file WorkspaceManager.cpp.

176 {
177 if (installationPath.empty() || IsPortableMode()) {
178 std::cout << "CopyResources: skipped (empty path or portable mode)" << std::endl;
179 return false;
180 }
181
182 std::cout << "CopyResources: installation path = " << installationPath << std::endl;
183 std::cout << "CopyResources: workspace path = " << mWorkspacePath << std::endl;
184
185 // Copy specific documentation files
186 std::string docDest = mWorkspacePath + "/doc";
187 CreateDir(docDest);
188
189 std::vector<std::string> docFiles = {
190 std::string("doc/pman/PEBLManual") + PEBL_VERSION + ".pdf",
191 "doc/ReleaseNotes.txt",
192 "Notes_for_LLMs.txt"
193 };
194
195 std::cout << "Copying documentation files..." << std::endl;
196 for (const auto& relPath : docFiles) {
197 std::string srcFile = installationPath + "/" + relPath;
198 // Flatten the structure - put all files directly in doc/
199 std::string filename = relPath;
200 size_t lastSlash = filename.find_last_of('/');
201 if (lastSlash != std::string::npos) {
202 filename = filename.substr(lastSlash + 1);
203 }
204 std::string destFile = docDest + "/" + filename;
205
206 if (CopyFileContents(srcFile, destFile)) {
207 std::cout << " ✓ Copied " << filename << std::endl;
208 } else {
209 std::cout << " ✗ Failed to copy " << relPath << std::endl;
210 }
211 }
212
213 // Copy demos (entire directory, excluding tests subdirectory)
214 std::string demoSource = installationPath + "/demo";
215 std::string demoDest = mWorkspacePath + "/demo";
216 std::cout << "Checking for demo: " << demoSource << std::endl;
217 if (DirectoryExists(demoSource)) {
218 std::cout << "Copying demos from " << demoSource << " to " << demoDest << " (excluding tests/)" << std::endl;
219 std::vector<std::string> excludeDirs = {"tests"};
220 if (CopyDirectory(demoSource, demoDest, false, excludeDirs)) {
221 std::cout << " ✓ Demos copied successfully" << std::endl;
222 } else {
223 std::cout << " ✗ Failed to copy demos" << std::endl;
224 }
225 } else {
226 std::cout << " Demo source not found" << std::endl;
227 }
228
229 // Copy tutorials (entire directory)
230 std::string tutorialSource = installationPath + "/tutorials";
231 std::string tutorialDest = mWorkspacePath + "/tutorials";
232 std::cout << "Checking for tutorials: " << tutorialSource << std::endl;
233 if (DirectoryExists(tutorialSource)) {
234 std::cout << "Copying tutorials from " << tutorialSource << " to " << tutorialDest << std::endl;
235 if (CopyDirectory(tutorialSource, tutorialDest, false)) {
236 std::cout << " ✓ Tutorials copied successfully" << std::endl;
237 } else {
238 std::cout << " ✗ Failed to copy tutorials" << std::endl;
239 }
240 } else {
241 std::cout << " Tutorials source not found" << std::endl;
242 }
243
244 return true;
245}

References IsPortableMode(), and PEBL_VERSION.

◆ CreateSnapshot()

bool WorkspaceManager::CreateSnapshot ( const std::string &  studyPath,
const std::string &  snapshotName 
)

Definition at line 325 of file WorkspaceManager.cpp.

325 {
326 std::string snapshotPath = GetSnapshotsPath() + "/" + snapshotName;
327
328 if (DirectoryExists(snapshotPath)) {
329 return false; // Already exists
330 }
331
332 // Copy study directory, excluding data/ subdirectories
333 return CopyDirectory(studyPath, snapshotPath, true);
334}
std::string GetSnapshotsPath() const

References GetSnapshotsPath().

◆ CreateStudyDirectory()

bool WorkspaceManager::CreateStudyDirectory ( const std::string &  studyName)

Definition at line 305 of file WorkspaceManager.cpp.

305 {
306 std::string studyPath = GetStudiesPath() + "/" + studyName;
307
308 if (DirectoryExists(studyPath)) {
309 return false; // Already exists
310 }
311
312 // Create study directory
313 if (!CreateDir(studyPath)) {
314 return false;
315 }
316
317 // Create subdirectories
318 CreateDir(studyPath + "/chains");
319 CreateDir(studyPath + "/tests");
320 CreateDir(studyPath + "/data");
321
322 return true;
323}
std::string GetStudiesPath() const

References GetStudiesPath().

◆ GetChainsPath()

std::string WorkspaceManager::GetChainsPath ( ) const
inline

Definition at line 44 of file WorkspaceManager.h.

44{ return mWorkspacePath + "/chains"; }

◆ GetDemoPath()

std::string WorkspaceManager::GetDemoPath ( ) const
inline

Definition at line 47 of file WorkspaceManager.h.

47{ return mWorkspacePath + "/demo"; }

◆ GetDocsPath()

std::string WorkspaceManager::GetDocsPath ( ) const
inline

Definition at line 46 of file WorkspaceManager.h.

46{ return mWorkspacePath + "/doc"; }

◆ GetScalesPath()

std::string WorkspaceManager::GetScalesPath ( ) const
inline

Definition at line 45 of file WorkspaceManager.h.

45{ return mWorkspacePath + "/scales"; }

◆ GetSnapshotDirectories()

std::vector< std::string > WorkspaceManager::GetSnapshotDirectories ( ) const

Definition at line 278 of file WorkspaceManager.cpp.

278 {
279 std::vector<std::string> snapshots;
280 std::string snapshotsPath = GetSnapshotsPath();
281
282 try {
283 for (const auto& entry : fs::directory_iterator(snapshotsPath)) {
284 if (!entry.is_directory()) {
285 continue;
286 }
287
288 std::string dirName = entry.path().filename().string();
289 std::string fullPath = entry.path().string();
290
291 // Check if directory contains study-info.json
292 std::string studyInfoPath = fullPath + "/study-info.json";
293 struct stat fileInfo;
294 if (stat(studyInfoPath.c_str(), &fileInfo) == 0) {
295 snapshots.push_back(dirName);
296 }
297 }
298 } catch (const fs::filesystem_error&) {
299 // Directory doesn't exist or can't be read
300 }
301
302 return snapshots;
303}

References GetSnapshotsPath().

◆ GetSnapshotsPath()

std::string WorkspaceManager::GetSnapshotsPath ( ) const
inline

Definition at line 43 of file WorkspaceManager.h.

43{ return mWorkspacePath + "/snapshots"; }

Referenced by CreateSnapshot(), and GetSnapshotDirectories().

◆ GetStudiesPath()

std::string WorkspaceManager::GetStudiesPath ( ) const
inline

Definition at line 42 of file WorkspaceManager.h.

42{ return mWorkspacePath + "/my_studies"; }

Referenced by CreateStudyDirectory(), GetStudyDirectories(), and ImportSnapshot().

◆ GetStudyDirectories()

std::vector< std::string > WorkspaceManager::GetStudyDirectories ( ) const

Definition at line 251 of file WorkspaceManager.cpp.

251 {
252 std::vector<std::string> studies;
253 std::string studiesPath = GetStudiesPath();
254
255 try {
256 for (const auto& entry : fs::directory_iterator(studiesPath)) {
257 if (!entry.is_directory()) {
258 continue;
259 }
260
261 std::string dirName = entry.path().filename().string();
262 std::string fullPath = entry.path().string();
263
264 // Check if directory contains study-info.json
265 std::string studyInfoPath = fullPath + "/study-info.json";
266 struct stat fileInfo;
267 if (stat(studyInfoPath.c_str(), &fileInfo) == 0) {
268 studies.push_back(dirName);
269 }
270 }
271 } catch (const fs::filesystem_error&) {
272 // Directory doesn't exist or can't be read
273 }
274
275 return studies;
276}

References GetStudiesPath().

◆ GetTutorialPath()

std::string WorkspaceManager::GetTutorialPath ( ) const
inline

Definition at line 48 of file WorkspaceManager.h.

48{ return mWorkspacePath + "/tutorial"; }

◆ GetWorkspacePath()

std::string WorkspaceManager::GetWorkspacePath ( ) const
inline

Definition at line 41 of file WorkspaceManager.h.

41{ return mWorkspacePath; }

◆ ImportSnapshot()

bool WorkspaceManager::ImportSnapshot ( const std::string &  snapshotPath,
const std::string &  newStudyName 
)

Definition at line 336 of file WorkspaceManager.cpp.

336 {
337 std::string studyPath = GetStudiesPath() + "/" + newStudyName;
338
339 if (DirectoryExists(studyPath)) {
340 return false; // Already exists
341 }
342
343 // Copy snapshot directory (includes everything since snapshots don't have data/)
344 return CopyDirectory(snapshotPath, studyPath, false);
345}

References GetStudiesPath().

◆ Initialize()

bool WorkspaceManager::Initialize ( )

Definition at line 138 of file WorkspaceManager.cpp.

138 {
139 // Skip workspace creation in portable mode
140 if (IsPortableMode()) {
141 mInitialized = true;
142 return true;
143 }
144
145 // Create main workspace directory
146 if (!DirectoryExists(mWorkspacePath)) {
147 if (!CreateDir(mWorkspacePath)) {
148 return false;
149 }
150 }
151
152 // Create subdirectories
153 std::vector<std::string> subdirs = {
154 "/my_studies",
155 "/snapshots",
156 "/scales",
157 "/doc",
158 "/demo",
159 "/tutorials",
160 "/logs"
161 };
162
163 for (const auto& subdir : subdirs) {
164 std::string fullPath = mWorkspacePath + subdir;
165 if (!DirectoryExists(fullPath)) {
166 if (!CreateDir(fullPath)) {
167 return false;
168 }
169 }
170 }
171
172 mInitialized = true;
173 return true;
174}

References IsPortableMode().

◆ IsFirstRun()

bool WorkspaceManager::IsFirstRun ( ) const

Definition at line 122 of file WorkspaceManager.cpp.

122 {
123 // In portable mode, workspace always "exists" (current directory)
124 if (IsPortableMode()) {
125 return false;
126 }
127
128 // Check if workspace directory exists and has content
129 if (!DirectoryExists(mWorkspacePath)) {
130 return true;
131 }
132
133 // Check if my_studies exists (primary indicator)
134 std::string studiesPath = mWorkspacePath + "/my_studies";
135 return !DirectoryExists(studiesPath);
136}

References IsPortableMode().

◆ IsInitialized()

bool WorkspaceManager::IsInitialized ( ) const

Definition at line 247 of file WorkspaceManager.cpp.

247 {
248 return mInitialized || DirectoryExists(mWorkspacePath);
249}

◆ IsPortableMode()

bool WorkspaceManager::IsPortableMode ( ) const

Definition at line 51 of file WorkspaceManager.cpp.

51 {
52 // Check for STANDALONE.txt or PORTABLE.txt marker file.
53 // The marker should live at the portable distribution root, which is
54 // one level above bin\ on Windows (where the exe lives).
55 // We also accept it in ./ (same dir as CWD) in case the user places it
56 // next to the exe — GetPortableWorkspacePath() will still return ".."
57 // so studies are never created inside bin\.
58 if (FileExists("./STANDALONE.txt") || FileExists("../STANDALONE.txt") || FileExists("../../STANDALONE.txt")) {
59 return true;
60 }
61
62 if (FileExists("./PORTABLE.txt") || FileExists("../PORTABLE.txt") || FileExists("../../PORTABLE.txt")) {
63 return true;
64 }
65
66 // PEBL_PORTABLE environment variable (set by a batch file before launching)
67 const char* portableEnv = getenv("PEBL_PORTABLE");
68 if (portableEnv && strcmp(portableEnv, "1") == 0) {
69 return true;
70 }
71
72 return false;
73}

Referenced by CopyResources(), Initialize(), IsFirstRun(), and WorkspaceManager().

◆ SetWorkspacePath()

void WorkspaceManager::SetWorkspacePath ( const std::string &  path)
inline

Definition at line 38 of file WorkspaceManager.h.

38{ mWorkspacePath = path; }

The documentation for this class was generated from the following files: