From 776d66c158cb5208943497556654097c6b473945 Mon Sep 17 00:00:00 2001 From: Luca Bianconi Date: Fri, 27 Jan 2023 13:35:40 +0100 Subject: [PATCH] refactor: new interface --- buildcache/build_cache.go | 26 +++++++++++++++++--------- buildcache/build_cache_test.go | 4 ++-- commands/compile/compile.go | 6 +++--- legacy/builder/phases/core_builder.go | 2 +- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/buildcache/build_cache.go b/buildcache/build_cache.go index 5b7c8fb1d49..d3d12b6f61d 100644 --- a/buildcache/build_cache.go +++ b/buildcache/build_cache.go @@ -25,26 +25,29 @@ import ( const lastUsedFileName = ".last-used" +type buildCache struct { + baseDir *paths.Path +} + // GetOrCreate retrieves or creates the cache directory at the given path // If the cache already exists the lifetime of the cache is extended. -func GetOrCreate(dir *paths.Path) (*paths.Path, error) { - if !dir.Exist() { - if err := dir.MkdirAll(); err != nil { - return nil, err - } +func (bc *buildCache) GetOrCreate(key string) (*paths.Path, error) { + keyDir := bc.baseDir.Join(key) + if err := keyDir.MkdirAll(); err != nil { + return nil, err } - if err := dir.Join(lastUsedFileName).WriteFile([]byte{}); err != nil { + if err := keyDir.Join(lastUsedFileName).WriteFile([]byte{}); err != nil { return nil, err } - return dir, nil + return keyDir, nil } // Purge removes all cache directories within baseDir that have expired // To know how long ago a directory has been last used // it checks into the .last-used file. -func Purge(baseDir *paths.Path, ttl time.Duration) { - files, err := baseDir.ReadDir() +func (bc *buildCache) Purge(ttl time.Duration) { + files, err := bc.baseDir.ReadDir() if err != nil { return } @@ -55,6 +58,11 @@ func Purge(baseDir *paths.Path, ttl time.Duration) { } } +// New instantiates a build cache +func New(baseDir *paths.Path) *buildCache { + return &buildCache{baseDir} +} + func removeIfExpired(dir *paths.Path, ttl time.Duration) { fileInfo, err := dir.Join().Stat() if err != nil { diff --git a/buildcache/build_cache_test.go b/buildcache/build_cache_test.go index 5aae8ca022c..e08b10b2962 100644 --- a/buildcache/build_cache_test.go +++ b/buildcache/build_cache_test.go @@ -43,7 +43,7 @@ func Test_UpdateLastUsedFileExisting(t *testing.T) { } func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time) { - _, err := GetOrCreate(dir) + _, err := New(dir.Parent()).GetOrCreate(dir.Base()) require.NoError(t, err) expectedFile := dir.Join(lastUsedFileName) fileInfo, err := expectedFile.Stat() @@ -71,7 +71,7 @@ func TestPurge(t *testing.T) { require.NoError(t, infoFilePath.Chtimes(accesstime, lastUsedTime)) } - Purge(dirToPurge, ttl) + New(dirToPurge).Purge(ttl) files, err := dirToPurge.Join("fresh").Stat() require.Nil(t, err) diff --git a/commands/compile/compile.go b/commands/compile/compile.go index ced440bebf5..664368e067c 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -138,7 +138,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build directory"), Cause: err} } - buildcache.GetOrCreate(builderCtx.BuildPath) + buildcache.New(builderCtx.BuildPath.Parent()).GetOrCreate(builderCtx.BuildPath.Base()) // cache is purged after compilation to not remove entries that might be required defer maybePurgeBuildCache() @@ -312,6 +312,6 @@ func maybePurgeBuildCache() { } inventory.Store.Set("build_cache.compilation_count_since_last_purge", 0) cacheTTL := configuration.Settings.GetDuration("build_cache.ttl").Abs() - buildcache.Purge(paths.TempDir().Join("arduino", "cores"), cacheTTL) - buildcache.Purge(paths.TempDir().Join("arduino", "sketches"), cacheTTL) + buildcache.New(paths.TempDir().Join("arduino", "cores")).Purge(cacheTTL) + buildcache.New(paths.TempDir().Join("arduino", "sketches")).Purge(cacheTTL) } diff --git a/legacy/builder/phases/core_builder.go b/legacy/builder/phases/core_builder.go index cf5596a6792..bd2c07cc784 100644 --- a/legacy/builder/phases/core_builder.go +++ b/legacy/builder/phases/core_builder.go @@ -98,7 +98,7 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path archivedCoreName := GetCachedCoreArchiveDirName(buildProperties.Get(constants.BUILD_PROPERTIES_FQBN), buildProperties.Get("compiler.optimization_flags"), realCoreFolder) targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a") - _, buildCacheErr = buildcache.GetOrCreate(targetArchivedCore.Parent()) + _, buildCacheErr = buildcache.New(buildCachePath).GetOrCreate(archivedCoreName) canUseArchivedCore := !ctx.OnlyUpdateCompilationDatabase && !ctx.Clean &&