-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathpptxgenjs_runtime.go
More file actions
91 lines (82 loc) · 2.51 KB
/
Copy pathpptxgenjs_runtime.go
File metadata and controls
91 lines (82 loc) · 2.51 KB
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
package main
import (
"os"
"path/filepath"
goRuntime "runtime"
"strings"
)
const (
pptxgenjsNodeEnv = "OFFICECLI_PPTXGENJS_NODE"
pptxgenjsNodeModulesEnv = "OFFICECLI_PPTXGENJS_NODE_MODULES"
)
func bundledPptxgenjsRuntimeEnv() []string {
executable, _ := os.Executable()
cwd, _ := os.Getwd()
return findPptxgenjsRuntimeEnv(goRuntime.GOOS, executable, cwd, pathExists)
}
func findPptxgenjsRuntimeEnv(goos, executable, cwd string, exists func(string) bool) []string {
if exists == nil {
exists = pathExists
}
if root, packaged := packagedPptxgenjsRuntimeRoot(goos, executable); packaged {
return runtimeEnvForRoot(goos, root)
}
if strings.TrimSpace(cwd) == "" {
return nil
}
root := filepath.Join(cwd, "build", "pptxgenjs-runtime")
env := runtimeEnvForRoot(goos, root)
nodePath := strings.TrimPrefix(env[0], pptxgenjsNodeEnv+"=")
moduleRoot := strings.TrimPrefix(env[1], pptxgenjsNodeModulesEnv+"=")
if !exists(nodePath) || !exists(filepath.Join(moduleRoot, "pptxgenjs")) {
return nil
}
return env
}
func packagedPptxgenjsRuntimeRoot(goos, executable string) (string, bool) {
executable = filepath.Clean(strings.TrimSpace(executable))
if executable == "." || executable == "" {
return "", false
}
switch goos {
case "darwin":
macOSDir := filepath.Dir(executable)
contentsDir := filepath.Dir(macOSDir)
if strings.EqualFold(filepath.Base(executable), "officedex") &&
filepath.Base(macOSDir) == "MacOS" && filepath.Base(contentsDir) == "Contents" {
return filepath.Join(contentsDir, "Resources", "pptxgenjs-runtime"), true
}
case "windows":
if strings.EqualFold(filepath.Base(executable), "officedex.exe") {
return filepath.Join(filepath.Dir(executable), "pptxgenjs-runtime"), true
}
}
return "", false
}
func runtimeEnvForRoot(goos, root string) []string {
nodeName := "node"
if goos == "windows" {
nodeName = "node.exe"
}
return []string{
pptxgenjsNodeEnv + "=" + filepath.Join(root, "bin", nodeName),
pptxgenjsNodeModulesEnv + "=" + filepath.Join(root, "node_modules"),
}
}
func appendPptxgenjsRuntimeEnv(base, runtimeEnv []string) []string {
if len(runtimeEnv) == 0 {
return append([]string(nil), base...)
}
result := make([]string, 0, len(base)+len(runtimeEnv))
for _, entry := range base {
if strings.HasPrefix(entry, pptxgenjsNodeEnv+"=") || strings.HasPrefix(entry, pptxgenjsNodeModulesEnv+"=") {
continue
}
result = append(result, entry)
}
return append(result, runtimeEnv...)
}
func pathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}