Skip to content
代码片段 群组 项目
提交 119f8e81 编辑于 作者: Timotej Ecimovic's avatar Timotej Ecimovic
浏览文件

Implement the types options in gen-template.json.

上级 58dae160
No related branches found
No related tags found
无相关合并请求
...@@ -71,9 +71,10 @@ The following sections describe the details specific for use in certain more com ...@@ -71,9 +71,10 @@ The following sections describe the details specific for use in certain more com
Options are loaded into the database, keyed to the given generation package. While you can always use them in templates via the `template_options` key, certain keys have special meanings. Options are loaded into the database, keyed to the given generation package. While you can always use them in templates via the `template_options` key, certain keys have special meanings.
The following is the list of special meanings: The following is the list of special meanings:
| Category | Meaning | | Category | Meaning |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------- | | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| manufacturerCodes | This category backs a code/label map of valid manufacturer codes. They can be used in UI when selecting manufacturers. | | manufacturerCodes | This category backs a code/label map of valid manufacturer codes. They can be used in UI when selecting manufacturers. |
| types | This category backs a code/label map of type mappings. For a code, you can use any of the atomic or other ZCL types, and label is an override for a generator, detemining what type it shuld translate into |
## Template key: zcl ## Template key: zcl
......
...@@ -85,6 +85,17 @@ This content is output ONLY if there are no clusters at all. ...@@ -85,6 +85,17 @@ This content is output ONLY if there are no clusters at all.
``` ```
## Global context
Whichever helpers get executed they all contain `global` inside their context that will always contain:
- `db`: connection to the database being used
- `sessionId`: session id for the user data being used
- `genTemplatePackageId`: toplevel package id for gen-template.json file, used for querying options and other
- `zclPackageId`: populated after you call `templateUtil.ensureZclPackageId()` the first time, used for querying the ZCL static data.
Whenever a context switches, the switch will preserve this `global` object inside the context for helpers to access.
## Helper API ## Helper API
Helper API is listed in the generated JSDoc documentation under "Template API modules". There are many helpers that can be used for all kind of different cases of ZCL generation. Helper API is listed in the generated JSDoc documentation under "Template API modules". There are many helpers that can be used for all kind of different cases of ZCL generation.
...@@ -30,6 +30,7 @@ exports.map = { ...@@ -30,6 +30,7 @@ exports.map = {
crc: x.CRC, crc: x.CRC,
type: x.TYPE, type: x.TYPE,
version: x.VERSION, version: x.VERSION,
parentId: x.PARENT_PACKAGE_REF,
} }
}, },
options: (x) => { options: (x) => {
......
...@@ -145,7 +145,7 @@ function getPackageByPackageId(db, packageId) { ...@@ -145,7 +145,7 @@ function getPackageByPackageId(db, packageId) {
return dbApi return dbApi
.dbGet( .dbGet(
db, db,
'SELECT PACKAGE_ID, PATH, TYPE, CRC, VERSION FROM PACKAGE WHERE PACKAGE_ID = ?', 'SELECT PACKAGE_ID, PARENT_PACKAGE_REF, PATH, TYPE, CRC, VERSION FROM PACKAGE WHERE PACKAGE_ID = ?',
[packageId] [packageId]
) )
.then(dbMapping.map.package) .then(dbMapping.map.package)
......
...@@ -1473,7 +1473,7 @@ function selectAtomicType(db, packageId, typeName) { ...@@ -1473,7 +1473,7 @@ function selectAtomicType(db, packageId, typeName) {
.dbGet( .dbGet(
db, db,
'SELECT ATOMIC_IDENTIFIER, NAME, DESCRIPTION, ATOMIC_SIZE FROM ATOMIC WHERE PACKAGE_REF = ? AND NAME = ?', 'SELECT ATOMIC_IDENTIFIER, NAME, DESCRIPTION, ATOMIC_SIZE FROM ATOMIC WHERE PACKAGE_REF = ? AND NAME = ?',
[packageId, typeName.toLowerCase()] [packageId, typeName == null ? typeName : typeName.toLowerCase()]
) )
.then(dbMapping.map.atomic) .then(dbMapping.map.atomic)
} }
......
...@@ -205,13 +205,17 @@ function loadTemplates(db, genTemplatesJson) { ...@@ -205,13 +205,17 @@ function loadTemplates(db, genTemplatesJson) {
* Generates all the templates inside a toplevel package. * Generates all the templates inside a toplevel package.
* *
* @param {*} genResult * @param {*} genResult
* @param {*} pkg * @param {*} genTemplateJsonPkg Package that points to genTemplate.json file
* @param {*} generateOnly if NULL then generate all templates, else only generate template whose out file name matches this. * @param {*} generateOnly if NULL then generate all templates, else only generate template whose out file name matches this.
* @returns Promise that resolves with genResult, that contains all the generated templates, keyed by their 'output' * @returns Promise that resolves with genResult, that contains all the generated templates, keyed by their 'output'
*/ */
function generateAllTemplates(genResult, pkg, generateOnly = null) { function generateAllTemplates(
genResult,
genTemplateJsonPkg,
generateOnly = null
) {
return queryPackage return queryPackage
.getPackageByParent(genResult.db, pkg.id) .getPackageByParent(genResult.db, genTemplateJsonPkg.id)
.then((packages) => { .then((packages) => {
var generationPromises = [] var generationPromises = []
var helperPromises = [] var helperPromises = []
...@@ -219,7 +223,11 @@ function generateAllTemplates(genResult, pkg, generateOnly = null) { ...@@ -219,7 +223,11 @@ function generateAllTemplates(genResult, pkg, generateOnly = null) {
if (singlePkg.type == dbEnum.packageType.genSingleTemplate) { if (singlePkg.type == dbEnum.packageType.genSingleTemplate) {
if (generateOnly == null || generateOnly == singlePkg.version) { if (generateOnly == null || generateOnly == singlePkg.version) {
generationPromises.push( generationPromises.push(
generateSingleTemplate(genResult, singlePkg) generateSingleTemplate(
genResult,
singlePkg,
genTemplateJsonPkg.id
)
) )
} }
} else if (singlePkg.type == dbEnum.packageType.genHelper) { } else if (singlePkg.type == dbEnum.packageType.genHelper) {
...@@ -240,14 +248,23 @@ function generateAllTemplates(genResult, pkg, generateOnly = null) { ...@@ -240,14 +248,23 @@ function generateAllTemplates(genResult, pkg, generateOnly = null) {
* Function that generates a single package and adds it to the generation result. * Function that generates a single package and adds it to the generation result.
* *
* @param {*} genResult * @param {*} genResult
* @param {*} pkg * @param {*} singleTemplatePkg Single template package.
* @returns promise that resolves with the genResult, with newly generated content added. * @returns promise that resolves with the genResult, with newly generated content added.
*/ */
function generateSingleTemplate(genResult, pkg) { function generateSingleTemplate(
genResult,
singleTemplatePkg,
genTemplateJsonPackageId
) {
return templateEngine return templateEngine
.produceContent(genResult.db, genResult.sessionId, pkg) .produceContent(
genResult.db,
genResult.sessionId,
singleTemplatePkg,
genTemplateJsonPackageId
)
.then((data) => { .then((data) => {
genResult.content[pkg.version] = data genResult.content[singleTemplatePkg.version] = data
genResult.partial = true genResult.partial = true
return genResult return genResult
}) })
...@@ -257,7 +274,7 @@ function generateSingleTemplate(genResult, pkg) { ...@@ -257,7 +274,7 @@ function generateSingleTemplate(genResult, pkg) {
* Main API function to generate stuff. * Main API function to generate stuff.
* *
* @param {*} db Database * @param {*} db Database
* @param {*} packageId packageId Template package id * @param {*} packageId packageId Template package id. It can be either single template or gen template json.
* @returns Promise that resolves into a generation result. * @returns Promise that resolves into a generation result.
*/ */
function generate(db, sessionId, packageId, generateOnly = null) { function generate(db, sessionId, packageId, generateOnly = null) {
...@@ -271,7 +288,7 @@ function generate(db, sessionId, packageId, generateOnly = null) { ...@@ -271,7 +288,7 @@ function generate(db, sessionId, packageId, generateOnly = null) {
if (pkg.type === dbEnum.packageType.genTemplatesJson) { if (pkg.type === dbEnum.packageType.genTemplatesJson) {
return generateAllTemplates(genResult, pkg, generateOnly) return generateAllTemplates(genResult, pkg, generateOnly)
} else if (pkg.type === dbEnum.packageType.genSingleTemplate) { } else if (pkg.type === dbEnum.packageType.genSingleTemplate) {
return generateSingleTemplate(genResult, pkg) return generateSingleTemplate(genResult, pkg, pkg.parentId)
} else { } else {
throw `Invalid package type: ${pkg.type}` throw `Invalid package type: ${pkg.type}`
} }
...@@ -422,7 +439,7 @@ function generateSingleFileForPreview(db, sessionId, outFileName) { ...@@ -422,7 +439,7 @@ function generateSingleFileForPreview(db, sessionId, outFileName) {
} }
exports.loadTemplates = loadTemplates exports.loadTemplates = loadTemplates
exports.generate = generate
exports.generateAndWriteFiles = generateAndWriteFiles exports.generateAndWriteFiles = generateAndWriteFiles
exports.generateSingleFileForPreview = generateSingleFileForPreview exports.generateSingleFileForPreview = generateSingleFileForPreview
exports.contentIndexer = contentIndexer exports.contentIndexer = contentIndexer
exports.generate = generate
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
*/ */
const queryZcl = require('../db/query-zcl.js') const queryZcl = require('../db/query-zcl.js')
const queryPackage = require('../db/query-package.js')
const templateUtil = require('./template-util.js') const templateUtil = require('./template-util.js')
const bin = require('../util/bin.js') const bin = require('../util/bin.js')
const { logInfo } = require('../util/env.js') const { logInfo } = require('../util/env.js')
...@@ -196,9 +197,19 @@ function asUnderlyingType(value) { ...@@ -196,9 +197,19 @@ function asUnderlyingType(value) {
}) })
.then((atomic) => { .then((atomic) => {
if (atomic == null) { if (atomic == null) {
return `EmberAf${value}` return `/* TYPE WARNING: not a valid atomic type: ${value} */ ${value}`
} else { } else {
return defaultAtomicType(atomic) return queryPackage
.selectSpecificOptionValue(
this.global.db,
this.global.genTemplatePackageId,
'types',
atomic.name
)
.then((opt) => {
if (opt == null) return defaultAtomicType(atomic)
else return opt.optionLabel
})
} }
}) })
} }
......
...@@ -31,14 +31,14 @@ const templateCompileOptions = { ...@@ -31,14 +31,14 @@ const templateCompileOptions = {
const precompiledTemplates = {} const precompiledTemplates = {}
function produceCompiledTemplate(singlePkg) { function produceCompiledTemplate(singleTemplatePkg) {
initializeGlobalHelpers() initializeGlobalHelpers()
if (singlePkg.id in precompiledTemplates) if (singleTemplatePkg.id in precompiledTemplates)
return Promise.resolve(precompiledTemplates[singlePkg.id]) return Promise.resolve(precompiledTemplates[singleTemplatePkg.id])
else else
return fsPromise.readFile(singlePkg.path, 'utf8').then((data) => { return fsPromise.readFile(singleTemplatePkg.path, 'utf8').then((data) => {
var template = handlebars.compile(data, templateCompileOptions) var template = handlebars.compile(data, templateCompileOptions)
precompiledTemplates[singlePkg.id] = template precompiledTemplates[singleTemplatePkg.id] = template
return template return template
}) })
} }
...@@ -51,13 +51,19 @@ function produceCompiledTemplate(singlePkg) { ...@@ -51,13 +51,19 @@ function produceCompiledTemplate(singlePkg) {
* @param {*} singlePkg * @param {*} singlePkg
* @returns Promise that resolves with the 'utf8' string that contains the generated content. * @returns Promise that resolves with the 'utf8' string that contains the generated content.
*/ */
function produceContent(db, sessionId, singlePkg) { function produceContent(
return produceCompiledTemplate(singlePkg).then((template) => db,
sessionId,
singleTemplatePkg,
genTemplateJsonPackageId
) {
return produceCompiledTemplate(singleTemplatePkg).then((template) =>
template({ template({
global: { global: {
db: db, db: db,
sessionId: sessionId, sessionId: sessionId,
promises: [], promises: [],
genTemplatePackageId: genTemplateJsonPackageId,
}, },
}) })
) )
......
...@@ -10,7 +10,10 @@ ...@@ -10,7 +10,10 @@
"code5": "label5" "code5": "label5"
}, },
"externalOption": "externalOptions.json", "externalOption": "externalOptions.json",
"cli": "cli.json" "cli": "cli.json",
"types": {
"bacnet_oid": "bacnet_type_t"
}
}, },
"helpers": ["addon-helper.js"], "helpers": ["addon-helper.js"],
"templates": [ "templates": [
......
...@@ -4,4 +4,6 @@ Header:{{zap_header}} ...@@ -4,4 +4,6 @@ Header:{{zap_header}}
SessionId: {{global.sessionId}} SessionId: {{global.sessionId}}
Addon: {{test_addon_helper}} Addon: {{test_addon_helper}}
\ No newline at end of file
Strange type: {{asUnderlyingType "bacnet_oid"}}
\ No newline at end of file
...@@ -115,6 +115,7 @@ test( ...@@ -115,6 +115,7 @@ test(
expect(genResult.content).not.toBeNull() expect(genResult.content).not.toBeNull()
var simpleTest = genResult.content['simple-test.out'] var simpleTest = genResult.content['simple-test.out']
expect(simpleTest.startsWith('Test template file.')).toBeTruthy() expect(simpleTest.startsWith('Test template file.')).toBeTruthy()
expect(simpleTest.includes('Strange type: bacnet_type_t')).toBeTruthy()
}), }),
genTimeout genTimeout
) )
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册