Skip to content

Commit 776d66c

Browse files
author
Luca Bianconi
committed
refactor: new interface
1 parent 3b11d67 commit 776d66c

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

buildcache/build_cache.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,29 @@ import (
2525

2626
const lastUsedFileName = ".last-used"
2727

28+
type buildCache struct {
29+
baseDir *paths.Path
30+
}
31+
2832
// GetOrCreate retrieves or creates the cache directory at the given path
2933
// If the cache already exists the lifetime of the cache is extended.
30-
func GetOrCreate(dir *paths.Path) (*paths.Path, error) {
31-
if !dir.Exist() {
32-
if err := dir.MkdirAll(); err != nil {
33-
return nil, err
34-
}
34+
func (bc *buildCache) GetOrCreate(key string) (*paths.Path, error) {
35+
keyDir := bc.baseDir.Join(key)
36+
if err := keyDir.MkdirAll(); err != nil {
37+
return nil, err
3538
}
3639

37-
if err := dir.Join(lastUsedFileName).WriteFile([]byte{}); err != nil {
40+
if err := keyDir.Join(lastUsedFileName).WriteFile([]byte{}); err != nil {
3841
return nil, err
3942
}
40-
return dir, nil
43+
return keyDir, nil
4144
}
4245

4346
// Purge removes all cache directories within baseDir that have expired
4447
// To know how long ago a directory has been last used
4548
// it checks into the .last-used file.
46-
func Purge(baseDir *paths.Path, ttl time.Duration) {
47-
files, err := baseDir.ReadDir()
49+
func (bc *buildCache) Purge(ttl time.Duration) {
50+
files, err := bc.baseDir.ReadDir()
4851
if err != nil {
4952
return
5053
}
@@ -55,6 +58,11 @@ func Purge(baseDir *paths.Path, ttl time.Duration) {
5558
}
5659
}
5760

61+
// New instantiates a build cache
62+
func New(baseDir *paths.Path) *buildCache {
63+
return &buildCache{baseDir}
64+
}
65+
5866
func removeIfExpired(dir *paths.Path, ttl time.Duration) {
5967
fileInfo, err := dir.Join().Stat()
6068
if err != nil {

buildcache/build_cache_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Test_UpdateLastUsedFileExisting(t *testing.T) {
4343
}
4444

4545
func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time) {
46-
_, err := GetOrCreate(dir)
46+
_, err := New(dir.Parent()).GetOrCreate(dir.Base())
4747
require.NoError(t, err)
4848
expectedFile := dir.Join(lastUsedFileName)
4949
fileInfo, err := expectedFile.Stat()
@@ -71,7 +71,7 @@ func TestPurge(t *testing.T) {
7171
require.NoError(t, infoFilePath.Chtimes(accesstime, lastUsedTime))
7272
}
7373

74-
Purge(dirToPurge, ttl)
74+
New(dirToPurge).Purge(ttl)
7575

7676
files, err := dirToPurge.Join("fresh").Stat()
7777
require.Nil(t, err)

commands/compile/compile.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
138138
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build directory"), Cause: err}
139139
}
140140

141-
buildcache.GetOrCreate(builderCtx.BuildPath)
141+
buildcache.New(builderCtx.BuildPath.Parent()).GetOrCreate(builderCtx.BuildPath.Base())
142142
// cache is purged after compilation to not remove entries that might be required
143143
defer maybePurgeBuildCache()
144144

@@ -312,6 +312,6 @@ func maybePurgeBuildCache() {
312312
}
313313
inventory.Store.Set("build_cache.compilation_count_since_last_purge", 0)
314314
cacheTTL := configuration.Settings.GetDuration("build_cache.ttl").Abs()
315-
buildcache.Purge(paths.TempDir().Join("arduino", "cores"), cacheTTL)
316-
buildcache.Purge(paths.TempDir().Join("arduino", "sketches"), cacheTTL)
315+
buildcache.New(paths.TempDir().Join("arduino", "cores")).Purge(cacheTTL)
316+
buildcache.New(paths.TempDir().Join("arduino", "sketches")).Purge(cacheTTL)
317317
}

legacy/builder/phases/core_builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
9898
archivedCoreName := GetCachedCoreArchiveDirName(buildProperties.Get(constants.BUILD_PROPERTIES_FQBN),
9999
buildProperties.Get("compiler.optimization_flags"), realCoreFolder)
100100
targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a")
101-
_, buildCacheErr = buildcache.GetOrCreate(targetArchivedCore.Parent())
101+
_, buildCacheErr = buildcache.New(buildCachePath).GetOrCreate(archivedCoreName)
102102

103103
canUseArchivedCore := !ctx.OnlyUpdateCompilationDatabase &&
104104
!ctx.Clean &&

0 commit comments

Comments
 (0)