|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2019 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to modify or |
| 12 | +// otherwise use the software for commercial activities involving the Arduino |
| 13 | +// software without disclosing the source code of your own applications. To purchase |
| 14 | +// a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package sketch |
| 17 | + |
| 18 | +import ( |
| 19 | + "io/ioutil" |
| 20 | + "path/filepath" |
| 21 | + "sort" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/arduino/globals" |
| 25 | + "github.com/pkg/errors" |
| 26 | +) |
| 27 | + |
| 28 | +// Item holds the source and the path for a single sketch file |
| 29 | +type Item struct { |
| 30 | + Path string |
| 31 | + Source []byte |
| 32 | +} |
| 33 | + |
| 34 | +// NewItem reads the source code for a sketch item and returns an |
| 35 | +// Item instance |
| 36 | +func NewItem(itemPath string) (*Item, error) { |
| 37 | + // read the file |
| 38 | + source, err := ioutil.ReadFile(itemPath) |
| 39 | + if err != nil { |
| 40 | + return nil, errors.Wrap(err, "error reading source file") |
| 41 | + } |
| 42 | + |
| 43 | + return &Item{itemPath, source}, nil |
| 44 | +} |
| 45 | + |
| 46 | +// ItemByPath implements sort.Interface for []Item based on |
| 47 | +// lexicographic order of the path string. |
| 48 | +type ItemByPath []*Item |
| 49 | + |
| 50 | +func (ibn ItemByPath) Len() int { return len(ibn) } |
| 51 | +func (ibn ItemByPath) Swap(i, j int) { ibn[i], ibn[j] = ibn[j], ibn[i] } |
| 52 | +func (ibn ItemByPath) Less(i, j int) bool { return ibn[i].Path < ibn[j].Path } |
| 53 | + |
| 54 | +// Sketch holds all the files composing a sketch |
| 55 | +type Sketch struct { |
| 56 | + MainFile *Item |
| 57 | + OtherSketchFiles []*Item |
| 58 | + AdditionalFiles []*Item |
| 59 | +} |
| 60 | + |
| 61 | +// New creates an Sketch instance by reading all the files composing a sketch and grouping them |
| 62 | +// by file type. |
| 63 | +func New(sketchFolderPath, mainFilePath, buildPath string, allFilesPaths []string) (*Sketch, error) { |
| 64 | + var mainFile *Item |
| 65 | + |
| 66 | + // read all the sketch contents and create sketch Items |
| 67 | + pathToItem := make(map[string]*Item) |
| 68 | + for _, p := range allFilesPaths { |
| 69 | + // create an Item |
| 70 | + item, err := NewItem(p) |
| 71 | + if err != nil { |
| 72 | + return nil, errors.Wrap(err, "error creating the sketch") |
| 73 | + } |
| 74 | + |
| 75 | + if p == mainFilePath { |
| 76 | + // store the main sketch file |
| 77 | + mainFile = item |
| 78 | + } else { |
| 79 | + // map the file path to sketch.Item |
| 80 | + pathToItem[p] = item |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // organize the Items |
| 85 | + additionalFiles := []*Item{} |
| 86 | + otherSketchFiles := []*Item{} |
| 87 | + for p, item := range pathToItem { |
| 88 | + ext := strings.ToLower(filepath.Ext(p)) |
| 89 | + if _, found := globals.MainFileValidExtensions[ext]; found { |
| 90 | + // item is a valid main file, see if it's stored at the |
| 91 | + // sketch root and ignore if it's not. |
| 92 | + if filepath.Dir(p) == sketchFolderPath { |
| 93 | + otherSketchFiles = append(otherSketchFiles, item) |
| 94 | + } |
| 95 | + } else if _, found := globals.AdditionalFileValidExtensions[ext]; found { |
| 96 | + // item is a valid sketch file, grab it only if the buildPath is empty |
| 97 | + // or the file is within the buildPath |
| 98 | + if buildPath == "" || !strings.Contains(filepath.Dir(p), buildPath) { |
| 99 | + additionalFiles = append(additionalFiles, item) |
| 100 | + } |
| 101 | + } else { |
| 102 | + return nil, errors.Errorf("unknown sketch file extension '%s'", ext) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + sort.Sort(ItemByPath(additionalFiles)) |
| 107 | + sort.Sort(ItemByPath(otherSketchFiles)) |
| 108 | + |
| 109 | + return &Sketch{ |
| 110 | + MainFile: mainFile, |
| 111 | + OtherSketchFiles: otherSketchFiles, |
| 112 | + AdditionalFiles: additionalFiles, |
| 113 | + }, nil |
| 114 | +} |
0 commit comments