added AST feature and test case

This commit is contained in:
2021-08-28 21:46:36 +04:00
parent 85bf56ec01
commit 8fd9ccfa2d
9 changed files with 133 additions and 6 deletions

1
.gitignore vendored
View File

@@ -17,3 +17,4 @@ plugin/deploy/
client/trackcontainers/
# Test generated files
generate/p2prctest
generate/Test

View File

@@ -249,7 +249,7 @@ var CliAction = func(ctx *cli.Context) error {
// of the project is created to repurpose
// the project for custom purpose
if Generate != "" {
err := generate.GenerateNewProject(Generate)
err := generate.GenerateNewProject(Generate,"test")
if err != nil {
fmt.Println(err)
} else {

0
generate/Test/.gitkeep Normal file
View File

View File

@@ -5,22 +5,31 @@ package generate
import (
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
"github.com/otiai10/copy"
"go/ast"
"go/token"
"os"
"os/exec"
"strings"
)
type NewProject struct {
Name string
Module string
NewDir string
P2PRCPath string
Option *copy.Options
Token *token.FileSet
AST *ast.File
FileNameAST string
}
// GenerateNewProject creates a new copy of the P2PRC
// project for custom modification
func GenerateNewProject(name string) error {
func GenerateNewProject(name string, module string) error {
// Create new variable of type NewProject
var newProject NewProject
//Setting module name to the new project
newProject.Module = module
// Get path of the current directory
curDir, err := config.GetCurrentPath()
if err != nil {
@@ -78,6 +87,8 @@ func GenerateNewProject(name string) error {
return err
}
// Creating a new go.mod file in the appropriate directory
return nil
}
@@ -90,3 +101,14 @@ func CreateFolder(name string,path string) error {
}
return nil
}
// CreateGoMod Creates a new go module for the new project created
func (a *NewProject)CreateGoMod() error {
// Create new go.mod in the appropriate directory
cmd := exec.Command("go","mod","init",a.Module)
cmd.Dir = a.NewDir
if err := cmd.Start(); err != nil {
return err
}
return nil
}

View File

@@ -2,6 +2,7 @@ package generate
import (
"fmt"
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
"testing"
)
@@ -18,9 +19,51 @@ func TestCreateFolder(t *testing.T) {
// Testing if a new project is created successfully
func TestGenerateNewProject(t *testing.T) {
// Checking if a new project is created successfully
err := GenerateNewProject("p2prctest")
err := GenerateNewProject("p2prctest","p2prctest")
if err != nil {
fmt.Println(err)
t.Error(err)
}
}
// Testing AST function to ensure imports are
// working as intended
func TestChangingImportAST(t *testing.T) {
// Create a new variable of type
var np NewProject
// Get current directory
path, err := config.GetCurrentPath()
if err != nil {
fmt.Println(err)
t.Error(err)
}
// Create testcase scenario
err = config.Copy(path + "testcaseAST.go", path + "/Test/testcaseAST.go")
if err != nil {
fmt.Println(err)
t.Error(err)
}
// Sets new directory to the folder test
np.NewDir = path + "Test/"
// Sets file name to be opened and modified in the AST
np.FileNameAST = path + "Test/" + "testcaseAST.go"
// Call the Read AST function
err = np.GetASTGoFile()
if err != nil {
fmt.Println(err)
t.Error(err)
}
// Change an import
err = np.ChangeImports("fmt", "lolol")
if err != nil {
fmt.Println(err)
t.Error(err)
}
// Write those saved changes
err = np.WriteGoAst()
if err != nil {
fmt.Println(err)
t.Error(err)
}
}

49
generate/modfyGenerate.go Normal file
View File

@@ -0,0 +1,49 @@
package generate
import (
"go/parser"
"go/printer"
"go/token"
"os"
)
// GetASTGoFile Gets AST of the Go file provided
func (np *NewProject)GetASTGoFile() error{
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, np.FileNameAST, nil, parser.ParseComments)
if err != nil {
return err
}
//Write Token information to the struct
np.Token = fset
// Write AST information the struct
np.AST = node
return nil
}
// ChangeImports Changes import of the AST
func (np *NewProject)ChangeImports(CurrentImport string,ChangedImport string) error {
// Iterating through the loop and changing the appropriate import
for i, spec := range np.AST.Imports {
// If the current import is found then change it
if spec.Path.Value == "\"" + CurrentImport + "\"" {
np.AST.Imports[i].Path.Value = "\"" + ChangedImport + "\""
}
}
return nil
}
// WriteGoAst Write changed imports back to the AST
func (np *NewProject)WriteGoAst() error {
// write new AST to file
f, err := os.Create(np.FileNameAST)
if err != nil {
return nil
}
defer f.Close()
if err := printer.Fprint(f, np.Token, np.AST); err != nil {
return err
}
return nil
}

9
generate/testcaseAST.go Normal file
View File

@@ -0,0 +1,9 @@
package generate
import (
"fmt"
)
func TestCaseAST() {
fmt.Println("lol")
}

1
go.mod
View File

@@ -7,6 +7,7 @@ require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/apenella/go-ansible v1.1.0
github.com/containerd/continuity v0.0.0-20210315143101-93e15499afd5 // indirect
github.com/dave/jennifer v1.4.1
github.com/docker/docker v20.10.0-beta1.0.20201113105859-b6bfff2a628f+incompatible
github.com/docker/go-connections v0.4.0
github.com/gin-gonic/gin v1.6.3

2
go.sum
View File

@@ -177,6 +177,8 @@ github.com/d2g/dhcp4 v0.0.0-20170904100407-a1d1b6c41b1c/go.mod h1:Ct2BUK8SB0YC1S
github.com/d2g/dhcp4client v1.0.0/go.mod h1:j0hNfjhrt2SxUOw55nL0ATM/z4Yt3t2Kd1mW34z5W5s=
github.com/d2g/dhcp4server v0.0.0-20181031114812-7d4a0a7f59a5/go.mod h1:Eo87+Kg/IX2hfWJfwxMzLyuSZyxSoAug2nGa1G2QAi8=
github.com/d2g/hardwareaddr v0.0.0-20190221164911-e7d9fbe030e4/go.mod h1:bMl4RjIciD2oAxI7DmWRx6gbeqrkoLqv3MV0vzNad+I=
github.com/dave/jennifer v1.4.1 h1:XyqG6cn5RQsTj3qlWQTKlRGAyrTcsk1kUmWdZBzRjDw=
github.com/dave/jennifer v1.4.1/go.mod h1:7jEdnm+qBcxl8PC0zyp7vxcpSRnzXSt9r39tpTVGlwA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=