forked from limetext/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.go
124 lines (110 loc) · 2.31 KB
/
package.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2015 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package packages
import (
"io/ioutil"
"path/filepath"
"sync"
"github.com/jxo/lime/log"
)
// Package defines the functionality each package needs to implement
// so the lime could manage the loading, watching and etc
type Package interface {
Load()
UnLoad()
Name() string
Path() string
}
// Record is a registration of a package, Check function for
// checking if the path suits for the registered package an Action
// function for creating package from the path
type Record struct {
Check func(string) bool
Action func(string) Package
}
var (
// Registered records
recs []*Record
recl sync.Mutex
// Loaded packages
loaded = make(map[string]Package)
)
func Register(r *Record) {
recl.Lock()
defer recl.Unlock()
recs = append(recs, r)
}
func Unregister(r *Record) {
recl.Lock()
defer recl.Unlock()
for i, rec := range recs {
if rec == r {
recs, recs[len(recs)-1] = append(recs[:i], recs[i+1:]...), nil
break
}
}
}
func Scan(dir string) {
if !filepath.IsAbs(dir) {
var err error
dir, err = filepath.Abs(dir)
if err != nil {
log.Error("Error on getting scanning dir absolute path: %s", err)
return
}
}
log.Debug("Scanning %s for packages", dir)
fis, err := ioutil.ReadDir(dir)
if err != nil {
log.Error("Error while scanning %s: %s", dir, err)
return
}
watchDir(dir)
var pkgs []Package
for _, fi := range fis {
pkgPath := filepath.Join(dir, fi.Name())
if _, ok := loaded[pkgPath]; ok {
continue
}
if pkg := record(pkgPath); pkg != nil {
pkgs = append(pkgs, pkg)
}
}
// TODO: we cant run this in a go routine because currently there is
// no way to frontends to know when for example the color scheme
// is ready
func() {
for _, pkg := range pkgs {
load(pkg)
}
}()
}
func UnLoad(name string) {
for _, pkg := range loaded {
if pkg.Name() == name {
unLoad(pkg)
return
}
}
}
func record(path string) Package {
recl.Lock()
defer recl.Unlock()
for _, rec := range recs {
if rec.Check(path) {
return rec.Action(path)
}
}
return nil
}
func load(pkg Package) {
pkg.Load()
watch(pkg)
loaded[pkg.Path()] = pkg
}
func unLoad(pkg Package) {
pkg.UnLoad()
unWatch(pkg)
delete(loaded, pkg.Path())
}