Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added binaries/4.4.198/aarch64-apple-darwin/TabNine
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added binaries/4.4.198/aarch64-apple-darwin/WD-TabNine
Binary file not shown.
Empty file.
84 changes: 67 additions & 17 deletions go/pkg/tabnine/tabnine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tabnine

import (
"archive/zip"
"bufio"
"encoding/json"
"fmt"
Expand All @@ -15,7 +16,7 @@ import (
"strings"
"sync"

"github.com/coreos/go-semver/semver"
// "github.com/coreos/go-semver/semver"
)

type TabNine struct {
Expand Down Expand Up @@ -45,8 +46,8 @@ type ResultEntry struct {
}

const (
updateVersionUrl = "https://update.tabnine.com/version"
downloadUrlPrefix = "https://update.tabnine.com"
tabnineServerUrl = "https://update.tabnine.com/bundles"
pluginVersion = "1.2.3"
)

var systemMap = map[string]string{
Expand Down Expand Up @@ -81,7 +82,13 @@ func (t *TabNine) init() (err error) {
t.outPipeReader, t.outPipeWriter = io.Pipe()
wg.Wait()
if err == nil {
t.cmd = exec.Command(binaryPath, "--client=jupyter-server")
t.cmd = exec.Command(
binaryPath,
"--client=jupyter",
fmt.Sprintf("--log-file-path=%s/tabnine.log", filepath.Dir(binaryPath)),
"--client-metadata",
fmt.Sprintf("pluginVersion=%s", pluginVersion),
)
t.cmd.Stdin = t.inPipeReader
t.cmd.Stdout = t.outPipeWriter
t.outReader = bufio.NewReader(t.outPipeReader)
Expand Down Expand Up @@ -150,17 +157,18 @@ func (t *TabNine) getBinaryPath() (binaryPath string, err error) {
os.MkdirAll(binaryDir, os.ModePerm)
}

dirs, err := ioutil.ReadDir(binaryDir)
if err != nil {
return
}
// dirs, err := ioutil.ReadDir(binaryDir)
// if err != nil {
// return
// }

var versions []*semver.Version
// var versions []*semver.Version

for _, d := range dirs {
versions = append(versions, semver.New(d.Name()))
}
semver.Sort(versions)
// for _, d := range dirs {
// versions = append(versions, semver.New(d.Name()))
// }
// semver.Sort(versions)
versions := []string {"4.4.198",}
arch := parseArch(runtime.GOARCH)
sys := systemMap[strings.ToLower(runtime.GOOS)]
exeName := "TabNine"
Expand All @@ -169,14 +177,15 @@ func (t *TabNine) getBinaryPath() (binaryPath string, err error) {
}
triple := fmt.Sprintf("%s-%s", arch, sys)
for _, v := range versions {
binaryPath = filepath.Join(binaryDir, v.String(), triple, exeName)
// binaryPath = filepath.Join(binaryDir, v.String(), triple, exeName)
binaryPath = filepath.Join(binaryDir, v, triple, exeName)
if isFile(binaryPath) {
err = os.Chmod(binaryPath, 0755)
return
}
}
// need download
resp, err := http.Get(updateVersionUrl)
resp, err := http.Get(fmt.Sprintf("%s/version", tabnineServerUrl))
if err != nil {
return
}
Expand All @@ -194,14 +203,52 @@ func (t *TabNine) getBinaryPath() (binaryPath string, err error) {
log.Printf("Latest version: %s\n", latestVersion)
subPath := filepath.Join(latestVersion, triple, exeName)
binaryPath = filepath.Join(binaryDir, subPath)
downloadUrl := fmt.Sprintf("%s/%s", downloadUrlPrefix, subPath)
zipFilePath := fmt.Sprintf("%s.zip", binaryPath)
downloadUrl := fmt.Sprintf("%s/%s.zip", tabnineServerUrl, subPath)
log.Printf("Download url: %s, Binary path: %s", downloadUrl, binaryPath)
err = t.downloadBinary(downloadUrl, binaryPath)
err = t.downloadBinary(downloadUrl, zipFilePath)
if err != nil {
log.Fatal("Download failed ", err)
return
}

archive, err := zip.OpenReader(zipFilePath)
if err != nil {
panic(err)
}

defer archive.Close()

outDir := filepath.Join(binaryDir, latestVersion, triple)
for _, f := range archive.File {
filePath := filepath.Join(outDir, f.Name)
fmt.Println("unzipping file ", filePath)
dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
panic(err)
}
defer dstFile.Close()

fileInArchive, err := f.Open()
if err != nil {
panic(err)
}
defer fileInArchive.Close()

if _, err := io.Copy(dstFile, fileInArchive); err != nil {
panic(err)
}
}

err = os.Remove(zipFilePath)
if err != nil {
panic(err)
}

err = os.Chmod(binaryPath, 0755)
if err != nil {
panic(err)
}
log.Println("Download finished.")
return
}
Expand Down Expand Up @@ -260,5 +307,8 @@ func parseArch(arch string) string {
if strings.ToLower(arch) == "amd64" {
return "x86_64"
}
if strings.ToLower(arch) == "arm64" {
return "aarch64"
}
return arch
}