-
-
Couldn't load subscription status.
- Fork 431
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 was deleted.
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?