Skip to content

Commit

Permalink
update for latest GoSublime
Browse files Browse the repository at this point in the history
  • Loading branch information
slene committed Aug 23, 2013
1 parent 02a5407 commit f8dd8b3
Show file tree
Hide file tree
Showing 95 changed files with 567 additions and 6,707 deletions.
14 changes: 7 additions & 7 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ type Job struct {
}

type Broker struct {
sync.Mutex

tag string
served uint
served counter
start time.Time
rLck sync.Mutex
wLck sync.Mutex
r io.Reader
w io.Writer
in *bufio.Reader
Expand All @@ -60,8 +60,8 @@ func (b *Broker) Send(resp Response) error {
}

func (b *Broker) SendNoLog(resp Response) error {
b.wLck.Lock()
defer b.wLck.Unlock()
b.Lock()
defer b.Unlock()

if resp.Data == nil {
resp.Data = M{}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (b *Broker) SendNoLog(resp Response) error {
}

func (b *Broker) call(req *Request, cl Caller) {
b.served++
b.served.next()

defer func() {
err := recover()
Expand Down Expand Up @@ -226,7 +226,7 @@ func (b *Broker) Loop(decorate bool, wait bool) {
b.SendNoLog(Response{
Token: "margo.bye-ni",
Data: M{
"served": b.served,
"served": b.served.val(),
"uptime": time.Now().Sub(b.start).String(),
},
})
Expand Down
61 changes: 61 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/printer"
Expand All @@ -19,6 +20,8 @@ var (
sRuneError = eRune()
)

type void struct{}

type jString string

func (s jString) String() string {
Expand Down Expand Up @@ -223,3 +226,61 @@ func tempDir(env map[string]string, subDirs ...string) string {

return dir
}

func post(r Response) {
sendCh <- r
}

func postMessage(format string, a ...interface{}) {
post(Response{
Token: "margo.message",
Data: M{
"message": fmt.Sprintf(format, a...),
},
})
}

func fileImportPaths(af *ast.File) []string {
l := []string{}

if af != nil {
for _, decl := range af.Decls {
if gdecl, ok := decl.(*ast.GenDecl); ok {
for _, spec := range gdecl.Specs {
if ispec, ok := spec.(*ast.ImportSpec); ok {
ipath := unquote(ispec.Path.Value)
if ipath != "C" {
l = append(l, ipath)
}
}
}
}
}
}

return l
}

func pathList(p, pathSep string) []string {
if pathSep == "" {
pathSep = string(filepath.ListSeparator)
}
l := strings.Split(p, pathSep)

i := 0
for _, s := range l {
if s != "" {
l[i] = s
i += 1
}
}

return l[:i]
}

func envRootList(env map[string]string) (string, []string) {
if env == nil {
return "", []string{}
}
return env["GOROOT"], pathList(env["GOPATH"], env["_pathsep"])
}
111 changes: 111 additions & 0 deletions fx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"path/filepath"
"strings"
)

var (
fileExts = map[string]void{}
)

func init() {
exts := []string{
".c",
".h",
".go",
".goc",
".md",
".txt",
".git",
".bzr",
".tmp",
".swig",
".swigcxx",
".a",
".s",
".S",
".syso",
".so",
".dll",
".o",
".5",
".6",
".8",
".out",
".cc",
".hh",
".dat",
".py",
".pyc",
".zip",
".z",
".7z",
".gz",
".tar",
".bz2",
".tgz",
".rar",
".pro",
".occ",
".asc",
".conf",
".html",
".jpg",
".png",
".js",
".json",
".src",
".log",
".patch",
".diff",
".php",
".rit",
".css",
".lua",
".less",
".ttf",
".expected",
".ps",
".bak",
".cix",
".d",
".hac",
".hrb",
".java",
".lexres",
".lst",
".pan",
".phpt",
".prof",
".set",
".sol",
".vrs",
".exe",
".bat",
".sh",
".rc",
".bash",
}

for _, ext := range exts {
fileExts[ext] = void{}
}
}

func fx(nm string) (isFileExt, isGoFileExt bool) {
nm = strings.ToLower(nm)
ext := filepath.Ext(nm)

if ext == ".go" {
return true, true
}

if !strings.HasPrefix(nm, "go.") {
if _, ok := fileExts[ext]; ok {
return true, false
}
}

return false, false
}
114 changes: 114 additions & 0 deletions g_autoinst.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"go/parser"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)

var (
autoInstCh = make(chan AutoInstOptions, 10)
)

func autoInstall(ao AutoInstOptions) {
select {
case autoInstCh <- ao:
default:
}
}

type AutoInstOptions struct {
// if ImportPaths is empty, Src is parsed in order to populate it
ImportPaths []string
Src string

// the environment variables as passed by the client - they should not be merged with os.Environ(...)
// GOPATH is be valid
Env map[string]string
}

func (a *AutoInstOptions) imports() map[string]string {
m := map[string]string{}

if len(a.ImportPaths) == 0 {
_, af, _ := parseAstFile("a.go", a.Src, parser.ImportsOnly)
a.ImportPaths = fileImportPaths(af)
}

for _, p := range a.ImportPaths {
m[p] = filepath.FromSlash(p) + ".a"
}

return m
}

func (a *AutoInstOptions) install() {
if a.Env == nil {
a.Env = map[string]string{}
}

osArch := runtime.GOOS + "_" + runtime.GOARCH
roots := []string{}

if p := a.Env["GOROOT"]; p != "" {
roots = append(roots, filepath.Join(p, "pkg", osArch))
}

psep := string(filepath.ListSeparator)
if s := a.Env["_pathsep"]; s != "" {
psep = s
}

for _, p := range strings.Split(a.Env["GOPATH"], psep) {
if p != "" {
roots = append(roots, filepath.Join(p, "pkg", osArch))
}
}

if len(roots) == 0 {
return
}

archiveOk := func(fn string) bool {
for _, root := range roots {
_, err := os.Stat(filepath.Join(root, fn))
if err == nil {
return true
}
}
return false
}

el := envSlice(a.Env)
installed := []string{}

for path, fn := range a.imports() {
if !archiveOk(fn) {
cmd := exec.Command("go", "install", path)
cmd.Env = el
cmd.Stderr = ioutil.Discard
cmd.Stdout = ioutil.Discard
cmd.Run()

if archiveOk(fn) {
installed = append(installed, path)
}
}
}

if len(installed) > 0 {
postMessage("auto-installed: %v", strings.Join(installed, ", "))
}
}

func init() {
go func() {
for ao := range autoInstCh {
ao.install()
}
}()
}
1 change: 0 additions & 1 deletion gosublime.org/fsnotify.head

This file was deleted.

1 change: 0 additions & 1 deletion gosublime.org/fsnotify/.travis.yml

This file was deleted.

27 changes: 0 additions & 27 deletions gosublime.org/fsnotify/LICENSE

This file was deleted.

Loading

0 comments on commit f8dd8b3

Please sign in to comment.