bundling.bash 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env bash
  2. export XPI_PATH="$PROJECT_ROOT"/interfacer/src/browsh/browsh.xpi
  3. export XPI_SOURCE_DIR=$PROJECT_ROOT/webext/dist/web-ext-artifacts
  4. export NODE_BIN=$PROJECT_ROOT/webext/node_modules/.bin
  5. MDN_USER="user:13243312:78"
  6. function versioned_xpi_file() {
  7. echo "$XPI_SOURCE_DIR/browsh-$(browsh_version).xpi"
  8. }
  9. # You'll want to use this with `go run ./cmd/browsh --debug --firefox.use-existing`
  10. function build_webextension_watch() {
  11. "$NODE_BIN"/web-ext run \
  12. --firefox contrib/firefoxheadless.sh \
  13. --verbose
  14. }
  15. function build_webextension_production() {
  16. local version && version=$(browsh_version)
  17. cd "$PROJECT_ROOT"/webext && "$NODE_BIN"/webpack
  18. cd "$PROJECT_ROOT"/webext/dist && rm ./*.map
  19. if [ -f core ]; then
  20. # Is this a core dump for some failed process?
  21. rm core
  22. fi
  23. ls -alh .
  24. "$NODE_BIN"/web-ext build --overwrite-dest
  25. ls -alh web-ext-artifacts
  26. webextension_sign
  27. local source_file && source_file=$(versioned_xpi_file)
  28. echo "Bundling $source_file to $XPI_PATH"
  29. cp -f "$source_file" "$XPI_PATH"
  30. echo "Making extra copy for Goreleaser to put in Github release:"
  31. local goreleaser_pwd="$PROJECT_ROOT"/interfacer/
  32. cp -a "$source_file" "$goreleaser_pwd"
  33. ls -alh "$goreleaser_pwd"
  34. }
  35. # It is possible to use unsigned webextensions in Firefox but it requires that Firefox
  36. # uses problematically insecure config. I know it's a hassle having to jump through all
  37. # these signing hoops, but I think it's better to use a standard Firefox configuration.
  38. # Moving away from the webextension alltogether is another story, but something I'm still
  39. # thinking about.
  40. #
  41. # NB: There can only be one canonical XPI for each semantic version.
  42. #
  43. # shellcheck disable=2120
  44. function webextension_sign() {
  45. local use_existing=$1
  46. if [ "$use_existing" == "" ]; then
  47. "$NODE_BIN"/web-ext sign --api-key "$MDN_USER" --api-secret "$MDN_KEY"
  48. _rename_built_xpi
  49. else
  50. echo "Skipping signing, downloading existing webextension"
  51. local base="https://github.com/browsh-org/browsh/releases/download"
  52. curl -L \
  53. -o "$(versioned_xpi_file)" \
  54. "$base/v$LATEST_TAGGED_VERSION/browsh-$LATEST_TAGGED_VERSION.xpi"
  55. fi
  56. }
  57. function _rename_built_xpi() {
  58. pushd "$XPI_SOURCE_DIR" || _panic
  59. local xpi_file
  60. xpi_file="$(
  61. find ./*.xpi \
  62. -printf "%T@ %f\n" |
  63. sort |
  64. cut -d' ' -f2 |
  65. tail -n1
  66. )"
  67. cp -a "$xpi_file" "$(versioned_xpi_file)"
  68. popd || _panic
  69. }
  70. function bundle_production_webextension() {
  71. local version && version=$(browsh_version)
  72. local base='https://github.com/browsh-org/browsh/releases/download'
  73. local release_url="$base/v$version/browsh-$version.xpi"
  74. echo "Downloading webextension from: $release_url"
  75. curl -L -o "$XPI_PATH" "$release_url"
  76. local size && size=$(wc -c <"$XPI_PATH")
  77. if [ "$size" -lt 500 ]; then
  78. echo "XPI size seems too small: $size"
  79. _panic "Problem downloading latest webextension XPI"
  80. fi
  81. cp -a "$XPI_PATH" "$(versioned_xpi_file)"
  82. }