Skip to content

Commit

Permalink
Bug 1875045 - [devtools] Add a script helper to easily create a new c…
Browse files Browse the repository at this point in the history
…ommand. r=devtools-reviewers,nchevobbe

Differential Revision: https://phabricator.services.mozilla.com/D198930
  • Loading branch information
ochameau committed Jan 29, 2024
1 parent 287d428 commit bd77930
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
7 changes: 7 additions & 0 deletions devtools/shared/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ This layer already exists in some panels, but we are using slightly different na
Last, but not least, this layer may allow us to slowly get rid of protocol.js.
Command classes aren't Fronts, nor are they particularly connected to protocol.js.
If we make it so that all the Frontend code using Fronts uses Commands instead, we might more easily get away from protocol.js.

If you want to create a new command, you can use a bash script to help your bootstrap all basic required files:
```
$ ./create-command.sh command-file-name CommandName
```
Where the first argument will be the name used for folder and files, using lower case and dash as separator.
And the second argument will be the class name in code, using camlcase.
129 changes: 129 additions & 0 deletions devtools/shared/commands/create-command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/bin/bash

# Script to easily create a new command, including:
# - a template for the main command file
# - test folder and test head.js file
# - a template for a first test
# - all necessary build manifests

if [[ -z $1 || -z $2 ]]; then
echo "$0 expects two arguments:"
echo "$(basename $0) command-file-name CommandName"
echo " 1) The file name for the command, with '-' as separators between words"
echo " This will be the name of the folder"
echo " 2) The command name being caml cased"
echo " This will be used to craft the name of the JavaScript class"
exit
fi

if [ -e $1 ]; then
echo "$1 already exists. Please use a new folder/command name."
fi

CMD_FOLDER=$1
CMD_FILE_NAME=$1-command.js
CMD_NAME=$2Command

pushd `dirname $0`

echo "Creating a new command called '$CMD_NAME' in $CMD_FOLDER"

mkdir $CMD_FOLDER

cat > $CMD_FOLDER/moz.build <<EOF
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
DevToolsModules(
"$CMD_FILE_NAME",
)
if CONFIG["MOZ_BUILD_APP"] != "mobile/android":
BROWSER_CHROME_MANIFESTS += ["tests/browser.toml"]
EOF

cat > $CMD_FOLDER/$CMD_FILE_NAME <<EOF
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/**
* The $CMD_NAME ...
*/
class $CMD_NAME {
constructor({ commands, descriptorFront, watcherFront }) {
this.#commands = commands;
}
#commands = null;
}
module.exports = $CMD_NAME;
EOF

mkdir $CMD_FOLDER/tests
cat > $CMD_FOLDER/tests/browser.toml <<EOF
[DEFAULT]
tags = "devtools"
subsuite = "devtools"
support-files = [
"!/devtools/client/shared/test/shared-head.js",
"head.js",
]
[browser_$1.js]
EOF


cat > $CMD_FOLDER/tests/head.js <<EOF
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/* eslint no-unused-vars: [2, {"vars": "local"}] */
Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/devtools/client/shared/test/shared-head.js",
this
);
EOF

CMD_NAME_FIRST_LOWERCASE=${CMD_NAME,}
cat > $CMD_FOLDER/tests/browser_$1.js <<EOF
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Test the $CMD_NAME
add_task(async function () {
info("Setup the test page");
const tab = await addTab("data:text/html;charset=utf-8,Test page");
info("Create a target list for a tab target");
const commands = await CommandsFactory.forTab(tab);
await commands.targetCommand.startListening();
const { $CMD_NAME_FIRST_LOWERCASE } = commands;
// assertions...
await commands.destroy();
BrowserTestUtils.removeTab(tab);
});
EOF

popd

echo ""
echo "Command created!"
echo ""
echo "Now:"
echo " - edit moz.build to add '\"$CMD_FOLDER\",' in DIRS (this need to be kept sorted)"
echo " - edit index.js to add '$CMD_NAME_FIRST_LOWERCASE: \"devtools/shared/commands/$CMD_FOLDER/$1-command\"' in Commands dictionary"

0 comments on commit bd77930

Please sign in to comment.