|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 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 |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package sketch |
| 17 | + |
| 18 | +import ( |
| 19 | + "archive/zip" |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + |
| 26 | + rpc "github.com/arduino/arduino-cli/rpc/commands" |
| 27 | + paths "github.com/arduino/go-paths-helper" |
| 28 | +) |
| 29 | + |
| 30 | +// ArchiveSketch FIXMEDOC |
| 31 | +func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchReq) (*rpc.ArchiveSketchResp, error) { |
| 32 | + // sketchName is the name of the sketch without extension, for example "MySketch" |
| 33 | + var sketchName string |
| 34 | + |
| 35 | + sketchPath := paths.New(req.SketchPath) |
| 36 | + if sketchPath == nil { |
| 37 | + sketchPath = paths.New(".") |
| 38 | + } |
| 39 | + |
| 40 | + sketchPath, err := sketchPath.Clean().Abs() |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("Error getting absolute sketch path %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + // Get the sketch name and make sketchPath point to the ino file |
| 46 | + if sketchPath.IsDir() { |
| 47 | + sketchName = sketchPath.Base() |
| 48 | + sketchPath = sketchPath.Join(sketchName + ".ino") |
| 49 | + } else if sketchPath.Ext() == ".ino" { |
| 50 | + sketchName = strings.TrimSuffix(sketchPath.Base(), ".ino") |
| 51 | + } |
| 52 | + |
| 53 | + // Checks if it's really a sketch |
| 54 | + if sketchPath.NotExist() { |
| 55 | + return nil, fmt.Errorf("specified path is not a sketch: %v", sketchPath.String()) |
| 56 | + } |
| 57 | + |
| 58 | + archivePath := paths.New(req.ArchivePath) |
| 59 | + if archivePath == nil { |
| 60 | + archivePath = sketchPath.Parent().Parent() |
| 61 | + } |
| 62 | + |
| 63 | + archivePath, err = archivePath.Clean().Abs() |
| 64 | + if err != nil { |
| 65 | + return nil, fmt.Errorf("Error getting absolute archive path %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + // Makes archivePath point to a zip file |
| 69 | + if archivePath.IsDir() { |
| 70 | + archivePath = archivePath.Join(sketchName + ".zip") |
| 71 | + } else if archivePath.Ext() == "" { |
| 72 | + archivePath = paths.New(archivePath.String() + ".zip") |
| 73 | + } |
| 74 | + |
| 75 | + if archivePath.Exist() { |
| 76 | + return nil, fmt.Errorf("archive already exists") |
| 77 | + } |
| 78 | + |
| 79 | + filesToZip, err := sketchPath.Parent().ReadDirRecursive() |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("Error retrieving sketch files: %v", err) |
| 82 | + } |
| 83 | + filesToZip.FilterOutDirs() |
| 84 | + |
| 85 | + archive, err := archivePath.Create() |
| 86 | + if err != nil { |
| 87 | + return nil, fmt.Errorf("Error creating archive: %v", err) |
| 88 | + } |
| 89 | + defer archive.Close() |
| 90 | + |
| 91 | + zipWriter := zip.NewWriter(archive) |
| 92 | + defer zipWriter.Close() |
| 93 | + |
| 94 | + for _, f := range filesToZip { |
| 95 | + |
| 96 | + if !req.IncludeBuildDir { |
| 97 | + filePath, err := sketchPath.Parent().Parent().RelTo(f) |
| 98 | + if err != nil { |
| 99 | + return nil, fmt.Errorf("Error calculating relative file path: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + // Skips build folder |
| 103 | + if strings.HasPrefix(filePath.String(), sketchName+string(filepath.Separator)+"build") { |
| 104 | + continue |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // We get the parent path since we want the archive to unpack as a folder. |
| 109 | + // If we don't do this the archive would contain all the sketch files as top level. |
| 110 | + err = addFileToSketchArchive(zipWriter, f, sketchPath.Parent().Parent()) |
| 111 | + if err != nil { |
| 112 | + return nil, fmt.Errorf("Error adding file to archive: %v", err) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return &rpc.ArchiveSketchResp{}, nil |
| 117 | +} |
| 118 | + |
| 119 | +// Adds a single file to an existing zip file |
| 120 | +func addFileToSketchArchive(zipWriter *zip.Writer, filePath, sketchPath *paths.Path) error { |
| 121 | + f, err := filePath.Open() |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + defer f.Close() |
| 126 | + |
| 127 | + info, err := f.Stat() |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + header, err := zip.FileInfoHeader(info) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + filePath, err = sketchPath.RelTo(filePath) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + header.Name = filePath.String() |
| 143 | + header.Method = zip.Deflate |
| 144 | + |
| 145 | + writer, err := zipWriter.CreateHeader(header) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + _, err = io.Copy(writer, f) |
| 151 | + return err |
| 152 | +} |
0 commit comments