github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/dev/update_closure_compiler.go (about) 1 /* 2 Copyright 2013 The Camlistore Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // update_closure_compiler downloads a new version 18 // of the closure compiler if the one in tmp/closure-compiler 19 // doesn't exist or is older than the requested version. 20 package main 21 22 import ( 23 "archive/zip" 24 "io" 25 "log" 26 "net/http" 27 "os" 28 "os/exec" 29 "path/filepath" 30 "regexp" 31 32 "camlistore.org/pkg/osutil" 33 ) 34 35 const ( 36 compilerDirURL = "http://closure-compiler.googlecode.com/files/" 37 compilerVersion = "20121212" 38 ) 39 40 var rgxVersion = regexp.MustCompile(`.*Version: (.*) \(revision.*`) 41 42 func main() { 43 44 // check JRE presence 45 _, err := exec.LookPath("java") 46 if err != nil { 47 log.Fatal("Didn't find 'java' in $PATH. The Java Runtime Environment is needed to run the closure compiler.\n") 48 } 49 50 camliRootPath, err := osutil.GoPackagePath("camlistore.org") 51 if err != nil { 52 log.Fatal("Package camlistore.org not found in $GOPATH (or $GOPATH not defined).") 53 } 54 destDir := filepath.Join(camliRootPath, "tmp", "closure-compiler") 55 // check if compiler already exists 56 jarFile := filepath.Join(destDir, "compiler.jar") 57 _, err = os.Stat(jarFile) 58 if err == nil { 59 // if compiler exists, check version 60 cmd := exec.Command("java", "-jar", jarFile, "--version", "--help", "2>&1") 61 output, _ := cmd.CombinedOutput() 62 m := rgxVersion.FindStringSubmatch(string(output)) 63 if m == nil { 64 log.Fatalf("Could not find compiler version in %q", output) 65 } 66 if m[1] == compilerVersion { 67 log.Printf("compiler already at version %v , nothing to do.", compilerVersion) 68 os.Exit(0) 69 } 70 if err := os.Remove(jarFile); err != nil { 71 log.Fatalf("Could not remove %v: %v", jarFile, err) 72 } 73 } else { 74 if !os.IsNotExist(err) { 75 log.Fatalf("Could not stat %v: %v", jarFile, err) 76 } 77 } 78 79 // otherwise, download compiler 80 log.Printf("Getting closure compiler version %s.\n", compilerVersion) 81 if err := os.MkdirAll(destDir, 0755); err != nil { 82 log.Fatal(err) 83 } 84 if err := os.Chdir(destDir); err != nil { 85 log.Fatal(err) 86 } 87 zipFilename := "compiler-" + compilerVersion + ".zip" 88 compilerURL := compilerDirURL + zipFilename 89 resp, err := http.Get(compilerURL) 90 if err != nil { 91 log.Fatal(err) 92 } 93 defer resp.Body.Close() 94 f, err := os.Create(zipFilename) 95 if err != nil { 96 log.Fatal(err) 97 } 98 if _, err := io.Copy(f, resp.Body); err != nil { 99 log.Fatal(err) 100 } 101 if err := f.Close(); err != nil { 102 log.Fatal(err) 103 } 104 105 r, err := zip.OpenReader(zipFilename) 106 if err != nil { 107 log.Fatal(err) 108 } 109 for x, f := range r.File { 110 if f.FileHeader.Name != "compiler.jar" { 111 if x == len(r.File)-1 { 112 log.Fatal("compiler.jar was not found in the zip archive") 113 } 114 continue 115 } 116 rc, err := f.Open() 117 if err != nil { 118 log.Fatal(err) 119 } 120 g, err := os.Create(jarFile) 121 if err != nil { 122 log.Fatal(err) 123 } 124 defer g.Close() 125 if _, err = io.Copy(g, rc); err != nil { 126 log.Fatal(err) 127 } 128 rc.Close() 129 break 130 } 131 132 if err := r.Close(); err != nil { 133 log.Fatal(err) 134 } 135 if err := os.Remove(zipFilename); err != nil { 136 log.Fatal(err) 137 } 138 log.Printf("Success. Installed at %v", jarFile) 139 }