From d685dbbceacf8d583a1671f6549f3a0c52c9c2e3 Mon Sep 17 00:00:00 2001 From: Ahbong Chang Date: Fri, 25 Oct 2024 16:34:43 +0800 Subject: [PATCH] Fix ups for debugging flow --- .vscode/launch.json | 15 ++++++----- .vscodeignore | 10 +++++++ esbuild-debug-adapter.js | 56 ++++++++++++++++++++++++++++++++++++++++ eslint.config.js | 3 ++- package.json | 6 ++--- scripts/build.sh | 18 ++++--------- scripts/package.sh | 17 ++++-------- scripts/prepare.sh | 35 +++++++++++++++++++++++++ scripts/test.sh | 5 ++-- tsconfig.json | 2 ++ 10 files changed, 128 insertions(+), 39 deletions(-) create mode 100644 .vscodeignore create mode 100644 esbuild-debug-adapter.js create mode 100755 scripts/prepare.sh diff --git a/.vscode/launch.json b/.vscode/launch.json index 18a926be..0be26099 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,28 +6,29 @@ "type": "extensionHost", "request": "launch", "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceRoot}"], - "stopOnEntry": false, + "args": ["--extensionDevelopmentPath=${workspaceFolder}"], "sourceMaps": true, - "outFiles": ["${workspaceRoot}/out/src/**/*.js"], - "preLaunchTask": "npm" + "outFiles": ["${workspaceFolder}/dist/**/*.js"], + "preLaunchTask": "npm: compile" }, { "name": "Launch Debug Server", "type": "node", "request": "launch", "cwd": "${workspaceFolder}", - "program": "${workspaceFolder}/src/debug-adapter/client.ts", + "program": "${workspaceFolder}/dist/debug-adapter-client.js", "args": ["--server=4711"], "sourceMaps": true, - "outFiles": ["${workspaceRoot}/out/src/**/*.js"] + "outFiles": ["${workspaceFolder}/dist/**/*.js"] }, { "name": "Launch Extension Tests", "type": "extensionHost", "request": "launch", "testConfiguration": "${workspaceFolder}/.vscode-test.js", - "args": ["--extensionDevelopmentPath=${workspaceFolder}"] + "args": ["--extensionDevelopmentPath=${workspaceFolder}"], + "sourceMaps": true, + "outFiles": ["${workspaceFolder}/out/**/*.js"] } ], "compounds": [ diff --git a/.vscodeignore b/.vscodeignore new file mode 100644 index 00000000..2b02878a --- /dev/null +++ b/.vscodeignore @@ -0,0 +1,10 @@ +# Ignore all files by default. +**/* + +!README.md +!LICENSE +!package.json +!dist/**/*.js +!icons/**/*.+(svg|license) +!media/**/* +!syntaxes/**/*.+(json|yaml|license) diff --git a/esbuild-debug-adapter.js b/esbuild-debug-adapter.js new file mode 100644 index 00000000..ff118862 --- /dev/null +++ b/esbuild-debug-adapter.js @@ -0,0 +1,56 @@ +const esbuild = require("esbuild"); + +const production = process.argv.includes("--production"); +const watch = process.argv.includes("--watch"); + +/** + * @type {import('esbuild').Plugin} + */ +const esbuildProblemMatcherPlugin = { + name: "esbuild-problem-matcher", + + setup(build) { + build.onStart(() => { + console.log("[watch] build started"); + }); + build.onEnd((result) => { + result.errors.forEach(({ text, location }) => { + console.error(`✘ [ERROR] ${text}`); + console.error( + ` ${location.file}:${location.line}:${location.column}:`, + ); + }); + console.log("[watch] build finished"); + }); + }, +}; + +async function main() { + const ctx = await esbuild.context({ + entryPoints: ["src/debug-adapter/client.ts"], + bundle: true, + format: "cjs", + minify: production, + sourcemap: !production, + sourcesContent: false, + platform: "node", + outfile: "dist/debug-adapter-client.js", + external: ["vscode"], + logLevel: "silent", + plugins: [ + /* add to the end of plugins array */ + esbuildProblemMatcherPlugin, + ], + }); + if (watch) { + await ctx.watch(); + } else { + await ctx.rebuild(); + await ctx.dispose(); + } +} + +main().catch((e) => { + console.error(e); + process.exit(1); +}); diff --git a/eslint.config.js b/eslint.config.js index 4965dbea..7becab35 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -12,7 +12,8 @@ module.exports = tseslint.config( eslintConfigPrettier, { ignores: [ - "esbuild.js", + "esbuild*.js", + "dist/", "out/", "src/protos/protos.js", "src/protos/protos.d.ts", diff --git a/package.json b/package.json index fbb5205c..465936eb 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "onCommand:bazel.info.output_path", "onCommand:bazel.info.workspace" ], - "main": "./extension.js", + "main": "./dist/extension.js", "contributes": { "breakpoints": [ { @@ -200,7 +200,7 @@ { "type": "bazel-launch-build", "label": "Launch Bazel Build", - "program": "./out/src/debug-adapter/client.js", + "program": "./dist/debug-adapter-client.js", "runtime": "node", "configurationAttributes": { "launch": { @@ -467,7 +467,7 @@ "test": "./scripts/test.sh", "package": "./scripts/package.sh", "update-snapshot": "./scripts/test.sh -u", - "watch": "./scripts/build.sh -watch" + "watch": "./scripts/build.sh --watch" }, "devDependencies": { "@types/mocha": "^10.0.6", diff --git a/scripts/build.sh b/scripts/build.sh index 70341ffc..c0fc60af 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -19,20 +19,12 @@ set -eu # Move into the top-level directory of the project. cd "$(dirname "${BASH_SOURCE[0]}")/.." > /dev/null -# Only regenerate the .js and .t.ds file if the protos have changed (i.e., -# it's a fresh checkout or update_protos.sh has been executed again and -# deleted the old generated files). This shaves several seconds off the -# extension's build time. -if [[ ! -f src/protos/protos.js ]] ; then - sed -e "s#^#src/protos/#" src/protos/protos_list.txt | \ - xargs pbjs -t static-module -o src/protos/protos.js -fi -if [[ ! -f src/protos/protos.d.ts ]] ; then - pbts -o src/protos/protos.d.ts src/protos/protos.js -fi +./scripts/prepare.sh -# Convert yaml language definition to json form requred by vscode. -js-yaml syntaxes/bazelrc.tmLanguage.yaml > syntaxes/bazelrc.tmLanguage.json +# Do not watch. Drop minify as well before we have a better arg parse here. +node esbuild-debug-adapter.js + +node esbuild.js # Compile the rest of the project for non-release development flow. tsc "$@" -p ./ diff --git a/scripts/package.sh b/scripts/package.sh index 84a80809..057469b1 100755 --- a/scripts/package.sh +++ b/scripts/package.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# Copyright 2018 The Bazel Authors. All rights reserved. +# Copyright 2024 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -19,18 +19,11 @@ set -eu # Move into the top-level directory of the project. cd "$(dirname "${BASH_SOURCE[0]}")/.." > /dev/null -./scripts/build.sh +./scripts/prepare.sh -# Prepare dist/ content besides the entry js. -mkdir -p dist -cp ./package.json ./dist/package.json -cp ./LICENSE ./dist/LICENSE -cp ./README.md ./dist/README.md -cp -r ./icons ./dist/icons -cp -r ./media ./dist/media -cp -r ./syntaxes ./dist/syntaxes +# Do not watch. Drop minify as well before we have a better arg parse here. +node esbuild-debug-adapter.js --production node esbuild.js --production -cd dist -vsce package --no-dependencies -o .. +vsce package --no-dependencies diff --git a/scripts/prepare.sh b/scripts/prepare.sh new file mode 100755 index 00000000..24479e48 --- /dev/null +++ b/scripts/prepare.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +# Copyright 2024 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -eu + +# Move into the top-level directory of the project. +cd "$(dirname "${BASH_SOURCE[0]}")/.." > /dev/null + +# Only regenerate the .js and .t.ds file if the protos have changed (i.e., +# it's a fresh checkout or update_protos.sh has been executed again and +# deleted the old generated files). This shaves several seconds off the +# extension's build time. +if [[ ! -f src/protos/protos.js ]] ; then + sed -e "s#^#src/protos/#" src/protos/protos_list.txt | \ + xargs pbjs -t static-module -o src/protos/protos.js +fi +if [[ ! -f src/protos/protos.d.ts ]] ; then + pbts -o src/protos/protos.d.ts src/protos/protos.js +fi + +# Convert yaml language definition to json form requred by vscode. +js-yaml syntaxes/bazelrc.tmLanguage.yaml > syntaxes/bazelrc.tmLanguage.json diff --git a/scripts/test.sh b/scripts/test.sh index b586e603..f38d41a8 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -19,9 +19,8 @@ set -eu # Move into the top-level directory of the project. cd "$(dirname "${BASH_SOURCE[0]}")/.." > /dev/null -# If tests are eventually added for other things, this line should probably -# be replaced by just running scripts/build.sh. -js-yaml syntaxes/bazelrc.tmLanguage.yaml > syntaxes/bazelrc.tmLanguage.json +# This should be invoked by either `npm test` or `npm run test` in pretest +# implicitly, thus the build build steps are all moved into build.sh. # Regression test for bazelrc grammar vscode-tmgrammar-snap "$@" test/example.bazelrc diff --git a/tsconfig.json b/tsconfig.json index d7cd7024..6d125fe8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,8 +13,10 @@ "allowJs": true, }, "exclude": [ + "dist", "node_modules", "out", + "esbuild*.js", "eslint.config.js", "scripts", ]