-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
154 lines (120 loc) · 3.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"
"time"
c "github.com/otiai10/copy"
"github.com/toyz/imvu-patcher/core"
)
var working_folder = ""
var versions_folder = ""
var logPath = ""
func main() {
user, err := user.Current()
if err != nil {
panic(err)
}
working_folder = path.Join(user.HomeDir, ".imvu-patcher")
makeDir(working_folder)
versions_folder = path.Join(working_folder, "versions")
makeDir(versions_folder)
logPath = path.Join(working_folder, "logs")
makeDir(logPath)
log.Printf("Working in directory: %s", working_folder)
log.Printf("Running on: %s", runtime.GOOS)
os.Chdir(working_folder)
items := core.VersionList()
latest := items[len(items)-1]
versionFolder := path.Join(versions_folder, latest.Hash)
logPath = versionFolder
if !existsDir(versionFolder) {
patch(latest, versionFolder)
if runtime.GOOS == "linux" {
log.Println("Patching paths for linux host")
filepath.Walk(versionFolder, visit)
}
log.Println("Installing electron")
cmd := exec.Command("npm", "install", "electron", "--save-dev")
cmd.Dir = path.Join(versionFolder, "app")
cmdReader, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
os.Exit(1)
}
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
fmt.Printf("%s\n", scanner.Text())
}
}()
cmd.Run()
}
ioutil.WriteFile(path.Join(versionFolder, "app", "package.json"), core.CreatePackageJson(path.Join(versionFolder, "app", "package.json")), 0755)
log.Printf("Launching from: %s", path.Join(versionFolder, "app"))
cmd := exec.Command("npm", "start")
cmd.Dir = path.Join(versionFolder, "app")
if err := cmd.Start(); err != nil {
log.Panicln(err)
}
time.Sleep(5 * time.Second)
}
func patch(latest core.Versions, versionPath string) {
zip := latest.Download()
log.Printf("Downloaded: %s", zip.Filename)
zip.Extract()
c.Copy(path.Join(zip.Path, zip.FileNameNoExt, "IMVU Desktop.app", "Contents", "Resources", "app"), path.Join(versionPath, "app"))
os.RemoveAll(path.Join(zip.Path, zip.FileNameNoExt))
os.Remove(path.Join(zip.Path, zip.Filename))
ioutil.WriteFile("latest", []byte(latest.String()), 0755)
}
func visit(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if !!fi.IsDir() {
return nil //
}
matched, err := filepath.Match("*.js", fi.Name())
if err != nil {
panic(err)
return err
}
if matched {
read, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
newContents := ""
newContents = strings.Replace(string(read), "/var', 'local", logPath, -1)
newContents = strings.Replace(newContents, "/var', 'log", logPath, -1)
err = ioutil.WriteFile(path, []byte(newContents), 0)
if err != nil {
panic(err)
}
}
return nil
}
func makeDir(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0755)
}
}
func existsDir(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}