@@ -14,6 +14,7 @@ package main
14
14
15
15
import (
16
16
"archive/zip"
17
+ "flag"
17
18
"fmt"
18
19
"io/ioutil"
19
20
"log"
@@ -23,7 +24,27 @@ import (
23
24
"strings"
24
25
)
25
26
27
+ const (
28
+ GODOC_LOCAL_ROOT = "GODOC_LOCAL_ROOT"
29
+ )
30
+
31
+ var (
32
+ flagGoroot = flag .String ("goroot" , runtime .GOROOT (), "Go root directory" )
33
+ flagLocalRoot = flag .String ("godoc-local-root" , "" , "Godoc translations root, default is $(GOROOT)/translations" )
34
+ )
35
+
26
36
func main () {
37
+ flag .Parse ()
38
+
39
+ if * flagLocalRoot == "" {
40
+ if s := os .Getenv (GODOC_LOCAL_ROOT ); s != "" {
41
+ * flagLocalRoot = s
42
+ }
43
+ }
44
+ if * flagLocalRoot == "" || * flagLocalRoot == "translations" {
45
+ * flagLocalRoot = * flagGoroot + "/translations"
46
+ }
47
+
27
48
file , err := os .Create ("goroot.zip" )
28
49
if err != nil {
29
50
log .Fatal ("os.Create: " , err )
@@ -41,21 +62,20 @@ func main() {
41
62
if _ , err = f .Write ([]byte ("" )); err != nil {
42
63
log .Fatal (err )
43
64
}
44
-
45
- filepath .Walk (runtime .GOROOT (), func (path string , info os.FileInfo , err error ) error {
65
+ filepath .Walk (* flagGoroot , func (path string , info os.FileInfo , err error ) error {
46
66
if err != nil {
47
67
log .Fatal ("filepath.Walk: " , err )
48
68
}
49
69
if info .IsDir () {
50
70
return nil
51
71
}
52
- relpath , err := filepath .Rel (runtime . GOROOT () , path )
72
+ relpath , err := filepath .Rel (* flagGoroot , path )
53
73
if err != nil {
54
74
log .Fatal ("filepath.Rel: " , err )
55
75
}
56
76
57
77
filename := filepath .ToSlash (relpath )
58
- if isIngoreFile (filename ) {
78
+ if isIngoreFile (filename ) || isTranslationsFile ( filename ) {
59
79
return nil
60
80
}
61
81
@@ -75,23 +95,73 @@ func main() {
75
95
fmt .Printf ("%s\n " , filename )
76
96
return nil
77
97
})
98
+
99
+ // create /goroot/translations/
100
+ f , err = zipFile .Create ("goroot/translations/" )
101
+ if err != nil {
102
+ log .Fatal (err )
103
+ }
104
+ if _ , err = f .Write ([]byte ("" )); err != nil {
105
+ log .Fatal (err )
106
+ }
107
+ filepath .Walk (* flagLocalRoot , func (path string , info os.FileInfo , err error ) error {
108
+ if err != nil {
109
+ log .Fatal ("filepath.Walk: " , err )
110
+ }
111
+ if info .IsDir () {
112
+ return nil
113
+ }
114
+ relpath , err := filepath .Rel (* flagLocalRoot , path )
115
+ if err != nil {
116
+ log .Fatal ("filepath.Rel: " , err )
117
+ }
118
+
119
+ filename := filepath .ToSlash (relpath )
120
+ if isIngoreFile (filename ) {
121
+ return nil
122
+ }
123
+
124
+ data , err := ioutil .ReadFile (path )
125
+ if err != nil {
126
+ log .Fatal ("ioutil.ReadFile: " , err )
127
+ }
128
+
129
+ f , err := zipFile .Create ("goroot/translations/" + filename )
130
+ if err != nil {
131
+ log .Fatal (err )
132
+ }
133
+ if _ , err = f .Write (data ); err != nil {
134
+ log .Fatal (err )
135
+ }
136
+
137
+ fmt .Printf ("translations/%s\n " , filename )
138
+ return nil
139
+ })
140
+
78
141
fmt .Printf ("Done\n " )
79
142
}
80
143
144
+ func isTranslationsFile (path string ) bool {
145
+ if strings .HasPrefix (path , "translations" ) {
146
+ return true
147
+ }
148
+ return false
149
+ }
150
+
81
151
func isIngoreFile (path string ) bool {
82
152
if strings .HasPrefix (path , "bin" ) {
83
153
return true
84
154
}
85
155
if strings .HasPrefix (path , "pkg" ) {
86
156
return true
87
157
}
88
- if strings .HasPrefix (path , "translations/ .git" ) {
158
+ if strings .HasPrefix (path , ".git" ) {
89
159
return true
90
160
}
91
- if strings .HasPrefix (path , "translations/ talks" ) {
161
+ if strings .HasPrefix (path , "talks" ) {
92
162
return true
93
163
}
94
- if strings .HasPrefix (path , "translations/ tour" ) {
164
+ if strings .HasPrefix (path , "tour" ) {
95
165
return true
96
166
}
97
167
switch strings .ToLower (filepath .Ext (path )) {
0 commit comments