Skip to content

Commit

Permalink
internal/imports: script for generating import path redirect files (#915
Browse files Browse the repository at this point in the history
)

This script fills a directory with the html files needed to redirect
import paths to gocloud.dev/...

Later, we'll configure Travis to run the script and push the generated
files to the gh-pages branch, where they will be served by GitHub Pages.
  • Loading branch information
jba authored Dec 13, 2018
1 parent 342a581 commit e981f05
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/imports/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Import Redirects for Go Cloud

The packages in this repo have import paths beginning `gocloud.dev`. This works
because:

- The `gocloud.dev` domain redirects to the GitHub Pages servers. See the output of
`dig gocloud.dev +noall +answer` and compare to the IP addresses [documented by
GitHub](https://help.github.com/articles/setting-up-an-apex-domain/#configuring-a-records-with-your-dns-provider).

- This repo's settings configure its GitHub Page to display the contents of the
`gh-pages` branch, with custom domain `gocloud.dev`. See "GitHub Pages" under
the [settings](https://github.com/google/go-cloud/settings).

- Each package import path in this repo has a corresponding HTML page on the
`gh-pages` branch that contains the `go-import` meta tag that `go get`
expects. Those HTML pages are generated by the `makeimports.sh` script in this
directory.
61 changes: 61 additions & 0 deletions internal/imports/makeimports.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash
# Copyright 2018 The Go Cloud Authors
#
# 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
#
# https://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.

# This script generates html files that contain go-import meta tags,
# suitable for "go get"'s import path redirection feature (see
# https://golang.org/cmd/go/#hdr-Remote_import_paths).
# It also adds go-source tags so godoc.org can link to the proper source files.
#
# This script must be run at the repo root.


# https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-euxo-pipefail
# except x is too verbose
set -euo pipefail

OUTDIR=makeimports-output

TEMPLATE=internal/imports/makeimports.template.html

if [ ! -f "$TEMPLATE" ]; then
echo "Template not found: $TEMPLATE"
echo "This script must be run from the repo root."
exit 1
fi


if [ -e $OUTDIR ]; then
echo "The $OUTDIR directory exists. Move or remove it, then re-run."
exit 1
fi

shopt -s nullglob # glob patterns that don't match turn into the empty string, instead of themselves

function files_exist() { # assumes nullglob
[[ ${1:-""} != "" ]]
}


# Find all directories that do not begin with '.' or contain 'testdata'. Use the %P printf
# directive to remove the initial './'.
for pkg in $(find . -type d \( -name '.?*' -prune -o -name testdata -prune -o -printf '%P ' \)); do
# Only consider directories that contain Go source files.
if files_exist $pkg/*.go; then
mkdir -p "$OUTDIR/$pkg"
echo "Generating gocloud.dev/$pkg"
cat "$TEMPLATE" | sed -e "s|{{path}}|$pkg|" > "$OUTDIR/$pkg/index.html"
fi
done

12 changes: 12 additions & 0 deletions internal/imports/makeimports.template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
{# AUTOMATICALLY GENERATED FILE - DO NOT EDIT Source file: //internal/imports/cloud/makeimports.template.html Generated by: //internal/imports/makeimports.sh #}
<meta name="go-import" content="gocloud.dev git https://github.com/google/go-cloud">
<meta name="go-source" content="gocloud.dev https://github.com/google/go-cloud https://github.com/google/go-cloud/tree/master{/dir} https://github.com/google/go-cloud/tree/master{/dir}/{file}#L{line}">
<meta http-equiv="refresh" content="0; url=https://godoc.org/gocloud.dev/{{path}}">
</head>
<body>
Nothing to see here; <a href="https://godoc.org/gocloud.dev/{{path}}">see the package on godoc</a>.
</body>
</html>

0 comments on commit e981f05

Please sign in to comment.