Skip to content

Commit d03a4bd

Browse files
committed
Archive command now works with sketches containing .pde files
1 parent b677d7c commit d03a4bd

File tree

6 files changed

+77
-12
lines changed

6 files changed

+77
-12
lines changed

cli/sketch/archive.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import (
1919
"context"
2020
"os"
2121

22+
"github.com/arduino/arduino-cli/arduino/sketches"
2223
"github.com/arduino/arduino-cli/cli/errorcodes"
2324
"github.com/arduino/arduino-cli/cli/feedback"
2425
"github.com/arduino/arduino-cli/commands/sketch"
2526
rpc "github.com/arduino/arduino-cli/rpc/commands"
27+
"github.com/arduino/go-paths-helper"
2628
"github.com/sirupsen/logrus"
2729
"github.com/spf13/cobra"
2830
)
@@ -53,11 +55,19 @@ func initArchiveCommand() *cobra.Command {
5355
func runArchiveCommand(cmd *cobra.Command, args []string) {
5456
logrus.Info("Executing `arduino sketch archive`")
5557

56-
sketchPath := ""
58+
sketchPath := "."
5759
if len(args) >= 1 {
5860
sketchPath = args[0]
5961
}
6062

63+
// .pde files are still supported but deprecated, this warning urges the user to rename them
64+
if files := sketches.CheckForPdeFiles(paths.New(sketchPath)); len(files) > 0 {
65+
feedback.Error("Sketches with .pde extension are deprecated, please rename the following files to .ino:")
66+
for _, f := range files {
67+
feedback.Error(f)
68+
}
69+
}
70+
6171
archivePath := ""
6272
if len(args) == 2 {
6373
archivePath = args[1]

commands/sketch/archive.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import (
2323
"path/filepath"
2424
"strings"
2525

26+
"github.com/arduino/arduino-cli/arduino/globals"
2627
rpc "github.com/arduino/arduino-cli/rpc/commands"
2728
paths "github.com/arduino/go-paths-helper"
29+
"github.com/pkg/errors"
2830
)
2931

3032
// ArchiveSketch FIXMEDOC
@@ -42,22 +44,38 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchReq) (*rpc.Archive
4244
return nil, fmt.Errorf("Error getting absolute sketch path %v", err)
4345
}
4446

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")
47+
// Make sketchPath point to the sketch folder
48+
if sketchPath.IsNotDir() {
49+
sketchPath = sketchPath.Parent()
50+
}
51+
52+
sketchName = sketchPath.Base()
53+
54+
// sketchMainFile is the path to the sketch main file
55+
var sketchMainFile *paths.Path
56+
57+
for ext := range globals.MainFileValidExtensions {
58+
candidateSketchMainFile := sketchPath.Join(sketchPath.Base() + ext)
59+
if candidateSketchMainFile.Exist() {
60+
if sketchMainFile == nil {
61+
sketchMainFile = candidateSketchMainFile
62+
} else {
63+
return nil, errors.Errorf("multiple main sketch files found (%v, %v)",
64+
sketchMainFile,
65+
candidateSketchMainFile,
66+
)
67+
}
68+
}
5169
}
5270

5371
// Checks if it's really a sketch
54-
if sketchPath.NotExist() {
72+
if sketchMainFile == nil {
5573
return nil, fmt.Errorf("specified path is not a sketch: %v", sketchPath.String())
5674
}
5775

5876
archivePath := paths.New(req.ArchivePath)
5977
if archivePath == nil {
60-
archivePath = sketchPath.Parent().Parent()
78+
archivePath = sketchPath.Parent()
6179
}
6280

6381
archivePath, err = archivePath.Clean().Abs()
@@ -76,7 +94,7 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchReq) (*rpc.Archive
7694
return nil, fmt.Errorf("archive already exists")
7795
}
7896

79-
filesToZip, err := sketchPath.Parent().ReadDirRecursive()
97+
filesToZip, err := sketchPath.ReadDirRecursive()
8098
if err != nil {
8199
return nil, fmt.Errorf("Error retrieving sketch files: %v", err)
82100
}
@@ -94,7 +112,7 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchReq) (*rpc.Archive
94112
for _, f := range filesToZip {
95113

96114
if !req.IncludeBuildDir {
97-
filePath, err := sketchPath.Parent().Parent().RelTo(f)
115+
filePath, err := sketchPath.Parent().RelTo(f)
98116
if err != nil {
99117
return nil, fmt.Errorf("Error calculating relative file path: %v", err)
100118
}
@@ -107,7 +125,7 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchReq) (*rpc.Archive
107125

108126
// We get the parent path since we want the archive to unpack as a folder.
109127
// 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())
128+
err = addFileToSketchArchive(zipWriter, f, sketchPath.Parent())
111129
if err != nil {
112130
return nil, fmt.Errorf("Error adding file to archive: %v", err)
113131
}

test/test_sketch.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,3 +818,31 @@ def test_sketch_archive_absolute_sketch_path_with_absolute_zip_path_and_name_wit
818818
verify_zip_contains_sketch_including_build_dir(archive_files)
819819

820820
archive.close()
821+
822+
823+
def test_sketch_archive_with_pde_main_file(run_command, copy_sketch, working_dir):
824+
sketch_name = "sketch_pde_main_file"
825+
sketch_dir = copy_sketch(sketch_name)
826+
sketch_file = Path(sketch_dir, f"{sketch_name}.pde")
827+
res = run_command("sketch archive", sketch_dir)
828+
assert res.ok
829+
assert "Sketches with .pde extension are deprecated, please rename the following files to .ino" in res.stderr
830+
assert str(sketch_file.relative_to(sketch_dir)) in res.stderr
831+
832+
archive = zipfile.ZipFile(f"{working_dir}/{sketch_name}.zip")
833+
archive_files = archive.namelist()
834+
835+
assert f"{sketch_name}/{sketch_name}.pde" in archive_files
836+
837+
archive.close()
838+
839+
840+
def test_sketch_archive_with_multiple_main_files(run_command, copy_sketch, working_dir):
841+
sketch_name = "sketch_multiple_main_files"
842+
sketch_dir = copy_sketch(sketch_name)
843+
sketch_file = Path(sketch_dir, f"{sketch_name}.pde")
844+
res = run_command("sketch archive", sketch_dir)
845+
assert res.failed
846+
assert "Sketches with .pde extension are deprecated, please rename the following files to .ino" in res.stderr
847+
assert str(sketch_file.relative_to(sketch_dir)) in res.stderr
848+
assert "Error archiving: multiple main sketch files found" in res.stderr
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void setup() { }
2+
3+
void loop() { }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void setup() { }
2+
3+
void loop() { }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void setup() { }
2+
3+
void loop() { }

0 commit comments

Comments
 (0)