run.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env node
  2. // const cp = require('child_process');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const format = require('./format-lines');
  6. function getVersion(path) {
  7. try {
  8. return fs.readFileSync(path, 'utf8').match(/\/\/ OpenZeppelin Contracts \(last updated v[^)]+\)/)[0];
  9. } catch (err) {
  10. return null;
  11. }
  12. }
  13. function generateFromTemplate(file, template, outputPrefix = '') {
  14. const script = path.relative(path.join(__dirname, '../..'), __filename);
  15. const input = path.join(path.dirname(script), template);
  16. const output = path.join(outputPrefix, file);
  17. const version = getVersion(output);
  18. const content = format(
  19. '// SPDX-License-Identifier: MIT',
  20. ...(version ? [version + ` (${file})`] : []),
  21. `// This file was procedurally generated from ${input}.`,
  22. '',
  23. require(template).trimEnd(),
  24. );
  25. fs.writeFileSync(output, content);
  26. // cp.execFileSync('prettier', ['--write', output]);
  27. }
  28. // Contracts
  29. for (const [file, template] of Object.entries({
  30. 'utils/math/SafeCast.sol': './templates/SafeCast.js',
  31. 'utils/structs/EnumerableSet.sol': './templates/EnumerableSet.js',
  32. 'utils/structs/EnumerableMap.sol': './templates/EnumerableMap.js',
  33. 'utils/structs/Checkpoints.sol': './templates/Checkpoints.js',
  34. 'utils/SlotDerivation.sol': './templates/SlotDerivation.js',
  35. 'utils/StorageSlot.sol': './templates/StorageSlot.js',
  36. 'utils/Arrays.sol': './templates/Arrays.js',
  37. 'utils/Packing.sol': './templates/Packing.js',
  38. 'mocks/StorageSlotMock.sol': './templates/StorageSlotMock.js',
  39. })) {
  40. generateFromTemplate(file, template, './contracts/');
  41. }
  42. // Tests
  43. for (const [file, template] of Object.entries({
  44. 'utils/structs/Checkpoints.t.sol': './templates/Checkpoints.t.js',
  45. 'utils/Packing.t.sol': './templates/Packing.t.js',
  46. 'utils/SlotDerivation.t.sol': './templates/SlotDerivation.t.js',
  47. })) {
  48. generateFromTemplate(file, template, './test/');
  49. }