Skip to content

Commit

Permalink
Format generate-gltf-classes with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Oct 10, 2021
1 parent ee5c9f0 commit a57b438
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 171 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ node_modules
.vs
CMakeSettings.json
*.user
.cache
*.DS_Store
206 changes: 106 additions & 100 deletions tools/generate-gltf-classes/SchemaCache.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,139 @@
const path = require("path");
const fs = require("fs");
const request = require('sync-request');
const request = require("sync-request");

function readJsonStringFromFile(path) {
try {
const result = fs.readFileSync(path, "utf-8");
return result;
} catch(e) {
return undefined;
}
try {
const result = fs.readFileSync(path, "utf-8");
return result;
} catch (e) {
return undefined;
}
}
function readJsonStringFromUrl(url) {
try {
const res = request('GET', url);
return res.getBody("utf-8");
} catch (e) {
return undefined;
}
try {
const res = request("GET", url);
return res.getBody("utf-8");
} catch (e) {
return undefined;
}
}
function readJsonString(pathOrUrl) {
if (pathOrUrl.startsWith("http")) {
return readJsonStringFromUrl(pathOrUrl);
}
return readJsonStringFromFile(pathOrUrl);
if (pathOrUrl.startsWith("http")) {
return readJsonStringFromUrl(pathOrUrl);
}
return readJsonStringFromFile(pathOrUrl);
}

class SchemaCache {
constructor(schemaPath, extensionPath) {
this.schemaPath = schemaPath;
this.extensionPath = extensionPath;
this.cache = {};
this.contextStack = [];
this.byTitle = {};
constructor(schemaPath, extensionPath) {
this.schemaPath = schemaPath;
this.extensionPath = extensionPath;
this.cache = {};
this.contextStack = [];
this.byTitle = {};
}

load(name) {
// First try loading relative to the current context
let path = this.resolveRelativePath(name);

const existing = this.cache[path];
if (existing) {
return existing;
}

load(name) {
// First try loading relative to the current context
let path = this.resolveRelativePath(name);

const existing = this.cache[path];
if (existing) {
return existing;
}

let jsonString = readJsonString(path);
if (jsonString === undefined) {
// Next try resolving relative to the base URL
const pathFromBase = this.resolvePathFromBase(name);

const existingFromBase = this.cache[pathFromBase];
if (existingFromBase) {
return existingFromBase;
}

jsonString = readJsonString(pathFromBase);

if (jsonString === undefined) {
console.warn(`Could not resolve ${name}. Tried:\n * ${path}\n *${pathFromBase}`);
return undefined;
}

path = pathFromBase;
}

const result = JSON.parse(jsonString);
result.sourcePath = path;
this.cache[path] = result;

const upperTitle = result.title.toUpperCase();
if (this.byTitle[upperTitle]) {
console.warn(`*** Two schema files share the same title, things will be broken:\n ${this.byTitle[upperTitle].sourcePath}\n ${path}`);
}

this.byTitle[upperTitle] = result;

return result;
}
let jsonString = readJsonString(path);
if (jsonString === undefined) {
// Next try resolving relative to the base URL
const pathFromBase = this.resolvePathFromBase(name);

loadExtension(name) {
const path = this.resolvePath(this.extensionPath, name);
const existingFromBase = this.cache[pathFromBase];
if (existingFromBase) {
return existingFromBase;
}

const existing = this.cache[path];
if (existing) {
return existing;
}
jsonString = readJsonString(pathFromBase);

const jsonString = readJsonString(path);
const result = JSON.parse(jsonString, "utf-8");
result.sourcePath = path;
this.cache[name] = result;
if (jsonString === undefined) {
console.warn(
`Could not resolve ${name}. Tried:\n * ${path}\n *${pathFromBase}`
);
return undefined;
}

const upperTitle = result.title.toUpperCase();
if (this.byTitle[upperTitle]) {
console.warn(`*** Two schema files share the same title, things will be broken:\n ${this.byTitle[upperTitle].sourcePath}\n ${path}`);
}
path = pathFromBase;
}

this.byTitle[result.title.toUpperCase()] = result;
const result = JSON.parse(jsonString);
result.sourcePath = path;
this.cache[path] = result;

return result;
const upperTitle = result.title.toUpperCase();
if (this.byTitle[upperTitle]) {
console.warn(
`*** Two schema files share the same title, things will be broken:\n ${this.byTitle[upperTitle].sourcePath}\n ${path}`
);
}

pushContext(schema) {
this.contextStack.push(schema);
}
this.byTitle[upperTitle] = result;

popContext() {
this.contextStack.pop();
return result;
}

loadExtension(name) {
const path = this.resolvePath(this.extensionPath, name);

const existing = this.cache[path];
if (existing) {
return existing;
}

resolveRelativePath(name) {
if (this.contextStack.length === 0) {
return name;
}
const jsonString = readJsonString(path);
const result = JSON.parse(jsonString, "utf-8");
result.sourcePath = path;
this.cache[name] = result;

const base = this.contextStack[this.contextStack.length - 1].sourcePath;
return this.resolvePath(base, name);
const upperTitle = result.title.toUpperCase();
if (this.byTitle[upperTitle]) {
console.warn(
`*** Two schema files share the same title, things will be broken:\n ${this.byTitle[upperTitle].sourcePath}\n ${path}`
);
}

resolvePathFromBase(name) {
return this.resolvePath(this.schemaPath, name);
this.byTitle[result.title.toUpperCase()] = result;

return result;
}

pushContext(schema) {
this.contextStack.push(schema);
}

popContext() {
this.contextStack.pop();
}

resolveRelativePath(name) {
if (this.contextStack.length === 0) {
return name;
}

resolvePath(base, name) {
if (base.startsWith("http")) {
return new URL(name, base).href;
} else {
return path.resolve(base, "..", name);
}
const base = this.contextStack[this.contextStack.length - 1].sourcePath;
return this.resolvePath(base, name);
}

resolvePathFromBase(name) {
return this.resolvePath(this.schemaPath, name);
}

resolvePath(base, name) {
if (base.startsWith("http")) {
return new URL(name, base).href;
} else {
return path.resolve(base, "..", name);
}
}
}

module.exports = SchemaCache;
68 changes: 45 additions & 23 deletions tools/generate-gltf-classes/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ function generate(options, schema) {

const properties = Object.keys(schema.properties)
.map((key) =>
resolveProperty(schemaCache, config, name, key, schema.properties[key], required)
resolveProperty(
schemaCache,
config,
name,
key,
schema.properties[key],
required
)
)
.filter((property) => property !== undefined);

Expand All @@ -37,13 +44,14 @@ function generate(options, schema) {
schemaCache.popContext();

const headers = lodash.uniq([
`"Library.h"`,
`"${base}.h"`,
...lodash.flatten(properties.map((property) => property.headers))
`"Library.h"`,
`"${base}.h"`,
...lodash.flatten(properties.map((property) => property.headers)),
]);

headers.sort();

// prettier-ignore
const header = `
// This file was generated by generate-gltf-classes.
// DO NOT EDIT THIS FILE!
Expand Down Expand Up @@ -75,22 +83,24 @@ function generate(options, schema) {

const headerOutputDir = path.join(outputDir, "include", "CesiumGltf");
fs.mkdirSync(headerOutputDir, { recursive: true });
const headerOutputPath = path.join(headerOutputDir, `${name}${thisConfig.toBeInherited ? "Spec" : ""}.h`);
const headerOutputPath = path.join(
headerOutputDir,
`${name}${thisConfig.toBeInherited ? "Spec" : ""}.h`
);
fs.writeFileSync(headerOutputPath, unindent(header), "utf-8");

const readerHeaders = lodash.uniq(
[
`"CesiumGltf/ReaderContext.h"`,
`"${base}JsonHandler.h"`,
...lodash.flatten(properties.map((property) => property.readerHeaders))
]
);
const readerHeaders = lodash.uniq([
`"CesiumGltf/ReaderContext.h"`,
`"${base}JsonHandler.h"`,
...lodash.flatten(properties.map((property) => property.readerHeaders)),
]);
readerHeaders.sort();

const readerLocalTypes = lodash.uniq(
lodash.flatten(properties.map((property) => property.readerLocalTypes))
);

// prettier-ignore
const readerHeader = `
// This file was generated by generate-gltf-classes.
// DO NOT EDIT THIS FILE!
Expand Down Expand Up @@ -176,25 +186,31 @@ function generate(options, schema) {

const readerHeaderOutputDir = path.join(readerOutputDir, "generated");
fs.mkdirSync(readerHeaderOutputDir, { recursive: true });
const readerHeaderOutputPath = path.join(readerHeaderOutputDir, name + "JsonHandler.h");
const readerHeaderOutputPath = path.join(
readerHeaderOutputDir,
name + "JsonHandler.h"
);
fs.writeFileSync(readerHeaderOutputPath, unindent(readerHeader), "utf-8");

const readerLocalTypesImpl = lodash.uniq(
lodash.flatten(properties.map((property) => property.readerLocalTypesImpl))
);

const readerHeadersImpl = lodash.uniq(
[...lodash.flatten(properties.map((property) => property.readerHeadersImpl))]
);
const readerHeadersImpl = lodash.uniq([
...lodash.flatten(properties.map((property) => property.readerHeadersImpl)),
]);
readerHeadersImpl.sort();

function generateReaderOptionsInitializerList(properties, varName) {
function generateReaderOptionsInitializerList(properties, varName) {
const initializerList = properties
.filter(p => p.readerType.toLowerCase().indexOf("jsonhandler") != -1)
.map(p => `_${p.name}(${p.schemas && p.schemas.length > 0 ? varName : ""})`)
.filter((p) => p.readerType.toLowerCase().indexOf("jsonhandler") != -1)
.map(
(p) => `_${p.name}(${p.schemas && p.schemas.length > 0 ? varName : ""})`
)
.join(", ");
return initializerList == "" ? "" : ", " + initializerList;
}
}
// prettier-ignore
const readerImpl = `
// This file was generated by generate-gltf-classes.
// DO NOT EDIT THIS FILE!
Expand Down Expand Up @@ -246,10 +262,16 @@ function generateReaderOptionsInitializerList(properties, varName) {
`;

if (options.oneHandlerFile) {
const readerSourceOutputPath = path.join(readerHeaderOutputDir, "GeneratedJsonHandlers.cpp");
const readerSourceOutputPath = path.join(
readerHeaderOutputDir,
"GeneratedJsonHandlers.cpp"
);
fs.appendFileSync(readerSourceOutputPath, unindent(readerImpl), "utf-8");
} else {
const readerSourceOutputPath = path.join(readerHeaderOutputDir, name + "JsonHandler.cpp");
const readerSourceOutputPath = path.join(
readerHeaderOutputDir,
name + "JsonHandler.cpp"
);
fs.writeFileSync(readerSourceOutputPath, unindent(readerImpl), "utf-8");
}

Expand Down Expand Up @@ -286,7 +308,7 @@ function formatProperty(property) {
}

function formatReaderProperty(property) {
return `${property.readerType} _${property.name};`
return `${property.readerType} _${property.name};`;
}

function formatReaderPropertyImpl(property) {
Expand Down
6 changes: 4 additions & 2 deletions tools/generate-gltf-classes/getNameFromSchema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
function getNameFromSchema(config, schema) {
const title = schema.title;
return config.classes[title] && config.classes[title].overrideName ? config.classes[title].overrideName : makeName(title);
return config.classes[title] && config.classes[title].overrideName
? config.classes[title].overrideName
: makeName(title);
}

function makeName(title) {
Expand All @@ -14,4 +16,4 @@ function makeName(title) {
return parts.join("");
}

module.exports = getNameFromSchema;
module.exports = getNameFromSchema;
Loading

0 comments on commit a57b438

Please sign in to comment.