bundling.bash 2.9 KB

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