Skip to content

Commit

Permalink
refactor: just simple
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Bianconi committed Jan 17, 2023
1 parent d7cacff commit 80edb72
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 110 deletions.
45 changes: 38 additions & 7 deletions buildcache/build_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,51 @@ import (
"time"

"github.com/arduino/go-paths-helper"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// GetOrCreate retrieves or creates the cache directory for the given key inside basePath.
const lastUsedFileName = ".last-used"

// 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, key string) error {
unusedTTL := time.Hour
_, err := newDirectoryCache(dir, unusedTTL).
GetOrCreate(key, time.Now())
return err
func GetOrCreate(dir *paths.Path) error {
if !dir.Exist() {
if err := dir.MkdirAll(); err != nil {
return err
}
}

return dir.Join(lastUsedFileName).WriteFile([]byte{})
}

// 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) {
newDirectoryCache(baseDir, ttl).Purge()
files, err := baseDir.ReadDir()
if err != nil {
return
}
for _, file := range files {
if file.IsDir() {
removeIfExpired(file, ttl)
}
}
}

func removeIfExpired(dir *paths.Path, ttl time.Duration) {
fileInfo, err := dir.Join().Stat()
if err != nil {
return
}
lifeExpectancy := ttl - time.Since(fileInfo.ModTime())
if lifeExpectancy > 0 {
return
}
logrus.Tracef(`Purging cache directory "%s". Expired by %s`, dir, lifeExpectancy.Abs())
err = dir.RemoveAll()
if err != nil {
logrus.Tracef(`Error while pruning cache directory "%s": %s`, dir, errors.WithStack(err))
}
}
2 changes: 1 addition & 1 deletion buildcache/build_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Test_UpdateLastUsedFileExisting(t *testing.T) {
}

func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time) {
require.NoError(t, GetOrCreate(dir.Parent(), dir.Base()))
require.NoError(t, GetOrCreate(dir))
expectedFile := dir.Join(lastUsedFileName)
fileInfo, err := expectedFile.Stat()
require.Nil(t, err)
Expand Down
100 changes: 0 additions & 100 deletions buildcache/directory_cache.go

This file was deleted.

2 changes: 1 addition & 1 deletion commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Parent(), builderCtx.BuildPath.Base())
buildcache.GetOrCreate(builderCtx.BuildPath)
// cache is purged after compilation to not remove entries that might be required
defer maybePurgeBuildCache()

Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/phases/core_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
if buildCachePath != nil {
archivedCoreName := GetCachedCoreArchiveDirName(buildProperties.Get(constants.BUILD_PROPERTIES_FQBN),
buildProperties.Get("compiler.optimization_flags"), realCoreFolder)
buildcache.GetOrCreate(buildCachePath, archivedCoreName)
buildcache.GetOrCreate(buildCachePath.Join(archivedCoreName))
targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a")
canUseArchivedCore := !ctx.OnlyUpdateCompilationDatabase &&
!ctx.Clean &&
Expand Down

0 comments on commit 80edb72

Please sign in to comment.