demergent-labs / azle

A WebAssembly runtime for TypeScript and JavaScript on ICP
MIT License
209 stars 36 forks source link

Mass Conversion of examples to class based syntax #1852

Closed bdemann closed 4 months ago

bdemann commented 5 months ago
bdemann commented 4 months ago

I didn't end up saving most of the regex for the conversions I think that would have slowed down the process a lot of not a lot of benefit. After a while of writing regex it started to come more naturally and it felt less important to keep a record as I was feeling confident that I could repeat the process again if I needed to and might even produce better regex. With that being said, here are some of the ones I did save, since they are already saved we might as well record them for posterity instead of deleting them.

  1. Update syntax for canister
    1. Convert canisters to classes
      1. export default Canister({\n((.\n)*)});
      2. export default class {\n$1}
    2. Export queries and updates to decorators
      1. (.): ((update)|(query))(([.]),\s(.),\s((.))\s=>\s
      2. @$2($5, $6)\n$1$7
    3. Export async queries and updates to decorators
      1. \s(.): ((update)|(query))(([.]),\s(.),\s\sasync\s((.))\s=>\s*
      2. @$2($5, $6)\nasync $1$7
    4. Update init and postUpgrade to decorators
      1. (.): ((init)|(postUpgrade))(([.]),\s\s((.))\s=>\s*
      2. @$2($5)\n$1$6
    5. Convert async queries and updates with lots of line breaks to decorators
      1. \s(.):\s((update)|(query))(\n\s([.]),\n\s(.),\n\sasync\s((.))\s=>\s
      2. \n @$2($5, $6)\n async $1$7
    6. Convert queries and updates with lots of line breaks to decorators
      1. \s(.):\s((update)|(query))(\n\s([.]),\n\s(.),\n\s\s((.))\s=>\s
      2. \n @$2($5, $6)\n $1$7
    7. Change all manual return values to manual options args
      1. Manual((.*)))
      2. $1, { manual: true})
bdemann commented 4 months ago

Here are the steps I used

  1. add name to similar package.json

  2. Copy to new location

  3. Delete node_modules, .dfx, .azle, and test/tests.ts

  4. add "experimentalDecorators": true, "preserveSymlinks": true to tsconfig

  5. Add coresponding package to package.json ("audio_recorder_end_to_end_test_functional_syntax": "file:../../../../../examples/audio_recorder",)

  6. Add to pretest the linking hack

import { linkAndInstallPatch } from 'azle/test/jest_link'; import { join } from 'path'; linkAndInstallPatch(join('examples', 'query'));

  1. Update getTests to be from the right package

There will be scripts in script/class_migration that will do a lot of these tasks automatically

The do_all_something.js script will run a given script for all of the projects in a directory so for example I would start in the examples dir and run ../../scripts/class_migration/do_all_something.js ../../script/class_migration/the_script_I_want_to_run.js and then I would jump down to motoko examples and run it again (with the paths updated to reflect the cd to motoko_examples)

bdemann commented 4 months ago

closed by https://github.com/demergent-labs/azle/pull/1853