github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/godoc/setup-godoc-app.bash (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2011 The Go Authors. All rights reserved.
     4  # Use of this source code is governed by a BSD-style
     5  # license that can be found in the LICENSE file.
     6  
     7  # This script creates a complete godoc app in $APPDIR.
     8  # It copies the cmd/godoc and src/pkg/go/... sources from GOROOT,
     9  # synthesizes an app.yaml file, and creates the .zip, index, and
    10  # configuration files.
    11  #
    12  # If an argument is provided it is assumed to be the app-engine godoc directory.
    13  # Without an argument, $APPDIR is used instead. If GOROOT is not set, "go env"
    14  # is consulted to find the $GOROOT.
    15  #
    16  # The script creates a .zip file representing the $GOROOT file system
    17  # and computes the correspondig search index files. These files are then
    18  # copied to $APPDIR. A corresponding godoc configuration file is created
    19  # in $APPDIR/appconfig.go.
    20  
    21  ZIPFILE=godoc.zip
    22  INDEXFILE=godoc.index
    23  SPLITFILES=index.split.
    24  CONFIGFILE=godoc/appconfig.go
    25  
    26  error() {
    27  	echo "error: $1"
    28  	exit 2
    29  }
    30  
    31  getArgs() {
    32  	if [ -z $GOROOT ]; then
    33  		GOROOT=$(go env GOROOT)
    34  		echo "GOROOT not set explicitly, using $GOROOT instead"
    35  	fi
    36  	if [ -z $APPDIR ]; then
    37  		if [ $# == 0 ]; then
    38  			error "APPDIR not set, and no argument provided"
    39  		fi
    40  		APPDIR=$1
    41  		echo "APPDIR not set, using argument instead"
    42  	fi
    43  	
    44  	# safety checks
    45  	if [ ! -d $GOROOT ]; then
    46  		error "$GOROOT is not a directory"
    47  	fi
    48  	if [ ! -x $GOROOT/bin/godoc ]; then
    49  		error "$GOROOT/bin/godoc does not exist or is not executable"
    50  	fi
    51  	if [ -e $APPDIR ]; then
    52  		error "$APPDIR exists; check and remove it before trying again"
    53  	fi
    54  
    55  	# reporting
    56  	echo "GOROOT = $GOROOT"
    57  	echo "APPDIR = $APPDIR"
    58  }
    59  
    60  copyGodoc() {
    61  	echo "*** copy $GOROOT/src/cmd/godoc to $APPDIR/godoc"
    62  	cp -r $GOROOT/src/cmd/godoc $APPDIR/godoc
    63  }
    64  
    65  copyGoPackages() {
    66  	echo "*** copy $GOROOT/src/pkg/go to $APPDIR/newgo and rewrite imports"
    67  	cp -r $GOROOT/src/pkg/go $APPDIR/newgo
    68  	find $APPDIR/newgo -type d -name testdata | xargs rm -r
    69  	gofiles=$(find $APPDIR -name '*.go')
    70  	sed -i '' 's_^\(."\)\(go/[a-z]*\)"$_\1new\2"_' $gofiles
    71  	sed -i '' 's_^\(import "\)\(go/[a-z]*\)"$_\1new\2"_' $gofiles
    72  }
    73  
    74  makeAppYaml() {
    75  	echo "*** make $APPDIR/app.yaml"
    76  	cat > $APPDIR/app.yaml <<EOF
    77  application: godoc
    78  version: 1
    79  runtime: go
    80  api_version: go1
    81  
    82  handlers:
    83  - url: /.*
    84    script: _go_app
    85  EOF
    86  }
    87  
    88  makeZipfile() {
    89  	echo "*** make $APPDIR/$ZIPFILE"
    90  	zip -q -r $APPDIR/$ZIPFILE $GOROOT -i \*.go -i \*.html -i \*.xml -i \*.css -i \*.js -i \*.txt -i \*.c -i \*.h -i \*.s -i \*.png -i \*.jpg -i \*.sh -i \*.ico
    91  }
    92  
    93  makeIndexfile() {
    94  	echo "*** make $APPDIR/$INDEXFILE"
    95  	OUT=/tmp/godoc.out
    96  	$GOROOT/bin/godoc -write_index -index_files=$APPDIR/$INDEXFILE -zip=$APPDIR/$ZIPFILE 2> $OUT
    97  	if [ $? != 0 ]; then
    98  		error "$GOROOT/bin/godoc failed - see $OUT for details"
    99  	fi
   100  }
   101  
   102  splitIndexfile() {
   103  	echo "*** split $APPDIR/$INDEXFILE"
   104  	split -b8m $APPDIR/$INDEXFILE $APPDIR/$SPLITFILES
   105  }
   106  
   107  makeConfigfile() {
   108  	echo "*** make $APPDIR/$CONFIGFILE"
   109  	cat > $APPDIR/$CONFIGFILE <<EOF
   110  package main
   111  
   112  // GENERATED FILE - DO NOT MODIFY BY HAND.
   113  // (generated by $GOROOT/src/cmd/godoc/setup-godoc-app.bash)
   114  
   115  const (
   116  	// .zip filename
   117  	zipFilename = "$ZIPFILE"
   118  
   119  	// goroot directory in .zip file
   120  	zipGoroot = "$GOROOT"
   121  
   122  	// glob pattern describing search index files
   123  	// (if empty, the index is built at run-time)
   124  	indexFilenames = "$SPLITFILES*"
   125  )
   126  EOF
   127  }
   128  
   129  getArgs "$@"
   130  set -e
   131  mkdir $APPDIR
   132  copyGodoc
   133  copyGoPackages
   134  makeAppYaml
   135  makeZipfile
   136  makeIndexfile
   137  splitIndexfile
   138  makeConfigfile
   139  
   140  echo "*** setup complete"