Skip to content

Commit 7771404

Browse files
committed
Change archive command to sketch archive and other
1 parent 5dbeb05 commit 7771404

File tree

12 files changed

+915
-1393
lines changed

12 files changed

+915
-1393
lines changed

Diff for: cli/cli.go

-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"os"
2222
"strings"
2323

24-
"github.com/arduino/arduino-cli/cli/archive"
2524
"github.com/arduino/arduino-cli/cli/board"
2625
"github.com/arduino/arduino-cli/cli/burnbootloader"
2726
"github.com/arduino/arduino-cli/cli/cache"
@@ -80,7 +79,6 @@ func NewCommand() *cobra.Command {
8079

8180
// this is here only for testing
8281
func createCliCommandTree(cmd *cobra.Command) {
83-
cmd.AddCommand(archive.NewCommand())
8482
cmd.AddCommand(board.NewCommand())
8583
cmd.AddCommand(cache.NewCommand())
8684
cmd.AddCommand(compile.NewCommand())

Diff for: cli/archive/archive.go renamed to cli/sketch/archive.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package archive
16+
package sketch
1717

1818
import (
1919
"context"
2020
"os"
2121

2222
"github.com/arduino/arduino-cli/cli/errorcodes"
2323
"github.com/arduino/arduino-cli/cli/feedback"
24-
"github.com/arduino/arduino-cli/commands"
24+
"github.com/arduino/arduino-cli/commands/sketch"
2525
rpc "github.com/arduino/arduino-cli/rpc/commands"
2626
"github.com/sirupsen/logrus"
2727
"github.com/spf13/cobra"
2828
)
2929

3030
var includeBuildDir bool
3131

32-
// NewCommand creates a new `archive` command
33-
func NewCommand() *cobra.Command {
32+
// initArchiveCommand creates a new `archive` command
33+
func initArchiveCommand() *cobra.Command {
3434
command := &cobra.Command{
3535
Use: "archive <sketchPath> <archivePath>",
3636
Short: "Creates a zip file containing all sketch files.",
@@ -51,7 +51,7 @@ func NewCommand() *cobra.Command {
5151
}
5252

5353
func runArchiveCommand(cmd *cobra.Command, args []string) {
54-
logrus.Info("Executing `arduino archive`")
54+
logrus.Info("Executing `arduino sketch archive`")
5555

5656
sketchPath := ""
5757
if len(args) >= 1 {
@@ -63,7 +63,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) {
6363
archivePath = args[1]
6464
}
6565

66-
_, err := commands.ArchiveSketch(context.Background(),
66+
_, err := sketch.ArchiveSketch(context.Background(),
6767
&rpc.ArchiveSketchReq{
6868
SketchPath: sketchPath,
6969
ArchivePath: archivePath,

Diff for: cli/sketch/sketch.go

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func NewCommand() *cobra.Command {
3131
}
3232

3333
cmd.AddCommand(initNewCommand())
34+
cmd.AddCommand(initArchiveCommand())
3435

3536
return cmd
3637
}

Diff for: commands/daemon/daemon.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/arduino/arduino-cli/commands/compile"
2727
"github.com/arduino/arduino-cli/commands/core"
2828
"github.com/arduino/arduino-cli/commands/lib"
29+
"github.com/arduino/arduino-cli/commands/sketch"
2930
"github.com/arduino/arduino-cli/commands/upload"
3031
rpc "github.com/arduino/arduino-cli/rpc/commands"
3132
)
@@ -340,5 +341,5 @@ func (s *ArduinoCoreServerImpl) LibraryList(ctx context.Context, req *rpc.Librar
340341

341342
// ArchiveSketch FIXMEDOC
342343
func (s *ArduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchReq) (*rpc.ArchiveSketchResp, error) {
343-
return commands.ArchiveSketch(ctx, req)
344+
return sketch.ArchiveSketch(ctx, req)
344345
}

Diff for: commands/instances.go

-153
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@
1616
package commands
1717

1818
import (
19-
"archive/zip"
2019
"context"
2120
"errors"
2221
"fmt"
23-
"io"
2422
"io/ioutil"
2523
"net/url"
26-
"os"
2724
"path"
28-
"path/filepath"
29-
"strings"
3025

3126
"github.com/arduino/arduino-cli/arduino/builder"
3227
"github.com/arduino/arduino-cli/arduino/cores"
@@ -707,151 +702,3 @@ func LoadSketch(ctx context.Context, req *rpc.LoadSketchReq) (*rpc.LoadSketchRes
707702
AdditionalFiles: additionalFiles,
708703
}, nil
709704
}
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

Comments
 (0)