Skip to content

Commit 39c1e09

Browse files
committed
tools: make upgrade tool copy docs from go-mysql-server
Fixes #793 There are some docs we want to have in gitbase as well as go-mysql-server. To avoid possibly having different versions in each project, this will automatically copy certain parts and files from one to the other when there is an update. Signed-off-by: Miguel Molina <[email protected]>
1 parent fd71314 commit 39c1e09

File tree

1 file changed

+128
-4
lines changed

1 file changed

+128
-4
lines changed

tools/rev-upgrade/main.go

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ import (
77
"io"
88
"io/ioutil"
99
"log"
10+
"os"
1011
"os/exec"
1112
"path/filepath"
1213
"regexp"
14+
"strings"
1315
"sync"
1416

1517
"github.com/BurntSushi/toml"
1618
git "gopkg.in/src-d/go-git.v4"
1719
)
1820

19-
const lockFile = "Gopkg.lock"
21+
const (
22+
lockFile = "Gopkg.lock"
23+
goMysqlServer = "gopkg.in/src-d/go-mysql-server.v0"
24+
)
2025

2126
type project struct {
2227
Name string
@@ -44,7 +49,7 @@ func main() {
4449
err error
4550
)
4651

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)")
4853
flag.StringVar(&newRev, "r", "", "revision (by default the latest allowed by Gopkg.toml)")
4954
flag.Parse()
5055

@@ -81,11 +86,16 @@ func main() {
8186
}
8287
}
8388

84-
err = ensure(prj)
85-
if err != nil {
89+
if err = ensure(prj); err != nil {
8690
return
8791
}
8892

93+
if prj == goMysqlServer {
94+
if err = importDocs(); err != nil {
95+
return
96+
}
97+
}
98+
8999
if newRev == "" {
90100
newRev, err = revision(filepath.Join(w.Filesystem.Root(), "Gopkg.lock"), prj)
91101
fmt.Printf("Project: %s\nOld rev: %s\nNew rev: %s\n", prj, oldRev, newRev)
@@ -190,3 +200,117 @@ func ensure(prj string) error {
190200

191201
return nil
192202
}
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

Comments
 (0)