127 lines
3 KiB
Go
127 lines
3 KiB
Go
package project
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/dialog"
|
|
"fyne.io/fyne/v2/widget"
|
|
)
|
|
|
|
type Project struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
FolderName string `json:"folderName"`
|
|
}
|
|
|
|
func DeployProject(index int, projects []Project, projectsFS embed.FS, mainWindow fyne.Window) {
|
|
project := projects[index]
|
|
|
|
homeDir, err := os.UserHomeDir()
|
|
if err != nil {
|
|
dialog.ShowError(fmt.Errorf("error getting home directory: %v", err), mainWindow)
|
|
return
|
|
}
|
|
|
|
projectPath := filepath.Join(homeDir, "Projects", project.FolderName)
|
|
|
|
confirmDialog := dialog.NewConfirm(
|
|
"deploying project",
|
|
fmt.Sprintf("the project '%s' will be deployed to '%s' and opend in VS Code. Continue?", project.Name, projectPath),
|
|
func(ok bool) {
|
|
if ok {
|
|
progress := widget.NewProgressBar()
|
|
progressDiag := dialog.NewCustomWithoutButtons("project will be deployed", progress, mainWindow)
|
|
|
|
progressDiag.Show()
|
|
|
|
go func() {
|
|
defer progressDiag.Hide()
|
|
|
|
progress.SetValue(0.1)
|
|
err := os.MkdirAll(projectPath, 0755)
|
|
if err != nil {
|
|
dialog.ShowError(fmt.Errorf("error creating directory: %v", err), mainWindow)
|
|
return
|
|
}
|
|
|
|
progress.SetValue(0.3)
|
|
err = CopyEmbeddedProject(projectsFS, project.FolderName, projectPath)
|
|
if err != nil {
|
|
dialog.ShowError(fmt.Errorf("error copying project files: %v", err), mainWindow)
|
|
return
|
|
}
|
|
|
|
progress.SetValue(0.7)
|
|
|
|
var openCmd *exec.Cmd
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
openCmd = exec.Command("code", projectPath)
|
|
case "darwin":
|
|
openCmd = exec.Command("open", "-a", "Visual Studio Code", projectPath)
|
|
case "linux":
|
|
openCmd = exec.Command("code", projectPath)
|
|
}
|
|
|
|
if openCmd != nil {
|
|
progress.SetValue(0.9)
|
|
err = openCmd.Run()
|
|
if err != nil {
|
|
dialog.ShowError(fmt.Errorf("error opening VS Code: %v", err), mainWindow)
|
|
return
|
|
}
|
|
}
|
|
|
|
progress.SetValue(1.0)
|
|
|
|
dialog.ShowInformation("project deployed",
|
|
fmt.Sprintf("the project '%s' was successfully deployed and opend in VS Code.\n\n"+
|
|
"Path: %s\n\n"+
|
|
"Application can be started with Docker Compose.",
|
|
project.Name, projectPath), mainWindow)
|
|
}()
|
|
}
|
|
},
|
|
mainWindow,
|
|
)
|
|
confirmDialog.Show()
|
|
}
|
|
|
|
func CopyEmbeddedProject(projectsFS embed.FS, projectFolder string, targetPath string) error {
|
|
sourcePath := fmt.Sprintf("projects/%s", projectFolder)
|
|
|
|
return fs.WalkDir(projectsFS, sourcePath, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
relPath, err := filepath.Rel(sourcePath, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
destPath := filepath.Join(targetPath, relPath)
|
|
|
|
if d.IsDir() {
|
|
return os.MkdirAll(destPath, 0755)
|
|
}
|
|
|
|
data, err := projectsFS.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil {
|
|
return err
|
|
}
|
|
|
|
return os.WriteFile(destPath, data, 0644)
|
|
})
|
|
}
|