Skip to content

Commit

Permalink
refactor UnusedCompiledLibrariesRemover in a function
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 8, 2023
1 parent 8d6c6d3 commit cc6f676
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
9 changes: 8 additions & 1 deletion legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ func (s *Builder) Run(ctx *types.Context) error {
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.libraries.prebuild", ".pattern", false)
}),
&UnusedCompiledLibrariesRemover{},

types.BareCommand(func(ctx *types.Context) error {
return UnusedCompiledLibrariesRemover(
ctx.LibrariesBuildPath,
ctx.SketchLibrariesDetector.ImportedLibraries(),
)
}),

types.BareCommand(func(ctx *types.Context) error {
librariesObjectFiles, err := phases.LibrariesBuilder(
ctx.LibrariesBuildPath,
Expand Down
18 changes: 12 additions & 6 deletions legacy/builder/test/unused_compiled_libraries_remover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func TestUnusedCompiledLibrariesRemover(t *testing.T) {
)
ctx.SketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"})

cmd := builder.UnusedCompiledLibrariesRemover{}
err = cmd.Run(ctx)
err = builder.UnusedCompiledLibrariesRemover(
ctx.LibrariesBuildPath,
ctx.SketchLibrariesDetector.ImportedLibraries(),
)
require.NoError(t, err)

exist, err := temp.Join("SPI").ExistCheck()
Expand All @@ -65,8 +67,10 @@ func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) {
)
ctx.SketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"})

cmd := builder.UnusedCompiledLibrariesRemover{}
err := cmd.Run(ctx)
err := builder.UnusedCompiledLibrariesRemover(
ctx.LibrariesBuildPath,
ctx.SketchLibrariesDetector.ImportedLibraries(),
)
require.NoError(t, err)
}

Expand All @@ -85,8 +89,10 @@ func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) {
)
ctx.LibrariesBuildPath = temp

cmd := builder.UnusedCompiledLibrariesRemover{}
err = cmd.Run(ctx)
err = builder.UnusedCompiledLibrariesRemover(
ctx.LibrariesBuildPath,
ctx.SketchLibrariesDetector.ImportedLibraries(),
)
require.NoError(t, err)

exist, err := temp.Join("SPI").ExistCheck()
Expand Down
12 changes: 4 additions & 8 deletions legacy/builder/unused_compiled_libraries_remover.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,22 @@ package builder

import (
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/go-paths-helper"
"github.com/pkg/errors"
"golang.org/x/exp/slices"
)

type UnusedCompiledLibrariesRemover struct{}

func (s *UnusedCompiledLibrariesRemover) Run(ctx *types.Context) error {
librariesBuildPath := ctx.LibrariesBuildPath

func UnusedCompiledLibrariesRemover(librariesBuildPath *paths.Path, importedLibraries libraries.List) error {
if librariesBuildPath.NotExist() {
return nil
}

libraryNames := toLibraryNames(ctx.SketchLibrariesDetector.ImportedLibraries())

files, err := librariesBuildPath.ReadDir()
if err != nil {
return errors.WithStack(err)
}

libraryNames := toLibraryNames(importedLibraries)
for _, file := range files {
if file.IsDir() {
if !slices.Contains(libraryNames, file.Base()) {
Expand Down

0 comments on commit cc6f676

Please sign in to comment.