Skip to content

Commit

Permalink
cached project dirs, remote setupdated, warn if op overriden, opdocs …
Browse files Browse the repository at this point in the history
…from files
  • Loading branch information
steam0r committed Sep 20, 2024
1 parent 55b63a8 commit 810885a
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 157 deletions.
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const watchers = [];
function _watch(done)
{
const watchOptions = { "usePolling": true, "ignored": (fileName) => { return fileName.includes("node_modules"); } };
watchers.push(gulp.watch(["src_client/*.js", "src_client/**/*.js", "../shared/**/*.js"], watchOptions, gulp.series(defaultSeries)));
watchers.push(gulp.watch(["src/*.js", "src/**/*.js"], watchOptions, gulp.series(electronChanges)));
watchers.push(gulp.watch(["src_client/*.js", "src_client/**/*.js", "../shared/client/**/*.js"], watchOptions, gulp.series(defaultSeries)));
watchers.push(gulp.watch(["src/*.js", "src/**/*.js", "../shared/api/**/*.js"], watchOptions, gulp.series(electronChanges)));
done();
}

Expand Down
92 changes: 13 additions & 79 deletions src/electron/electron_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ class ElectronApi
const currentProject = settings.getCurrentProject();
if (currentProject)
{
const opDocs = doc.getOpDocsInProjectDirs(currentProject);
let opDocs = projectsUtil.getOpDocsInProjectDirs(currentProject);
opDocs = opDocs.filter((opDoc) => { return opDoc.name === opName; });
opDocs.forEach((opDoc) =>
{
if (opDoc.overrides)
Expand All @@ -133,7 +134,7 @@ class ElectronApi
warns.push({
"type": "project",
"id": "",
"text": "this op overrides another op in <a onclick=\"CABLESUILOADER.talkerAPI.send('openDir', { 'dir': '" + override + "'});\">" + override + "</a>"
"text": "<a onclick=\"CABLESUILOADER.talkerAPI.send('openDir', { 'dir': '" + override + "'});\"><span class=\"icon icon-folder\"></span> this op overrides another op</a>"
});
});
}
Expand Down Expand Up @@ -289,7 +290,7 @@ class ElectronApi
});

// add all ops in any of the project op directory
const otherDirsOps = doc.getOpDocsInProjectDirs(project).map((opDoc) => { return opDoc.name; });
const otherDirsOps = projectsUtil.getOpDocsInProjectDirs(project).map((opDoc) => { return opDoc.name; });
projectOps = projectOps.concat(otherDirsOps);

// now we should have all the ops that are used in the project, walk subPatchOps
Expand Down Expand Up @@ -338,7 +339,7 @@ class ElectronApi
const currentProject = settings.getCurrentProject();
let opDocs = doc.getOpDocs(true, true);
opDocs = opDocs.concat(doc.getCollectionOpDocs("Ops.Extension.Standalone", currentUser));
opDocs = opDocs.concat(doc.getOpDocsInProjectDirs(currentProject));
opDocs = opDocs.concat(projectsUtil.getOpDocsInProjectDirs(currentProject));
const cleanDocs = doc.makeReadable(opDocs);
opsUtil.addPermissionsToOps(cleanDocs, null);

Expand Down Expand Up @@ -845,7 +846,7 @@ class ElectronApi
return this.success("OK", result.slice(0, 10), true);
}

opCreate(data)
async opCreate(data)
{
let opName = data.opname;
const currentUser = settings.getCurrentUser();
Expand Down Expand Up @@ -1224,68 +1225,10 @@ class ElectronApi
return this.success("OK", { "filename": sanitizedFileName }, true);
}

async setProjectUpdated()
{
const now = Date.now();
const project = settings.getCurrentProject();
let data = {};
if (project)
{
project.updated = now;
const projectFile = settings.getCurrentProjectFile();
if (projectFile)
{
projectsUtil.writeProjectToFile(projectFile, project);
}
data = project;
}
else
{
data.updated = now;
}
return this.success("OK", data);
}

getProjectOpDirs()
{
const currentProject = settings.getCurrentProject();
const dirs = projectsUtil.getProjectOpDirs(currentProject, true);
if (!cables.isPackaged()) dirs.unshift(cables.getOpsPath());
const dirInfos = [];
const usedOpFiles = {};
if (currentProject && currentProject.ops)
{
currentProject.ops.forEach((op) =>
{
const opName = opsUtil.getOpNameById(op.opId);
if (opName)
{
const opDir = opsUtil.getOpAbsolutePath(opName);
if (opDir)
{
if (!usedOpFiles.hasOwnProperty(opName)) usedOpFiles[opName] = opDir;
}
}
});
}
dirs.forEach((dir) =>
{
const opJsons = helper.getFileNamesRecursive(dir, ".json").map((fileName) => { return path.basename(fileName, ".json"); });
const opNames = opJsons.filter((jsonName) => { return opsUtil.isOpNameValid(jsonName); });
let numUsedOps = 0;
opNames.forEach((opName) =>
{
const opFile = usedOpFiles[opName];
if (opFile && opFile.startsWith(dir) && opFile.endsWith(path.join(opName, "/"))) numUsedOps++;
});
dirInfos.push({
"dir": dir,
"opNames": opNames,
"numOps": opNames.length,
"numUsedOps": numUsedOps,
"fixedPlace": projectsUtil.isFixedPositionOpDir(dir)
});
});
const dirInfos = projectsUtil.getOpDirs(currentProject);
return this.success("OK", dirInfos);
}

Expand Down Expand Up @@ -1319,19 +1262,9 @@ class ElectronApi

saveProjectOpDirOrder(order)
{
const currentProject = settings.getCurrentProject();
const currentProjectFile = settings.getCurrentProjectFile();
let currentProject = settings.getCurrentProject();
if (!currentProject || !order) return this.error("NO_PROJECT");
const newOrder = [];
order.forEach((opDir) =>
{
if (fs.existsSync(opDir)) newOrder.push(opDir);
});
if (!currentProject.dirs) currentProject.dirs = {};
if (!currentProject.dirs.ops) currentProject.dirs.ops = [];
currentProject.dirs.ops = newOrder.filter((dir) => { return !projectsUtil.isFixedPositionOpDir(dir); });
currentProject.dirs.ops = helper.uniqueArray(currentProject.dirs.ops);
projectsUtil.writeProjectToFile(currentProjectFile, currentProject);
currentProject = projectsUtil.reorderOpDirs(currentProject, order);
return this.success("OK", projectsUtil.getProjectOpDirs(currentProject, true));
}

Expand Down Expand Up @@ -1424,7 +1357,8 @@ class ElectronApi
if (project)
{
settings.setProject(projectFile, project);
doc.getOpDocsInProjectDirs(project);
projectsUtil.invalidateProjectCaches();
projectsUtil.getOpDocsInProjectDirs(project);
// add ops in project dirs to lookup
filesUtil.registerAssetChangeListeners(project, true);
if (project.ops)
Expand All @@ -1445,7 +1379,7 @@ class ElectronApi
else
{
settings.setProject(null, null);
doc.getOpDocsInProjectDirs(project);
projectsUtil.getOpDocsInProjectDirs(project);
}
electronApp.updateTitle();
}
Expand Down Expand Up @@ -1615,7 +1549,7 @@ class ElectronApi

if (project)
{
const projectOpDocs = doc.getOpDocsInProjectDirs(project);
const projectOpDocs = projectsUtil.getOpDocsInProjectDirs(project);
availableNamespaces = projectOpDocs.map((opDoc) => { return opsUtil.getNamespace(opDoc.name, true); });
}

Expand Down
23 changes: 21 additions & 2 deletions src/electron/electron_endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import subPatchOpUtil from "../utils/subpatchop_util.js";
import settings from "./electron_settings.js";
import helper from "../utils/helper_util.js";
import electronApp from "./main.js";
import projectsUtil from "../utils/projects_util.js";


protocol.registerSchemesAsPrivileged([
Expand Down Expand Up @@ -203,6 +204,23 @@ class ElectronEndpoint
});
}
}
else if (urlPath.startsWith("/op/screenshot"))
{
let opName = urlPath.split("/", 4)[3];
if (opName) opName = opName.replace(/.png$/, "");
const buffer = opsUtil.getScreenshot(opName);
const bufferSize = buffer ? Buffer.byteLength(buffer) : 0;
const headers = {
"Content-Type": "image/png",
"Accept-Ranges": "bytes",
"Content-Length": bufferSize,
"Content-Range": "bytes 0-" + bufferSize + "/" + (bufferSize + 1),
};
return new Response(buffer, {
"data": buffer,
"headers": headers
});
}
else if (urlPath.startsWith("/edit/"))
{
let patchId = urlPath.split("/", 3)[2];
Expand Down Expand Up @@ -249,15 +267,15 @@ class ElectronEndpoint
if (project)
{
if (project.ops) missingOps = project.ops.filter((op) => { return !opDocs.some((d) => { return d.id === op.opId; }); });

const opsInProjectDir = doc.getOpDocsInProjectDirs(project).map((opDoc) => { return opDoc.name; });
const opsInProjectDir = projectsUtil.getOpDocsInProjectDirs(project).map((opDoc) => { return opDoc.name; });
const ops = subPatchOpUtil.getOpsUsedInSubPatches(project);
missingOps = missingOps.concat(opsInProjectDir);
missingOps = missingOps.concat(ops);
missingOps = missingOps.filter((op) => { return !opDocs.some((d) => { return d.id === op.opId; }); });
}
const opsWithCode = [];
let codeNamespaces = [];

missingOps.forEach((missingOp) =>
{
const opId = missingOp.opId || missingOp.id;
Expand Down Expand Up @@ -326,6 +344,7 @@ class ElectronEndpoint
{
const collectionName = opsUtil.getCollectionNamespace(opName);
opNames = opNames.concat(opsUtil.getCollectionOpNames(collectionName));
opNames.push(opName);
}
else
{
Expand Down
49 changes: 0 additions & 49 deletions src/utils/doc_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,11 @@ class DocUtil extends SharedDocUtil
if (!opName) return null;
if (!this._opsUtil.isOpNameValid(opName)) return null;

if (!docs) docs = this.getOpDocs();
for (let i = 0; i < docs.length; i++)
{
if (docs[i].name === opName)
{
return docs[i];
}
}

const fromFile = this.getOpDocsFromFile(opName);
if (fromFile) fromFile.name = opName;
return fromFile;
}

getOpDocsInProjectDirs(project)
{
const opDocs = {};
const opDirs = projectsUtil.getProjectOpDirs(project);
opDirs.forEach((opDir) =>
{
if (fs.existsSync(opDir))
{
const opJsons = helper.getFilesRecursive(opDir, ".json");
for (let jsonPath in opJsons)
{
const opName = path.basename(jsonPath, ".json");
if (opsUtil.isOpNameValid(opName))
{
if (opDocs.hasOwnProperty(opName))
{
if (!opDocs[opName].hasOwnProperty("overrides")) opDocs[opName].overrides = [];
opDocs[opName].overrides.push(path.join(opDir, path.dirname(jsonPath)));
}
else
{
try
{
const opDoc = jsonfile.readFileSync(path.join(opDir, jsonPath));
opDoc.name = opName;
opDocs[opName] = opDoc;
}
catch (e)
{
this._log.warn("failed to parse opdocs for", opName, "from", jsonPath);
}
}
}
}
}
});
const projectOpDocs = Object.values(opDocs);
this.addOpsToLookup(projectOpDocs);
return projectOpDocs;
}

getOpDocsInDir(opDir)
{
Expand Down
4 changes: 2 additions & 2 deletions src/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Logger extends SharedLogger

endTime(...args)
{
super.endTime("[" + this._initiator + "]", args.join(" "));
super.endTime(...args);
}

error(...args)
Expand All @@ -39,7 +39,7 @@ class Logger extends SharedLogger

startTime(...args)
{
super.startTime("[" + this._initiator + "]", "startTime", args.join(" "));
super.startTime(...args);
}

uncaught(...args)
Expand Down
25 changes: 7 additions & 18 deletions src/utils/ops_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ class OpsUtil extends SharedOpsUtil

getOpAbsolutePath(opName)
{
return this._getAbsoluteOpDirFromHierarchy(opName, super.getOpAbsolutePath(opName));
return projectsUtil.getAbsoluteOpDirFromHierarchy(opName);
}

getOpSourceDir(opName, relative = false)
{
if (relative) return super.getOpSourceDir(opName, relative);
return this._getAbsoluteOpDirFromHierarchy(opName, super.getOpSourceDir(opName));
return projectsUtil.getAbsoluteOpDirFromHierarchy(opName);
}

getOpSourceNoHierarchy(opName, relative = false)
{
return super.getOpSourceDir(opName, relative);
}

getOpRenameConsequences(newName, oldName, targetDir = null)
Expand Down Expand Up @@ -138,22 +143,6 @@ class OpsUtil extends SharedOpsUtil
return assetPorts;
}

_getAbsoluteOpDirFromHierarchy(opName, defaultDir)
{
const relativePath = super.getOpSourceDir(opName, true);
if (relativePath)
{
const dirs = projectsUtil.getProjectOpDirs(settings.getCurrentProject());
for (let i = 0; i < dirs.length; i++)
{
const dir = dirs[i];
const opPath = path.join(dir, relativePath);
if (fs.existsSync(opPath)) return opPath;
}
}
return defaultDir;
}

getOpNameByAbsoluteFileName(fileName)
{
if (!fileName) return "";
Expand Down
Loading

0 comments on commit 810885a

Please sign in to comment.