feat: store template cache and reuse for extra prompts

resolves #2605
This commit is contained in:
Jan De Dobbeleer 2022-08-03 07:25:42 +02:00 committed by Jan De Dobbeleer
parent 4093b79649
commit d5f87c81be
3 changed files with 30 additions and 1 deletions

View file

@ -354,6 +354,8 @@ const (
) )
func (e *Engine) PrintExtraPrompt(promptType ExtraPromptType) string { func (e *Engine) PrintExtraPrompt(promptType ExtraPromptType) string {
// populate env with latest context
e.Env.LoadTemplateCache()
var prompt *Segment var prompt *Segment
switch promptType { switch promptType {
case Debug: case Debug:

View file

@ -3,6 +3,7 @@ package environment
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -36,6 +37,7 @@ const (
var ( var (
lock = sync.RWMutex{} lock = sync.RWMutex{}
TEMPLATECACHE = fmt.Sprintf("template_cache_%d", os.Getppid())
) )
type Flags struct { type Flags struct {
@ -181,6 +183,7 @@ type Environment interface {
ConvertToWindowsPath(path string) string ConvertToWindowsPath(path string) string
WifiNetwork() (*WifiInfo, error) WifiNetwork() (*WifiInfo, error)
TemplateCache() *TemplateCache TemplateCache() *TemplateCache
LoadTemplateCache()
Log(logType LogType, funcName, message string) Log(logType LogType, funcName, message string)
Trace(start time.Time, function string, args ...string) Trace(start time.Time, function string, args ...string)
} }
@ -659,9 +662,29 @@ func (env *ShellEnvironment) Cache() Cache {
} }
func (env *ShellEnvironment) Close() { func (env *ShellEnvironment) Close() {
defer env.Trace(time.Now(), "Close")
templateCache, err := json.Marshal(env.TemplateCache())
if err == nil {
env.fileCache.Set(TEMPLATECACHE, string(templateCache), 1440)
}
env.fileCache.Close() env.fileCache.Close()
} }
func (env *ShellEnvironment) LoadTemplateCache() {
defer env.Trace(time.Now(), "LoadTemplateCache")
val, OK := env.fileCache.Get(TEMPLATECACHE)
if !OK {
return
}
var templateCache TemplateCache
err := json.Unmarshal([]byte(val), &templateCache)
if err != nil {
env.Log(Error, "LoadTemplateCache", err.Error())
return
}
env.tmplCache = &templateCache
}
func (env *ShellEnvironment) Logs() string { func (env *ShellEnvironment) Logs() string {
return env.logBuilder.String() return env.logBuilder.String()
} }

View file

@ -218,6 +218,10 @@ func (env *MockedEnvironment) TemplateCache() *environment.TemplateCache {
return args.Get(0).(*environment.TemplateCache) return args.Get(0).(*environment.TemplateCache)
} }
func (env *MockedEnvironment) LoadTemplateCache() {
_ = env.Called()
}
func (env *MockedEnvironment) MockGitCommand(dir, returnValue string, args ...string) { func (env *MockedEnvironment) MockGitCommand(dir, returnValue string, args ...string) {
args = append([]string{"-C", dir, "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...) args = append([]string{"-C", dir, "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
env.On("RunCommand", "git", args).Return(returnValue, nil) env.On("RunCommand", "git", args).Return(returnValue, nil)