-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented most of the file system reading / writing on Electron's side
- Loading branch information
Showing
8 changed files
with
265 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,35 @@ | ||
import Study from "@common/study/Study"; | ||
|
||
export default class Project { | ||
private studies: Study[]; | ||
|
||
/** | ||
* A project is the global entity that keeps track of all the studies and assays that belong together. | ||
* | ||
* @param name Name of the project. This can be used by the user to distinguish between different projects and their | ||
* unique properties | ||
* @param location Value used to identify where this project can be retrieved from. In most cases this is a | ||
* directory somewhere on the local system, but it does not need to be. | ||
*/ | ||
constructor( | ||
public readonly name: string, | ||
public readonly location: string, | ||
public readonly studies: Study[], | ||
) {} | ||
) { | ||
this.studies = []; | ||
} | ||
|
||
public addStudy(study: Study) { | ||
this.studies.push(study); | ||
} | ||
|
||
public getStudies(): Study[] { | ||
return this.studies; | ||
} | ||
|
||
public removeStudy(study: Study) { | ||
const idx = this.studies.findIndex((val: Study) => val.getId() === study.getId()); | ||
if (idx !== -1) { | ||
this.studies.splice(idx, 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Project from "@common/project/Project"; | ||
|
||
export default interface ProjectManager { | ||
/** | ||
* Load an existing project. If the project files do not exist, this function will throw an error. | ||
* | ||
* @param projectLocation The main directory of the project on disk. | ||
* @param addToRecents Should this project be added to the list of recent projects? Set to false for no. | ||
*/ | ||
loadProject( | ||
projectLocation: string, | ||
addToRecents: boolean | ||
): Promise<Project>; | ||
|
||
/** | ||
* Create a new project and correctly initialize all project files in the provided directory. | ||
* | ||
* @param projectLocation The main directory of the project on disk. | ||
* @param addToRecents Should this project be added to the list of recent projects? Set to false for no. | ||
*/ | ||
createProject( | ||
projectLocation: string, | ||
addToRecents: boolean | ||
): Promise<Project>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
import Study from "@common/study/Study"; | ||
import DatabaseManager from "@main/database/DatabaseManager"; | ||
|
||
export default interface StudyManager { | ||
/** | ||
* Load a study from the given directory. The study's name is assumed to be the name of the directory. If the given | ||
* directory is empty, a new study will be created. | ||
* Load all studies that are associated to this study manager. Typically something to identify these studies (such | ||
* as a project) is passed along the constructor of this class. | ||
*/ | ||
loadStudies(): Promise<Study[]>; | ||
|
||
/** | ||
* Write the given study object (that is associated with the given project) to disk. | ||
* | ||
* @param study | ||
*/ | ||
writeStudy(study: Study): Promise<void>; | ||
|
||
/** | ||
* Remove the given study object from the underlying storage system. | ||
* | ||
* @param directory The directory that contains all assays and required metadata for this study. | ||
* @param dbManager A database manager connected to the project that this study belongs to and that can be used for | ||
* retrieving / updating this study's metadata. | ||
* @param study | ||
*/ | ||
loadStudy( | ||
directory: string, | ||
dbManager: DatabaseManager | ||
): Promise<Study>; | ||
removeStudy(study: Study): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { v4 as uuidv4 } from "uuid"; | ||
import path from "path"; | ||
import DatabaseManager from "@main/database/DatabaseManager"; | ||
import VersionUtils from "@main/utils/VersionUtils"; | ||
import Project from "@common/project/Project"; | ||
import Study from "@common/study/Study"; | ||
import ProjectManager from "@common/project/ProjectManager"; | ||
import FileSystemManager from "@main/file-system/FileSystemManager"; | ||
import AppManager from "@main/app/AppManager"; | ||
import FileSystemStudyManager from "@main/study/FileSystemStudyManager"; | ||
import FileSystemRecentProjectManager from "@main/project/FileSystemRecentProjectManager"; | ||
|
||
export default class FileSystemProjectManager implements ProjectManager { | ||
private static readonly DB_FILE_NAME: string = "metadata.sqlite"; | ||
|
||
public async loadProject( | ||
projectLocation: string, | ||
addToRecents: boolean = true | ||
): Promise<Project> { | ||
if (!projectLocation.endsWith("/")) { | ||
projectLocation += "/"; | ||
} | ||
|
||
const fsManager = new FileSystemManager(); | ||
|
||
// Check if a project is actually present in this directory. If there isn't, we cannot load the project. | ||
if (!await fsManager.fileExists(projectLocation + FileSystemProjectManager.DB_FILE_NAME)) { | ||
throw new Error("InvalidProjectException: Project metadata file was not found!"); | ||
} | ||
|
||
const appManager = new AppManager(); | ||
|
||
const dbManager = new DatabaseManager( | ||
projectLocation + FileSystemProjectManager.DB_FILE_NAME | ||
); | ||
await dbManager.initializeDatabase(appManager.getAppVersion()); | ||
const dbAppVersion = dbManager.getApplicationVersion(); | ||
|
||
if (VersionUtils.isVersionLargerThan(dbAppVersion, appManager.getAppVersion())) { | ||
throw new Error("ProjectVersionMismatchException: Project was made with a more recent version of Unipept Desktop!"); | ||
} else { | ||
await dbManager.setApplicationVersion(appManager.getAppVersion()); | ||
} | ||
|
||
const project = new Project( | ||
path.basename(projectLocation), | ||
projectLocation | ||
); | ||
|
||
const studyManager = new FileSystemStudyManager( | ||
dbManager, | ||
project | ||
); | ||
|
||
for (const study of await studyManager.loadStudies()) { | ||
project.addStudy(study); | ||
} | ||
|
||
if (addToRecents) { | ||
const recentProjectsMng = new FileSystemRecentProjectManager(); | ||
await recentProjectsMng.addRecentProject(projectLocation); | ||
} | ||
|
||
return project; | ||
} | ||
|
||
public async createProject( | ||
projectLocation: string, | ||
addToRecents: boolean = true | ||
): Promise<Project> { | ||
if (!projectLocation.endsWith("/")) { | ||
projectLocation += "/"; | ||
} | ||
|
||
const fsManager = new FileSystemManager(); | ||
|
||
// Create the project directory | ||
await fsManager.mkdir(projectLocation); | ||
|
||
const dbManager = new DatabaseManager( | ||
path.join(projectLocation, FileSystemProjectManager.DB_FILE_NAME) | ||
); | ||
|
||
const appManager = new AppManager(); | ||
await dbManager.initializeDatabase(appManager.getAppVersion()); | ||
|
||
// Create one dummy study for this project | ||
const study = new Study(uuidv4()); | ||
study.setName("Study name"); | ||
|
||
const project = new Project(path.basename(projectLocation), projectLocation); | ||
project.addStudy(study); | ||
|
||
const studyManager = new FileSystemStudyManager(dbManager, project); | ||
studyManager.writeStudy(study); | ||
|
||
if (addToRecents) { | ||
const recentProjectsMng = new FileSystemRecentProjectManager(); | ||
await recentProjectsMng.addRecentProject(projectLocation); | ||
} | ||
|
||
return project; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.