-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
111 lines (90 loc) · 2.01 KB
/
data.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
package main
import (
_ "embed"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
//go:embed data.txt
var Files string
// GetPackages returns all the packages need to install
func GetPackages(p map[string]bool) map[string]bool {
// load *database*
list := strings.Split(Files, "\n")
// get installed packages
installedPackages := GetInstalledPackages()
packages := map[string]bool{}
for _, f := range list {
basename := path.Base(f)
name := strings.TrimSuffix(basename, filepath.Ext(basename))
dir := path.Dir(f)
pkg := path.Base(dir)
// 如果此时的文件名 在 传进来的“包名”里
if _, ok := p[name]; ok {
if _, found := packages[pkg]; found {
continue
}
if _, found := installedPackages[pkg]; found {
continue
}
packages[pkg] = true
}
}
// 最后再把一些特例删除掉
for _, p := range notPackages() {
delete(packages, p)
}
for k := range getLocalStyFiles(".") {
delete(packages, k)
}
return packages
}
// GetInstalledPackages 获得本地已经安装了的包
func GetInstalledPackages() map[string]bool {
output, err := exec.Command(
"tlmgr",
"info",
"--only-installed",
"--data",
"name",
).Output()
if err != nil {
log.Fatal(err)
}
// if on windows, `\r\n` should be converted to `\n`
s := strings.ReplaceAll(string(output), "\r\n", "\n")
packages := make(map[string]bool)
p := strings.Split(s, "\n")
for _, s := range p {
packages[s] = true
}
return packages
}
// notPackages is a *blacklist* for packages
// need time to collect
func notPackages() []string {
return []string{
"base",
"config",
"tools",
}
}
func getLocalStyFiles(path string) map[string]bool {
files := map[string]bool{}
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
if filepath.Ext(info.Name()) == ".sty" {
name := strings.TrimSuffix(info.Name(), filepath.Ext(info.Name()))
files[name] = true
}
}
return nil
})
if err != nil {
log.Println(err)
}
return files
}