Skip to content

Commit

Permalink
Merge pull request #557 from MaslowCNC/Save-data-representation-to-gi…
Browse files Browse the repository at this point in the history
…thub

Save data representation to GitHub
  • Loading branch information
BarbourSmith committed Jun 3, 2021
2 parents aa6b562 + 5cfb3c3 commit e4f553d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 16 deletions.
9 changes: 9 additions & 0 deletions dist/maslowWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ const agent = async ({
const shape2getHash = await maslowRead(question.readPath);
return shape2getHash.geometry.hash;
break;
case "getJSON":
const shape2getJSON = await maslowRead(question.readPath);
return JSON.stringify(shape2getJSON.toGeometry());
break;
case "fromJSON":
const fromJson = api.Shape.fromGeometry(JSON.parse(question.json))
await api.saveGeometry(question.writePath, fromJson);
return false;
break;
case "getPathsList":
const listedFiles = await listFiles();
const inThisProject = listedFiles.filter((path) => path.startsWith(question.prefacePath));
Expand Down
74 changes: 60 additions & 14 deletions src/js/githubOauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,23 @@ export default function GitHubModule(){
message: "SVG Picture",
content: ""
}).then(()=>{
intervalTimer = setInterval(() => { this.saveProject() }, 1200000) //Save the project regularly

octokit.repos.createFile({ // Create empty file for SVG string
owner: currentUser,
repo: currentRepoName,
path: ".gitattributes",
message: "Create gitattributes",
content: window.btoa("data binary")
}).then(()=>{
octokit.repos.createFile({ // Create empty file for SVG string
owner: currentUser,
repo: currentRepoName,
path: "data.json",
message: "Data file",
content: ""
}).then(()=>{
intervalTimer = setInterval(() => { this.saveProject() }, 1200000) //Save the project regularly
})
})
})
})
})
Expand Down Expand Up @@ -892,18 +907,24 @@ export default function GitHubModule(){
var decoder = new TextDecoder('utf8')
var finalSVG = decoder.decode(contentSvg)

this.createCommit(octokit,{
owner: saveUser,
repo: saveRepoName,
changes: {
files: {
'BillOfMaterials.md': bomContent,
'README.md': readmeContent,
'project.svg': finalSVG,
'project.maslowcreate': projectContent
},
commit: 'Autosave'
}

const askJsonVals = {key: "getJSON", readPath: GlobalVariables.topLevelMolecule.path}
window.ask(askJsonVals).then( JSONData => {

this.createCommit(octokit,{
owner: saveUser,
repo: saveRepoName,
changes: {
files: {
'BillOfMaterials.md': bomContent,
'README.md': readmeContent,
'project.svg': finalSVG,
'project.maslowcreate': projectContent,
'data.json': JSONData
},
commit: 'Autosave'
}
})
})

intervalTimer = setInterval(() => this.saveProject(), 1200000)
Expand Down Expand Up @@ -1148,6 +1169,31 @@ export default function GitHubModule(){
}
}

/**
* Loads a project's data from github by its github ID.
*/
this.getProjectDataByID = async function(id){
let repo = await octokit.request('GET /repositories/:id', {id})
//Find out the owners info;
const user = repo.data.owner.login
const repoName = repo.data.name

try{
let jsonData = await octokit.repos.getContents({
owner: user,
repo: repoName,
path: 'data.json'
})

jsonData = atob(jsonData.data.content)
return jsonData

}catch(err){
console.warn("Unable to load project data from github")
return false
}
}

/**
* Export a molecule as a new github project.
*/
Expand Down
12 changes: 10 additions & 2 deletions src/js/molecules/githubmolecule.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export default class GitHubMolecule extends Molecule {
//Get the repo by ID
const json = await GlobalVariables.gitHub.getProjectByID(id, this.topLevel)

const projectData = await GlobalVariables.gitHub.getProjectDataByID(id)
if(projectData){
const values = {key: "fromJSON", writePath: this.path, json: projectData}
window.ask(values)
}

//Store values that we want to overwrite in the loaded version
var valuesToOverwriteInLoadedVersion
if(this.topLevel){
Expand All @@ -80,8 +86,10 @@ export default class GitHubMolecule extends Molecule {
}
}
const promsie = this.deserialize(json, valuesToOverwriteInLoadedVersion, true).then( () => {
this.setValues(valuesToOverwriteInLoadedVersion)
this.loadTree()
if(!projectData){ //If we haven't loaded the project directly then recompute the ouput
this.setValues(valuesToOverwriteInLoadedVersion)
this.loadTree()
}
})
return promsie
}
Expand Down

0 comments on commit e4f553d

Please sign in to comment.