Skip to content

Commit f1631fd

Browse files
committed
Using paths.* methods and structure (almost) everywhere
There are still some helper function to remove from utils packages, but the increase in readability is already impressive. There are some minor tweaks and small refactors here and there, but no substantial changes the code should perform exactly as before.
1 parent 067f1d0 commit f1631fd

File tree

68 files changed

+1303
-1362
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1303
-1362
lines changed

Diff for: add_additional_entries_to_context.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
package builder
3131

3232
import (
33-
"path/filepath"
34-
3533
"github.com/arduino/arduino-builder/constants"
3634
"github.com/arduino/arduino-builder/i18n"
3735
"github.com/arduino/arduino-builder/types"
@@ -41,21 +39,21 @@ import (
4139
type AddAdditionalEntriesToContext struct{}
4240

4341
func (*AddAdditionalEntriesToContext) Run(ctx *types.Context) error {
44-
if ctx.BuildPath != "" {
42+
if ctx.BuildPath != nil {
4543
buildPath := ctx.BuildPath
46-
preprocPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_PREPROC))
44+
preprocPath, err := buildPath.Join(constants.FOLDER_PREPROC).Abs()
4745
if err != nil {
4846
return i18n.WrapError(err)
4947
}
50-
sketchBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_SKETCH))
48+
sketchBuildPath, err := buildPath.Join(constants.FOLDER_SKETCH).Abs()
5149
if err != nil {
5250
return i18n.WrapError(err)
5351
}
54-
librariesBuildPath, err := filepath.Abs(filepath.Join(buildPath, "libraries"))
52+
librariesBuildPath, err := buildPath.Join("libraries").Abs()
5553
if err != nil {
5654
return i18n.WrapError(err)
5755
}
58-
coreBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_CORE))
56+
coreBuildPath, err := buildPath.Join(constants.FOLDER_CORE).Abs()
5957
if err != nil {
6058
return i18n.WrapError(err)
6159
}
@@ -66,8 +64,8 @@ func (*AddAdditionalEntriesToContext) Run(ctx *types.Context) error {
6664
ctx.CoreBuildPath = coreBuildPath
6765
}
6866

69-
if ctx.BuildCachePath != "" {
70-
coreBuildCachePath, err := filepath.Abs(filepath.Join(ctx.BuildCachePath, constants.FOLDER_CORE))
67+
if ctx.BuildCachePath != nil {
68+
coreBuildCachePath, err := ctx.BuildCachePath.Join(constants.FOLDER_CORE).Abs()
7169
if err != nil {
7270
return i18n.WrapError(err)
7371
}

Diff for: additional_sketch_files_copier.go

+12-16
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@
3030
package builder
3131

3232
import (
33+
"bytes"
34+
3335
"github.com/arduino/arduino-builder/i18n"
3436
"github.com/arduino/arduino-builder/types"
35-
"github.com/arduino/arduino-builder/utils"
36-
"bytes"
37-
"io/ioutil"
38-
"path/filepath"
37+
"github.com/arduino/go-paths-helper"
3938
)
4039

4140
type AdditionalSketchFilesCopier struct{}
@@ -44,33 +43,30 @@ func (s *AdditionalSketchFilesCopier) Run(ctx *types.Context) error {
4443
sketch := ctx.Sketch
4544
sketchBuildPath := ctx.SketchBuildPath
4645

47-
err := utils.EnsureFolderExists(sketchBuildPath)
48-
if err != nil {
46+
if err := sketchBuildPath.MkdirAll(); err != nil {
4947
return i18n.WrapError(err)
5048
}
5149

52-
sketchBasePath := filepath.Dir(sketch.MainFile.Name)
50+
sketchBasePath := sketch.MainFile.Name.Parent()
5351

5452
for _, file := range sketch.AdditionalFiles {
55-
relativePath, err := filepath.Rel(sketchBasePath, file.Name)
53+
relativePath, err := sketchBasePath.RelTo(file.Name)
5654
if err != nil {
5755
return i18n.WrapError(err)
5856
}
5957

60-
targetFilePath := filepath.Join(sketchBuildPath, relativePath)
61-
err = utils.EnsureFolderExists(filepath.Dir(targetFilePath))
62-
if err != nil {
58+
targetFilePath := sketchBuildPath.JoinPath(relativePath)
59+
if err = targetFilePath.Parent().MkdirAll(); err != nil {
6360
return i18n.WrapError(err)
6461
}
6562

66-
bytes, err := ioutil.ReadFile(file.Name)
63+
bytes, err := file.Name.ReadFile()
6764
if err != nil {
6865
return i18n.WrapError(err)
6966
}
7067

7168
if targetFileChanged(bytes, targetFilePath) {
72-
err := utils.WriteFileBytes(targetFilePath, bytes)
73-
if err != nil {
69+
if err := targetFilePath.WriteFile(bytes); err != nil {
7470
return i18n.WrapError(err)
7571
}
7672
}
@@ -79,8 +75,8 @@ func (s *AdditionalSketchFilesCopier) Run(ctx *types.Context) error {
7975
return nil
8076
}
8177

82-
func targetFileChanged(currentBytes []byte, targetFilePath string) bool {
83-
oldBytes, err := ioutil.ReadFile(targetFilePath)
78+
func targetFileChanged(currentBytes []byte, targetFilePath *paths.Path) bool {
79+
oldBytes, err := targetFilePath.ReadFile()
8480
if err != nil {
8581
return true
8682
}

Diff for: arduino-builder/main.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ import (
4040
"strings"
4141
"syscall"
4242

43+
"github.com/arduino/go-paths-helper"
44+
4345
"github.com/arduino/arduino-builder"
4446
"github.com/arduino/arduino-builder/gohasissues"
4547
"github.com/arduino/arduino-builder/i18n"
4648
"github.com/arduino/arduino-builder/types"
47-
"github.com/arduino/arduino-builder/utils"
4849
"github.com/arduino/go-properties-map"
4950
"github.com/go-errors/errors"
5051
)
@@ -194,7 +195,7 @@ func main() {
194195
if hardwareFolders, err := toSliceOfUnquoted(hardwareFoldersFlag); err != nil {
195196
printCompleteError(err)
196197
} else if len(hardwareFolders) > 0 {
197-
ctx.HardwareFolders = hardwareFolders
198+
ctx.HardwareFolders = paths.NewPathList(hardwareFolders...)
198199
}
199200
if len(ctx.HardwareFolders) == 0 {
200201
printErrorMessageAndFlagUsage(errors.New("Parameter '" + FLAG_HARDWARE + "' is mandatory"))
@@ -204,7 +205,7 @@ func main() {
204205
if toolsFolders, err := toSliceOfUnquoted(toolsFoldersFlag); err != nil {
205206
printCompleteError(err)
206207
} else if len(toolsFolders) > 0 {
207-
ctx.ToolsFolders = toolsFolders
208+
ctx.ToolsFolders = paths.NewPathList(toolsFolders...)
208209
}
209210
if len(ctx.ToolsFolders) == 0 {
210211
printErrorMessageAndFlagUsage(errors.New("Parameter '" + FLAG_TOOLS + "' is mandatory"))
@@ -214,14 +215,14 @@ func main() {
214215
if librariesFolders, err := toSliceOfUnquoted(librariesFoldersFlag); err != nil {
215216
printCompleteError(err)
216217
} else if len(librariesFolders) > 0 {
217-
ctx.OtherLibrariesFolders = librariesFolders
218+
ctx.OtherLibrariesFolders = paths.NewPathList(librariesFolders...)
218219
}
219220

220221
// FLAG_BUILT_IN_LIBRARIES
221222
if librariesBuiltInFolders, err := toSliceOfUnquoted(librariesBuiltInFoldersFlag); err != nil {
222223
printCompleteError(err)
223224
} else if len(librariesBuiltInFolders) > 0 {
224-
ctx.BuiltInLibrariesFolders = librariesBuiltInFolders
225+
ctx.BuiltInLibrariesFolders = paths.NewPathList(librariesBuiltInFolders...)
225226
}
226227

227228
// FLAG_PREFS
@@ -242,38 +243,38 @@ func main() {
242243
}
243244

244245
// FLAG_BUILD_PATH
245-
buildPath, err := gohasissues.Unquote(*buildPathFlag)
246+
buildPathUnquoted, err := gohasissues.Unquote(*buildPathFlag)
246247
if err != nil {
247248
printCompleteError(err)
248249
}
249-
if buildPath != "" {
250-
_, err := os.Stat(buildPath)
251-
if err != nil {
250+
buildPath := paths.New(buildPathUnquoted)
251+
if buildPath != nil {
252+
// TODO: mmmmhhh... this one looks like a bug, why check existence?
253+
if _, err := buildPath.Stat(); err != nil {
252254
fmt.Fprintln(os.Stderr, err)
253255
os.Exit(1)
254256
}
255257

256-
err = utils.EnsureFolderExists(buildPath)
257-
if err != nil {
258+
if err := buildPath.MkdirAll(); err != nil {
258259
printCompleteError(err)
259260
}
260261
}
261262
ctx.BuildPath = buildPath
262263

263264
// FLAG_BUILD_CACHE
264-
buildCachePath, err := gohasissues.Unquote(*buildCachePathFlag)
265+
buildCachePathUnquoted, err := gohasissues.Unquote(*buildCachePathFlag)
265266
if err != nil {
266267
printCompleteError(err)
267268
}
268-
if buildCachePath != "" {
269-
_, err := os.Stat(buildCachePath)
270-
if err != nil {
269+
buildCachePath := paths.New(buildCachePathUnquoted)
270+
if buildCachePath != nil {
271+
// TODO: mmmmhhh... this one looks like a bug, why check existence?
272+
if _, err := buildCachePath.Stat(); err != nil {
271273
fmt.Fprintln(os.Stderr, err)
272274
os.Exit(1)
273275
}
274276

275-
err = utils.EnsureFolderExists(buildCachePath)
276-
if err != nil {
277+
if err := buildCachePath.MkdirAll(); err != nil {
277278
printCompleteError(err)
278279
}
279280
}
@@ -285,12 +286,11 @@ func main() {
285286
}
286287

287288
if flag.NArg() > 0 {
288-
sketchLocation := flag.Arg(0)
289-
sketchLocation, err := gohasissues.Unquote(sketchLocation)
289+
sketchLocationUnquoted, err := gohasissues.Unquote(flag.Arg(0))
290290
if err != nil {
291291
printCompleteError(err)
292292
}
293-
ctx.SketchLocation = sketchLocation
293+
ctx.SketchLocation = paths.New(sketchLocationUnquoted)
294294
}
295295

296296
if *verboseFlag && *quietFlag {

0 commit comments

Comments
 (0)