github.com/wiselike/revel-cmd@v1.2.1/revel/package.go (about)

     1  // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
     2  // Revel Framework source code and usage is governed by a MIT style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"github.com/wiselike/revel-cmd/model"
    14  	"github.com/wiselike/revel-cmd/utils"
    15  )
    16  
    17  var cmdPackage = &Command{
    18  	UsageLine: "package [-r [run mode]] [application] ",
    19  	Short:     "package a Revel application (e.g. for deployment)",
    20  	Long: `
    21  Package the Revel web application named by the given import path.
    22  This allows it to be deployed and run on a machine that lacks a Go installation.
    23  
    24  The run mode is used to select which set of app.conf configuration should
    25  apply and may be used to determine logic in the application itself.
    26  
    27  Run mode defaults to "dev".
    28  
    29  For example:
    30  
    31      revel package github.com/revel/examples/chat
    32  `,
    33  }
    34  
    35  func init() {
    36  	cmdPackage.RunWith = packageApp
    37  	cmdPackage.UpdateConfig = updatePackageConfig
    38  }
    39  
    40  // Called when unable to parse the command line automatically and assumes an old launch.
    41  func updatePackageConfig(c *model.CommandConfig, args []string) bool {
    42  	c.Index = model.PACKAGE
    43  	if len(args) == 0 && c.Package.ImportPath != "" {
    44  		return true
    45  	}
    46  	c.Package.ImportPath = args[0]
    47  	if len(args) > 1 {
    48  		c.Package.Mode = args[1]
    49  	}
    50  	return true
    51  }
    52  
    53  // Called to package the app.
    54  func packageApp(c *model.CommandConfig) (err error) {
    55  	// Determine the run mode.
    56  	mode := c.Package.Mode
    57  
    58  	appImportPath := c.ImportPath
    59  	revelPaths, err := model.NewRevelPaths(mode, appImportPath, c.AppPath, model.NewWrappedRevelCallback(nil, c.PackageResolver))
    60  	if err != nil {
    61  		return
    62  	}
    63  
    64  	// Remove the archive if it already exists.
    65  	destFile := filepath.Join(c.AppPath, filepath.Base(revelPaths.BasePath)+".tar.gz")
    66  	if c.Package.TargetPath != "" {
    67  		if filepath.IsAbs(c.Package.TargetPath) {
    68  			destFile = c.Package.TargetPath
    69  		} else {
    70  			destFile = filepath.Join(c.AppPath, c.Package.TargetPath)
    71  		}
    72  	}
    73  	if err := os.Remove(destFile); err != nil && !os.IsNotExist(err) {
    74  		return utils.NewBuildError("Unable to remove target file", "error", err, "file", destFile)
    75  	}
    76  
    77  	// Collect stuff in a temp directory.
    78  	tmpDir, err := ioutil.TempDir("", filepath.Base(revelPaths.BasePath))
    79  	utils.PanicOnError(err, "Failed to get temp dir")
    80  
    81  	// Build expects the command the build to contain the proper data
    82  	c.Build.Mode = c.Package.Mode
    83  	c.Build.TargetPath = tmpDir
    84  	c.Build.CopySource = c.Package.CopySource
    85  	if err = buildApp(c); err != nil {
    86  		return
    87  	}
    88  
    89  	// Create the zip file.
    90  
    91  	archiveName, err := utils.TarGzDir(destFile, tmpDir)
    92  	if err != nil {
    93  		return
    94  	}
    95  
    96  	fmt.Println("Your archive is ready:", archiveName)
    97  	return
    98  }