@@ -7,16 +7,21 @@ import (
7
7
"io"
8
8
"io/ioutil"
9
9
"log"
10
+ "os"
10
11
"os/exec"
11
12
"path/filepath"
12
13
"regexp"
14
+ "strings"
13
15
"sync"
14
16
15
17
"github.com/BurntSushi/toml"
16
18
git "gopkg.in/src-d/go-git.v4"
17
19
)
18
20
19
- const lockFile = "Gopkg.lock"
21
+ const (
22
+ lockFile = "Gopkg.lock"
23
+ goMysqlServer = "gopkg.in/src-d/go-mysql-server.v0"
24
+ )
20
25
21
26
type project struct {
22
27
Name string
@@ -44,7 +49,7 @@ func main() {
44
49
err error
45
50
)
46
51
47
- flag .StringVar (& prj , "p" , "gopkg.in/src-d/go-mysql-server.v0" , "project name (e.g.: gopkg.in/src-d/go-mysql-server.v0)" )
52
+ flag .StringVar (& prj , "p" , goMysqlServer , "project name (e.g.: gopkg.in/src-d/go-mysql-server.v0)" )
48
53
flag .StringVar (& newRev , "r" , "" , "revision (by default the latest allowed by Gopkg.toml)" )
49
54
flag .Parse ()
50
55
@@ -81,11 +86,16 @@ func main() {
81
86
}
82
87
}
83
88
84
- err = ensure (prj )
85
- if err != nil {
89
+ if err = ensure (prj ); err != nil {
86
90
return
87
91
}
88
92
93
+ if prj == goMysqlServer {
94
+ if err = importDocs (); err != nil {
95
+ return
96
+ }
97
+ }
98
+
89
99
if newRev == "" {
90
100
newRev , err = revision (filepath .Join (w .Filesystem .Root (), "Gopkg.lock" ), prj )
91
101
fmt .Printf ("Project: %s\n Old rev: %s\n New rev: %s\n " , prj , oldRev , newRev )
@@ -190,3 +200,117 @@ func ensure(prj string) error {
190
200
191
201
return nil
192
202
}
203
+
204
+ var docsToCopy = []struct {
205
+ from []string
206
+ to []string
207
+ blocks []string
208
+ }{
209
+ {
210
+ from : []string {"SUPPORTED.md" },
211
+ to : []string {"docs" , "using-gitbase" , "supported-syntax.md" },
212
+ },
213
+ {
214
+ from : []string {"SUPPORTED_CLIENTS.md" },
215
+ to : []string {"docs" , "using-gitbase" , "supported-clients.md" },
216
+ },
217
+ {
218
+ from : []string {"README.md" },
219
+ to : []string {"docs" , "using-gitbase" , "functions.md" },
220
+ blocks : []string {"FUNCTIONS" },
221
+ },
222
+ {
223
+ from : []string {"README.md" },
224
+ to : []string {"docs" , "using-gitbase" , "configuration.md" },
225
+ blocks : []string {"CONFIG" },
226
+ },
227
+ }
228
+
229
+ func importDocs () error {
230
+ pwd , err := os .Getwd ()
231
+ if err != nil {
232
+ return err
233
+ }
234
+ dirs := strings .Split (goMysqlServer , "/" )
235
+ goMysqlServerPath := filepath .Join (append ([]string {pwd , "vendor" }, dirs ... )... )
236
+
237
+ for _ , c := range docsToCopy {
238
+ src := filepath .Join (append ([]string {goMysqlServerPath }, c .from ... )... )
239
+ dst := filepath .Join (append ([]string {pwd }, c .to ... )... )
240
+
241
+ if len (c .blocks ) == 0 {
242
+ if err := copyFile (src , dst ); err != nil {
243
+ return err
244
+ }
245
+ } else {
246
+ if err := copyFileBlocks (src , dst , c .blocks ); err != nil {
247
+ return err
248
+ }
249
+ }
250
+ }
251
+
252
+ return nil
253
+ }
254
+
255
+ func copyFile (src , dst string ) error {
256
+ fout , err := os .Create (dst )
257
+ if err != nil {
258
+ return err
259
+ }
260
+ defer fout .Close ()
261
+
262
+ fin , err := os .Open (src )
263
+ if err != nil {
264
+ return err
265
+ }
266
+ defer fin .Close ()
267
+
268
+ _ , err = io .Copy (fout , fin )
269
+ return err
270
+ }
271
+
272
+ func copyFileBlocks (src , dst string , blocks []string ) error {
273
+ fout , err := ioutil .ReadFile (dst )
274
+ if err != nil {
275
+ return err
276
+ }
277
+
278
+ fin , err := ioutil .ReadFile (src )
279
+ if err != nil {
280
+ return err
281
+ }
282
+
283
+ for _ , b := range blocks {
284
+ open := []byte (fmt .Sprintf ("<!-- BEGIN %s -->" , b ))
285
+ close := []byte (fmt .Sprintf ("<!-- END %s -->" , b ))
286
+
287
+ outOpenIdx := bytes .Index (fout , open )
288
+ outCloseIdx := bytes .Index (fout , close )
289
+ inOpenIdx := bytes .Index (fin , open )
290
+ inCloseIdx := bytes .Index (fin , close )
291
+
292
+ if outOpenIdx < 0 || outCloseIdx < 0 {
293
+ return fmt .Errorf ("block %q not found on %s" , b , dst )
294
+ }
295
+
296
+ if inOpenIdx < 0 || inCloseIdx < 0 {
297
+ return fmt .Errorf ("block %q not found on %s" , b , src )
298
+ }
299
+
300
+ var newOut []byte
301
+ newOut = append (newOut , fout [:outOpenIdx ]... )
302
+ newOut = append (newOut , []byte (strings .TrimSpace (string (fin [inOpenIdx :inCloseIdx + len (close )])))... )
303
+ newOut = append (newOut , fout [outCloseIdx + len (close ):]... )
304
+
305
+ fout = newOut
306
+ }
307
+
308
+ f , err := os .Create (dst )
309
+ if err != nil {
310
+ return err
311
+ }
312
+ defer f .Close ()
313
+
314
+ _ , err = f .Write (fout )
315
+ return err
316
+ }
0 commit comments