|
16 | 16 | package commands
|
17 | 17 |
|
18 | 18 | import (
|
19 |
| - "archive/zip" |
20 | 19 | "context"
|
21 | 20 | "errors"
|
22 | 21 | "fmt"
|
23 |
| - "io" |
24 | 22 | "io/ioutil"
|
25 | 23 | "net/url"
|
26 |
| - "os" |
27 | 24 | "path"
|
28 |
| - "path/filepath" |
29 |
| - "strings" |
30 | 25 |
|
31 | 26 | "github.com/arduino/arduino-cli/arduino/builder"
|
32 | 27 | "github.com/arduino/arduino-cli/arduino/cores"
|
@@ -707,151 +702,3 @@ func LoadSketch(ctx context.Context, req *rpc.LoadSketchReq) (*rpc.LoadSketchRes
|
707 | 702 | AdditionalFiles: additionalFiles,
|
708 | 703 | }, nil
|
709 | 704 | }
|
710 |
| - |
711 |
| -// ArchiveSketch FIXMEDOC |
712 |
| -func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchReq) (*rpc.ArchiveSketchResp, error) { |
713 |
| - // sketchName is the name of the sketch without extension, for example "MySketch" |
714 |
| - var sketchName string |
715 |
| - |
716 |
| - sketchPath := paths.New(req.SketchPath) |
717 |
| - if sketchPath == nil { |
718 |
| - sketchPath = paths.New(".") |
719 |
| - } |
720 |
| - |
721 |
| - sketchPath, err := sketchPath.Clean().Abs() |
722 |
| - if err != nil { |
723 |
| - return nil, fmt.Errorf("Error getting absolute sketch path %v", err) |
724 |
| - } |
725 |
| - |
726 |
| - // Get the sketch name and make sketchPath point to the ino file |
727 |
| - if sketchPath.IsDir() { |
728 |
| - sketchName = sketchPath.Base() |
729 |
| - sketchPath = sketchPath.Join(sketchName + ".ino") |
730 |
| - } else if sketchPath.Ext() == ".ino" { |
731 |
| - sketchName = strings.TrimSuffix(sketchPath.Base(), ".ino") |
732 |
| - } |
733 |
| - |
734 |
| - // Checks if it's really a sketch |
735 |
| - if sketchPath.NotExist() { |
736 |
| - return nil, fmt.Errorf("specified path is not a sketch: %v", sketchPath.String()) |
737 |
| - } |
738 |
| - |
739 |
| - archivePath := paths.New(req.ArchivePath) |
740 |
| - if archivePath == nil { |
741 |
| - archivePath = sketchPath.Parent().Parent() |
742 |
| - } |
743 |
| - |
744 |
| - archivePath, err = archivePath.Clean().Abs() |
745 |
| - if err != nil { |
746 |
| - return nil, fmt.Errorf("Error getting absolute archive path %v", err) |
747 |
| - } |
748 |
| - |
749 |
| - // Makes archivePath point to a zip file |
750 |
| - if archivePath.IsDir() { |
751 |
| - archivePath = archivePath.Join(sketchName + ".zip") |
752 |
| - } else if archivePath.Ext() == "" { |
753 |
| - archivePath = paths.New(archivePath.String() + ".zip") |
754 |
| - } |
755 |
| - |
756 |
| - if archivePath.Exist() { |
757 |
| - return nil, fmt.Errorf("archive already exists") |
758 |
| - } |
759 |
| - |
760 |
| - archive, err := os.Create(archivePath.Clean().String()) |
761 |
| - if err != nil { |
762 |
| - return nil, fmt.Errorf("Error creating archive: %v", err) |
763 |
| - } |
764 |
| - defer archive.Close() |
765 |
| - |
766 |
| - zipWriter := zip.NewWriter(archive) |
767 |
| - defer zipWriter.Close() |
768 |
| - |
769 |
| - filesToZip, err := getSketchContent(sketchPath.Parent()) |
770 |
| - if err != nil { |
771 |
| - return nil, fmt.Errorf("Error retrieving sketch files: %v", err) |
772 |
| - } |
773 |
| - |
774 |
| - for _, f := range filesToZip { |
775 |
| - |
776 |
| - if !req.IncludeBuildDir { |
777 |
| - filePath, err := sketchPath.Parent().Parent().RelTo(f) |
778 |
| - if err != nil { |
779 |
| - return nil, fmt.Errorf("Error calculating relative file path: %v", err) |
780 |
| - } |
781 |
| - |
782 |
| - // Skips build folder |
783 |
| - if strings.HasPrefix(filePath.String(), sketchName+string(filepath.Separator)+"build") { |
784 |
| - continue |
785 |
| - } |
786 |
| - } |
787 |
| - |
788 |
| - // We get the parent path since we want the archive to unpack as a folder. |
789 |
| - // If we don't do this the archive would contain all the sketch files as top level. |
790 |
| - err = addFileToSketchArchive(zipWriter, f, sketchPath.Parent().Parent()) |
791 |
| - if err != nil { |
792 |
| - return nil, fmt.Errorf("Error adding file to archive: %v", err) |
793 |
| - } |
794 |
| - } |
795 |
| - |
796 |
| - return &rpc.ArchiveSketchResp{}, nil |
797 |
| -} |
798 |
| - |
799 |
| -// Recursively retrieves all files in the sketch folder |
800 |
| -func getSketchContent(sketchFolder *paths.Path) (paths.PathList, error) { |
801 |
| - sketchFiles, err := sketchFolder.ReadDir() |
802 |
| - if err != nil { |
803 |
| - return nil, err |
804 |
| - } |
805 |
| - for _, f := range sketchFiles { |
806 |
| - if f.IsDir() { |
807 |
| - files, err := getSketchContent(f) |
808 |
| - if err != nil { |
809 |
| - return nil, err |
810 |
| - } |
811 |
| - |
812 |
| - sketchFiles = append(sketchFiles, files...) |
813 |
| - } |
814 |
| - } |
815 |
| - finalFiles := paths.PathList{} |
816 |
| - for _, f := range sketchFiles { |
817 |
| - if f.IsNotDir() { |
818 |
| - finalFiles = append(finalFiles, f) |
819 |
| - } |
820 |
| - } |
821 |
| - return finalFiles, nil |
822 |
| -} |
823 |
| - |
824 |
| -// Adds a single file to an existing zip file |
825 |
| -func addFileToSketchArchive(zipWriter *zip.Writer, filePath, sketchPath *paths.Path) error { |
826 |
| - f, err := filePath.Open() |
827 |
| - if err != nil { |
828 |
| - return err |
829 |
| - } |
830 |
| - defer f.Close() |
831 |
| - |
832 |
| - info, err := f.Stat() |
833 |
| - if err != nil { |
834 |
| - return err |
835 |
| - } |
836 |
| - |
837 |
| - header, err := zip.FileInfoHeader(info) |
838 |
| - if err != nil { |
839 |
| - return err |
840 |
| - } |
841 |
| - |
842 |
| - filePath, err = sketchPath.RelTo(filePath) |
843 |
| - if err != nil { |
844 |
| - return err |
845 |
| - } |
846 |
| - |
847 |
| - header.Name = filePath.String() |
848 |
| - header.Method = zip.Deflate |
849 |
| - |
850 |
| - writer, err := zipWriter.CreateHeader(header) |
851 |
| - if err != nil { |
852 |
| - return err |
853 |
| - } |
854 |
| - |
855 |
| - _, err = io.Copy(writer, f) |
856 |
| - return err |
857 |
| -} |
0 commit comments