Skip to content

Commit

Permalink
First working save to LocalDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
james-strauss-uwa committed Nov 1, 2024
1 parent 113713c commit 7b839aa
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 15 deletions.
81 changes: 71 additions & 10 deletions src/Eagle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1570,8 +1570,6 @@ export class Eagle {

Utils.showNotification("Graph Config Saved", "Config '" + activeConfig.getName() + "' saved to Logical Graph", "success");
});


}

saveGraph = () : void => {
Expand All @@ -1583,27 +1581,47 @@ export class Eagle {
case Repository.Service.GitLab:
this.commitToGit(Eagle.FileType.Graph);
break;
case Repository.Service.LocalDirectory:
this.saveFileToLocalDirectory(Eagle.FileType.Graph);
break;
default:
this.saveGraphAs();
break;
}
}

saveGraphAs = () : void => {
const isLocalFile = this.logicalGraph().fileInfo().repositoryService === Repository.Service.File;
let defaultSelection: number = 0;
switch (this.logicalGraph().fileInfo().repositoryService){
case Repository.Service.File:
defaultSelection = 0;
break;
case Repository.Service.LocalDirectory:
defaultSelection = 1;
break;
default:
defaultSelection = 2;
break;
}

Utils.requestUserChoice("Save Graph As", "Please choose where to save the graph", ["Local File", "Remote Git Repository"], isLocalFile?0:1, false, "", (completed: boolean, userChoiceIndex: number) => {
Utils.requestUserChoice("Save Graph As", "Please choose where to save the graph", ["Local File", "Local Directory", "Remote Git Repository"], defaultSelection, false, "", (completed: boolean, userChoiceIndex: number) => {
if (!completed)
{ // Cancelling action.
return;
}

const fileType = this.logicalGraph().fileInfo().type;

if (userChoiceIndex === 0){
this.saveFileToLocal(fileType);
} else {
this.commitToGitAs(fileType);
switch (userChoiceIndex){
case 0:
this.saveFileToLocal(fileType);
break;
case 1:
this.saveFileToLocalDirectory(fileType);
break;
default:
this.commitToGitAs(fileType);
break;
}
});
}
Expand Down Expand Up @@ -1640,6 +1658,49 @@ export class Eagle {
}
}

/**
* Saves the file to a repository that is actually a "LocalDirectory"-type
*/
saveFileToLocalDirectory = async (fileType: Eagle.FileType) : Promise<void> => {
let fileInfo: FileInfo = null;

switch(fileType){
case Eagle.FileType.Graph:
fileInfo = this.logicalGraph().fileInfo();
break;
default:
console.error("Only graph saving supported here!")
return;
}

// find repo by name
const defaultRepository = Repositories.get(Repository.Service.LocalDirectory, fileInfo.repositoryName, fileInfo.repositoryBranch);

Utils.requestUserGitCommit(defaultRepository, Repositories.getList(Repository.Service.LocalDirectory), fileInfo.path, fileInfo.name, fileType, (completed : boolean, repositoryService : Repository.Service, repositoryName : string, repositoryBranch : string, filePath : string, fileName : string, commitMessage : string) : void => {
// check completed boolean
if (!completed){
console.log("Abort save!");
return;
}

// check repository name
const repository : Repository = Repositories.get(repositoryService, repositoryName, repositoryBranch);

// write logical graph contents to a string
const contents: string = LogicalGraph.toOJSJsonString(this.logicalGraph(), false);

// save
Repository.saveLocalDirectoryFile(repositoryService, repositoryName, filePath, fileName, contents, (error: string) => {
if (error !== null){
console.error(error);
}

// refresh the repository
repository.refresh();
});
});
}

/**
* Saves a file to the remote server repository.
*/
Expand All @@ -1660,7 +1721,6 @@ export class Eagle {
return;
}


Utils.httpPostJSONString(url, jsonString, (error : string, data: string) : void => {
if (error !== null){
Utils.showUserMessage("Error", data + "<br/><br/>These error messages provided by " + repository.service + " are not very helpful. Please contact EAGLE admin to help with further investigation.");
Expand Down Expand Up @@ -1832,6 +1892,7 @@ export class Eagle {
fileInfo().repositoryService === Repository.Service.Unknown ||
fileInfo().repositoryService === Repository.Service.File ||
fileInfo().repositoryService === Repository.Service.Url ||
fileInfo().repositoryService === Repository.Service.LocalDirectory ||
fileInfo().repositoryName === null
) {
this.commitToGitAs(fileType);
Expand Down Expand Up @@ -2038,7 +2099,7 @@ export class Eagle {
openRemoteFileFunc = Utils.openRemoteFileFromUrl;
break;
case Repository.Service.LocalDirectory:
openRemoteFileFunc = Repository.openLocalFile;
openRemoteFileFunc = Repository.loadLocalDirectoryFile;
break;
default:
console.warn("Unsure how to fetch file with unknown service ", file.repository.service);
Expand Down
69 changes: 65 additions & 4 deletions src/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class Repository {
for (const folder of pointer.folders()){
if (folder.name === pathPart){
pointer = folder;
break;
}
}
}
Expand Down Expand Up @@ -134,6 +135,7 @@ export class Repository {
if (folder.name === pathPart){
lastPointer = pointer;
pointer = folder;
break;
}
}
}
Expand All @@ -160,6 +162,35 @@ export class Repository {
}
}

getDirectoryHandleFromPath = async (path: string): Promise<FileSystemDirectoryHandle> => {
const pathParts: string[] = path.split('/');
let pointer: Repository | RepositoryFolder = this;

// traverse down the folder structure
for (const pathPart of pathParts){
let found: boolean = false;

for (const folder of pointer.folders()){
if (folder.name === pathPart){
pointer = folder;
found = true;
break;
}
}

// if this step in the path hierarchy was not found, create a new FileSystemDirectoryHandle here
if (!found){
const handle: FileSystemDirectoryHandle = await pointer.handle.getDirectoryHandle(pathPart, {create: true});

// store the handle in an orphaned RepositoryFolder
pointer = new RepositoryFolder(pathPart);
pointer.handle = handle;
}
}

return pointer.handle;
}

// sorting order
// 1. alphabetically by service
// 2. alphabetically by name
Expand Down Expand Up @@ -239,13 +270,14 @@ export class Repository {
}
}

static async openLocalFile(repositoryService : Repository.Service, repositoryName : string, repositoryBranch : string, filePath : string, fileName : string, callback: (error : string, data : string) => void ): Promise<void> {
// load a file from a "LocalDirectory"-type repository
static async loadLocalDirectoryFile(repositoryService : Repository.Service, repositoryName : string, repositoryBranch : string, filePath : string, fileName : string, callback: (error : string, data : string) => void ): Promise<void> {
// find the repository
const localDirectory: Repository = Repositories.get(repositoryService, repositoryName, repositoryBranch);

// check we found it
if (localDirectory === null){
callback("Repository.openLocalFile(): can't find Repository. Service: " + repositoryService + " Name: " + repositoryName + " Branch: " + repositoryBranch, null);
callback("Can't find Repository. Service: " + repositoryService + " Name: " + repositoryName + " Branch: " + repositoryBranch, null);
return;
}

Expand All @@ -254,7 +286,7 @@ export class Repository {

// abort if file not found
if (repositoryFile === null){
callback("openLocalFile(): can't find file in directory: " + filePath + " " + fileName, null);
callback("Can't find file in directory: " + filePath + " " + fileName, null);
return;
}

Expand All @@ -263,7 +295,7 @@ export class Repository {

// abort if file doesn't have a fileHandle
if (fileHandle === null){
callback("openLocalFile(): no handle attached to RepositoryFile: " + filePath + " " + fileName, null);
callback("No handle attached to RepositoryFile: " + filePath + " " + fileName, null);
return;
}

Expand All @@ -273,6 +305,35 @@ export class Repository {

callback(null, fileData);
}

// save a file to a "LocalDirectory"-type repository
static async saveLocalDirectoryFile(repositoryService : Repository.Service, repositoryName : string, filePath : string, fileName : string, contents: string, callback: (error : string) => void ): Promise<void> {
// find the repository
const localDirectory: Repository = Repositories.get(repositoryService, repositoryName, "");

// check we found it
if (localDirectory === null){
callback("Can't find Repository. Service: " + repositoryService + " Name: " + repositoryName);
return;
}

// get (or create) a FileSystemDirectoryHandle for the directory containing the file
const pathDirectoryHandle: FileSystemDirectoryHandle = await localDirectory.getDirectoryHandleFromPath(filePath);

// create a fileHandle for the new file
const fileHandle: FileSystemFileHandle = await pathDirectoryHandle.getFileHandle(fileName, {create: true});

// create a FileSystemWritableFileStream to write to
const writable = await fileHandle.createWritable();

// write the contents of the file to the stream
await writable.write(contents);

// Close the file and write the contents to disk
await writable.close();

callback(null);
}
}

export namespace Repository {
Expand Down
2 changes: 1 addition & 1 deletion templates/modals.html
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ <h5 class="alert-heading">NOTE:</h5>
</div>
</div>
</div>
<div id="gitCustomRepositoryModalSectionLocal">
<div id="gitCustomRepositoryModalSectionLocal" style="display: none;">
<div class="row">
<div class="col-3">
<span>Directory *</span>
Expand Down

0 comments on commit 7b839aa

Please sign in to comment.