-
-
Notifications
You must be signed in to change notification settings - Fork 398
Changed AdditionalSketchFilesCopier legacy command into new builder API function #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
package builder | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
@@ -39,16 +40,16 @@ func QuoteCppString(str string) string { | |
return "\"" + str + "\"" | ||
} | ||
|
||
// SaveSketchItemCpp saves a preprocessed .cpp sketch file on disk | ||
func SaveSketchItemCpp(item *sketch.Item, buildPath string) error { | ||
// SketchSaveItemCpp saves a preprocessed .cpp sketch file on disk | ||
func SketchSaveItemCpp(item *sketch.Item, destPath string) error { | ||
|
||
sketchName := filepath.Base(item.Path) | ||
|
||
if err := os.MkdirAll(buildPath, os.FileMode(0755)); err != nil { | ||
if err := os.MkdirAll(destPath, os.FileMode(0755)); err != nil { | ||
return errors.Wrap(err, "unable to create a folder to save the sketch") | ||
} | ||
|
||
destFile := filepath.Join(buildPath, sketchName+".cpp") | ||
destFile := filepath.Join(destPath, sketchName+".cpp") | ||
|
||
if err := ioutil.WriteFile(destFile, item.Source, os.FileMode(0644)); err != nil { | ||
return errors.Wrap(err, "unable to save the sketch on disk") | ||
|
@@ -57,10 +58,10 @@ func SaveSketchItemCpp(item *sketch.Item, buildPath string) error { | |
return nil | ||
} | ||
|
||
// LoadSketch collects all the files composing a sketch. | ||
// SketchLoad collects all the files composing a sketch. | ||
// The parameter `sketchPath` holds a path pointing to a single sketch file or a sketch folder, | ||
// the path must be absolute. | ||
func LoadSketch(sketchPath, buildPath string) (*sketch.Sketch, error) { | ||
func SketchLoad(sketchPath, buildPath string) (*sketch.Sketch, error) { | ||
stat, err := os.Stat(sketchPath) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "unable to stat Sketch location") | ||
|
@@ -133,8 +134,8 @@ func LoadSketch(sketchPath, buildPath string) (*sketch.Sketch, error) { | |
return sketch.New(sketchFolder, mainSketchFile, buildPath, files) | ||
} | ||
|
||
// MergeSketchSources merges all the source files included in a sketch | ||
func MergeSketchSources(sketch *sketch.Sketch) (int, string) { | ||
// SketchMergeSources merges all the source files included in a sketch | ||
func SketchMergeSources(sketch *sketch.Sketch) (int, string) { | ||
lineOffset := 0 | ||
mergedSource := "" | ||
|
||
|
@@ -155,3 +156,60 @@ func MergeSketchSources(sketch *sketch.Sketch) (int, string) { | |
|
||
return lineOffset, mergedSource | ||
} | ||
|
||
// SketchCopyAdditionalFiles copies the additional files for a sketch to the | ||
// specified destination directory. | ||
func SketchCopyAdditionalFiles(sketch *sketch.Sketch, destPath string) error { | ||
if err := os.MkdirAll(destPath, os.FileMode(0755)); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at some point we may think to replace those path strings with |
||
return errors.Wrap(err, "unable to create a folder to save the sketch files") | ||
} | ||
|
||
for _, item := range sketch.AdditionalFiles { | ||
relpath, err := filepath.Rel(sketch.LocationPath, item.Path) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to compute relative path to the sketch for the item") | ||
} | ||
|
||
targetPath := filepath.Join(destPath, relpath) | ||
// create the directory containing the target | ||
if err = os.MkdirAll(filepath.Dir(targetPath), os.FileMode(0755)); err != nil { | ||
return errors.Wrap(err, "unable to create the folder containing the item") | ||
} | ||
|
||
err = writeIfDifferent(item.Path, targetPath) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to write to destination file") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func writeIfDifferent(sourcePath, destPath string) error { | ||
// read the source file | ||
newbytes, err := ioutil.ReadFile(sourcePath) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to read contents of the source item") | ||
} | ||
|
||
// check whether the destination file exists | ||
_, err = os.Stat(destPath) | ||
if os.IsNotExist(err) { | ||
// write directly | ||
return ioutil.WriteFile(destPath, newbytes, os.FileMode(0644)) | ||
} | ||
|
||
// read the destination file if it ex | ||
existingBytes, err := ioutil.ReadFile(destPath) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to read contents of the destination item") | ||
} | ||
|
||
// overwrite if contents are different | ||
if bytes.Compare(existingBytes, newbytes) != 0 { | ||
return ioutil.WriteFile(destPath, newbytes, os.FileMode(0644)) | ||
} | ||
|
||
// source and destination are the same, don't write anything | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if these functions (
SketchMergeSources
,SketchCopyAdditionalFiles
, etc.) may become methods ofsketch.Sketch
?