summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/create-command.sh
blob: 1f95f96df6dec108718ac515a9def8a4087b13bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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"