Skip to content

Instantly share code, notes, and snippets.

@ochameau
Created December 5, 2018 09:32
Show Gist options
  • Save ochameau/91457f1d8f887dcbdb6f3dbb72e517a3 to your computer and use it in GitHub Desktop.
Save ochameau/91457f1d8f887dcbdb6f3dbb72e517a3 to your computer and use it in GitHub Desktop.
front rewrite script
#!/bin/bash
FOLDER=devtools/shared/fronts
if [ ! -z "$1" ]; then
FOLDER=$1
fi
echo $FOLDER
set -x
### Get rid of "protocol" symbol and import protocol.js symbols individually
# First replace import where a module uses preEvent and FrontClassWithSpec, all other only use FrontClassWithSpec
sed -ie "s/protocol\( = require\)/{ FrontClassWithSpec, preEvent }\1/g" $(git grep -l protocol.preEvent $FOLDER)
# To do the same for protocol.custom
sed -ie "s/protocol\( = require\)/{ FrontClassWithSpec, custom }\1/g" $(git grep -l protocol.custom $FOLDER)
# Then do similarly for modules importing custom via const {custom} = protocol
sed -ie "s/protocol\( = require\)/{ FrontClassWithSpec, custom }\1/g" $(git grep -lE "custom\s*} = protocol" $FOLDER)
find $FOLDER -type f -exec sed -ie "s/protocol\( = require\)/{ FrontClassWithSpec }\1/g" {} \;
# And remove the `const {custom} = protocol`
find $FOLDER -type f -exec sed -ie "/const {\s*custom\s*} = protocol;/d" {} \;
# Replace protocol.FrontClassWithSpec/preEvent with FrontClassWithSpec/preEvent
find $FOLDER -type f -exec sed -ie "s/protocol.FrontClassWithSpec/FrontClassWithSpec/g" {} \;
find $FOLDER -type f -exec sed -ie "s/protocol.preEvent/preEvent/g" {} \;
find $FOLDER -type f -exec sed -ie "s/protocol.custom/custom/g" {} \;
# Merge lines const *Front =\nFrontClassWithSpec to help the next sed command
find $FOLDER -type f -exec sed -i ':begin;$!N;s/\(Front =\)\n\(FrontClassWithSpec\)/\1 \2/;tbegin;P;D' {} \;
# rewrite class header definition
find $FOLDER -type f -exec sed -ie "s/\(const\|var\) \(\w*\) =\( \|\n\)FrontClassWithSpec(\(\w*Spec\), {/class \2 extends FrontClassWithSpec(\4) {/g" {} \;
# end of class
find $FOLDER -type f -exec sed -ie "s/^});/}/g" {} \;
# remove unecessary "," in classes
find $FOLDER -type f -exec sed -ie "s/^\(\s\s}\),/\1/g" {} \;
# rewrite function header into es6 classes without "function "
find $FOLDER -type f -exec sed -ie "s/^\(\s\s\w*\): function(/\1(/g" {} \;
# same with async
find $FOLDER -type f -exec sed -ie "s/^\(\s\s\)\(\w*\): async function(/\1async \2(/g" {} \;
# Rename initialize into constructor
find $FOLDER -type f -exec sed -ie "s/ initialize(/ constructor(/g" {} \;
# refactor Front.initialize with super
find $FOLDER -type f -exec sed -ie "s/\(protocol.\)\?Front.prototype.initialize.call(this, /super(/g" {} \;
# similar, but for destroy
find $FOLDER -type f -exec sed -ie "s/\(protocol.\)\?Front.prototype.destroy.call(this/super.destroy(/g" {} \;
# refactor custom header
find $FOLDER -type f -exec sed -ie "s/^\(\s\s\w*\): custom(function(/\1(/g" {} \;
# same with async
find $FOLDER -type f -exec sed -ie "s/^\(\s\s\)\(\w*\): custom(async function(/\1async \2(/g" {} \;
# These rules require manual work to remove the { impl: "_xxx" } object and replace this._xxx() by super.xxx()
# Register all front classes
find $FOLDER -type f -exec sed -ie "s/^\(exports.\w*Front = \)\(\w*Front\);/\1\2;\nregisterFront(\2);/g" {} \;
# Import registerFront
# on multiline imports
find $FOLDER -type f -exec sed -ie "s/^\(} = require(\"devtools\/shared\/protocol\)/ registerFront,\n\1/g" {} \;
# on single line imports
find $FOLDER -type f -exec sed -ie "s/\( } = require(\"devtools\/shared\/protocol\)/, registerFront\1/g" {} \;
# Remove now useless Front imports
find $FOLDER -type f -exec sed -ie "s/, Front\(.* } = require(\"devtools\/shared\/protocol\)/\1/g" {} \;
find $FOLDER -type f -exec sed -ie "/^ Front,$/d" {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment