-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.go
119 lines (109 loc) · 2.42 KB
/
launcher.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
type RunConfig struct {
Name string `json:"name"` // Flutter
Request string `json:"request"` // attach
DeviceId string `json:"deviceId"` // flutter-tester
ObservatoryUri string `json:"observatoryUri"` // dynamic
Type string `json:"type"` // dart
}
type Launcher struct {
Version string `json:"version"` // 0.2.0
Configurations []interface{} `json:"configurations"` // RunConfig
}
var url string
var path string
var launchConfig string
const file string = "/.vscode/launch.json"
func main() {
cmd := exec.Command("./flutter_engine.exe")
out, _ := cmd.StdoutPipe()
err, _ := cmd.StderrPipe()
defer out.Close()
defer err.Close()
e := cmd.Start()
if e != nil {
fmt.Println(e)
}
go sync(out)
go sync(err)
wait := cmd.Wait()
if wait != nil {
fmt.Println(wait)
}
}
// 获取输出
func sync(read io.ReadCloser) {
buf := make([]byte, 1024, 1024)
for {
length, _ := read.Read(buf)
if length > 0 {
bytes := buf[:length]
msg := string(bytes)
fmt.Print(msg)
if url == "" || path == "" {
subStrings(msg)
} else {
if launchConfig == "" {
makeJson()
saveConfig()
}
}
}
}
}
// 获取参数
func subStrings(text string) {
begin := "http://"
if strings.Contains(text, begin) {
index := strings.Index(text, begin)
temp := text[index : len(text)-1]
temp = strings.TrimSuffix(temp, "\r")
url = strings.TrimSuffix(temp, "\n")
} else {
start := "Project:"
index := strings.Index(text, start)
temp := text[index+len(start)+1 : len(text)-1]
temp = strings.TrimSuffix(temp, "\r")
path = strings.TrimSuffix(temp, "\n")
}
}
// 保存配置文件
func saveConfig() {
content := []byte(launchConfig)
saveJsonConfig := path + file
err := os.MkdirAll(filepath.Dir(saveJsonConfig), os.ModePerm)
if err != nil {
fmt.Println(err)
}
err = ioutil.WriteFile(saveJsonConfig, content, 0644)
if err != nil {
fmt.Println(err.Error())
}
}
// 生成启动配置
func makeJson() {
launcher := &Launcher{
Version: "0.2.0",
Configurations: []interface{}{
&RunConfig{
Name: "Flutter",
Request: "attach",
DeviceId: "flutter-tester",
Type: "dart",
ObservatoryUri: url,
},
},
}
bytes, _ := json.Marshal(launcher)
launchConfig = string(bytes)
}