Skip to content

Commit

Permalink
Update the zcl and template packages during generation (project-chip#…
Browse files Browse the repository at this point in the history
…1443)

- Create a flag called --upgradeZapFile which can be passed during generation to update the package paths during generation. add this to apack.json
- During generation use the flag to determine if packages should be updated in the .zap file. If the .zap file does not already have the packages mentioned in generation(uc_generate) then the .zap file is updated with those packages and saved.
- Normalize the paths in the options.zcl/template such that they can be searched accurately in the database
- Adding unit tests for checking the path upgrades to packages in the .zap file before and after generation is complete.
- JIRA: ZAPP-1456
  • Loading branch information
brdandu authored Oct 22, 2024
1 parent 5b45cff commit 1425198
Show file tree
Hide file tree
Showing 4 changed files with 4,001 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apack.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"cmd": "$(zap-cli) --version"
},
"uc_generate": {
"cmd": "$(zap-cli) generate --noUi --noServer -o ${generationOutput} --packageMatch fuzzy --zcl ${sdkRoot}/app/zcl/zcl-zap.json --zcl ${sdkRoot}/extension/matter_extension/src/app/zap-templates/zcl/zcl.json --generationTemplate ${sdkRoot}/protocol/zigbee/app/framework/gen-template/gen-templates.json --generationTemplate ${sdkRoot}/extension/matter_extension/src/app/zap-templates/app-templates.json --in ${contentFolder} --noLoadingFailure --appendGenerationSubdirectory"
"cmd": "$(zap-cli) generate --noUi --noServer -o ${generationOutput} --packageMatch fuzzy --zcl ${sdkRoot}/app/zcl/zcl-zap.json --zcl ${sdkRoot}/extension/matter_extension/src/app/zap-templates/zcl/zcl.json --generationTemplate ${sdkRoot}/protocol/zigbee/app/framework/gen-template/gen-templates.json --generationTemplate ${sdkRoot}/extension/matter_extension/src/app/zap-templates/app-templates.json --in ${contentFolder} --noLoadingFailure --appendGenerationSubdirectory --upgradeZapFile"
},
"uc_upgrade": {
"cmd": "$(zap-cli) upgrade --results ${results} -d ${tempContentFolder} --zcl ${sdkRoot}/app/zcl/zcl-zap.json --zcl ${sdkRoot}/extension/matter_extension/src/app/zap-templates/zcl/zcl.json --generationTemplate ${sdkRoot}/protocol/zigbee/app/framework/gen-template/gen-templates.json --generationTemplate ${sdkRoot}/extension/matter_extension/src/app/zap-templates/app-templates.json --noLoadingFailure"
Expand Down
102 changes: 101 additions & 1 deletion src-electron/main-process/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,87 @@ async function generateSingleFile(
generationLog: null
}
) {
// Normalize the paths in options.zcl/template to search paths in database accurately
if (Array.isArray(options.zcl)) {
options.zcl.forEach((filePath, index) => {
if (filePath != null) {
options.zcl[index] = path.normalize(filePath)
}
})
} else if (typeof options.zcl === 'string') {
options.zcl = path.normalize(options.zcl)
}
if (Array.isArray(options.template)) {
options.template.forEach((filePath, index) => {
if (filePath != null) {
options.template[index] = path.normalize(filePath)
}
})
} else if (typeof options.template === 'string') {
options.template = path.normalize(options.template)
}

let hrstart = process.hrtime.bigint()
let sessionId
let output
let upgradeZclPackages = []
let upgradeTemplatePackages = []
let isZapFileUpgradeNeeded = false
// Do not run upgrade if zap package in .zap file are present
let isZapPackagePathPresent = true

// Upgrade the .zap file with generation packages if the upgradeZapFile flag
// is passed during generation.
if (options.upgradeZapFile) {
let state = await importJs.readDataFromFile(zapFile)
let zapFileZclPackages = []
let zapFileTemplatePackages = []
for (let i = 0; i < state.package.length; i++) {
let zapFileDir = path.dirname(zapFile)
let packagePath = path.resolve(zapFileDir, state.package[i].path)
if (!fs.existsSync(packagePath)) {
isZapPackagePathPresent = false
}
if (state.package[i].type == dbEnum.packageType.zclProperties) {
zapFileZclPackages.push(packagePath)
} else if (state.package[i].type == dbEnum.packageType.genTemplatesJson) {
zapFileTemplatePackages.push(packagePath)
}
}

if (!isZapPackagePathPresent) {
upgradeZclPackages = await getUpgradePackageMatch(
db,
options.zcl,
state.package,
dbEnum.packageType.zclProperties
)
upgradeTemplatePackages = await getUpgradePackageMatch(
db,
options.template,
state.package,
dbEnum.packageType.genTemplatesJson
)
for (let i = 0; i < upgradeZclPackages.length; i++) {
if (!zapFileZclPackages.includes(upgradeZclPackages[i].path)) {
isZapFileUpgradeNeeded = true
break
}
}
for (let i = 0; i < upgradeTemplatePackages.length; i++) {
if (
!zapFileTemplatePackages.includes(upgradeTemplatePackages[i].path)
) {
isZapFileUpgradeNeeded = true
break
}
}
if (isZapFileUpgradeNeeded) {
options.upgradeZclPackages = upgradeZclPackages
options.upgradeTemplatePackages = upgradeTemplatePackages
}
}
}
if (zapFile === BLANK_SESSION) {
options.logger(`👉 using empty configuration`)
sessionId = await querySession.createBlankSession(db)
Expand All @@ -754,7 +832,9 @@ async function generateSingleFile(
defaultZclMetafile: options.zcl,
postImportScript: options.postImportScript,
packageMatch: options.packageMatch,
defaultTemplateFile: options.template
defaultTemplateFile: options.template,
upgradeZclPackages: upgradeZclPackages,
upgradeTemplatePackages: upgradeTemplatePackages
})
sessionId = importResult.sessionId
output = outputFile(zapFile, outputPattern, index)
Expand Down Expand Up @@ -798,6 +878,23 @@ async function generateSingleFile(
genResults.push(genResult)
}

if (options.upgradeZapFile && isZapFileUpgradeNeeded) {
options.logger(
`🕐 Updating the zap file with the correct SDK meta data: ${zapFile}`
)
// Now we need to write the sessionKey for the file path
await querySession.updateSessionKeyValue(
db,
sessionId,
dbEnum.sessionKey.filePath,
zapFile
)

await exportJs.exportDataIntoFile(db, sessionId, zapFile, {
createBackup: true
})
}

return genResults
}

Expand Down Expand Up @@ -859,6 +956,9 @@ async function startGeneration(argv, options) {
options.appendGenerationSubdirectory = argv.appendGenerationSubdirectory
options.packageMatch = argv.packageMatch
options.generationLog = argv.generationLog
// Used to upgrade the zap file during generation. Makes sure packages are
// updated in .zap file during project creation in Studio.
options.upgradeZapFile = argv.upgradeZapFile

let nsDuration = process.hrtime.bigint() - hrstart
options.logger(`🕐 Setup time: ${util.duration(nsDuration)} `)
Expand Down
Loading

0 comments on commit 1425198

Please sign in to comment.