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

Clean up some convoluted loading logic.

上级 fd029a05
No related branches found
No related tags found
无相关合并请求
......@@ -322,6 +322,33 @@ function createAbsolutePath(relativePath, relativity, zapFilePath) {
return relativePath
}
/**
* This method takes an array of root locations and a relative path.
* It will attempt to locate an absolute file at the path, combining
* the root location and a relative path, until a file is found and returned.
*
* If none of the combined root locations and relative paths results
* in an actual file, null is returned.
*
* @param {*} rootFileLocations Array of root file locations, typically directories
* @param {*} relativeFilePath Relative path
* @returns A fully resolved path that exists, or null if none is available.
*/
function locateRelativeFilePath(rootFileLocations, relativeFilePath) {
if (relativeFilePath) {
for (var i = 0; i < rootFileLocations.length; i++) {
var resolvedFile = path.resolve(
rootFileLocations[i],
relativeFilePath.trim()
)
if (fs.existsSync(resolvedFile)) {
return resolvedFile
}
}
}
return null
}
/**
* Returns a promise of an execution of an external program.
*
......@@ -370,3 +397,4 @@ exports.sessionReport = sessionReport
exports.executePromisesSequentially = executePromisesSequentially
exports.createAbsolutePath = createAbsolutePath
exports.executeExternalProgram = executeExternalProgram
exports.locateRelativeFilePath = locateRelativeFilePath
......@@ -37,6 +37,7 @@ function collectDataFromJsonFile(ctx) {
env.logInfo(`Collecting ZCL files from JSON file: ${ctx.metadataFile}`)
return new Promise((resolve, reject) => {
var obj = JSON.parse(ctx.data)
var f
var fileLocations
if (Array.isArray(obj.xmlRoot)) {
......@@ -47,28 +48,24 @@ function collectDataFromJsonFile(ctx) {
fileLocations = [path.join(path.dirname(ctx.metadataFile), obj.xmlRoot)]
}
var zclFiles = []
obj.xmlFile.forEach((f) => {
mapOverFileLocations(fileLocations, f, (x) => {
zclFiles.push(x)
})
obj.xmlFile.forEach((xmlF) => {
f = util.locateRelativeFilePath(fileLocations, xmlF)
if (f != null) zclFiles.push(f)
})
ctx.zclFiles = zclFiles
// Manufacturers XML file.
mapOverFileLocations(fileLocations, obj.manufacturersXml, (x) => {
ctx.manufacturersXml = x
})
f = util.locateRelativeFilePath(fileLocations, obj.manufacturersXml)
if (f != null) ctx.manufacturersXml = f
// Zcl XSD file
mapOverFileLocations(fileLocations, obj.zclSchema, (x) => {
ctx.zclSchema = x
})
f = util.locateRelativeFilePath(fileLocations, obj.zclSchema)
if (f != null) ctx.zclSchema = f
// Zcl Validation Script
mapOverFileLocations(fileLocations, obj.zclValidation, (x) => {
ctx.zclValidation = x
})
f = util.locateRelativeFilePath(fileLocations, obj.zclValidation)
if (f != null) ctx.zclValidation = f
// General options
// Note that these values when put into OPTION_CODE will generally be converted to lowercase.
......@@ -86,18 +83,6 @@ function collectDataFromJsonFile(ctx) {
})
}
function mapOverFileLocations(fileLocations, f, destinationResolver) {
if (f) {
for (var i = 0; i < fileLocations.length; i++) {
var resolvedFile = path.resolve(fileLocations[i], f.trim())
if (fs.existsSync(resolvedFile)) {
destinationResolver(resolvedFile)
break
}
}
}
}
/**
* Promises to read the properties file, extract all the actual xml files, and resolve with the array of files.
*
......@@ -119,30 +104,30 @@ function collectDataFromPropertiesFile(ctx) {
.split(',')
.map((p) => path.join(path.dirname(ctx.metadataFile), p))
var zclFiles = []
var f
// Iterate over all XML files in the properties file, and check
// if they exist in one or the other directory listed in xmlRoot
zclProps.xmlFile.split(',').forEach((f) => {
mapOverFileLocations(fileLocations, f, (x) => {
zclFiles.push(x)
})
f = util.locateRelativeFilePath(fileLocations, f)
if (f != null) zclFiles.push(f)
})
ctx.zclFiles = zclFiles
// Manufacturers XML file.
mapOverFileLocations(fileLocations, zclProps.manufacturersXml, (x) => {
ctx.manufacturersXml = x
})
f = util.locateRelativeFilePath(
fileLocations,
zclProps.manufacturersXml
)
if (f != null) ctx.manufacturersXml = f
// Zcl XSD file
mapOverFileLocations(fileLocations, zclProps.zclSchema, (x) => {
ctx.zclSchema = x
})
f = util.locateRelativeFilePath(fileLocations, zclProps.zclSchema)
if (f != null) ctx.zclSchema = f
// Zcl Validation Script
mapOverFileLocations(fileLocations, zclProps.zclValidation, (x) => {
ctx.zclValidation = x
})
f = util.locateRelativeFilePath(fileLocations, zclProps.zclValidation)
if (f != null) ctx.zclValidation = f
// General options
// Note that these values when put into OPTION_CODE will generally be converted to lowercase.
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册