Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a version file to revel/cmd #137

Merged
merged 1 commit into from
Sep 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 80 additions & 5 deletions model/command_config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package model

// The constants
import (
"go/build"
"os"
"path/filepath"
"strings"
"github.com/revel/cmd/utils"
)

const (
NEW COMMAND = iota + 1
RUN
BUILD
PACKAGE
CLEAN
TEST
VERSION
)

type (
// The Revel command type
COMMAND int
Expand Down Expand Up @@ -28,31 +47,31 @@ type (
// The build command
Build struct {
TargetPath string `short:"t" long:"target-path" description:"Path to target folder. Folder will be completely deleted if it exists" required:"true"`
ImportPath string `short:"a" long:"application-path" description:"Path to applicaiton folder" required:"true"`
ImportPath string `short:"a" long:"application-path" description:"Path to applicaiton folder" `
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
CopySource bool `short:"s" long:"include-source" description:"Copy the source code as well"`
} `command:"build"`
// The run command
Run struct {
ImportPath string `short:"a" long:"application-path" description:"Path to applicaiton folder" required:"true"`
ImportPath string `short:"a" long:"application-path" description:"Path to applicaiton folder" `
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
Port string `short:"p" long:"port" description:"The port to listen"`
NoProxy bool `short:"n" long:"no-proxy" description:"True if proxy server should not be started. This will only update the main and routes files on change"`
} `command:"run"`
// The package command
Package struct {
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
ImportPath string `short:"a" long:"application-path" description:"Path to applicaiton folder" required:"true"`
ImportPath string `short:"a" long:"application-path" description:"Path to applicaiton folder" `
CopySource bool `short:"s" long:"include-source" description:"Copy the source code as well"`
} `command:"package"`
// The clean command
Clean struct {
ImportPath string `short:"a" long:"application-path" description:"Path to applicaiton folder" required:"true"`
ImportPath string `short:"a" long:"application-path" description:"Path to applicaiton folder" `
} `command:"clean"`
// The test command
Test struct {
Mode string `short:"m" long:"run-mode" description:"The mode to run the application in"`
ImportPath string `short:"a" long:"application-path" description:"Path to applicaiton folder" required:"true"`
ImportPath string `short:"a" long:"application-path" description:"Path to applicaiton folder" `
Function string `short:"f" long:"suite-function" description:"The suite.function"`
} `command:"test"`
// The version command
Expand All @@ -61,3 +80,59 @@ type (
} `command:"version"`
}
)

// Updates the import path depending on the command
func (c *CommandConfig) UpdateImportPath() bool {
var importPath string
required := true
switch c.Index {
case NEW:
importPath = c.New.ImportPath
case RUN:
importPath = c.Run.ImportPath
case BUILD:
importPath = c.Build.ImportPath
case PACKAGE:
importPath = c.Package.ImportPath
case CLEAN:
importPath = c.Clean.ImportPath
case TEST:
importPath = c.Test.ImportPath
case VERSION:
importPath = c.Version.ImportPath
required = false
}

if len(importPath) == 0 || filepath.IsAbs(importPath) || importPath[0] == '.' {
// Try to determine the import path from the GO paths and the command line
currentPath, err := os.Getwd()
if len(importPath) > 0 {
if importPath[0] == '.' {
// For a relative path
importPath = filepath.Join(currentPath, importPath)
}
// For an absolute path
currentPath, _ = filepath.Abs(importPath)

}
if err == nil {
for _, path := range strings.Split(build.Default.GOPATH, string(filepath.ListSeparator)) {
utils.Logger.Infof("Checking import path %s with %s", currentPath, path)
if strings.HasPrefix(currentPath, path) {
importPath = currentPath[len(path) + 1:]
// Remove the source from the path if it is there
if len(importPath)>4 && strings.ToLower(importPath[0:4]) == "src/" {
importPath = importPath[4:]
} else if importPath == "src" {
importPath = ""
}
utils.Logger.Info("Updated import path", "path", importPath)
}
}
}
}

c.ImportPath = importPath
utils.Logger.Info("Returned import path", "path", importPath)
return (len(importPath) > 0 || !required)
}
5 changes: 2 additions & 3 deletions revel/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func init() {

// The update config updates the configuration command so that it can run
func updateBuildConfig(c *model.CommandConfig, args []string) bool {
c.Index = BUILD
c.Index = model.BUILD
if len(args) < 2 {
fmt.Fprintf(os.Stderr, "%s\n%s", cmdBuild.UsageLine, cmdBuild.Long)
return false
Expand All @@ -52,8 +52,7 @@ func updateBuildConfig(c *model.CommandConfig, args []string) bool {

// The main entry point to build application from command line
func buildApp(c *model.CommandConfig) {
c.ImportPath = c.Build.ImportPath
appImportPath, destPath, mode := c.Build.ImportPath, c.Build.TargetPath, DefaultRunMode
appImportPath, destPath, mode := c.ImportPath, c.Build.TargetPath, DefaultRunMode
if len(c.Build.Mode) > 0 {
mode = c.Build.Mode
}
Expand Down
4 changes: 1 addition & 3 deletions revel/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {

// Update the clean command configuration, using old method
func updateCleanConfig(c *model.CommandConfig, args []string) bool {
c.Index = CLEAN
c.Index = model.CLEAN
if len(args) == 0 {
fmt.Fprintf(os.Stderr, cmdClean.Long)
return false
Expand All @@ -47,8 +47,6 @@ func updateCleanConfig(c *model.CommandConfig, args []string) bool {

// Clean the source directory of generated files
func cleanApp(c *model.CommandConfig) {
c.ImportPath = c.Clean.ImportPath

appPkg, err := build.Import(c.ImportPath, "", build.FindOnly)
if err != nil {
utils.Logger.Fatal("Abort: Failed to find import path:", "error", err)
Expand Down
4 changes: 1 addition & 3 deletions revel/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func init() {

// Called when unable to parse the command line automatically and assumes an old launch
func updateNewConfig(c *model.CommandConfig, args []string) bool {
c.Index = NEW
c.Index = model.NEW
if len(args) == 0 {
fmt.Fprintf(os.Stderr, cmdNew.Long)
return false
Expand All @@ -61,7 +61,6 @@ func updateNewConfig(c *model.CommandConfig, args []string) bool {
// Call to create a new application
func newApp(c *model.CommandConfig) {
// check for proper args by count
c.ImportPath = c.New.ImportPath
c.SkeletonPath = c.New.Skeleton

// Check for an existing folder so we dont clober it
Expand Down Expand Up @@ -125,7 +124,6 @@ func newApp(c *model.CommandConfig) {
fmt.Fprintln(os.Stdout, "Your application is ready:\n ", c.AppPath)
// Check to see if it should be run right off
if c.New.Run {
c.Run.ImportPath = c.ImportPath
runApp(c)
} else {
fmt.Fprintln(os.Stdout, "\nYou can run it with:\n revel run -a ", c.ImportPath)
Expand Down
5 changes: 2 additions & 3 deletions revel/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func init() {

// Called when unable to parse the command line automatically and assumes an old launch
func updatePackageConfig(c *model.CommandConfig, args []string) bool {
c.Index = PACAKAGE
c.Index = model.PACKAGE
if len(args) == 0 {
fmt.Fprintf(os.Stderr, cmdPackage.Long)
return false
Expand All @@ -60,7 +60,7 @@ func packageApp(c *model.CommandConfig) {
mode = c.Package.Mode
}

appImportPath := c.Package.ImportPath
appImportPath := c.ImportPath
revel_paths := model.NewRevelPaths(mode, appImportPath, "", model.DoNothingRevelCallback)

// Remove the archive if it already exists.
Expand All @@ -75,7 +75,6 @@ func packageApp(c *model.CommandConfig) {
utils.PanicOnError(err, "Failed to get temp dir")

// Build expects the command the build to contain the proper data
c.Build.ImportPath = appImportPath
if len(c.Package.Mode) >= 0 {
c.Build.Mode = c.Package.Mode
}
Expand Down
37 changes: 19 additions & 18 deletions revel/revel.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ func (cmd *Command) Name() string {
return name
}

// The constants
const (
NEW model.COMMAND = iota +1
RUN
BUILD
PACAKAGE
CLEAN
TEST
VERSION
)
// The commands
var commands = []*Command{
nil, // Safety net, prevent missing index from running
Expand Down Expand Up @@ -113,19 +103,19 @@ func main() {
} else {
switch parser.Active.Name {
case "new":
c.Index = NEW
c.Index = model.NEW
case "run":
c.Index = RUN
c.Index = model.RUN
case "build":
c.Index = BUILD
c.Index = model.BUILD
case "package":
c.Index = PACAKAGE
c.Index = model.PACKAGE
case "clean":
c.Index = CLEAN
c.Index = model.CLEAN
case "test":
c.Index = TEST
c.Index = model.TEST
case "version":
c.Index = VERSION
c.Index = model.VERSION
}
}
}
Expand All @@ -136,10 +126,18 @@ func main() {
} else {
utils.InitLogger(wd, logger.LvlWarn)
}

if c.Index==0 {
utils.Logger.Fatal("Unknown command line arguements")
}
if !c.UpdateImportPath() {
utils.Logger.Fatal("Unable to determine application path")
}
println("Revel executing:", commands[c.Index].Short)
// checking and setting go paths
initGoPaths(c)


commands[c.Index].RunWith(c)


Expand Down Expand Up @@ -292,7 +290,10 @@ func initGoPaths(c *model.CommandConfig) {
}

if len(c.SrcRoot) == 0 {
utils.Logger.Fatal("Abort: could not create a Revel application outside of GOPATH.")
if c.Index != model.VERSION {
utils.Logger.Fatal("Abort: could not create a Revel application outside of GOPATH.")
}
return
}

// set go src path
Expand Down
5 changes: 2 additions & 3 deletions revel/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,16 @@ func updateRunConfig(c *model.CommandConfig, args []string) bool {
case 0:
return false
}
c.Index = RUN
c.Index = model.RUN
return true
}

func runApp(c *model.CommandConfig) {
if c.Run.Mode == "" {
c.Run.Mode = "dev"
}
c.ImportPath = c.Run.ImportPath

revel_path := model.NewRevelPaths(c.Run.Mode, c.Run.ImportPath, "", model.DoNothingRevelCallback)
revel_path := model.NewRevelPaths(c.Run.Mode, c.ImportPath, "", model.DoNothingRevelCallback)
if c.Run.Port != "" {
port, err := strconv.Atoi(c.Run.Port)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions revel/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func init() {

// Called to update the config command with from the older stype
func updateTestConfig(c *model.CommandConfig, args []string) bool {
c.Index = TEST
c.Index = model.TEST
// The full test runs
// revel test <import path> (run mode) (suite(.function))
if len(args) < 1 {
Expand All @@ -78,10 +78,9 @@ func testApp(c *model.CommandConfig) {
if c.Test.Mode != "" {
mode = c.Test.Mode
}
c.ImportPath = c.Test.ImportPath

// Find and parse app.conf
revel_path := model.NewRevelPaths(mode, c.Test.ImportPath, "", model.DoNothingRevelCallback)
revel_path := model.NewRevelPaths(mode, c.ImportPath, "", model.DoNothingRevelCallback)

// Ensure that the testrunner is loaded in this mode.
// todo checkTestRunner()
Expand Down
Loading