From d5f87c81befcbe51c47e0ae67e2df6346908bce7 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 3 Aug 2022 07:25:42 +0200 Subject: [PATCH] feat: store template cache and reuse for extra prompts resolves #2605 --- src/engine/engine.go | 2 ++ src/environment/shell.go | 25 ++++++++++++++++++++++++- src/mock/environment.go | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/engine/engine.go b/src/engine/engine.go index ded15381..177ace3c 100644 --- a/src/engine/engine.go +++ b/src/engine/engine.go @@ -354,6 +354,8 @@ const ( ) func (e *Engine) PrintExtraPrompt(promptType ExtraPromptType) string { + // populate env with latest context + e.Env.LoadTemplateCache() var prompt *Segment switch promptType { case Debug: diff --git a/src/environment/shell.go b/src/environment/shell.go index 984f1eef..6e375c5e 100644 --- a/src/environment/shell.go +++ b/src/environment/shell.go @@ -3,6 +3,7 @@ package environment import ( "bytes" "context" + "encoding/json" "errors" "fmt" "io" @@ -35,7 +36,8 @@ const ( ) var ( - lock = sync.RWMutex{} + lock = sync.RWMutex{} + TEMPLATECACHE = fmt.Sprintf("template_cache_%d", os.Getppid()) ) type Flags struct { @@ -181,6 +183,7 @@ type Environment interface { ConvertToWindowsPath(path string) string WifiNetwork() (*WifiInfo, error) TemplateCache() *TemplateCache + LoadTemplateCache() Log(logType LogType, funcName, message string) Trace(start time.Time, function string, args ...string) } @@ -659,9 +662,29 @@ func (env *ShellEnvironment) Cache() Cache { } 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() } +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 { return env.logBuilder.String() } diff --git a/src/mock/environment.go b/src/mock/environment.go index ae0fa510..344ca870 100644 --- a/src/mock/environment.go +++ b/src/mock/environment.go @@ -218,6 +218,10 @@ func (env *MockedEnvironment) TemplateCache() *environment.TemplateCache { return args.Get(0).(*environment.TemplateCache) } +func (env *MockedEnvironment) LoadTemplateCache() { + _ = env.Called() +} + 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...) env.On("RunCommand", "git", args).Return(returnValue, nil)