-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.go
More file actions
145 lines (125 loc) · 2.71 KB
/
Copy pathloader.go
File metadata and controls
145 lines (125 loc) · 2.71 KB
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
package plugins
import (
"archive/zip"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path"
"plugin"
)
type Loader struct {
cwd string
fileList []*os.File
}
func NewLoader(projectCwd string) *Loader {
ldr := &Loader{
cwd: projectCwd,
}
return ldr
}
func (l *Loader) Load(name, zipUrl string) (*plugin.Plugin, error) {
fmt.Printf("Start download plugin: %s\n", name)
fmt.Printf("plugin zip url: %s\n", zipUrl)
resp, err := http.Get(zipUrl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
zipFile := path.Join(l.cwd, fmt.Sprintf("%s.zip", name))
w, err := os.Create(zipFile)
if err != nil {
return nil, err
}
l.fileList = append(l.fileList, w)
counter := NewWriteCounter(uint64(resp.ContentLength))
_, err = io.Copy(w, io.TeeReader(resp.Body, counter))
if err != nil {
return nil, err
}
w.Close()
fmt.Printf("\nDownload finished.\n")
fmt.Println("Decompressing zip...")
rootPath, err := l.Decompress(zipFile, l.cwd)
if err != nil {
return nil, err
}
fmt.Println("Building plugin...")
cmd := exec.Command("make")
cmd.Dir = rootPath
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return nil, err
}
fmt.Println("Loading plugin...")
pluginFile := path.Join(rootPath, "generator.so")
return plugin.Open(pluginFile)
}
func (l *Loader) Clear() error {
fmt.Println("Clear loader cache files...")
defer fmt.Printf("Clear finished.\n")
for _, f := range l.fileList {
fmt.Printf("Removing file: %s ...\n", f.Name())
err := os.RemoveAll(f.Name())
if err != nil {
return err
}
}
return nil
}
func (l *Loader) Decompress(zipFile, outputPath string) (rootPath string, err error) {
reader, err := zip.OpenReader(zipFile)
if err != nil {
return
}
defer reader.Close()
for _, file := range reader.File {
filename := path.Join(outputPath, file.Name)
if file.FileInfo().Name() == "Makefile" && !file.FileInfo().IsDir() && rootPath == "" {
rootPath = path.Dir(filename)
}
if file.FileInfo().IsDir() {
err = os.MkdirAll(filename, 0755)
if err != nil {
return rootPath, err
}
continue
} else {
err = os.MkdirAll(path.Dir(filename), 0755)
if err != nil {
return rootPath, err
}
}
w, err := os.Create(filename)
if err != nil {
return rootPath, err
}
rc, err := file.Open()
if err != nil {
w.Close()
return rootPath, err
}
_, err = io.Copy(w, rc)
if err != nil {
w.Close()
rc.Close()
return rootPath, err
}
w.Close()
rc.Close()
}
if rootPath == "" {
err = fmt.Errorf("cannot find Makefile in plugins")
return
}
root, err := os.Open(rootPath)
if err != nil {
err = fmt.Errorf("open rootPath %s failed", rootPath)
return
}
l.fileList = append(l.fileList, root)
return
}