@itwin/ecschema-locaters 5.0.0-dev.90 → 5.0.0-dev.93
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/SchemaFileLocater.d.ts +94 -0
- package/lib/esm/SchemaFileLocater.d.ts.map +1 -0
- package/lib/esm/SchemaFileLocater.js +213 -0
- package/lib/esm/SchemaFileLocater.js.map +1 -0
- package/lib/esm/SchemaJsonFileLocater.d.ts +42 -0
- package/lib/esm/SchemaJsonFileLocater.d.ts.map +1 -0
- package/lib/esm/SchemaJsonFileLocater.js +96 -0
- package/lib/esm/SchemaJsonFileLocater.js.map +1 -0
- package/lib/esm/SchemaStringLocater.d.ts +54 -0
- package/lib/esm/SchemaStringLocater.d.ts.map +1 -0
- package/lib/esm/SchemaStringLocater.js +83 -0
- package/lib/esm/SchemaStringLocater.js.map +1 -0
- package/lib/esm/SchemaXml.d.ts +22 -0
- package/lib/esm/SchemaXml.d.ts.map +1 -0
- package/lib/esm/SchemaXml.js +55 -0
- package/lib/esm/SchemaXml.js.map +1 -0
- package/lib/esm/SchemaXmlFileLocater.d.ts +70 -0
- package/lib/esm/SchemaXmlFileLocater.d.ts.map +1 -0
- package/lib/esm/SchemaXmlFileLocater.js +140 -0
- package/lib/esm/SchemaXmlFileLocater.js.map +1 -0
- package/lib/esm/SchemaXmlStringLocater.d.ts +48 -0
- package/lib/esm/SchemaXmlStringLocater.d.ts.map +1 -0
- package/lib/esm/SchemaXmlStringLocater.js +100 -0
- package/lib/esm/SchemaXmlStringLocater.js.map +1 -0
- package/lib/esm/StubSchemaXmlFileLocater.d.ts +81 -0
- package/lib/esm/StubSchemaXmlFileLocater.d.ts.map +1 -0
- package/lib/esm/StubSchemaXmlFileLocater.js +184 -0
- package/lib/esm/StubSchemaXmlFileLocater.js.map +1 -0
- package/lib/esm/ecschema-locaters.d.ts +20 -0
- package/lib/esm/ecschema-locaters.d.ts.map +1 -0
- package/lib/esm/ecschema-locaters.js +24 -0
- package/lib/esm/ecschema-locaters.js.map +1 -0
- package/lib/esm/test/ConcurrentGetSchemaJson.test.d.ts +2 -0
- package/lib/esm/test/ConcurrentGetSchemaJson.test.d.ts.map +1 -0
- package/lib/esm/test/ConcurrentGetSchemaJson.test.js +249 -0
- package/lib/esm/test/ConcurrentGetSchemaJson.test.js.map +1 -0
- package/lib/esm/test/ConcurrentGetSchemaXml.test.d.ts +2 -0
- package/lib/esm/test/ConcurrentGetSchemaXml.test.d.ts.map +1 -0
- package/lib/esm/test/ConcurrentGetSchemaXml.test.js +186 -0
- package/lib/esm/test/ConcurrentGetSchemaXml.test.js.map +1 -0
- package/lib/esm/test/SchemaJsonFileLocator.test.d.ts +2 -0
- package/lib/esm/test/SchemaJsonFileLocator.test.d.ts.map +1 -0
- package/lib/esm/test/SchemaJsonFileLocator.test.js +118 -0
- package/lib/esm/test/SchemaJsonFileLocator.test.js.map +1 -0
- package/lib/esm/test/SchemaXml.test.d.ts +2 -0
- package/lib/esm/test/SchemaXml.test.d.ts.map +1 -0
- package/lib/esm/test/SchemaXml.test.js +55 -0
- package/lib/esm/test/SchemaXml.test.js.map +1 -0
- package/lib/esm/test/SchemaXmlFileLocator.test.d.ts +2 -0
- package/lib/esm/test/SchemaXmlFileLocator.test.d.ts.map +1 -0
- package/lib/esm/test/SchemaXmlFileLocator.test.js +182 -0
- package/lib/esm/test/SchemaXmlFileLocator.test.js.map +1 -0
- package/lib/esm/test/SchemaXmlStringLocater.test.d.ts +2 -0
- package/lib/esm/test/SchemaXmlStringLocater.test.d.ts.map +1 -0
- package/lib/esm/test/SchemaXmlStringLocater.test.js +219 -0
- package/lib/esm/test/SchemaXmlStringLocater.test.js.map +1 -0
- package/lib/esm/test/StubSchemaXmlFileLocater.test.d.ts +2 -0
- package/lib/esm/test/StubSchemaXmlFileLocater.test.d.ts.map +1 -0
- package/lib/esm/test/StubSchemaXmlFileLocater.test.js +169 -0
- package/lib/esm/test/StubSchemaXmlFileLocater.test.js.map +1 -0
- package/package.json +7 -5
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/** @packageDocumentation
|
|
2
|
+
* @module Locaters
|
|
3
|
+
*/
|
|
4
|
+
import { Schema, SchemaContext, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
|
|
5
|
+
/**
|
|
6
|
+
* A SchemaKey implementation that aids in identifying Schema files via the
|
|
7
|
+
* addition of two properties: fileName and schemaText. The fileName contains the
|
|
8
|
+
* full path to the file on disk and schemaText is the full string representation
|
|
9
|
+
* of the Schema.
|
|
10
|
+
* @beta
|
|
11
|
+
*/
|
|
12
|
+
export declare class FileSchemaKey extends SchemaKey {
|
|
13
|
+
fileName: string;
|
|
14
|
+
schemaText?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Initializes a new FileSchemaKey object.
|
|
17
|
+
* @param key The EC SchemaKey identifying the Schema.
|
|
18
|
+
* @param fileName The full path to the Schema file.
|
|
19
|
+
* @param schemaText The string representation of the Schema
|
|
20
|
+
* loaded from disk. Optional.
|
|
21
|
+
*/
|
|
22
|
+
constructor(key: SchemaKey, fileName: string, schemaJson?: string);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Abstract class to hold common/overlapping functionality between SchemaJsonFileLocater and SchemaXmlFileLocater
|
|
26
|
+
* @beta - Needs further testing and possibly moved to a separate package.
|
|
27
|
+
*/
|
|
28
|
+
export declare abstract class SchemaFileLocater {
|
|
29
|
+
searchPaths: string[];
|
|
30
|
+
constructor();
|
|
31
|
+
readUtf8FileToString(filePath: string): Promise<string | undefined>;
|
|
32
|
+
readUtf8FileToStringSync(filePath: string): string | undefined;
|
|
33
|
+
fileExists(filePath: string): Promise<boolean | undefined>;
|
|
34
|
+
fileExistsSync(filePath: string): boolean | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Adds more search paths used by this locator to find the
|
|
37
|
+
* Schema files.
|
|
38
|
+
* @param schemaPaths An array of search paths to add
|
|
39
|
+
*/
|
|
40
|
+
addSchemaSearchPaths(schemaPaths: string[]): void;
|
|
41
|
+
/**
|
|
42
|
+
* Add one search path used by this locator to find the
|
|
43
|
+
* Schema files.
|
|
44
|
+
* @param schemaPath A search path to add
|
|
45
|
+
*/
|
|
46
|
+
addSchemaSearchPath(schemaPath: string): void;
|
|
47
|
+
protected abstract getSchemaKey(data: string): SchemaKey;
|
|
48
|
+
/**
|
|
49
|
+
* Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey. This method
|
|
50
|
+
* only attempts to find schema files that have no version in the file name.
|
|
51
|
+
* @param foundFiles The collection of SchemaKeys found in the given directory.
|
|
52
|
+
* @param schemaPath The directory in which to search for the Schemas.
|
|
53
|
+
* @param schemaName The short name of the Schema (without version).
|
|
54
|
+
* @param desiredKey The SchemaKey used to find matching Schema files.
|
|
55
|
+
* @param matchType The SchemaMatchType to use when comparing the desiredKey and the keys found during the search.
|
|
56
|
+
* @param format The type of file that the schema key refers to. json or xml
|
|
57
|
+
*/
|
|
58
|
+
private addCandidateNoExtSchemaKey;
|
|
59
|
+
/**
|
|
60
|
+
* Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey
|
|
61
|
+
* @param foundFiles The collection of SchemaKeys found in the given directory
|
|
62
|
+
* @param schemaPath The directory in which to search for the Schemas
|
|
63
|
+
* @param fileFilter The file filter, potentially with wildcards, used to locate the Schema files.
|
|
64
|
+
* @param desiredKey The schemaKey used to find matching Schema files
|
|
65
|
+
* @param matchType The SchemaMatchType to use when comparing the desired Key and the keys found during the search.
|
|
66
|
+
* @param format The type of file that the schema key refers to. json or xml
|
|
67
|
+
*/
|
|
68
|
+
private addCandidateSchemaKeys;
|
|
69
|
+
/**
|
|
70
|
+
* Attempts to find all Schema files in the configurable search paths that match
|
|
71
|
+
* the desired SchemaKey.
|
|
72
|
+
* @param desiredKey The SchemaKey to match.
|
|
73
|
+
* @param matchType The SchemaMatchType.
|
|
74
|
+
* @param format The type of file that the schema key refers to. json or xml
|
|
75
|
+
*/
|
|
76
|
+
protected findEligibleSchemaKeys(desiredKey: SchemaKey, matchType: SchemaMatchType, format: string): FileSchemaKey[];
|
|
77
|
+
/**
|
|
78
|
+
* Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths
|
|
79
|
+
* to locate the XML Schema file from the file system. Returns only Schemas from XML files with
|
|
80
|
+
* their keys populated.
|
|
81
|
+
* @param key The SchemaKey of the Schema to retrieve.
|
|
82
|
+
* @param matchType The SchemaMatchType.
|
|
83
|
+
* @param context The SchemaContext that will control the lifetime of the schema.
|
|
84
|
+
*/
|
|
85
|
+
abstract getSchema(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<Schema | undefined>;
|
|
86
|
+
/**
|
|
87
|
+
* Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the
|
|
88
|
+
* left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.
|
|
89
|
+
* @param lhs The 'left-hand' FileSchemaKey.
|
|
90
|
+
* @param rhs The 'right-hand' FileSchemaKey.
|
|
91
|
+
*/
|
|
92
|
+
compareSchemaKeyByVersion: (lhs: FileSchemaKey, rhs: FileSchemaKey) => number;
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=SchemaFileLocater.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaFileLocater.d.ts","sourceRoot":"","sources":["../../src/SchemaFileLocater.ts"],"names":[],"mappings":"AAIA;;GAEG;AAKH,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAwB7F;;;;;;GAMG;AACH,qBAAa,aAAc,SAAQ,SAAS;IAEnC,QAAQ,EAAE,MAAM,CAAC;IAEjB,UAAU,CAAC,EAAE,MAAM,CAAC;IAE3B;;;;;;OAMG;gBACS,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;CAKlE;AAED;;;GAGG;AACH,8BAAsB,iBAAiB;IAC9B,WAAW,EAAE,MAAM,EAAE,CAAC;;IAMhB,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAWzE,wBAAwB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAIxD,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;IAQhE,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS;IAI5D;;;;OAIG;IACI,oBAAoB,CAAC,WAAW,EAAE,MAAM,EAAE;IAMjD;;;;OAIG;IACI,mBAAmB,CAAC,UAAU,EAAE,MAAM;IAM7C,SAAS,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS;IAExD;;;;;;;;;OASG;IACH,OAAO,CAAC,0BAA0B;IAoBlC;;;;;;;;OAQG;IACH,OAAO,CAAC,sBAAsB;IAwB9B;;;;;;OAMG;IACH,SAAS,CAAC,sBAAsB,CAAC,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,GAAG,aAAa,EAAE;IAmCpH;;;;;;;OAOG;aACa,SAAS,CAAC,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAE1H;;;;;OAKG;IACI,yBAAyB,QAAS,aAAa,OAAO,aAAa,KAAG,MAAM,CAEjF;CACH"}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
3
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
/** @packageDocumentation
|
|
6
|
+
* @module Locaters
|
|
7
|
+
*/
|
|
8
|
+
import * as fs from "fs";
|
|
9
|
+
import { globSync } from "glob";
|
|
10
|
+
import * as path from "path";
|
|
11
|
+
import { SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
|
|
12
|
+
const formatString = (format, ...args) => {
|
|
13
|
+
return format.replace(/{(\d+)}/g, (match, theNumber) => {
|
|
14
|
+
return typeof args[theNumber] !== "undefined"
|
|
15
|
+
? args[theNumber]
|
|
16
|
+
: match;
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
const padStartEx = (str, targetLength, padString) => {
|
|
20
|
+
targetLength = targetLength >> 0; // truncate if number or convert non-number to 0;
|
|
21
|
+
padString = String((typeof padString !== "undefined" ? padString : " "));
|
|
22
|
+
if (str.length > targetLength) {
|
|
23
|
+
return String(str);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
targetLength = targetLength - str.length;
|
|
27
|
+
if (targetLength > padString.length) {
|
|
28
|
+
padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed
|
|
29
|
+
}
|
|
30
|
+
return padString.slice(0, targetLength) + str;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* A SchemaKey implementation that aids in identifying Schema files via the
|
|
35
|
+
* addition of two properties: fileName and schemaText. The fileName contains the
|
|
36
|
+
* full path to the file on disk and schemaText is the full string representation
|
|
37
|
+
* of the Schema.
|
|
38
|
+
* @beta
|
|
39
|
+
*/
|
|
40
|
+
export class FileSchemaKey extends SchemaKey {
|
|
41
|
+
// The schema file associated with the SchemaKey
|
|
42
|
+
fileName;
|
|
43
|
+
// The JSON text for the schema loaded
|
|
44
|
+
schemaText;
|
|
45
|
+
/**
|
|
46
|
+
* Initializes a new FileSchemaKey object.
|
|
47
|
+
* @param key The EC SchemaKey identifying the Schema.
|
|
48
|
+
* @param fileName The full path to the Schema file.
|
|
49
|
+
* @param schemaText The string representation of the Schema
|
|
50
|
+
* loaded from disk. Optional.
|
|
51
|
+
*/
|
|
52
|
+
constructor(key, fileName, schemaJson) {
|
|
53
|
+
super(key.name, key.version);
|
|
54
|
+
this.fileName = fileName;
|
|
55
|
+
this.schemaText = schemaJson;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Abstract class to hold common/overlapping functionality between SchemaJsonFileLocater and SchemaXmlFileLocater
|
|
60
|
+
* @beta - Needs further testing and possibly moved to a separate package.
|
|
61
|
+
*/
|
|
62
|
+
export class SchemaFileLocater {
|
|
63
|
+
searchPaths;
|
|
64
|
+
constructor() {
|
|
65
|
+
this.searchPaths = [];
|
|
66
|
+
}
|
|
67
|
+
async readUtf8FileToString(filePath) {
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
fs.readFile(filePath, "utf-8", (err, data) => {
|
|
70
|
+
if (err)
|
|
71
|
+
reject(err);
|
|
72
|
+
else
|
|
73
|
+
resolve(data);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
readUtf8FileToStringSync(filePath) {
|
|
78
|
+
return fs.readFileSync(filePath, "utf-8");
|
|
79
|
+
}
|
|
80
|
+
async fileExists(filePath) {
|
|
81
|
+
return new Promise((resolve) => {
|
|
82
|
+
fs.access(filePath, fs.constants.F_OK, (err) => {
|
|
83
|
+
resolve(err ? false : true);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
fileExistsSync(filePath) {
|
|
88
|
+
return fs.existsSync(filePath);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Adds more search paths used by this locator to find the
|
|
92
|
+
* Schema files.
|
|
93
|
+
* @param schemaPaths An array of search paths to add
|
|
94
|
+
*/
|
|
95
|
+
addSchemaSearchPaths(schemaPaths) {
|
|
96
|
+
// If the path is not in the schemaPaths array, add it
|
|
97
|
+
for (const schemaPath of schemaPaths)
|
|
98
|
+
this.addSchemaSearchPath(schemaPath);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Add one search path used by this locator to find the
|
|
102
|
+
* Schema files.
|
|
103
|
+
* @param schemaPath A search path to add
|
|
104
|
+
*/
|
|
105
|
+
addSchemaSearchPath(schemaPath) {
|
|
106
|
+
// If the path is not in the schemaPaths array, add it
|
|
107
|
+
if (!this.searchPaths.find((entry) => entry === schemaPath))
|
|
108
|
+
this.searchPaths.push(schemaPath);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey. This method
|
|
112
|
+
* only attempts to find schema files that have no version in the file name.
|
|
113
|
+
* @param foundFiles The collection of SchemaKeys found in the given directory.
|
|
114
|
+
* @param schemaPath The directory in which to search for the Schemas.
|
|
115
|
+
* @param schemaName The short name of the Schema (without version).
|
|
116
|
+
* @param desiredKey The SchemaKey used to find matching Schema files.
|
|
117
|
+
* @param matchType The SchemaMatchType to use when comparing the desiredKey and the keys found during the search.
|
|
118
|
+
* @param format The type of file that the schema key refers to. json or xml
|
|
119
|
+
*/
|
|
120
|
+
addCandidateNoExtSchemaKey(foundFiles, schemaPath, schemaName, desiredKey, matchType, format) {
|
|
121
|
+
const fullPath = path.join(schemaPath, `${schemaName}.ecschema.${format}`);
|
|
122
|
+
// If the file does not exist, end
|
|
123
|
+
if (!fs.existsSync(fullPath))
|
|
124
|
+
return;
|
|
125
|
+
// Read the file
|
|
126
|
+
const file = fs.readFileSync(fullPath);
|
|
127
|
+
if (!file)
|
|
128
|
+
return;
|
|
129
|
+
// Get the schema key
|
|
130
|
+
const key = this.getSchemaKey(file.toString());
|
|
131
|
+
// If the key matches, put it in foundFiles
|
|
132
|
+
if (key.matches(desiredKey, matchType))
|
|
133
|
+
foundFiles.push(new FileSchemaKey(key, fullPath, file.toString()));
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey
|
|
137
|
+
* @param foundFiles The collection of SchemaKeys found in the given directory
|
|
138
|
+
* @param schemaPath The directory in which to search for the Schemas
|
|
139
|
+
* @param fileFilter The file filter, potentially with wildcards, used to locate the Schema files.
|
|
140
|
+
* @param desiredKey The schemaKey used to find matching Schema files
|
|
141
|
+
* @param matchType The SchemaMatchType to use when comparing the desired Key and the keys found during the search.
|
|
142
|
+
* @param format The type of file that the schema key refers to. json or xml
|
|
143
|
+
*/
|
|
144
|
+
addCandidateSchemaKeys(foundFiles, schemaPath, fileFilter, desiredKey, matchType, format) {
|
|
145
|
+
const fullPath = path.join(schemaPath, fileFilter);
|
|
146
|
+
const result = globSync(fullPath, { windowsPathsNoEscape: true });
|
|
147
|
+
for (const match of result) {
|
|
148
|
+
let fileName = path.basename(match, (`.ecschema.${format}`));
|
|
149
|
+
// TODO: should this be moved or handled elsewhere?
|
|
150
|
+
// Handles two version file names - SchemaKey.parseString supports only 3 version names.
|
|
151
|
+
if (/[^\d]\.\d?\d\.\d?\d$/.test(fileName)) {
|
|
152
|
+
const parts = fileName.split(".");
|
|
153
|
+
parts.splice(2, 0, "00");
|
|
154
|
+
fileName = parts.join(".");
|
|
155
|
+
}
|
|
156
|
+
const file = fs.readFileSync(match);
|
|
157
|
+
if (!file)
|
|
158
|
+
continue;
|
|
159
|
+
const schemaKey = SchemaKey.parseString(fileName);
|
|
160
|
+
if (schemaKey.matches(desiredKey, matchType))
|
|
161
|
+
foundFiles.push(new FileSchemaKey(schemaKey, match, file.toString()));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Attempts to find all Schema files in the configurable search paths that match
|
|
166
|
+
* the desired SchemaKey.
|
|
167
|
+
* @param desiredKey The SchemaKey to match.
|
|
168
|
+
* @param matchType The SchemaMatchType.
|
|
169
|
+
* @param format The type of file that the schema key refers to. json or xml
|
|
170
|
+
*/
|
|
171
|
+
findEligibleSchemaKeys(desiredKey, matchType, format) {
|
|
172
|
+
const foundFiles = new Array();
|
|
173
|
+
let twoVersionSuffix;
|
|
174
|
+
let threeVersionSuffix;
|
|
175
|
+
const readVersion = desiredKey.readVersion.toString();
|
|
176
|
+
const writeVersion = desiredKey.writeVersion.toString();
|
|
177
|
+
const minorVersion = desiredKey.minorVersion.toString();
|
|
178
|
+
if (matchType === SchemaMatchType.Latest) {
|
|
179
|
+
twoVersionSuffix = (`.*.*.ecschema.${format}`);
|
|
180
|
+
threeVersionSuffix = (`.*.*.*.ecschema.${format}`);
|
|
181
|
+
}
|
|
182
|
+
else if (matchType === SchemaMatchType.LatestWriteCompatible) {
|
|
183
|
+
twoVersionSuffix = formatString(`.{0}.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"));
|
|
184
|
+
threeVersionSuffix = formatString(`.{0}.{1}.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"), padStartEx(writeVersion, 2, "0"));
|
|
185
|
+
}
|
|
186
|
+
else if (matchType === SchemaMatchType.LatestReadCompatible) {
|
|
187
|
+
twoVersionSuffix = formatString(`.{0}.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"));
|
|
188
|
+
threeVersionSuffix = formatString(`.{0}.*.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"));
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
twoVersionSuffix = formatString(`.{0}.{1}.ecschema.${format}`, padStartEx(readVersion, 2, "0"), padStartEx(writeVersion, 2, "0"));
|
|
192
|
+
threeVersionSuffix = formatString(`.{0}.{1}.{2}.ecschema.${format}`, padStartEx(readVersion, 2, "0"), padStartEx(writeVersion, 2, "0"), padStartEx(minorVersion, 2, "0"));
|
|
193
|
+
}
|
|
194
|
+
const twoVersionExpression = desiredKey.name + twoVersionSuffix;
|
|
195
|
+
const threeVersionExpression = desiredKey.name + threeVersionSuffix;
|
|
196
|
+
for (const searchPath of this.searchPaths) {
|
|
197
|
+
this.addCandidateNoExtSchemaKey(foundFiles, searchPath, desiredKey.name, desiredKey, matchType, format);
|
|
198
|
+
this.addCandidateSchemaKeys(foundFiles, searchPath, twoVersionExpression, desiredKey, matchType, format);
|
|
199
|
+
this.addCandidateSchemaKeys(foundFiles, searchPath, threeVersionExpression, desiredKey, matchType, format);
|
|
200
|
+
}
|
|
201
|
+
return foundFiles;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the
|
|
205
|
+
* left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.
|
|
206
|
+
* @param lhs The 'left-hand' FileSchemaKey.
|
|
207
|
+
* @param rhs The 'right-hand' FileSchemaKey.
|
|
208
|
+
*/
|
|
209
|
+
compareSchemaKeyByVersion = (lhs, rhs) => {
|
|
210
|
+
return lhs.compareByVersion(rhs);
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=SchemaFileLocater.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaFileLocater.js","sourceRoot":"","sources":["../../src/SchemaFileLocater.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAyB,SAAS,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAE7F,MAAM,YAAY,GAAG,CAAC,MAAc,EAAE,GAAG,IAAc,EAAE,EAAE;IACzD,OAAO,MAAM,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE;QACrD,OAAO,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,WAAW;YAC3C,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC;YACjB,CAAC,CAAC,KAAK,CAAC;IACZ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,UAAU,GAAG,CAAC,GAAW,EAAE,YAAoB,EAAE,SAAiB,EAAE,EAAE;IAC1E,YAAY,GAAG,YAAY,IAAI,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AS,EAAE,MAAM,CAAC,CAAC;YACxG,IAAI,CAAC,sBAAsB,CAAC,UAAU,EAAE,UAAU,EAAE,oBAAoB,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;YACzG,IAAI,CAAC,sBAAsB,CAAC,UAAU,EAAE,UAAU,EAAE,sBAAsB,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;QAC7G,CAAC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC;IAYD;;;;;OAKG;IACI,yBAAyB,GAAG,CAAC,GAAkB,EAAE,GAAkB,EAAU,EAAE;QACpF,OAAO,GAAG,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC,CAAC;CACH","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\n/** @packageDocumentation\n * @module Locaters\n */\n\nimport * as fs from \"fs\";\nimport { globSync } from \"glob\";\nimport * as path from \"path\";\nimport { Schema, SchemaContext, SchemaKey, SchemaMatchType } from \"@itwin/ecschema-metadata\";\n\nconst formatString = (format: string, ...args: string[]) => {\n return format.replace(/{(\\d+)}/g, (match, theNumber) => {\n return typeof args[theNumber] !== \"undefined\"\n ? args[theNumber]\n : match;\n });\n};\n\nconst padStartEx = (str: string, targetLength: number, padString: string) => {\n targetLength = targetLength >> 0; // truncate if number or convert non-number to 0;\n padString = String((typeof padString !== \"undefined\" ? padString : \" \"));\n if (str.length > targetLength) {\n return String(str);\n } else {\n targetLength = targetLength - str.length;\n if (targetLength > padString.length) {\n padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed\n }\n return padString.slice(0, targetLength) + str;\n }\n};\n\n/**\n * A SchemaKey implementation that aids in identifying Schema files via the\n * addition of two properties: fileName and schemaText. The fileName contains the\n * full path to the file on disk and schemaText is the full string representation\n * of the Schema.\n * @beta\n */\nexport class FileSchemaKey extends SchemaKey {\n // The schema file associated with the SchemaKey\n public fileName: string;\n // The JSON text for the schema loaded\n public schemaText?: string;\n\n /**\n * Initializes a new FileSchemaKey object.\n * @param key The EC SchemaKey identifying the Schema.\n * @param fileName The full path to the Schema file.\n * @param schemaText The string representation of the Schema\n * loaded from disk. Optional.\n */\n constructor(key: SchemaKey, fileName: string, schemaJson?: string) {\n super(key.name, key.version);\n this.fileName = fileName;\n this.schemaText = schemaJson;\n }\n}\n\n/**\n * Abstract class to hold common/overlapping functionality between SchemaJsonFileLocater and SchemaXmlFileLocater\n * @beta - Needs further testing and possibly moved to a separate package.\n */\nexport abstract class SchemaFileLocater {\n public searchPaths: string[];\n\n constructor() {\n this.searchPaths = [];\n }\n\n public async readUtf8FileToString(filePath: string): Promise<string | undefined> {\n return new Promise<string | undefined>((resolve, reject) => {\n fs.readFile(filePath, \"utf-8\", (err, data) => {\n if (err)\n reject(err);\n else\n resolve(data);\n });\n });\n }\n\n public readUtf8FileToStringSync(filePath: string): string | undefined {\n return fs.readFileSync(filePath, \"utf-8\");\n }\n\n public async fileExists(filePath: string): Promise<boolean | undefined> {\n return new Promise<boolean | undefined>((resolve) => {\n fs.access(filePath, fs.constants.F_OK, (err) => {\n resolve(err ? false : true);\n });\n });\n }\n\n public fileExistsSync(filePath: string): boolean | undefined {\n return fs.existsSync(filePath);\n }\n\n /**\n * Adds more search paths used by this locator to find the\n * Schema files.\n * @param schemaPaths An array of search paths to add\n */\n public addSchemaSearchPaths(schemaPaths: string[]) {\n // If the path is not in the schemaPaths array, add it\n for (const schemaPath of schemaPaths)\n this.addSchemaSearchPath(schemaPath);\n }\n\n /**\n * Add one search path used by this locator to find the\n * Schema files.\n * @param schemaPath A search path to add\n */\n public addSchemaSearchPath(schemaPath: string) {\n // If the path is not in the schemaPaths array, add it\n if (!this.searchPaths.find((entry) => entry === schemaPath))\n this.searchPaths.push(schemaPath);\n }\n\n protected abstract getSchemaKey(data: string): SchemaKey;\n\n /**\n * Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey. This method\n * only attempts to find schema files that have no version in the file name.\n * @param foundFiles The collection of SchemaKeys found in the given directory.\n * @param schemaPath The directory in which to search for the Schemas.\n * @param schemaName The short name of the Schema (without version).\n * @param desiredKey The SchemaKey used to find matching Schema files.\n * @param matchType The SchemaMatchType to use when comparing the desiredKey and the keys found during the search.\n * @param format The type of file that the schema key refers to. json or xml\n */\n private addCandidateNoExtSchemaKey(foundFiles: FileSchemaKey[], schemaPath: string, schemaName: string, desiredKey: SchemaKey, matchType: SchemaMatchType, format: string) {\n const fullPath = path.join(schemaPath, `${schemaName}.ecschema.${format}`);\n\n // If the file does not exist, end\n if (!fs.existsSync(fullPath))\n return;\n\n // Read the file\n const file = fs.readFileSync(fullPath);\n if (!file)\n return;\n\n // Get the schema key\n const key = this.getSchemaKey(file.toString());\n\n // If the key matches, put it in foundFiles\n if (key.matches(desiredKey, matchType))\n foundFiles.push(new FileSchemaKey(key, fullPath, file.toString()));\n }\n\n /**\n * Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey\n * @param foundFiles The collection of SchemaKeys found in the given directory\n * @param schemaPath The directory in which to search for the Schemas\n * @param fileFilter The file filter, potentially with wildcards, used to locate the Schema files.\n * @param desiredKey The schemaKey used to find matching Schema files\n * @param matchType The SchemaMatchType to use when comparing the desired Key and the keys found during the search.\n * @param format The type of file that the schema key refers to. json or xml\n */\n private addCandidateSchemaKeys(foundFiles: FileSchemaKey[], schemaPath: string, fileFilter: string, desiredKey: SchemaKey, matchType: SchemaMatchType, format: string) {\n const fullPath = path.join(schemaPath, fileFilter);\n\n const result = globSync(fullPath, { windowsPathsNoEscape: true });\n for (const match of result) {\n let fileName = path.basename(match, (`.ecschema.${format}`));\n // TODO: should this be moved or handled elsewhere?\n // Handles two version file names - SchemaKey.parseString supports only 3 version names.\n if (/[^\\d]\\.\\d?\\d\\.\\d?\\d$/.test(fileName)) {\n const parts = fileName.split(\".\");\n parts.splice(2, 0, \"00\");\n fileName = parts.join(\".\");\n }\n\n const file = fs.readFileSync(match);\n if (!file)\n continue;\n\n const schemaKey = SchemaKey.parseString(fileName);\n if (schemaKey.matches(desiredKey, matchType))\n foundFiles.push(new FileSchemaKey(schemaKey, match, file.toString()));\n }\n }\n\n /**\n * Attempts to find all Schema files in the configurable search paths that match\n * the desired SchemaKey.\n * @param desiredKey The SchemaKey to match.\n * @param matchType The SchemaMatchType.\n * @param format The type of file that the schema key refers to. json or xml\n */\n protected findEligibleSchemaKeys(desiredKey: SchemaKey, matchType: SchemaMatchType, format: string): FileSchemaKey[] {\n const foundFiles = new Array<FileSchemaKey>();\n\n let twoVersionSuffix: string;\n let threeVersionSuffix: string;\n const readVersion = desiredKey.readVersion.toString();\n const writeVersion = desiredKey.writeVersion.toString();\n const minorVersion = desiredKey.minorVersion.toString();\n\n if (matchType === SchemaMatchType.Latest) {\n twoVersionSuffix = (`.*.*.ecschema.${format}`);\n threeVersionSuffix = (`.*.*.*.ecschema.${format}`);\n } else if (matchType === SchemaMatchType.LatestWriteCompatible) {\n twoVersionSuffix = formatString(`.{0}.*.ecschema.${format}`, padStartEx(readVersion, 2, \"0\"));\n threeVersionSuffix = formatString(`.{0}.{1}.*.ecschema.${format}`, padStartEx(readVersion, 2, \"0\"), padStartEx(writeVersion, 2, \"0\"));\n } else if (matchType === SchemaMatchType.LatestReadCompatible) {\n twoVersionSuffix = formatString(`.{0}.*.ecschema.${format}`, padStartEx(readVersion, 2, \"0\"));\n threeVersionSuffix = formatString(`.{0}.*.*.ecschema.${format}`, padStartEx(readVersion, 2, \"0\"));\n } else {\n twoVersionSuffix = formatString(`.{0}.{1}.ecschema.${format}`, padStartEx(readVersion, 2, \"0\"), padStartEx(writeVersion, 2, \"0\"));\n threeVersionSuffix = formatString(`.{0}.{1}.{2}.ecschema.${format}`, padStartEx(readVersion, 2, \"0\"), padStartEx(writeVersion, 2, \"0\"), padStartEx(minorVersion, 2, \"0\"));\n }\n\n const twoVersionExpression = desiredKey.name + twoVersionSuffix;\n const threeVersionExpression = desiredKey.name + threeVersionSuffix;\n\n for (const searchPath of this.searchPaths) {\n this.addCandidateNoExtSchemaKey(foundFiles, searchPath, desiredKey.name, desiredKey, matchType, format);\n this.addCandidateSchemaKeys(foundFiles, searchPath, twoVersionExpression, desiredKey, matchType, format);\n this.addCandidateSchemaKeys(foundFiles, searchPath, threeVersionExpression, desiredKey, matchType, format);\n }\n\n return foundFiles;\n }\n\n /**\n * Attempts to retrieve a Schema with the given SchemaKey by using the configured search paths\n * to locate the XML Schema file from the file system. Returns only Schemas from XML files with\n * their keys populated.\n * @param key The SchemaKey of the Schema to retrieve.\n * @param matchType The SchemaMatchType.\n * @param context The SchemaContext that will control the lifetime of the schema.\n */\n public abstract getSchema(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<Schema | undefined>;\n\n /**\n * Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the\n * left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.\n * @param lhs The 'left-hand' FileSchemaKey.\n * @param rhs The 'right-hand' FileSchemaKey.\n */\n public compareSchemaKeyByVersion = (lhs: FileSchemaKey, rhs: FileSchemaKey): number => {\n return lhs.compareByVersion(rhs);\n };\n}\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** @packageDocumentation
|
|
2
|
+
* @module Locaters
|
|
3
|
+
*/
|
|
4
|
+
import { ISchemaLocater, Schema, SchemaContext, SchemaInfo, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
|
|
5
|
+
import { SchemaFileLocater } from "./SchemaFileLocater";
|
|
6
|
+
/**
|
|
7
|
+
* A SchemaLocator implementation for locating JSON Schema files
|
|
8
|
+
* from the file system using configurable search paths.
|
|
9
|
+
* @beta
|
|
10
|
+
*/
|
|
11
|
+
export declare class SchemaJsonFileLocater extends SchemaFileLocater implements ISchemaLocater {
|
|
12
|
+
/**
|
|
13
|
+
* Constructs a SchemaKey based on the information in the Schema JSON
|
|
14
|
+
* @param data The Schema JSON as a string
|
|
15
|
+
*/
|
|
16
|
+
protected getSchemaKey(data: string): SchemaKey;
|
|
17
|
+
/**
|
|
18
|
+
* Attempts to retrieve a Schema with the given SchemaKey by using the configured
|
|
19
|
+
* search paths to locate the JSON schema file from the file system.
|
|
20
|
+
* @param key The SchemaKey of the Schema to retrieve.
|
|
21
|
+
* @param matchType The SchemaMatchType.
|
|
22
|
+
* @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
|
|
23
|
+
*/
|
|
24
|
+
getSchema(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<Schema | undefined>;
|
|
25
|
+
/**
|
|
26
|
+
* Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.
|
|
27
|
+
* The fully loaded schema can be gotten later from the context using the getCachedSchema method.
|
|
28
|
+
* @param schemaKey The SchemaKey describing the schema to get from the cache.
|
|
29
|
+
* @param matchType The match type to use when locating the schema
|
|
30
|
+
* @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
|
|
31
|
+
*/
|
|
32
|
+
getSchemaInfo(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<SchemaInfo | undefined>;
|
|
33
|
+
/**
|
|
34
|
+
* Attempts to retrieve a Schema with the given SchemaKey by using the configured
|
|
35
|
+
* search paths to locate the JSON schema file from the file system.
|
|
36
|
+
* @param key The SchemaKey of the Schema to retrieve.
|
|
37
|
+
* @param matchType The SchemaMatchType
|
|
38
|
+
* @param context The SchemaContext that will control the lifetime of the schema.
|
|
39
|
+
*/
|
|
40
|
+
getSchemaSync(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Schema | undefined;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=SchemaJsonFileLocater.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaJsonFileLocater.d.ts","sourceRoot":"","sources":["../../src/SchemaJsonFileLocater.ts"],"names":[],"mappings":"AAIA;;GAEG;AAIH,OAAO,EACuC,cAAc,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAC1H,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAiB,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAEvE;;;;GAIG;AACH,qBAAa,qBAAsB,SAAQ,iBAAkB,YAAW,cAAc;IAEpF;;;OAGG;IACH,SAAS,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS;IAmB/C;;;;;;OAMG;IACU,SAAS,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAO7H;;;;;;QAMI;IACS,aAAa,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAsBrI;;;;;;OAMG;IACI,aAAa,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,MAAM,GAAG,SAAS;CAsBnH"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
3
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
/** @packageDocumentation
|
|
6
|
+
* @module Locaters
|
|
7
|
+
*/
|
|
8
|
+
import * as fs from "fs";
|
|
9
|
+
import * as path from "path";
|
|
10
|
+
import { ECObjectsError, ECObjectsStatus, ECVersion, Schema, SchemaKey, } from "@itwin/ecschema-metadata";
|
|
11
|
+
import { SchemaFileLocater } from "./SchemaFileLocater";
|
|
12
|
+
/**
|
|
13
|
+
* A SchemaLocator implementation for locating JSON Schema files
|
|
14
|
+
* from the file system using configurable search paths.
|
|
15
|
+
* @beta
|
|
16
|
+
*/
|
|
17
|
+
export class SchemaJsonFileLocater extends SchemaFileLocater {
|
|
18
|
+
/**
|
|
19
|
+
* Constructs a SchemaKey based on the information in the Schema JSON
|
|
20
|
+
* @param data The Schema JSON as a string
|
|
21
|
+
*/
|
|
22
|
+
getSchemaKey(data) {
|
|
23
|
+
const dataJson = JSON.parse(data);
|
|
24
|
+
// Check if the name is present
|
|
25
|
+
if (!(dataJson.name))
|
|
26
|
+
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `Could not retrieve the ECSchema name in the given file.`);
|
|
27
|
+
// Check if versions is present
|
|
28
|
+
if (!(dataJson.version))
|
|
29
|
+
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `Could not parse the ECSchema version in the given file.`);
|
|
30
|
+
// Get the name and version from the JSON
|
|
31
|
+
const schemaName = dataJson.name;
|
|
32
|
+
const schemaVersion = dataJson.version;
|
|
33
|
+
const key = new SchemaKey(schemaName.toString(), ECVersion.fromString(schemaVersion.toString()));
|
|
34
|
+
return key;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Attempts to retrieve a Schema with the given SchemaKey by using the configured
|
|
38
|
+
* search paths to locate the JSON schema file from the file system.
|
|
39
|
+
* @param key The SchemaKey of the Schema to retrieve.
|
|
40
|
+
* @param matchType The SchemaMatchType.
|
|
41
|
+
* @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
|
|
42
|
+
*/
|
|
43
|
+
async getSchema(schemaKey, matchType, context) {
|
|
44
|
+
await this.getSchemaInfo(schemaKey, matchType, context);
|
|
45
|
+
const schema = await context.getCachedSchema(schemaKey, matchType);
|
|
46
|
+
return schema;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.
|
|
50
|
+
* The fully loaded schema can be gotten later from the context using the getCachedSchema method.
|
|
51
|
+
* @param schemaKey The SchemaKey describing the schema to get from the cache.
|
|
52
|
+
* @param matchType The match type to use when locating the schema
|
|
53
|
+
* @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.
|
|
54
|
+
*/
|
|
55
|
+
async getSchemaInfo(schemaKey, matchType, context) {
|
|
56
|
+
// Grab all schema files that match the schema key
|
|
57
|
+
const candidates = this.findEligibleSchemaKeys(schemaKey, matchType, "json");
|
|
58
|
+
if (!candidates || candidates.length === 0)
|
|
59
|
+
return undefined;
|
|
60
|
+
const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
|
|
61
|
+
const schemaPath = maxCandidate.fileName;
|
|
62
|
+
// Load the file
|
|
63
|
+
if (!await this.fileExists(schemaPath))
|
|
64
|
+
return undefined;
|
|
65
|
+
const schemaText = await this.readUtf8FileToString(schemaPath);
|
|
66
|
+
if (!schemaText)
|
|
67
|
+
return undefined;
|
|
68
|
+
this.addSchemaSearchPaths([path.dirname(schemaPath)]);
|
|
69
|
+
return Schema.startLoadingFromJson(schemaText, context);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Attempts to retrieve a Schema with the given SchemaKey by using the configured
|
|
73
|
+
* search paths to locate the JSON schema file from the file system.
|
|
74
|
+
* @param key The SchemaKey of the Schema to retrieve.
|
|
75
|
+
* @param matchType The SchemaMatchType
|
|
76
|
+
* @param context The SchemaContext that will control the lifetime of the schema.
|
|
77
|
+
*/
|
|
78
|
+
getSchemaSync(schemaKey, matchType, context) {
|
|
79
|
+
// Grab all schema files that match the schema key
|
|
80
|
+
const candidates = this.findEligibleSchemaKeys(schemaKey, matchType, "json");
|
|
81
|
+
if (!candidates || candidates.length === 0)
|
|
82
|
+
return undefined;
|
|
83
|
+
const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];
|
|
84
|
+
const schemaPath = maxCandidate.fileName;
|
|
85
|
+
// Load the file
|
|
86
|
+
if (!fs.existsSync(schemaPath))
|
|
87
|
+
return undefined;
|
|
88
|
+
const schemaText = fs.readFileSync(schemaPath, "utf-8");
|
|
89
|
+
if (!schemaText)
|
|
90
|
+
return undefined;
|
|
91
|
+
this.addSchemaSearchPaths([path.dirname(schemaPath)]);
|
|
92
|
+
const schema = Schema.fromJsonSync(schemaText, context);
|
|
93
|
+
return schema;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=SchemaJsonFileLocater.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaJsonFileLocater.js","sourceRoot":"","sources":["../../src/SchemaJsonFileLocater.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EACL,cAAc,EAAE,eAAe,EAAE,SAAS,EAAkB,MAAM,EAA6B,SAAS,GACzG,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAiB,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAEvE;;;;GAIG;AACH,MAAM,OAAO,qBAAsB,SAAQ,iBAAiB;IAE1D;;;OAGG;IACO,YAAY,CAAC,IAAY;QACjC,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAElC,+BAA+B;QAC/B,IAAI,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;YAClB,MAAM,IAAI,cAAc,CAAC,eAAe,CAAC,aAAa,EAAE,yDAAyD,CAAC,CAAC;QAErH,+BAA+B;QAC/B,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC;YACrB,MAAM,IAAI,cAAc,CAAC,eAAe,CAAC,aAAa,EAAE,yDAAyD,CAAC,CAAC;QAErH,yCAAyC;QACzC,MAAM,UAAU,GAAG,QAAQ,CAAC,IAAI,CAAC;QACjC,MAAM,aAAa,GAAG,QAAQ,CAAC,OAAO,CAAC;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O,MAAM,CAAC,oBAAoB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAC1D,CAAC;IAED;;;;;;OAMG;IACI,aAAa,CAAC,SAAoB,EAAE,SAA0B,EAAE,OAAsB;QAC3F,kDAAkD;QAClD,MAAM,UAAU,GAAoB,IAAI,CAAC,sBAAsB,CAAC,SAAS,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;QAC9F,IAAI,CAAC,UAAU,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC;YACxC,OAAO,SAAS,CAAC;QAEnB,MAAM,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAC5F,MAAM,UAAU,GAAG,YAAY,CAAC,QAAQ,CAAC;QAEzC,gBAAgB;QAChB,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC;YAC5B,OAAO,SAAS,CAAC;QAEnB,MAAM,UAAU,GAAG,EAAE,CAAC,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QACxD,IAAI,CAAC,UAAU;YACb,OAAO,SAAS,CAAC;QAEnB,IAAI,CAAC,oBAAoB,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAEtD,MAAM,MAAM,GAAG,MAAM,CAAC,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QACxD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\n/** @packageDocumentation\n * @module Locaters\n */\n\nimport * as fs from \"fs\";\nimport * as path from \"path\";\nimport {\n ECObjectsError, ECObjectsStatus, ECVersion, ISchemaLocater, Schema, SchemaContext, SchemaInfo, SchemaKey, SchemaMatchType,\n} from \"@itwin/ecschema-metadata\";\nimport { FileSchemaKey, SchemaFileLocater } from \"./SchemaFileLocater\";\n\n/**\n * A SchemaLocator implementation for locating JSON Schema files\n * from the file system using configurable search paths.\n * @beta\n */\nexport class SchemaJsonFileLocater extends SchemaFileLocater implements ISchemaLocater {\n\n /**\n * Constructs a SchemaKey based on the information in the Schema JSON\n * @param data The Schema JSON as a string\n */\n protected getSchemaKey(data: string): SchemaKey {\n const dataJson = JSON.parse(data);\n\n // Check if the name is present\n if (!(dataJson.name))\n throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `Could not retrieve the ECSchema name in the given file.`);\n\n // Check if versions is present\n if (!(dataJson.version))\n throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `Could not parse the ECSchema version in the given file.`);\n\n // Get the name and version from the JSON\n const schemaName = dataJson.name;\n const schemaVersion = dataJson.version;\n\n const key = new SchemaKey(schemaName.toString(), ECVersion.fromString(schemaVersion.toString()));\n return key;\n }\n\n /**\n * Attempts to retrieve a Schema with the given SchemaKey by using the configured\n * search paths to locate the JSON schema file from the file system.\n * @param key The SchemaKey of the Schema to retrieve.\n * @param matchType The SchemaMatchType.\n * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.\n */\n public async getSchema(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<Schema | undefined> {\n await this.getSchemaInfo(schemaKey, matchType, context);\n\n const schema = await context.getCachedSchema(schemaKey, matchType);\n return schema;\n }\n\n /**\n * Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.\n * The fully loaded schema can be gotten later from the context using the getCachedSchema method.\n * @param schemaKey The SchemaKey describing the schema to get from the cache.\n * @param matchType The match type to use when locating the schema\n * @param context The SchemaContext that will control the lifetime of the schema and holds the schema's references, if they exist.\n */\n public async getSchemaInfo(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<SchemaInfo | undefined> {\n // Grab all schema files that match the schema key\n const candidates: FileSchemaKey[] = this.findEligibleSchemaKeys(schemaKey, matchType, \"json\");\n if (!candidates || candidates.length === 0)\n return undefined;\n\n const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];\n const schemaPath = maxCandidate.fileName;\n\n // Load the file\n if (!await this.fileExists(schemaPath))\n return undefined;\n\n const schemaText = await this.readUtf8FileToString(schemaPath);\n if (!schemaText)\n return undefined;\n\n this.addSchemaSearchPaths([path.dirname(schemaPath)]);\n\n return Schema.startLoadingFromJson(schemaText, context);\n }\n\n /**\n * Attempts to retrieve a Schema with the given SchemaKey by using the configured\n * search paths to locate the JSON schema file from the file system.\n * @param key The SchemaKey of the Schema to retrieve.\n * @param matchType The SchemaMatchType\n * @param context The SchemaContext that will control the lifetime of the schema.\n */\n public getSchemaSync(schemaKey: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Schema | undefined {\n // Grab all schema files that match the schema key\n const candidates: FileSchemaKey[] = this.findEligibleSchemaKeys(schemaKey, matchType, \"json\");\n if (!candidates || candidates.length === 0)\n return undefined;\n\n const maxCandidate = candidates.sort(this.compareSchemaKeyByVersion)[candidates.length - 1];\n const schemaPath = maxCandidate.fileName;\n\n // Load the file\n if (!fs.existsSync(schemaPath))\n return undefined;\n\n const schemaText = fs.readFileSync(schemaPath, \"utf-8\");\n if (!schemaText)\n return undefined;\n\n this.addSchemaSearchPaths([path.dirname(schemaPath)]);\n\n const schema = Schema.fromJsonSync(schemaText, context);\n return schema;\n }\n}\n"]}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/** @packageDocumentation
|
|
2
|
+
* @module Locaters
|
|
3
|
+
*/
|
|
4
|
+
import { Schema, SchemaContext, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
|
|
5
|
+
/**
|
|
6
|
+
* A SchemaKey implementation that aids in identifying Schema strings via the
|
|
7
|
+
* addition of the schemaText property. The schemaText is the full XML string
|
|
8
|
+
* representation of the Schema.
|
|
9
|
+
* @beta
|
|
10
|
+
*/
|
|
11
|
+
export declare class StringSchemaKey extends SchemaKey {
|
|
12
|
+
schemaText: string;
|
|
13
|
+
/**
|
|
14
|
+
* Initializes a new StringSchemaKey object.
|
|
15
|
+
* @param key The EC SchemaKey identifying the Schema.
|
|
16
|
+
* @param schemaText The string representation of the Schema
|
|
17
|
+
*/
|
|
18
|
+
constructor(key: SchemaKey, schemaText: string);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Abstract class to hold common/overlapping functionality between SchemaJsonStringLocater and SchemaXmlStringLocater
|
|
22
|
+
* @beta
|
|
23
|
+
*/
|
|
24
|
+
export declare abstract class SchemaStringLocater {
|
|
25
|
+
schemaStrings: string[];
|
|
26
|
+
constructor();
|
|
27
|
+
/**
|
|
28
|
+
* Adds schema strings used by this locator to find the
|
|
29
|
+
* Schemas.
|
|
30
|
+
* @param schemaPaths An array of Schema strings to add
|
|
31
|
+
*/
|
|
32
|
+
addSchemaStrings(schemaStrings: string[]): void;
|
|
33
|
+
/**
|
|
34
|
+
* Adds a schema string used by this locator to locate and load Schemas.
|
|
35
|
+
* @param schemaText The text of the Schema
|
|
36
|
+
*/
|
|
37
|
+
addSchemaString(schemaString: string): void;
|
|
38
|
+
protected abstract getSchemaKey(data: string): SchemaKey;
|
|
39
|
+
/**
|
|
40
|
+
* Attempts to find all Schema strings that match the desired SchemaKey.
|
|
41
|
+
* @param desiredKey The SchemaKey to match.
|
|
42
|
+
* @param matchType The SchemaMatchType.
|
|
43
|
+
*/
|
|
44
|
+
protected findEligibleSchemaKeys(desiredKey: SchemaKey, matchType: SchemaMatchType): StringSchemaKey[];
|
|
45
|
+
abstract getSchema(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<Schema | undefined>;
|
|
46
|
+
/**
|
|
47
|
+
* Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the
|
|
48
|
+
* left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.
|
|
49
|
+
* @param lhs The 'left-hand' StringSchemaKey.
|
|
50
|
+
* @param rhs The 'right-hand' StringSchemaKey.
|
|
51
|
+
*/
|
|
52
|
+
compareSchemaKeyByVersion: (lhs: StringSchemaKey, rhs: StringSchemaKey) => number;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=SchemaStringLocater.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaStringLocater.d.ts","sourceRoot":"","sources":["../../src/SchemaStringLocater.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAE7F;;;;;GAKG;AACH,qBAAa,eAAgB,SAAQ,SAAS;IAErC,UAAU,EAAE,MAAM,CAAC;IAE1B;;;;OAIG;gBACS,GAAG,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM;CAI/C;AAED;;;GAGG;AACH,8BAAsB,mBAAmB;IAChC,aAAa,EAAE,MAAM,EAAE,CAAC;;IAM/B;;;;OAIG;IACI,gBAAgB,CAAC,aAAa,EAAE,MAAM,EAAE;IAM/C;;;OAGG;IACI,eAAe,CAAC,YAAY,EAAE,MAAM;IAO3C,SAAS,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS;IAExD;;;;OAIG;IACH,SAAS,CAAC,sBAAsB,CAAC,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,GAAG,eAAe,EAAE;aAetF,SAAS,CAAC,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAE1H;;;;;OAKG;IACI,yBAAyB,QAAS,eAAe,OAAO,eAAe,KAAG,MAAM,CAErF;CACH"}
|