added generate function to the ClI

This commit is contained in:
2021-08-27 14:01:06 +04:00
parent e5334cbd3c
commit 85bf56ec01
5 changed files with 111 additions and 30 deletions

View File

@@ -3,29 +3,43 @@
package generate
import (
"fmt"
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
"github.com/otiai10/copy"
"os"
"strings"
)
type NewProject struct {
Name string
NewDir string
P2PRCPath string
Option *copy.Options
}
// GenerateNewProject creates a new copy of the P2PRC
// project for custom modification
func GenerateNewProject(name string) error {
// Create new variable of type NewProject
var newProject NewProject
// Get path of the current directory
curDir := os.Getenv("PWD")
// Add slash to the end
curDir = curDir + "/"
curDir, err := config.GetCurrentPath()
if err != nil {
return err
}
// Folder name of the new generated project
NewProject := curDir + name + "/"
fmt.Println(NewProject)
newProject.NewDir = curDir + name + "/"
// Create a new folder based on name entered
err := CreateFolder(name, curDir)
err = CreateFolder(name, curDir)
if err != nil {
return err
}
// get path of P2PRC
P2PRCPATH := os.Getenv("P2PRC")
// Add slash to the end
P2PRCPATH = P2PRCPATH + "/"
P2PRCPATH, err := config.GetPathP2PRC()
if err != nil {
return err
}
// Assign P2PRC path to the newly generated project
newProject.P2PRCPath = P2PRCPATH
// Steps:
// - copy all files from P2PRC
// - remove go.mod and go.sum and create new ones
@@ -33,13 +47,21 @@ func GenerateNewProject(name string) error {
// Files we require to skip
var Options copy.Options
// Skips the appropriate files and directories not needed
// Skip or have the appropriate files and directories not needed
Options.Skip = func(src string) (bool, error) {
switch {
case strings.HasSuffix(src, "main.go"):
return false, nil
case strings.HasSuffix(src, ".go"):
return true, nil
case strings.HasSuffix(src, "go.mod"):
return true, nil
case strings.HasSuffix(src, "go.sum"):
return true, nil
case strings.HasSuffix(src, ".idea"):
return true, nil
case strings.HasSuffix(src, "Makefile"):
return true, nil
case strings.HasSuffix(src, name):
return true, nil
default:
@@ -47,16 +69,14 @@ func GenerateNewProject(name string) error {
}
}
// Storing type option in the struct new project
newProject.Option = &Options
// Copies all files from P2PRC to the new project created
err = copy.Copy(P2PRCPATH, NewProject,Options)
err = copy.Copy(newProject.P2PRCPath, newProject.NewDir,*newProject.Option)
if err != nil {
return err
}
// Installing new project
//cmd := exec.Command("sh","install.sh",name)
//if err := cmd.Run(); err != nil {
// return err
//}
return nil
}