Skip to content

Commit

Permalink
go-aah/aah#200 cmd migrate update for go.mod creation and .gitignore …
Browse files Browse the repository at this point in the history
…update
  • Loading branch information
jeevatkm committed Nov 20, 2018
1 parent d47143c commit 94bf4e6
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 35 deletions.
92 changes: 89 additions & 3 deletions aah/migrate.go
Expand Up @@ -78,8 +78,9 @@ func migrateCodeAction(c *console.Context) error {
if err := app.InitForCLI(importPath); err != nil {
logFatal(err)
}
projectCfg := aahProjectCfg(app.BaseDir())
cliLog.Info("Loaded aah project file: ", filepath.Join(app.BaseDir(), aahProjectIdentifier))
appBaseDir := app.BaseDir()
projectCfg := aahProjectCfg(appBaseDir)
cliLog.Info("Loaded aah project file: ", filepath.Join(appBaseDir, aahProjectIdentifier))
cliLog = initCLILogger(projectCfg)

cliLog.Warn("Migrate command does not take file backup. Command assumes application use version control.")
Expand Down Expand Up @@ -115,7 +116,64 @@ func migrateCodeAction(c *console.Context) error {
}
}

// .gitignore
if ess.IsFileExists(".gitignore") {
gitIgnore, err := ioutil.ReadFile(".gitignore")
if err != nil {
cliLog.Error("Unable to read .gitignore file, so no auto migration. Please do it manually.")
} else {
gitIgnoreStr := string(gitIgnore)
replaceSet := []string{"build/"} // will be updated to "# aah application - end"
for _, entry := range []string{
"aah_*_vfs.go", "app/generated", "vendor/*/", "# aah application - end",
} {
if !strings.Contains(gitIgnoreStr, entry) {
replaceSet = append(replaceSet, entry)
}
}
if len(replaceSet) == 0 {
cliLog.Info("It seems file '.gitignore' are up-to-date")
} else {
cliLog.Info("Updating file '.gitignore' ...")
fixer := strings.NewReplacer("build/", strings.Join(replaceSet, "\n"))
gitIgnore = []byte(fixer.Replace(gitIgnoreStr))
if err = ioutil.WriteFile(".gitignore", gitIgnore, permRWXRXRX); err != nil {
cliLog.Error(err)
}
}
}
}

// go mod
aahBasePath := aahPath()
appTmplBaseDir := inferAppTmplBaseDir()
if ess.IsStrEmpty(appTmplBaseDir) {
logFatalf("Unable to find aah app template at %s/.aah/app-templates", aahBasePath)
}
cliLog.Infof("Creating file 'go.mod' for %s ...", app.Name())
if ess.IsFileExists("go.mod") {
cliLog.Info("File 'go.mod' already exists")
// TODO To be uncomment in next release
// if _, err = execCmd(gocmd, []string{"get", "aahframe.work@latest"}, false); err != nil {
// logError(err)
// }
} else {
modImportPath := filepath.Base(app.ImportPath())
if isInGoPath(appBaseDir) {
modImportPath = filepath.ToSlash(stripGoSrcPath(appBaseDir))
} else {
cliLog.Warn("Please check the file 'go.mod' and update your application import path.")
}
processFile(appBaseDir, file{
src: filepath.Join(appTmplBaseDir, "go.mod.atmpl"),
dst: filepath.Join(appBaseDir, "go.mod.atmpl"),
}, map[string]interface{}{
"App": appTmplData{ImportPath: modImportPath},
})
}

cliLog.Infof("Code migration successful for '%s' [%s]\n", app.Name(), app.ImportPath())
cliLog.Warn("PLEASE MOVE YOUR aah PROJECT OUTSIDE THE 'GOPATH'.")
return nil
}

Expand Down Expand Up @@ -202,7 +260,7 @@ func migrateViewFiles(projectCfg, grammarCfg *config.Config) int {

func migrateFile(f string, fixer *strings.Replacer) bool {
df := filepath.ToSlash(strings.TrimPrefix(f, aah.App().BaseDir()+"/"))
if strings.Index(filepath.ToSlash(aah.App().BaseDir()), "/src/") > 0 {
if isInGoPath(aah.App().BaseDir()) {
df = strings.TrimPrefix(filepath.ToSlash(stripGoSrcPath(f)), aah.App().ImportPath()+"/")
}
fileBytes, err := ioutil.ReadFile(f)
Expand Down Expand Up @@ -243,3 +301,31 @@ func migrateFile(f string, fixer *strings.Replacer) bool {

return true
}

func checkAndGenerateInitgoFile(importPath, baseDir string, appCfg *config.Config) {
initGoFile := filepath.Join(baseDir, "app", "init.go")
if !ess.IsFileExists(initGoFile) {
cliLog.Warn("***** In aah v0.10 'init.go' file introduced to evolve aah framework." +
" Since its not found, generating 'init.go' file. Please add 'init.go' into VCS. *****\n")

appTmplBaseDir := inferAppTmplBaseDir()
if ess.IsStrEmpty(appTmplBaseDir) {
logFatalf("Unable to find aah app template at %s/.aah/app-templates", aahPath())
}
appType := typeAPI
if ess.IsFileExists(filepath.Join(baseDir, "views")) {
appType = typeWeb
}
data := map[string]interface{}{
"App": &appTmplData{
Type: appType,
ViewEngine: appCfg.StringDefault("view.engine", "go"),
},
}

processFile(baseDir, file{
src: filepath.Join(appTmplBaseDir, "app", "init.go.atmpl"),
dst: filepath.Join(baseDir, "app", "init.go"),
}, data)
}
}
31 changes: 1 addition & 30 deletions aah/new.go
Expand Up @@ -16,7 +16,6 @@ import (
"path/filepath"
"strings"

"aahframe.work"
"aahframe.work/console"
"aahframe.work/essentials"
)
Expand Down Expand Up @@ -518,6 +517,7 @@ func processFile(appBaseDir string, f file, data map[string]interface{}) {
// open src and create dst
sf, _ := os.Open(f.src)
df, _ := os.Create(dst)
defer ess.CloseQuietly(df, sf)

// render or write it directly
if strings.HasSuffix(f.src, aahTmplExt) {
Expand All @@ -539,42 +539,13 @@ func processFile(appBaseDir string, f file, data map[string]interface{}) {
}

_ = ess.ApplyFileMode(dst, permRWRWRW)
ess.CloseQuietly(sf, df)
}

func isAuthSchemeSupported(authScheme string) bool {
return ess.IsStrEmpty(authScheme) || authScheme == authForm || authScheme == authBasic ||
authScheme == authGeneric || authScheme == authNone
}

func checkAndGenerateInitgoFile(importPath, baseDir string) {
initGoFile := filepath.Join(baseDir, "app", "init.go")
if !ess.IsFileExists(initGoFile) {
cliLog.Warn("***** In aah v0.10 'init.go' file introduced to evolve aah framework." +
" Since its not found, generating 'init.go' file. Please add 'init.go' into VCS. *****\n")

appTmplBaseDir := inferAppTmplBaseDir()
if ess.IsStrEmpty(appTmplBaseDir) {
logFatal("Unable to find aah app template at $HOME/.aah/app-templates")
}
appType := typeAPI
if ess.IsFileExists(filepath.Join(baseDir, "views")) {
appType = typeWeb
}
data := map[string]interface{}{
"App": &appTmplData{
Type: appType,
ViewEngine: aah.App().Config().StringDefault("view.engine", "go"),
},
}

processFile(baseDir, file{
src: filepath.Join(appTmplBaseDir, "app", "init.go.atmpl"),
dst: filepath.Join(baseDir, "app", "init.go"),
}, data)
}
}

const templateBranchName = "0.12.x"

func inferAppTmplBaseDir() string {
Expand Down
2 changes: 1 addition & 1 deletion aah/run.go
Expand Up @@ -81,7 +81,7 @@ func runAction(c *console.Context) error {
}
projectCfg := aahProjectCfg(app.BaseDir())
cliLog = initCLILogger(projectCfg)
checkAndGenerateInitgoFile(importPath, app.BaseDir())
checkAndGenerateInitgoFile(importPath, app.BaseDir(), app.Config())
cliLog.Infof("Loaded aah project file: %s", filepath.Join(app.BaseDir(), aahProjectIdentifier))

// Hot-Reload is applicable only to `dev` environment profile.
Expand Down
2 changes: 1 addition & 1 deletion aah/runcmd.go
Expand Up @@ -46,7 +46,7 @@ func runConsoleCmdAction(c *console.Context) error {
}
projectCfg := aahProjectCfg(app.BaseDir())
cliLog = initCLILogger(projectCfg)
checkAndGenerateInitgoFile(importPath, app.BaseDir())
checkAndGenerateInitgoFile(importPath, app.BaseDir(), app.Config())
cliLog.Infof("Loaded aah project file: %s", filepath.Join(app.BaseDir(), aahProjectIdentifier))

cleanupAutoGenFiles(app.BaseDir())
Expand Down
4 changes: 4 additions & 0 deletions aah/util.go
Expand Up @@ -350,6 +350,10 @@ func stripGoSrcPath(pkgFilePath string) string {
return filepath.Clean(pkgFilePath[idx+4:])
}

func isInGoPath(p string) bool {
return strings.Index(filepath.ToSlash(p), "/src/") > 0
}

func libDependencyImports(importPath string) []string {
args := []string{"list", "-f", "{{.Imports}}", importPath}
output, err := execCmd(gocmd, args, false)
Expand Down

0 comments on commit 94bf4e6

Please sign in to comment.