forked from shurcooL/Go-Package-Store
/
main.go
386 lines (320 loc) · 12 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Go Package Store displays updates for the Go packages in your GOPATH.
package main
import (
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"runtime"
"time"
"github.com/shurcooL/Go-Package-Store/presenter"
"github.com/shurcooL/go/exp/14"
"github.com/shurcooL/go/gists/gist7480523"
"github.com/shurcooL/go/gists/gist7651991"
"github.com/shurcooL/go/gists/gist7802150"
"github.com/shurcooL/go/gzip_file_server"
"github.com/shurcooL/go/u/u4"
"github.com/shurcooL/go/vfs/httpfs/html/vfstemplate"
"github.com/shurcooL/gostatus/status"
"golang.org/x/net/websocket"
)
func CommonHat(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
io.WriteString(w, `<html>
<head>
<title>Go Package Store</title>
<link href="/assets/style.css" rel="stylesheet" type="text/css" />
<script src="/assets/script/script.js" type="text/javascript"></script>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/octicons/2.1.2/octicons.css">`)
if production {
io.WriteString(w, `
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','http://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-56541369-2', 'auto');
ga('send', 'pageview');
</script>`)
}
io.WriteString(w, `
</head>
<body>
<div style="width: 100%; text-align: center; background-color: hsl(209, 51%, 92%);">
<span style="background-color: hsl(209, 51%, 88%); padding: 15px; display: inline-block;">Updates</span>
</div>`)
if production {
io.WriteString(w, `
<script type="text/javascript">
var sock = new WebSocket("ws://`+*httpFlag+`/opened");
sock.onopen = function () {
sock.onclose = function() { alert('Go Package Store server disconnected.'); };
};
</script>`)
}
io.WriteString(w, `
<div class="center-max-width"><div class="content">`)
}
func CommonTail(w io.Writer) {
// TODO: Make installed_updates available before all packages finish loading, so that it works when you update a package early.
io.WriteString(w, `<div id="installed_updates" style="display: none;"><h3 style="text-align: center;">Installed Updates</h3></div>`)
io.WriteString(w, "</div></div></body></html>")
}
// ---
// shouldPresentUpdate determines if the given goPackage should be presented as an available update.
// It checks that the Go package is on default branch, does not have a dirty working tree, and does not have the remote revision.
func shouldPresentUpdate(goPackage *gist7480523.GoPackage) bool {
return status.PlumbingPresenterV2(goPackage)[:3] == " +" // Ignore stash.
}
// Writes a <div> presentation for an available update.
func WriteRepoHtml(w http.ResponseWriter, repoPresenter presenter.Presenter) {
err := t.Execute(w, repoPresenter)
if err != nil {
log.Println("t.Execute:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// A cached list of Go packages to work with.
var goPackages exp14.GoPackageList
type updateRequest struct {
importPathPattern string
resultChan chan error
}
var updateRequestChan = make(chan updateRequest)
// updateWorker is a sequential updater of Go packages. It does not update them in parallel
// to avoid race conditions or other problems, since `go get -u` does not seem to protect against that.
func updateWorker() {
for updateRequest := range updateRequestChan {
if !production {
fmt.Println("got req:", updateRequest.importPathPattern)
time.Sleep(time.Second)
fmt.Println("Done.")
updateRequest.resultChan <- nil
continue
}
var updateErr = fmt.Errorf("import path pattern %q not found in GOPATH", updateRequest.importPathPattern)
gist7802150.MakeUpdated(goPackages)
for _, goPackage := range goPackages.List() {
if rootPath := getRootPath(goPackage); rootPath != "" {
if gist7480523.GetRepoImportPathPattern(rootPath, goPackage.Bpkg.SrcRoot) == updateRequest.importPathPattern {
vcs := goPackage.Dir.Repo.RepoRoot.VCS
fmt.Printf("cd %s\n", rootPath)
fmt.Printf("%s %s", vcs.Cmd, vcs.DownloadCmd)
updateErr = vcs.Download(rootPath)
// Invalidate cache of the package's local revision, since it's expected to change after updating.
gist7802150.ExternallyUpdated(goPackage.Dir.Repo.VcsLocal.GetSources()[1].(gist7802150.DepNode2ManualI))
break
}
}
}
updateRequest.resultChan <- updateErr
fmt.Println("\nDone.")
}
}
// Handler for update requests.
func updateHandler(w http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
if *godepsFlag != "" {
// TODO: Implement updating Godeps packages.
log.Fatalln("updating Godeps packages isn't supported yet")
}
updateRequest := updateRequest{
importPathPattern: req.PostFormValue("import_path_pattern"),
resultChan: make(chan error),
}
updateRequestChan <- updateRequest
err := <-updateRequest.resultChan
_ = err // Don't do anything about the error for now.
}
}
// getRootPath returns the root path of the given goPackage.
func getRootPath(goPackage *gist7480523.GoPackage) (rootPath string) {
if goPackage.Bpkg.Goroot {
return ""
}
goPackage.UpdateVcs()
if goPackage.Dir.Repo == nil {
return ""
} else {
return goPackage.Dir.Repo.Vcs.RootPath()
}
}
// Main index page handler.
func mainHandler(w http.ResponseWriter, req *http.Request) {
if err := loadTemplates(); err != nil {
fmt.Fprintln(w, "loadTemplates:", err)
return
}
//started := time.Now()
CommonHat(w)
defer CommonTail(w)
io.WriteString(w, `<div id="checking_updates"><h2 style="text-align: center;">Checking for updates...</h2></div>`)
io.WriteString(w, `<div id="no_updates" style="display: none;"><h2 style="text-align: center;">No Updates Available</h2></div>`)
defer io.WriteString(w, `<script>document.getElementById("checking_updates").style.display = "none";</script>`)
flusher := w.(http.Flusher)
flusher.Flush()
notifier := w.(http.CloseNotifier)
go func() {
<-notifier.CloseNotify()
//fmt.Println("Exiting, since the HTTP request was cancelled/interrupted.")
//close(updateRequestChan)
}()
//fmt.Printf("Part 1: %v ms.\n", time.Since(started).Seconds()*1000)
// rootPath -> []*gist7480523.GoPackage
var goPackagesInRepo = make(map[string][]*gist7480523.GoPackage)
gist7802150.MakeUpdated(goPackages)
//fmt.Printf("Part 1b: %v ms.\n", time.Since(started).Seconds()*1000)
if false {
for _, goPackage := range goPackages.List() {
if rootPath := getRootPath(goPackage); rootPath != "" {
goPackagesInRepo[rootPath] = append(goPackagesInRepo[rootPath], goPackage)
}
}
} else {
inChan := make(chan interface{})
go func() { // This needs to happen in the background because sending input will be blocked on reading output.
for _, goPackage := range goPackages.List() {
inChan <- goPackage
}
close(inChan)
}()
reduceFunc := func(in interface{}) interface{} {
goPackage := in.(*gist7480523.GoPackage)
if rootPath := getRootPath(goPackage); rootPath != "" {
return gist7480523.NewGoPackageRepo(rootPath, []*gist7480523.GoPackage{goPackage})
}
return nil
}
outChan := gist7651991.GoReduce(inChan, 64, reduceFunc)
for out := range outChan {
repo := out.(gist7480523.GoPackageRepo)
goPackagesInRepo[repo.RootPath()] = append(goPackagesInRepo[repo.RootPath()], repo.GoPackages()[0])
}
}
//goon.DumpExpr(len(goPackages.List()))
//goon.DumpExpr(len(goPackagesInRepo))
//fmt.Printf("Part 2: %v ms.\n", time.Since(started).Seconds()*1000)
updatesAvailable := 0
inChan := make(chan interface{})
go func() { // This needs to happen in the background because sending input will be blocked on reading output.
for rootPath, goPackages := range goPackagesInRepo {
inChan <- gist7480523.NewGoPackageRepo(rootPath, goPackages)
}
close(inChan)
}()
reduceFunc := func(in interface{}) interface{} {
repo := in.(gist7480523.GoPackageRepo)
goPackage := repo.GoPackages()[0]
goPackage.UpdateVcsFields()
if !shouldPresentUpdate(goPackage) {
return nil
}
repoPresenter := presenter.New(&repo)
return repoPresenter
}
outChan := gist7651991.GoReduce(inChan, 8, reduceFunc)
for out := range outChan {
//started2 := time.Now()
repoPresenter := out.(presenter.Presenter)
updatesAvailable++
WriteRepoHtml(w, repoPresenter)
flusher.Flush()
//fmt.Printf("Part 2b: %v ms.\n", time.Since(started2).Seconds()*1000)
/*log.Println("WriteRepoHtml")
goon.DumpExpr(repoPresenter.Repo().ImportPathPattern())
goon.DumpExpr(repoPresenter.Repo().ImportPaths())
goon.DumpExpr(len(repoPresenter.Repo().GoPackages()))
goon.DumpExpr(repoPresenter.Repo().GoPackages()[0].Bpkg.ImportPath)
goon.DumpExpr(repoPresenter.Repo().GoPackages()[0].Dir.Repo.VcsLocal.LocalRev)
goon.DumpExpr(repoPresenter.Repo().GoPackages()[0].Dir.Repo.VcsRemote.RemoteRev)
goon.DumpExpr(repoPresenter.HomePage())
goon.DumpExpr(repoPresenter.Image())
var changes []presenter.Change
if changesChan := repoPresenter.Changes(); changesChan != nil {
for c := range changesChan {
changes = append(changes, c)
}
}
goon.DumpExpr(changes)*/
}
if updatesAvailable == 0 {
io.WriteString(w, `<script>document.getElementById("no_updates").style.display = "";</script>`)
}
//fmt.Printf("Part 3: %v ms.\n", time.Since(started).Seconds()*1000)
}
// WebSocket handler, to exit when client tab is closed.
func openedHandler(ws *websocket.Conn) {
// Wait until connection is closed.
io.Copy(ioutil.Discard, ws)
//fmt.Println("Exiting, since the client tab was closed (detected closed WebSocket connection).")
//close(updateRequestChan)
}
// ---
var t *template.Template
func loadTemplates() error {
const filename = "/assets/repo.html.tmpl"
var err error
t, err = vfstemplate.ParseFiles(assets, nil, filename)
return err
}
var httpFlag = flag.String("http", "localhost:7043", "Listen for HTTP connections on this address.")
var stdinFlag = flag.Bool("stdin", false, "Read the list of newline separated Go packages from stdin.")
var godepsFlag = flag.String("godeps", "", "Read the list of Go packages from the specified Godeps file.")
func usage() {
fmt.Fprint(os.Stderr, "Usage: Go-Package-Store [flags]\n")
fmt.Fprint(os.Stderr, " [newline separated packages] | Go-Package-Store --stdin [flags]\n")
flag.PrintDefaults()
fmt.Fprint(os.Stderr, `
Examples:
# Check for updates for all Go packages in GOPATH.
Go-Package-Store
# Show updates for all dependencies (recursive) of package in cur working dir.
go list -f '{{join .Deps "\n"}}' . | Go-Package-Store --stdin
`)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Usage = usage
flag.Parse()
switch {
default:
fmt.Println("Using all Go packages in GOPATH.")
goPackages = &exp14.GoPackages{SkipGoroot: true} // All Go packages in GOPATH (not including GOROOT).
case *stdinFlag:
fmt.Println("Reading the list of newline separated Go packages from stdin.")
goPackages = &exp14.GoPackagesFromReader{Reader: os.Stdin}
case *godepsFlag != "":
fmt.Println("Reading the list of Go packages from Godeps file:", *godepsFlag)
goPackages = NewGoPackagesFromGodeps(*godepsFlag)
}
err := loadTemplates()
if err != nil {
log.Fatalln("loadTemplates:", err)
}
http.HandleFunc("/index.html", mainHandler)
http.HandleFunc("/-/update", updateHandler)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.Handle("/assets/", gzip_file_server.New(assets))
http.Handle("/opened", websocket.Handler(openedHandler)) // Exit server when client tab is closed.
go updateWorker()
// Start listening first.
listener, err := net.Listen("tcp", *httpFlag)
if err != nil {
log.Fatalf("failed to listen on %q: %v\n", *httpFlag, err)
}
// Open a browser tab and navigate to the main page.
if production {
go u4.Open("http://" + *httpFlag + "/index.html")
}
fmt.Println("Go Package Store server is running at http://" + *httpFlag + "/index.html.")
err = http.Serve(listener, nil)
if err != nil {
log.Fatalln(err)
}
}