/
update_closure_compiler.go
139 lines (125 loc) · 3.48 KB
/
update_closure_compiler.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
/*
Copyright 2013 The Camlistore Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// update_closure_compiler downloads a new version
// of the closure compiler if the one in tmp/closure-compiler
// doesn't exist or is older than the requested version.
package main
import (
"archive/zip"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"camlistore.org/pkg/osutil"
)
const (
compilerDirURL = "http://closure-compiler.googlecode.com/files/"
compilerVersion = "20121212"
)
var rgxVersion = regexp.MustCompile(`.*Version: (.*) \(revision.*`)
func main() {
// check JRE presence
_, err := exec.LookPath("java")
if err != nil {
log.Fatal("Didn't find 'java' in $PATH. The Java Runtime Environment is needed to run the closure compiler.\n")
}
camliRootPath, err := osutil.GoPackagePath("camlistore.org")
if err != nil {
log.Fatal("Package camlistore.org not found in $GOPATH (or $GOPATH not defined).")
}
destDir := filepath.Join(camliRootPath, "tmp", "closure-compiler")
// check if compiler already exists
jarFile := filepath.Join(destDir, "compiler.jar")
_, err = os.Stat(jarFile)
if err == nil {
// if compiler exists, check version
cmd := exec.Command("java", "-jar", jarFile, "--version", "--help", "2>&1")
output, _ := cmd.CombinedOutput()
m := rgxVersion.FindStringSubmatch(string(output))
if m == nil {
log.Fatalf("Could not find compiler version in %q", output)
}
if m[1] == compilerVersion {
log.Printf("compiler already at version %v , nothing to do.", compilerVersion)
os.Exit(0)
}
if err := os.Remove(jarFile); err != nil {
log.Fatalf("Could not remove %v: %v", jarFile, err)
}
} else {
if !os.IsNotExist(err) {
log.Fatalf("Could not stat %v: %v", jarFile, err)
}
}
// otherwise, download compiler
log.Printf("Getting closure compiler version %s.\n", compilerVersion)
if err := os.MkdirAll(destDir, 0755); err != nil {
log.Fatal(err)
}
if err := os.Chdir(destDir); err != nil {
log.Fatal(err)
}
zipFilename := "compiler-" + compilerVersion + ".zip"
compilerURL := compilerDirURL + zipFilename
resp, err := http.Get(compilerURL)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
f, err := os.Create(zipFilename)
if err != nil {
log.Fatal(err)
}
if _, err := io.Copy(f, resp.Body); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
r, err := zip.OpenReader(zipFilename)
if err != nil {
log.Fatal(err)
}
for x, f := range r.File {
if f.FileHeader.Name != "compiler.jar" {
if x == len(r.File)-1 {
log.Fatal("compiler.jar was not found in the zip archive")
}
continue
}
rc, err := f.Open()
if err != nil {
log.Fatal(err)
}
g, err := os.Create(jarFile)
if err != nil {
log.Fatal(err)
}
defer g.Close()
if _, err = io.Copy(g, rc); err != nil {
log.Fatal(err)
}
rc.Close()
break
}
if err := r.Close(); err != nil {
log.Fatal(err)
}
if err := os.Remove(zipFilename); err != nil {
log.Fatal(err)
}
log.Printf("Success. Installed at %v", jarFile)
}