forked from microsoft/typescript-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost.go
More file actions
128 lines (107 loc) · 4.33 KB
/
host.go
File metadata and controls
128 lines (107 loc) · 4.33 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
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
125
126
127
128
package build
import (
"time"
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/execute/incremental"
"github.com/microsoft/typescript-go/internal/execute/tsc"
"github.com/microsoft/typescript-go/internal/module"
"github.com/microsoft/typescript-go/internal/tsoptions"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
)
type host struct {
orchestrator *Orchestrator
host compiler.CompilerHost
// Caches that last only for build cycle and then cleared out
extendedConfigCache tsc.ExtendedConfigCache
sourceFiles parseCache[ast.SourceFileParseOptions, *ast.SourceFile]
configTimes collections.SyncMap[tspath.Path, time.Duration]
// caches that stay as long as they are needed
resolvedReferences parseCache[tspath.Path, *tsoptions.ParsedCommandLine]
mTimes *collections.SyncMap[tspath.Path, time.Time]
}
var (
_ compiler.CompilerHost = (*host)(nil)
_ incremental.BuildInfoReader = (*host)(nil)
_ incremental.Host = (*host)(nil)
)
func (h *host) FS() vfs.FS {
return h.host.FS()
}
func (h *host) MakeResolver(host module.ResolutionHost, options *core.CompilerOptions, typingsLocation string, projectName string) module.ResolverInterface {
return h.host.MakeResolver(host, options, typingsLocation, projectName)
}
// IsNodeSourceFile implements compiler.CompilerHost.
func (h *host) IsNodeSourceFile(path tspath.Path) bool {
return h.host.IsNodeSourceFile(path)
}
func (h *host) GetDenoForkContextInfo() ast.DenoForkContextInfo {
return h.host.GetDenoForkContextInfo()
}
func (h *host) DefaultLibraryPath() string {
return h.host.DefaultLibraryPath()
}
func (h *host) GetCurrentDirectory() string {
return h.host.GetCurrentDirectory()
}
func (h *host) Trace(msg string) {
panic("build.Orchestrator.host does not support tracing, use a different host for tracing")
}
func (h *host) GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile {
// Cache dts and json files as they will be reused
return h.sourceFiles.loadOrStoreNewIf(opts, h.host.GetSourceFile, func(value *ast.SourceFile) bool {
return value != nil && (tspath.IsDeclarationFileName(opts.FileName) || tspath.FileExtensionIs(opts.FileName, tspath.ExtensionJson))
})
}
func (h *host) GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine {
return h.resolvedReferences.loadOrStoreNew(path, func(path tspath.Path) *tsoptions.ParsedCommandLine {
configStart := h.orchestrator.opts.Sys.Now()
commandLine, _ := tsoptions.GetParsedCommandLineOfConfigFilePath(fileName, path, h.orchestrator.opts.Command.CompilerOptions, h, &h.extendedConfigCache)
configTime := h.orchestrator.opts.Sys.Now().Sub(configStart)
h.configTimes.Store(path, configTime)
return commandLine
})
}
func (h *host) ReadBuildInfo(config *tsoptions.ParsedCommandLine) *incremental.BuildInfo {
configPath := h.orchestrator.toPath(config.ConfigName())
task := h.orchestrator.getTask(configPath)
buildInfo, _ := task.loadOrStoreBuildInfo(h.orchestrator, h.orchestrator.toPath(config.ConfigName()), config.GetBuildInfoFileName())
return buildInfo
}
func (h *host) GetMTime(file string) time.Time {
return h.loadOrStoreMTime(file, nil, true)
}
func (h *host) SetMTime(file string, mTime time.Time) error {
return h.FS().Chtimes(file, time.Time{}, mTime)
}
func (h *host) loadOrStoreMTime(file string, oldCache *collections.SyncMap[tspath.Path, time.Time], store bool) time.Time {
path := h.orchestrator.toPath(file)
if existing, loaded := h.mTimes.Load(path); loaded {
return existing
}
var found bool
var mTime time.Time
if oldCache != nil {
mTime, found = oldCache.Load(path)
}
if !found {
mTime = incremental.GetMTime(h.host, file)
}
if store {
mTime, _ = h.mTimes.LoadOrStore(path, mTime)
}
return mTime
}
func (h *host) storeMTime(file string, mTime time.Time) {
path := h.orchestrator.toPath(file)
h.mTimes.Store(path, mTime)
}
func (h *host) storeMTimeFromOldCache(file string, oldCache *collections.SyncMap[tspath.Path, time.Time]) {
path := h.orchestrator.toPath(file)
if mTime, found := oldCache.Load(path); found {
h.mTimes.Store(path, mTime)
}
}