Skip to content

Commit e745d17

Browse files
authored
Merge pull request #56 from per1234/support-sketch-paths-outside-workspace
libraries/compile-examples: support sketch paths located outside the workspace
2 parents 32c143e + 5987f00 commit e745d17

File tree

3 files changed

+149
-99
lines changed

3 files changed

+149
-99
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ YAML-format list of platform dependencies to install.
2222

2323
Default `""`. If no `platforms` input is provided, the board's dependency will be automatically determined from the `fqbn` input and the latest version of that platform will be installed via Board Manager.
2424

25-
If a platform dependency from a non-Board Manager source of the same name as another Board Manager source platform dependency is defined, they will both be installed, with the non-Board Manager dependency overwriting the Board Manager platform installation. This permits testing against a non-release version of a platform while using Board Manager to install the platform's tools dependencies.
25+
If a platform dependency from a non-Board Manager source of the same name as another Board Manager source platform dependency is defined, they will both be installed, with the non-Board Manager dependency overwriting the Board Manager platform installation. This permits testing against a non-release version of a platform while using Board Manager to install the platform's tools dependencies.
2626
Example:
2727
```yaml
2828
platforms: |
@@ -68,14 +68,16 @@ YAML-format list of library dependencies to install.
6868

6969
Default `"- source-path: ./"`. This causes the repository to be installed as a library. If there are no library dependencies and you want to override the default, set the `libraries` input to an empty list (`- libraries: '-'`).
7070

71+
Libraries are installed under the Arduino user folder at `~/Arduino/libraries`.
72+
7173
Note: the original space-separated list format is also supported. When this syntax is used, the repository under test will always be installed as a library.
7274

7375
#### Sources:
7476

7577
##### Library Manager
7678

7779
Keys:
78-
- `name` - name of the library.
80+
- `name` - name of the library, as defined in the `name` field of its [library.properties](https://arduino.github.io/arduino-cli/library-specification/#libraryproperties-file-format) metadata file. The library will be installed to a folder matching the name, but with any spaces replaced by `_`.
7981
- `version` - version of the library to install. Default is the latest version.
8082

8183
##### Local path

compilesketches/compilesketches.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ def __init__(self, cli_version, fqbn_arg, platforms, libraries, sketch_paths, ve
111111

112112
# Save the space-separated list of paths as a Python list
113113
sketch_paths = parse_list_input(sketch_paths)
114-
sketch_paths = [pathlib.Path(sketch_path) for sketch_path in sketch_paths]
115-
self.sketch_paths = sketch_paths
114+
absolute_sketch_paths = [absolute_path(path=sketch_path) for sketch_path in sketch_paths]
115+
self.sketch_paths = absolute_sketch_paths
116116

117117
self.verbose = parse_boolean_input(boolean_input=verbose)
118118

@@ -383,10 +383,10 @@ def install_platforms_from_path(self, platform_list):
383383
"""
384384
for platform in platform_list:
385385
source_path = absolute_path(platform[self.dependency_source_path_key])
386-
self.verbose_print("Installing platform from path:", platform[self.dependency_source_path_key])
386+
self.verbose_print("Installing platform from path:", path_relative_to_workspace(path=source_path))
387387

388388
if not source_path.exists():
389-
print("::error::Platform source path:", platform[self.dependency_source_path_key], "doesn't exist")
389+
print("::error::Platform source path:", path_relative_to_workspace(path=source_path), "doesn't exist")
390390
sys.exit(1)
391391

392392
platform_installation_path = self.get_platform_installation_path(platform=platform)
@@ -583,7 +583,7 @@ def install_libraries(self):
583583

584584
# The original behavior of the action was to assume the root of the repo is a library to be installed, so
585585
# that behavior is retained when using the old input syntax
586-
library_list.path = [{self.dependency_source_path_key: pathlib.Path(os.environ["GITHUB_WORKSPACE"])}]
586+
library_list.path = [{self.dependency_source_path_key: os.environ["GITHUB_WORKSPACE"]}]
587587

588588
if len(library_list.manager) > 0:
589589
self.install_libraries_from_library_manager(library_list=library_list.manager)
@@ -626,7 +626,7 @@ def install_libraries_from_path(self, library_list):
626626
# If a name was specified, use it
627627
destination_name = library[self.dependency_destination_name_key]
628628
elif (
629-
source_path == pathlib.Path(os.environ["GITHUB_WORKSPACE"])
629+
source_path == absolute_path(os.environ["GITHUB_WORKSPACE"])
630630
):
631631
# If source_path is the root of the workspace (i.e., repository root), name the folder according to the
632632
# repository name, otherwise it will unexpectedly be "workspace"
@@ -700,36 +700,34 @@ def find_sketches(self):
700700
sketch_list = []
701701
self.verbose_print("Finding sketches under paths:", list_to_string(self.sketch_paths))
702702
for sketch_path in self.sketch_paths:
703-
# The absolute path is used in the code, the sketch path provided by the user is used in the log output
704-
absolute_sketch_path = absolute_path(sketch_path)
705703
sketch_path_sketch_list = []
706-
if not absolute_sketch_path.exists():
707-
print("::error::Sketch path:", sketch_path, "doesn't exist")
704+
if not sketch_path.exists():
705+
print("::error::Sketch path:", path_relative_to_workspace(path=sketch_path), "doesn't exist")
708706
sys.exit(1)
709707

710708
# Check if the specified path is a sketch (as opposed to containing sketches in subfolders)
711-
if absolute_sketch_path.is_file():
712-
if path_is_sketch(path=absolute_sketch_path):
709+
if sketch_path.is_file():
710+
if path_is_sketch(path=sketch_path):
713711
# The path directly to a sketch file was provided, so don't search recursively
714-
sketch_list.append(absolute_sketch_path.parent)
712+
sketch_list.append(sketch_path.parent)
715713
continue
716714
else:
717-
print("::error::Sketch path:", sketch_path, "is not a sketch")
715+
print("::error::Sketch path:", path_relative_to_workspace(path=sketch_path), "is not a sketch")
718716
sys.exit(1)
719717
else:
720718
# Path is a directory
721-
if path_is_sketch(path=absolute_sketch_path):
719+
if path_is_sketch(path=sketch_path):
722720
# Add sketch to list, but also search the path recursively for more sketches
723-
sketch_path_sketch_list.append(absolute_sketch_path)
721+
sketch_path_sketch_list.append(sketch_path)
724722

725723
# Search the sketch path recursively for sketches
726-
for sketch in sorted(absolute_sketch_path.rglob("*")):
724+
for sketch in sorted(sketch_path.rglob("*")):
727725
if sketch.is_dir() and path_is_sketch(path=sketch):
728726
sketch_path_sketch_list.append(sketch)
729727

730728
if len(sketch_path_sketch_list) == 0:
731729
# If a path provided via the sketch-paths input doesn't contain sketches, that indicates user error
732-
print("::error::No sketches were found in", sketch_path)
730+
print("::error::No sketches were found in", path_relative_to_workspace(path=sketch_path))
733731
sys.exit(1)
734732

735733
sketch_list.extend(sketch_path_sketch_list)
@@ -751,12 +749,12 @@ def compile_sketch(self, sketch_path):
751749
command=compilation_command, enable_output=self.RunCommandOutput.NONE, exit_on_failure=False)
752750
# Group compilation output to make the log easy to read
753751
# https://github.com/actions/toolkit/blob/master/docs/commands.md#group-and-ungroup-log-lines
754-
print("::group::Compiling sketch:", path_relative_to_workspace(sketch_path))
752+
print("::group::Compiling sketch:", path_relative_to_workspace(path=sketch_path))
755753
print(compilation_data.stdout)
756754
print("::endgroup::")
757755

758756
class CompilationResult:
759-
sketch = path_relative_to_workspace(sketch_path)
757+
sketch = sketch_path
760758
success = compilation_data.returncode == 0
761759
output = compilation_data.stdout
762760

@@ -795,7 +793,7 @@ def get_sketch_report(self, compilation_result):
795793

796794
# Compile the sketch again
797795
print("Compiling previous version of sketch to determine memory usage change")
798-
previous_compilation_result = self.compile_sketch(sketch_path=absolute_path(path=compilation_result.sketch))
796+
previous_compilation_result = self.compile_sketch(sketch_path=compilation_result.sketch)
799797

800798
# git checkout the PR's head ref to return the repository to its previous state
801799
repository.git.checkout(original_git_ref)
@@ -824,7 +822,7 @@ def get_sketch_report_from_output(self, compilation_result):
824822
ram_regex = r"Global variables use [0-9]+ bytes .*of dynamic memory"
825823

826824
# Set default report values
827-
sketch_report = {self.report_sketch_key: str(compilation_result.sketch),
825+
sketch_report = {self.report_sketch_key: str(path_relative_to_workspace(path=compilation_result.sketch)),
828826
self.report_compilation_success_key: compilation_result.success,
829827
self.report_flash_key: self.not_applicable_indicator,
830828
self.report_ram_key: self.not_applicable_indicator}
@@ -1047,8 +1045,14 @@ def path_relative_to_workspace(path):
10471045
Keyword arguments:
10481046
path -- the path to make relative
10491047
"""
1050-
path = pathlib.PurePath(path)
1051-
return path.relative_to(os.environ["GITHUB_WORKSPACE"])
1048+
path = absolute_path(path=path)
1049+
try:
1050+
relative_path = path.relative_to(absolute_path(path=os.environ["GITHUB_WORKSPACE"]))
1051+
except ValueError:
1052+
# Path is outside workspace, so just use the given path
1053+
relative_path = path
1054+
1055+
return relative_path
10521056

10531057

10541058
def absolute_path(path):
@@ -1058,11 +1062,15 @@ def absolute_path(path):
10581062
Keyword arguments:
10591063
path -- the path to make absolute
10601064
"""
1061-
path = pathlib.Path(path)
1065+
# Make path into a pathlib.Path object, with ~ expanded
1066+
path = pathlib.Path(path).expanduser()
10621067
if not path.is_absolute():
10631068
# path is relative
10641069
path = pathlib.Path(os.environ["GITHUB_WORKSPACE"], path)
10651070

1071+
# Resolve .. and symlinks to get a true absolute path
1072+
path = path.resolve()
1073+
10661074
return path
10671075

10681076

0 commit comments

Comments
 (0)