1// Copyright 2011 Google Inc. All rights reserved. 2// Use of this source code is governed by the Apache 2.0 3// license that can be found in the LICENSE file. 4 5package main 6 7import ( 8 "bytes" 9 "text/template" 10) 11 12// MakeMain creates the synthetic main package for a Go App Engine app. 13func MakeMain(app *App) (string, error) { 14 buf := new(bytes.Buffer) 15 if err := mainTemplate.Execute(buf, app); err != nil { 16 return "", err 17 } 18 return buf.String(), nil 19} 20 21// MakeExtraImports creates the synthetic extra-imports file for a single package. 22func MakeExtraImports(packageName string, extraImports []string) (string, error) { 23 buf := new(bytes.Buffer) 24 err := extraImportsTemplate.Execute(buf, struct { 25 PackageName string 26 ExtraImports []string 27 }{ 28 packageName, 29 extraImports, 30 }) 31 if err != nil { 32 return "", err 33 } 34 return buf.String(), nil 35} 36 37var mainTemplate = template.Must(template.New("main").Parse( 38 `package main 39 40import ( 41 internal "appengine_internal" 42 43 // Top-level app packages 44 {{range .RootPackages}} 45 _ "{{.ImportPath}}" 46 {{end}} 47) 48 49func main() { 50 internal.Main() 51} 52`)) 53 54var extraImportsTemplate = template.Must(template.New("extraImports").Parse( 55 `package {{.PackageName}} 56 57import ( 58 {{range .ExtraImports}} 59 _ "{{.}}" 60 {{end}} 61) 62`)) 63