upgrade.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // +build ignore
  2. package main
  3. import (
  4. "archive/zip"
  5. "bufio"
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "log"
  11. "net/http"
  12. "os"
  13. "path"
  14. "path/filepath"
  15. "strings"
  16. "github.com/PuerkitoBio/goquery"
  17. )
  18. func main() {
  19. site := "https://www.sqlite.org/download.html"
  20. fmt.Printf("scraping %v\n", site)
  21. doc, err := goquery.NewDocument(site)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. var url string
  26. doc.Find("a").Each(func(_ int, s *goquery.Selection) {
  27. if url == "" && strings.HasPrefix(s.Text(), "sqlite-amalgamation-") {
  28. url = "https://www.sqlite.org/2018/" + s.Text()
  29. }
  30. })
  31. if url == "" {
  32. return
  33. }
  34. fmt.Printf("downloading %v\n", url)
  35. resp, err := http.Get(url)
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. b, err := ioutil.ReadAll(resp.Body)
  40. if err != nil {
  41. resp.Body.Close()
  42. log.Fatal(err)
  43. }
  44. fmt.Printf("extracting %v\n", path.Base(url))
  45. r, err := zip.NewReader(bytes.NewReader(b), resp.ContentLength)
  46. if err != nil {
  47. resp.Body.Close()
  48. log.Fatal(err)
  49. }
  50. resp.Body.Close()
  51. for _, zf := range r.File {
  52. var f *os.File
  53. switch path.Base(zf.Name) {
  54. case "sqlite3.c":
  55. f, err = os.Create("sqlite3-binding.c")
  56. case "sqlite3.h":
  57. f, err = os.Create("sqlite3-binding.h")
  58. case "sqlite3ext.h":
  59. f, err = os.Create("sqlite3ext.h")
  60. default:
  61. continue
  62. }
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. zr, err := zf.Open()
  67. if err != nil {
  68. log.Fatal(err)
  69. }
  70. _, err = io.WriteString(f, "#ifndef USE_LIBSQLITE3\n")
  71. if err != nil {
  72. zr.Close()
  73. f.Close()
  74. log.Fatal(err)
  75. }
  76. scanner := bufio.NewScanner(zr)
  77. for scanner.Scan() {
  78. text := scanner.Text()
  79. if text == `#include "sqlite3.h"` {
  80. text = `#include "sqlite3-binding.h"`
  81. }
  82. _, err = fmt.Fprintln(f, text)
  83. if err != nil {
  84. break
  85. }
  86. }
  87. err = scanner.Err()
  88. if err != nil {
  89. zr.Close()
  90. f.Close()
  91. log.Fatal(err)
  92. }
  93. _, err = io.WriteString(f, "#else // USE_LIBSQLITE3\n // If users really want to link against the system sqlite3 we\n// need to make this file a noop.\n #endif")
  94. if err != nil {
  95. zr.Close()
  96. f.Close()
  97. log.Fatal(err)
  98. }
  99. zr.Close()
  100. f.Close()
  101. fmt.Printf("extracted %v\n", filepath.Base(f.Name()))
  102. }
  103. }