added AST feature and test case
This commit is contained in:
0
generate/Test/.gitkeep
Normal file
0
generate/Test/.gitkeep
Normal 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
|
||||
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
|
||||
}
|
||||
|
||||
@@ -89,4 +100,15 @@ func CreateFolder(name string,path string) error {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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
49
generate/modfyGenerate.go
Normal 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
9
generate/testcaseAST.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func TestCaseAST() {
|
||||
fmt.Println("lol")
|
||||
}
|
||||
Reference in New Issue
Block a user