Skip to content

Commit 3e0793b

Browse files
author
Federico Fissore
committed
Removing some duplication by introducing
- utils.EnsureFolderExists - utils.WriteFileBytes - utils.WriteFile - utils.TouchFile Signed-off-by: Federico Fissore <[email protected]>
1 parent d7a7b18 commit 3e0793b

19 files changed

+64
-58
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func main() {
245245
return
246246
}
247247

248-
err = os.MkdirAll(buildPath, os.FileMode(0755))
248+
err = utils.EnsureFolderExists(buildPath)
249249
if err != nil {
250250
printCompleteError(err)
251251
defer os.Exit(1)

src/arduino.cc/builder/additional_sketch_files_copier.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"arduino.cc/builder/types"
3535
"arduino.cc/builder/utils"
3636
"io/ioutil"
37-
"os"
3837
"path/filepath"
3938
)
4039

@@ -44,7 +43,7 @@ func (s *AdditionalSketchFilesCopier) Run(context map[string]interface{}) error
4443
sketch := context[constants.CTX_SKETCH].(*types.Sketch)
4544
sketchBuildPath := context[constants.CTX_SKETCH_BUILD_PATH].(string)
4645

47-
err := os.MkdirAll(sketchBuildPath, os.FileMode(0755))
46+
err := utils.EnsureFolderExists(sketchBuildPath)
4847
if err != nil {
4948
return utils.WrapError(err)
5049
}
@@ -58,14 +57,17 @@ func (s *AdditionalSketchFilesCopier) Run(context map[string]interface{}) error
5857
}
5958

6059
targetFilePath := filepath.Join(sketchBuildPath, relativePath)
61-
os.MkdirAll(filepath.Dir(targetFilePath), os.FileMode(0755))
60+
err = utils.EnsureFolderExists(filepath.Dir(targetFilePath))
61+
if err != nil {
62+
return utils.WrapError(err)
63+
}
6264

6365
bytes, err := ioutil.ReadFile(file.Name)
6466
if err != nil {
6567
return utils.WrapError(err)
6668
}
6769

68-
ioutil.WriteFile(targetFilePath, bytes, os.FileMode(0644))
70+
utils.WriteFileBytes(targetFilePath, bytes)
6971
}
7072

7173
return nil

src/arduino.cc/builder/builder_utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func compileFileWithRecipe(sourcePath string, source string, buildPath string, b
138138
}
139139
properties[constants.BUILD_PROPERTIES_OBJECT_FILE] = filepath.Join(buildPath, relativeSource+".o")
140140

141-
err = os.MkdirAll(filepath.Dir(properties[constants.BUILD_PROPERTIES_OBJECT_FILE]), os.FileMode(0755))
141+
err = utils.EnsureFolderExists(filepath.Dir(properties[constants.BUILD_PROPERTIES_OBJECT_FILE]))
142142
if err != nil {
143143
return "", utils.WrapError(err)
144144
}

src/arduino.cc/builder/coan_runner.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ import (
3535
"arduino.cc/builder/props"
3636
"arduino.cc/builder/utils"
3737
"fmt"
38-
"io/ioutil"
39-
"os"
4038
"path/filepath"
4139
"regexp"
4240
)
@@ -51,13 +49,13 @@ func (s *CoanRunner) Run(context map[string]interface{}) error {
5149
verbose := context[constants.CTX_VERBOSE].(bool)
5250

5351
preprocPath := context[constants.CTX_PREPROC_PATH].(string)
54-
err := os.MkdirAll(preprocPath, os.FileMode(0755))
52+
err := utils.EnsureFolderExists(preprocPath)
5553
if err != nil {
5654
return utils.WrapError(err)
5755
}
5856

5957
coanTargetFileName := filepath.Join(preprocPath, constants.FILE_COAN_TARGET)
60-
err = ioutil.WriteFile(coanTargetFileName, []byte(source), os.FileMode(0644))
58+
err = utils.WriteFile(coanTargetFileName, source)
6159
if err != nil {
6260
return utils.WrapError(err)
6361
}

src/arduino.cc/builder/ctags_target_file_saver.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ package builder
3232
import (
3333
"arduino.cc/builder/constants"
3434
"arduino.cc/builder/utils"
35-
"io/ioutil"
36-
"os"
3735
"path/filepath"
3836
)
3937

@@ -46,13 +44,13 @@ func (s *CTagsTargetFileSaver) Run(context map[string]interface{}) error {
4644
source := context[s.SourceField].(string)
4745

4846
preprocPath := context[constants.CTX_PREPROC_PATH].(string)
49-
err := os.MkdirAll(preprocPath, os.FileMode(0755))
47+
err := utils.EnsureFolderExists(preprocPath)
5048
if err != nil {
5149
return utils.WrapError(err)
5250
}
5351

5452
ctagsTargetFileName := filepath.Join(preprocPath, s.Filename)
55-
err = ioutil.WriteFile(ctagsTargetFileName, []byte(source), os.FileMode(0644))
53+
err = utils.WriteFile(ctagsTargetFileName, source)
5654
if err != nil {
5755
return utils.WrapError(err)
5856
}

src/arduino.cc/builder/ensure_buildpath_exists.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ package builder
3232
import (
3333
"arduino.cc/builder/constants"
3434
"arduino.cc/builder/utils"
35-
"os"
3635
)
3736

3837
type EnsureBuildPathExists struct{}
3938

4039
func (s *EnsureBuildPathExists) Run(context map[string]interface{}) error {
4140
buildPath := context[constants.CTX_BUILD_PATH].(string)
4241

43-
err := os.MkdirAll(buildPath, os.FileMode(0755))
42+
err := utils.EnsureFolderExists(buildPath)
4443
if err != nil {
4544
return utils.WrapError(err)
4645
}

src/arduino.cc/builder/gcc_preproc_source_saver.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,20 @@ package builder
3232
import (
3333
"arduino.cc/builder/constants"
3434
"arduino.cc/builder/utils"
35-
"io/ioutil"
36-
"os"
3735
"path/filepath"
3836
)
3937

4038
type GCCPreprocSourceSaver struct{}
4139

4240
func (s *GCCPreprocSourceSaver) Run(context map[string]interface{}) error {
4341
preprocPath := context[constants.CTX_PREPROC_PATH].(string)
44-
err := os.MkdirAll(preprocPath, os.FileMode(0755))
42+
err := utils.EnsureFolderExists(preprocPath)
4543
if err != nil {
4644
return utils.WrapError(err)
4745
}
4846

4947
source := context[constants.CTX_SOURCE].(string)
5048

51-
err = ioutil.WriteFile(filepath.Join(preprocPath, constants.FILE_GCC_PREPROC_TARGET), []byte(source), os.FileMode(0644))
49+
err = utils.WriteFile(filepath.Join(preprocPath, constants.FILE_GCC_PREPROC_TARGET), source)
5250
return utils.WrapError(err)
5351
}

src/arduino.cc/builder/merge_sketch_with_bootloader.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"arduino.cc/builder/i18n"
3535
"arduino.cc/builder/types"
3636
"arduino.cc/builder/utils"
37-
"io/ioutil"
3837
"os"
3938
"path/filepath"
4039
"strings"
@@ -101,5 +100,5 @@ func merge(builtSketchPath, bootloaderPath, mergedSketchPath string) error {
101100
sketch = append(sketch, row)
102101
}
103102

104-
return ioutil.WriteFile(mergedSketchPath, []byte(strings.Join(sketch, "\n")), os.FileMode(0644))
103+
return utils.WriteFile(mergedSketchPath, strings.Join(sketch, "\n"))
105104
}

src/arduino.cc/builder/phases/core_builder.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"arduino.cc/builder/constants"
3535
"arduino.cc/builder/i18n"
3636
"arduino.cc/builder/utils"
37-
"os"
3837
)
3938

4039
type CoreBuilder struct{}
@@ -46,7 +45,7 @@ func (s *CoreBuilder) Run(context map[string]interface{}) error {
4645
warningsLevel := context[constants.CTX_WARNINGS_LEVEL].(string)
4746
logger := context[constants.CTX_LOGGER].(i18n.Logger)
4847

49-
err := os.MkdirAll(coreBuildPath, os.FileMode(0755))
48+
err := utils.EnsureFolderExists(coreBuildPath)
5049
if err != nil {
5150
return utils.WrapError(err)
5251
}

src/arduino.cc/builder/phases/libraries_builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (s *LibrariesBuilder) Run(context map[string]interface{}) error {
5151
warningsLevel := context[constants.CTX_WARNINGS_LEVEL].(string)
5252
logger := context[constants.CTX_LOGGER].(i18n.Logger)
5353

54-
err := os.MkdirAll(librariesBuildPath, os.FileMode(0755))
54+
err := utils.EnsureFolderExists(librariesBuildPath)
5555
if err != nil {
5656
return utils.WrapError(err)
5757
}
@@ -83,7 +83,7 @@ func compileLibraries(libraries []*types.Library, buildPath string, buildPropert
8383
func compileLibrary(library *types.Library, buildPath string, buildProperties map[string]string, includes []string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) {
8484
libraryBuildPath := filepath.Join(buildPath, library.Name)
8585

86-
err := os.MkdirAll(libraryBuildPath, os.FileMode(0755))
86+
err := utils.EnsureFolderExists(libraryBuildPath)
8787
if err != nil {
8888
return nil, utils.WrapError(err)
8989
}

0 commit comments

Comments
 (0)