github.com/jacobsoderblom/buffalo@v0.11.0/meta/app.go (about)

     1  package meta
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  	"runtime"
     9  
    10  	"github.com/gobuffalo/envy"
    11  	"github.com/markbates/inflect"
    12  )
    13  
    14  // App represents meta data for a Buffalo application on disk
    15  type App struct {
    16  	Pwd         string       `json:"pwd"`
    17  	Root        string       `json:"root"`
    18  	GoPath      string       `json:"go_path"`
    19  	Name        inflect.Name `json:"name"`
    20  	Bin         string       `json:"bin"`
    21  	PackagePkg  string       `json:"package_path"`
    22  	ActionsPkg  string       `json:"actions_path"`
    23  	ModelsPkg   string       `json:"models_path"`
    24  	GriftsPkg   string       `json:"grifts_path"`
    25  	VCS         string       `json:"vcs"`
    26  	WithPop     bool         `json:"with_pop"`
    27  	WithDep     bool         `json:"with_dep"`
    28  	WithWebpack bool         `json:"with_webpack"`
    29  	WithYarn    bool         `json:"with_yarn"`
    30  	WithDocker  bool         `json:"with_docker"`
    31  	WithGrifts  bool         `json:"with_grifts"`
    32  }
    33  
    34  // New App based on the details found at the provided root path
    35  func New(root string) App {
    36  	pwd, _ := os.Getwd()
    37  	if root == "." {
    38  		root = pwd
    39  	}
    40  	name := Name(filepath.Base(root))
    41  	pp := envy.CurrentPackage()
    42  	if filepath.Base(pp) != string(name) {
    43  		pp = path.Join(pp, string(name))
    44  	}
    45  
    46  	app := App{
    47  		Pwd:        pwd,
    48  		Root:       root,
    49  		GoPath:     envy.GoPath(),
    50  		Name:       name,
    51  		PackagePkg: pp,
    52  		ActionsPkg: pp + "/actions",
    53  		ModelsPkg:  pp + "/models",
    54  		GriftsPkg:  pp + "/grifts",
    55  	}
    56  
    57  	app.Bin = filepath.Join("bin", filepath.Base(root))
    58  
    59  	if runtime.GOOS == "windows" {
    60  		app.Bin += ".exe"
    61  	}
    62  
    63  	if _, err := os.Stat(filepath.Join(root, "database.yml")); err == nil {
    64  		app.WithPop = true
    65  	}
    66  	if _, err := os.Stat(filepath.Join(root, "Gopkg.toml")); err == nil {
    67  		app.WithDep = true
    68  	}
    69  	if _, err := os.Stat(filepath.Join(root, "webpack.config.js")); err == nil {
    70  		app.WithWebpack = true
    71  	}
    72  	if _, err := os.Stat(filepath.Join(root, "yarn.lock")); err == nil {
    73  		app.WithYarn = true
    74  	}
    75  	if _, err := os.Stat(filepath.Join(root, "Dockerfile")); err == nil {
    76  		app.WithDocker = true
    77  	}
    78  	if _, err := os.Stat(filepath.Join(root, "grifts")); err == nil {
    79  		app.WithGrifts = true
    80  	}
    81  	if _, err := os.Stat(filepath.Join(root, ".git")); err == nil {
    82  		app.VCS = "git"
    83  	} else if _, err := os.Stat(filepath.Join(root, ".bzr")); err == nil {
    84  		app.VCS = "bzr"
    85  	}
    86  
    87  	return app
    88  }
    89  
    90  func (a App) String() string {
    91  	b, _ := json.Marshal(a)
    92  	return string(b)
    93  }