thestrukture/IDE

View on GitHub
launcher.go

Summary

Maintainability
A
3 hrs
Test Coverage
// File generated by Gopher Sauce
// DO NOT EDIT!!
package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"
    "runtime"
    "strings"

    "github.com/cheikhshift/gos/core"
    "github.com/ncruces/rethinkraw/pkg/chrome"
    "github.com/thestrukture/IDE/api/globals"
    "github.com/thestrukture/IDE/api/methods"
    "github.com/thestrukture/IDE/types"
)

func LaunchServer() {

    globals.Dfd = os.ExpandEnv("$GOPATH")
    globals.Windows = strings.Contains(runtime.GOOS, "windows")
    trailerEx := ""

    fmt.Println("Using $GOPATH @ home/workspace")
    if globals.Windows {
        os.Chdir(os.ExpandEnv("$USERPROFILE"))
        trailerEx = ".exe"
    } else {
        os.Chdir(os.ExpandEnv("$HOME"))
    }

    err := os.MkdirAll("workspace/", 0700)
    if err != nil {
        fmt.Println(err.Error())

    } else {
        //download go
        os.MkdirAll("workspace/src", 0700)
        os.MkdirAll("workspace/bin", 0700)
        os.MkdirAll("workspace/cache", 0700)
        os.MkdirAll("workspace/userData", 0700)
        cwd, _ := os.Getwd()
        cwd = cwd + "/workspace"
        os.Setenv("GOPATH", cwd)
        pathbin := os.ExpandEnv("$PATH")
        if globals.Windows {
            os.Setenv("PATH", pathbin+":"+strings.Replace(cwd+"/bin", "/", "\\", -1))
        } else {
            os.Setenv("PATH", pathbin+":"+cwd+"/bin")
        }

        globals.Dfd = cwd

    }

    if _, err := os.Stat(os.ExpandEnv("$GOPATH") + "/bin/gocode" + trailerEx); os.IsNotExist(err) {
        fmt.Println("Go code completion not present, installing from github.com/mdempsky/gocode")
        if globals.Windows {
            //
            core.RunCmdSmart("go get -u -ldflags -H=windowsgui github.com/mdempsky/gocode")
        } else {

            _, err1 := core.RunCmdSmart("go install github.com/mdempsky/gocode@latest")

            if err1 != nil {
                log.Fatal(err1)
            }
        }

    }

    if _, err := os.Stat(os.ExpandEnv("$GOPATH") + "/bin/dlv" + trailerEx); os.IsNotExist(err) {
        fmt.Println("Delve not present, installing from github.com/go-delve/delve/cmd/dlv")

        _, err1 := core.RunCmdSmart("go install github.com/go-delve/delve/cmd/dlv@latest")

        if err1 != nil {
            log.Fatal(err1)
        }

    }

    if _, err := os.Stat(os.ExpandEnv("$GOPATH") + "/bin/golint" + trailerEx); os.IsNotExist(err) {
        fmt.Println("Golint not present, installing from golang.org/x/lint/golint")

        _, err1 := core.RunCmdSmart("go install golang.org/x/lint/golint@latest")

        if err1 != nil {
            log.Fatal(err1)
        }

    }

    globals.AutocompletePath = filepath.Join(os.ExpandEnv("$GOPATH"), "strukture-autocomplete")

    if _, err := os.Stat(globals.AutocompletePath); os.IsNotExist(err) {
        fmt.Println("Creating autocomplete resource folder at " + globals.AutocompletePath)
        os.MkdirAll(globals.AutocompletePath, 0700)
    }

    apps := methods.GetApps()
    newapps := []types.App{}
    for _, app := range apps {
        if app.Name != "" {
            app.Pid = ""
            newapps = append(newapps, app)
        }
    }
    methods.SaveApps(newapps)

    log.Println("Strukture up on port 8884")

    if len(os.Args) == 1 {

        go func() {

            dataDir := filepath.Join(globals.Dfd, "userData")
            cache := filepath.Join(globals.Dfd, "cache")
            c := chrome.Command("http://localhost:8884", dataDir, cache)

            err := c.Run()

            if err != nil {
                log.Fatal(err)
            }

            os.Exit(0)
        }()
    }

}