Skip to content

Commit

Permalink
Refactor build process to not use pre-built bins
Browse files Browse the repository at this point in the history
This means that there's now a canonical script that allows other
distributers to build Browsh. The only caveat being that the web
extension cannot be built (Mozilla only allows one signed `.xpi` per
version), so it is downloaded.
  • Loading branch information
tombh committed Jul 26, 2018
1 parent d6afc20 commit 9abc3aa
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@ webextension.go
webext/node_modules
webext/dist/*
dist
*.xpi
90 changes: 69 additions & 21 deletions Dockerfile
@@ -1,39 +1,87 @@
FROM bitnami/minideb:stretch as build

RUN install_packages \
curl \
ca-certificates \
git \
autoconf \
automake \
g++ \
protobuf-compiler \
zlib1g-dev \
libncurses5-dev \
libssl-dev \
pkg-config \
libprotobuf-dev \
make

# Install Golang
ENV GOROOT=/go
ENV GOPATH=/go-home
ENV PATH=$GOROOT/bin:$GOPATH/bin:$PATH
RUN curl -L -o go.tar.gz https://dl.google.com/go/go1.9.2.linux-amd64.tar.gz
RUN mkdir -p $GOPATH/bin
RUN tar -C / -xzf go.tar.gz

ENV BASE=$GOPATH/src/browsh/interfacer
WORKDIR $BASE
ADD interfacer $BASE

# Install `dep` the current defacto dependency manager for Golang
RUN $BASE/contrib/setup_dep.sh

# Install the tool to convert the web extenstion file into a Go-compatible binary
RUN go get -u gopkg.in/shuLhan/go-bindata.v3/...

# Build Browsh
RUN dep ensure
RUN $BASE/contrib/build_browsh.sh


###########################
# Actual final Docker image
###########################
FROM bitnami/minideb:stretch

RUN install_packages xvfb libgtk-3-0 curl ca-certificates bzip2 libdbus-glib-1-2 procps
ENV HOME=/app
WORKDIR /app

COPY --from=build /go-home/src/browsh/interfacer/browsh /app/browsh

RUN install_packages \
xvfb \
libgtk-3-0 \
curl \
ca-certificates \
bzip2 \
libdbus-glib-1-2 \
procps

# Block ads, etc. This includes porn just because this image is also used on the
# public SSH demo: `ssh brow.sh`.
RUN curl -o /etc/hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn-social/hosts

# Don't use root
RUN useradd -m user --home /app
RUN chown user:user /app
USER user
ENV HOME=/app
WORKDIR /app

# These are needed to detect versions
ADD .travis.yml .
ADD ./webext/manifest.json .

# Setup Firefox
ENV PATH="/app/bin/firefox:${PATH}"
ADD ./interfacer/contrib/setup_firefox.sh .
ENV PATH="${HOME}/bin/firefox:${PATH}"
ADD .travis.yml .
ADD interfacer/contrib/setup_firefox.sh .
RUN ./setup_firefox.sh
RUN rm ./setup_firefox.sh && rm .travis.yml

# Setup Browsh
ADD ./interfacer/contrib/setup_browsh.sh .
ADD ./interfacer/src/browsh/version.go .
RUN VERSION_FILE=version.go ./setup_browsh.sh
RUN rm ./setup_browsh.sh && rm version.go
RUN rm setup_firefox.sh && rm .travis.yml

# Firefox behaves quite differently to normal on its first run, so by getting
# that over and done with here when there's no user to be dissapointed means
# that all future runs will be consistent.
RUN TERM=xterm script \
--return \
-c "/app/browsh" \
/dev/null \
>/dev/null & \
sleep 10
--return \
-c "/app/browsh" \
/dev/null \
>/dev/null & \
sleep 10

CMD ["/app/browsh"]

9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -59,7 +59,7 @@ package manager. The current Golang version being used is stored in `.travis.yml

You'll then need to install the project dependencies. For the webextension, just
run: `npm install` inside the `webext/` folder. For the CLI client you will first
need to install `dep`, there is a script for this in `interfacer/contrib/setup_go.sh`.
need to install `dep`, there is a script for this in `interfacer/contrib/setup_dep.sh`.
I don't fully understand Golang's best practices, but it seems you are forced to
keep your Go project's code under `$GOPATH/src`, you might be able to get away
with symlinks. Anyway, to install the dependencies use: `dep ensure` inside the
Expand All @@ -74,6 +74,13 @@ Then the ideal setup for development is:
webextension everytime webpack rebuilds it: (in `webext/dist`)
`web-ext run --verbose`

## Building a Browsh release
If you'd like to build Browsh for a new package manager, or for any other reason,
you can use the script at `interfacer/contrib/build_browsh.go` as a guide. Note
you won't be able to build the web extension as Mozilla only allows one canonical
version of web extensions per version number. So the build script downloads the
official web extension `.xpi` file from the Mozilla archives.

## Tests

For the webextension: in `webext/` folder, `npm test`
Expand Down
2 changes: 1 addition & 1 deletion contrib/release_if_new_version.sh
Expand Up @@ -4,7 +4,7 @@ set -e

PROJECT_ROOT=$(git rev-parse --show-toplevel)

browsh_version=$($PROJECT_ROOT/contrib/get_browsh_version.sh)
browsh_version=$($PROJECT_ROOT/interfacer/contrib/get_browsh_version.sh)
latest_tagged_version=$(git tag --sort=v:refname --list 'v*.*.*' | tail -n1 | sed -e "s/^v//")

echo "Browsh version: $browsh_version"
Expand Down
30 changes: 30 additions & 0 deletions interfacer/contrib/build_browsh.sh
@@ -0,0 +1,30 @@
#!/bin/bash

# This is for building a production version of Browsh.
# To build Browsh during development see:
# https://github.com/browsh-org/browsh#contributing

# This script depends on Golang and go-bindata
# `go get -u gopkg.in/shuLhan/go-bindata.v3/...`

set -e

INTERFACER_ROOT=$(readlink -m "$( cd "$(dirname "$0")" ; pwd -P )"/../)

version_file=$INTERFACER_ROOT/src/browsh/version.go
line=$(cat $version_file | grep 'browshVersion')
version=$(echo $line | grep -o '".*"' | sed 's/"//g')

base='https://github.com/browsh-org/browsh/releases/download'
release_url="$base/v$version/browsh-${version}-an.fx.xpi"

xpi_file=$INTERFACER_ROOT/browsh.xpi
destination=$INTERFACER_ROOT/src/browsh/webextension.go

curl -L -o $xpi_file $release_url

XPI_FILE=$xpi_file BIN_FILE=$destination \
$INTERFACER_ROOT/contrib/xpi2bin.sh

cd $INTERFACER_ROOT
go build -o browsh src/main.go
File renamed without changes.
22 changes: 0 additions & 22 deletions interfacer/contrib/setup_browsh.sh

This file was deleted.

File renamed without changes.
15 changes: 15 additions & 0 deletions interfacer/contrib/xpi2bin.sh
@@ -0,0 +1,15 @@
#!/bin/bash
set -e

INTERFACER_ROOT=$(readlink -m "$( cd "$(dirname "$0")" ; pwd -P )"/../)

go-bindata -version
go-bindata \
-nocompress \
-prefix $INTERFACER_ROOT \
-pkg browsh \
-o $BIN_FILE \
$XPI_FILE

ls -alh $INTERFACER_ROOT/src/browsh/webextension.go
echo "go-bindata exited with $(echo $?)"
2 changes: 1 addition & 1 deletion interfacer/src/browsh/firefox.go
Expand Up @@ -220,7 +220,7 @@ func firefoxMarionette() {
// Install the Browsh extension that was bundled with `go-bindata` under
// `webextension.go`.
func installWebextension() {
data, err := Asset("/webext/dist/web-ext-artifacts/browsh.xpi")
data, err := Asset("/browsh.xpi")
if err != nil {
Shutdown(err)
}
Expand Down
2 changes: 1 addition & 1 deletion interfacer/src/browsh/version.go
@@ -1,3 +1,3 @@
package browsh

var browshVersion = "1.4.8"
var browshVersion = "1.4.9"
14 changes: 3 additions & 11 deletions webext/contrib/bundle_webextension.sh
Expand Up @@ -22,7 +22,7 @@ ls -alh .
$NODE_BIN/web-ext build --overwrite-dest
ls -alh web-ext-artifacts

version=$($PROJECT_ROOT/contrib/get_browsh_version.sh)
version=$($PROJECT_ROOT/interfacer/contrib/get_browsh_version.sh)

xpi_file=browsh-$version-an+fx.xpi
zip_file=browsh-$version.zip
Expand Down Expand Up @@ -51,13 +51,5 @@ cp -f $source_file $bundle_file

echo "Bundling $source_file to $destination using internal path $bundle_file"

go-bindata -version
go-bindata \
-nocompress \
-prefix $PROJECT_ROOT \
-pkg browsh \
-o $destination \
$bundle_file

ls -alh $PROJECT_ROOT/interfacer/src/browsh/webextension.go
echo "go-bindata exited with $(echo $?)"
XPI_FILE=$bundle_file BIN_FILE=$destination \
$PROJECT_ROOT/interfacer/contrib/xpi2bin.sh

0 comments on commit 9abc3aa

Please sign in to comment.