sdkconfig_compare.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //https://github.com/nkolban/esp32-snippets/blob/master/tools/sdkconfig_compare/sdkconfig_compare.js
  2. // Node.js application for comparing two ESP-IDF configuration files (sdkconfig)
  3. const fs = require("fs"); // Require the file system processing library
  4. const readline = require("readline"); // Require the readline processing library
  5. var map_types = {
  6. "UNSET": 0,
  7. "VALUE": 1,
  8. "COMMENT": 2
  9. };
  10. // buildMap
  11. // Read the sdkconfig file specified by fileName and produce a map of the name/value pairs contained
  12. // within. A Promise is returned that is fulfilled when the file has been read.
  13. function buildMap(fileName) {
  14. const promise = new Promise(function (resolve, reject) {
  15. var readStream = fs.createReadStream(fileName);
  16. readStream.on("error", (err) => {
  17. reject(err);
  18. });
  19. const map = {};
  20. const lines_index = [];
  21. var lineNo = 1;
  22. const lineReader = readline.createInterface({
  23. input: readStream,
  24. crlfDelay: Infinity
  25. });
  26. // Called when a new line has been read from the file.
  27. lineReader.on("line", (line) => {
  28. line = line.trim(); // Trim whitespace from the line.
  29. if (line.length == 0) { // Ignore empty lines
  30. return;
  31. }
  32. // split lines using named capture group regex, and loop through the resulting array
  33. var regexp = /^\s*[#]\s*(?<unset_key>CONFIG[^\s]*) is not set\s*$|^\s*(?<comment>[#].*)$|^\s*(?<key>[^#]{1}[^\s=]*)=(?<value>.*)$/gm;
  34. var match = regexp.exec(line);
  35. var key="";
  36. var map_entry={};
  37. while (match != null) {
  38. if (match.groups.unset_key) {
  39. key = match.groups.unset_key;
  40. map_entry = {
  41. key:key,
  42. type: map_types.UNSET,
  43. value: undefined,
  44. index: lineNo++,
  45. };
  46. } else if (match.groups.comment) {
  47. // accumulate comments in an array
  48. map_entry={
  49. type: map_types.COMMENT,
  50. value: match.groups.comment,
  51. index: lineNo++,
  52. };
  53. }
  54. else {
  55. key = match.groups.key;
  56. map_entry = {
  57. key:key,
  58. type: map_types.VALUE,
  59. value: match.groups.value,
  60. index: lineNo++,
  61. };
  62. }
  63. if(map_entry.type!=map_types.COMMENT){
  64. map[key] = map_entry;
  65. }
  66. lines_index.push(map_entry);
  67. match = regexp.exec(line);
  68. }
  69. }); // on(line)
  70. // Called when all the lines from the file have been consumed.
  71. lineReader.on("close", () => {
  72. resolve({map, lines_index});
  73. }); // on(close)
  74. });
  75. return promise;
  76. } // buildMap
  77. const args = process.argv;
  78. if (args.length != 3) {
  79. console.log("Usage: node sdkconfig_compare file1 ");
  80. process.exit();
  81. }
  82. const sdkconfig = "./sdkconfig";
  83. const comparedFile = args[2];
  84. buildMap(sdkconfig).then((sdkConfigResult ) => {
  85. var sdkconfigMap = sdkConfigResult.map;
  86. var sdkconfigLines = sdkConfigResult.lines_index;
  87. buildMap(comparedFile).then((comparedResult) => {
  88. var comparedFileMap = comparedResult.map;
  89. var comparedLines = comparedResult.lines_index;
  90. buildMap("./sdkconfig.defaults").then((sdkconfigResults) => {
  91. var sdkconfig_defaults = sdkconfigResults.map;
  92. var sdkconfig_defaults_lines = sdkconfigResults.lines_index;
  93. // Three passes
  94. // In A and not B
  95. // in B and not A
  96. // value different in A and B
  97. var newmap = {};
  98. var entry={};
  99. console.log(`\n\n${sdkconfig} properties that are missing in ${comparedFile}\n**************************`);
  100. for (const prop in sdkconfigMap) {
  101. entry = sdkconfigMap[prop];
  102. if(entry.type==map_types.COMMENT || entry.type== map_types.UNSET) continue;
  103. if (!comparedFileMap.hasOwnProperty(prop) && !sdkconfig_defaults.hasOwnProperty(prop)) {
  104. newmap[prop] = entry;
  105. console.log(`${prop}=${entry.value}`);
  106. }
  107. else if(comparedFileMap.hasOwnProperty(prop)){
  108. newmap[prop] =entry;
  109. }
  110. }
  111. console.log(`\n\n${comparedFile} properties that are missing in ${sdkconfig}\n**************************`);
  112. for (const prop in comparedFileMap) {
  113. entry = comparedFileMap[prop];
  114. if(entry.type==map_types.COMMENT || entry.type== map_types.UNSET) continue;
  115. if (comparedFileMap.hasOwnProperty(prop)) {
  116. if (!sdkconfigMap.hasOwnProperty(prop) && !sdkconfig_defaults.hasOwnProperty(prop)) {
  117. console.log(`${prop}=${entry.value}`);
  118. }
  119. }
  120. }
  121. console.log(`\n\nproperties that are different ${sdkconfig}<->${comparedFile} \n**************************`);
  122. for (const prop in sdkconfigMap) {
  123. entry = sdkconfigMap[prop];
  124. if(entry.type==map_types.COMMENT) continue;
  125. if (sdkconfigMap.hasOwnProperty(prop)) {
  126. if (comparedFileMap.hasOwnProperty(prop)) {
  127. if (entry.value != comparedFileMap[prop].value) {
  128. console.log(`${prop} : [${entry.value}] != [${comparedFileMap[prop].value}]`);
  129. }
  130. }
  131. }
  132. }
  133. var newlines = [];
  134. for(const prop in newmap){
  135. newlines.splice(newmap[prop].index,0, `${prop}=${newmap[prop].value}`);
  136. }
  137. comparedLines.forEach(line=>{
  138. if(line.type==map_types.COMMENT ) {
  139. newlines.splice(line.index,0, line.value);
  140. }
  141. else if(line.type==map_types.UNSET){
  142. newlines.splice(line.index,0, `# ${line.key} is not set`);
  143. }
  144. });
  145. console.log(`\n\n${comparedFile} with missing properties\n**************************`);
  146. newlines.forEach(line => {
  147. console.log(line);
  148. });
  149. }).catch((err) => {
  150. console.log(err);
  151. process.exit();
  152. });
  153. }).catch((err) => {
  154. console.log(err);
  155. process.exit();
  156. });
  157. }).catch((err) => {
  158. console.log(err);
  159. process.exit();
  160. }
  161. );