Check project directory is correct and ensure there's a go.mod file
This commit is contained in:
parent
df9c7f9cf7
commit
80871fc02a
1 changed files with 32 additions and 4 deletions
|
|
@ -2,17 +2,18 @@ package astgen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/maddalax/htmgo/cli/htmgo/internal/dirutil"
|
|
||||||
"github.com/maddalax/htmgo/cli/htmgo/tasks/process"
|
|
||||||
"github.com/maddalax/htmgo/framework/h"
|
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"golang.org/x/mod/modfile"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/maddalax/htmgo/cli/htmgo/internal/dirutil"
|
||||||
|
"github.com/maddalax/htmgo/cli/htmgo/tasks/process"
|
||||||
|
"github.com/maddalax/htmgo/framework/h"
|
||||||
|
"golang.org/x/mod/modfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Page struct {
|
type Page struct {
|
||||||
|
|
@ -390,9 +391,20 @@ func writePagesFile() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HasModuleFile(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return !os.IsNotExist(err)
|
||||||
|
}
|
||||||
|
|
||||||
func GetModuleName() string {
|
func GetModuleName() string {
|
||||||
wd := process.GetWorkingDir()
|
wd := process.GetWorkingDir()
|
||||||
modPath := filepath.Join(wd, "go.mod")
|
modPath := filepath.Join(wd, "go.mod")
|
||||||
|
|
||||||
|
if HasModuleFile(modPath) == false {
|
||||||
|
fmt.Fprintf(os.Stderr, "Module not found: go.mod file does not exists.")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
goModBytes, err := os.ReadFile(modPath)
|
goModBytes, err := os.ReadFile(modPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error reading go.mod: %v\n", err)
|
fmt.Fprintf(os.Stderr, "error reading go.mod: %v\n", err)
|
||||||
|
|
@ -402,6 +414,18 @@ func GetModuleName() string {
|
||||||
return modName
|
return modName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func matchProjectDirectory() bool {
|
||||||
|
wd := process.GetWorkingDir()
|
||||||
|
dirs := strings.Split(wd, "/")
|
||||||
|
|
||||||
|
projectDir := dirs[len(dirs)-1]
|
||||||
|
|
||||||
|
if GetModuleName() == projectDir {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func GenAst(flags ...process.RunFlag) error {
|
func GenAst(flags ...process.RunFlag) error {
|
||||||
if GetModuleName() == "" {
|
if GetModuleName() == "" {
|
||||||
if slices.Contains(flags, process.ExitOnError) {
|
if slices.Contains(flags, process.ExitOnError) {
|
||||||
|
|
@ -429,5 +453,9 @@ func GenAst(flags ...process.RunFlag) error {
|
||||||
`, ChiModuleName)
|
`, ChiModuleName)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if matchProjectDirectory() == false {
|
||||||
|
return fmt.Errorf("The project directory does not match with the project name.")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue