Skip to content

Commit 41fc8f9

Browse files
authored
Merge pull request #82 from arduino/per1234/fix-subproject-discovery
Fix subproject discovery
2 parents 9d80c17 + ba5c3cb commit 41fc8f9

File tree

15 files changed

+321
-13
lines changed

15 files changed

+321
-13
lines changed

configuration/configuration.go

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
8383

8484
versionMode, _ = flags.GetBool("version")
8585

86+
targetPaths = nil
8687
if len(projectPaths) == 0 {
8788
// Default to using current working directory.
8889
workingDirectoryPath, err := os.Getwd()

configuration/configuration_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,14 @@ func TestInitializeVersion(t *testing.T) {
184184
}
185185

186186
func TestInitializeProjectPath(t *testing.T) {
187-
targetPaths = nil
188187
assert.Nil(t, Initialize(test.ConfigurationFlags(), []string{}))
189188
workingDirectoryPath, err := os.Getwd()
190189
require.Nil(t, err)
191190
assert.Equal(t, paths.NewPathList(workingDirectoryPath), TargetPaths(), "Default PROJECT_PATH to current working directory")
192191

193-
targetPaths = nil
194192
assert.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths))
195193
assert.Equal(t, paths.NewPathList(projectPaths[0]), TargetPaths())
196194

197-
targetPaths = nil
198195
assert.Error(t, Initialize(test.ConfigurationFlags(), []string{"/nonexistent"}))
199196
}
200197

project/project.go

+22-10
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ func findProjects(targetPath *paths.Path) ([]Type, error) {
7575
return nil, fmt.Errorf("specified path %s is not an Arduino project", targetPath)
7676
}
7777

78-
foundProjects = append(foundProjects, findProjectsUnderPath(targetPath, configuration.SuperprojectTypeFilter(), configuration.Recursive())...)
78+
foundParentProjects := findProjectsUnderPath(targetPath, configuration.SuperprojectTypeFilter(), configuration.Recursive())
79+
for _, foundParentProject := range foundParentProjects {
80+
foundProjects = append(foundProjects, foundParentProject)
81+
foundProjects = append(foundProjects, findSubprojects(foundParentProject, foundParentProject.ProjectType)...)
82+
}
7983

8084
if foundProjects == nil {
8185
return nil, fmt.Errorf("no projects found under %s", targetPath)
@@ -84,7 +88,7 @@ func findProjects(targetPath *paths.Path) ([]Type, error) {
8488
return foundProjects, nil
8589
}
8690

87-
// findProjectsUnderPath finds projects of the given type and subprojects of those projects. It returns a slice containing the definitions of all found projects.
91+
// findProjectsUnderPath finds projects of the given type under the given path. It returns a slice containing the definitions of all found projects.
8892
func findProjectsUnderPath(targetPath *paths.Path, projectTypeFilter projecttype.Type, recursive bool) []Type {
8993
var foundProjects []Type
9094

@@ -99,8 +103,6 @@ func findProjectsUnderPath(targetPath *paths.Path, projectTypeFilter projecttype
99103
}
100104
foundProjects = append(foundProjects, foundProject)
101105

102-
foundProjects = append(foundProjects, findSubprojects(foundProject, foundProject.ProjectType)...)
103-
104106
// Don't search recursively past a project.
105107
return foundProjects
106108
}
@@ -120,7 +122,7 @@ func findProjectsUnderPath(targetPath *paths.Path, projectTypeFilter projecttype
120122
// findSubprojects finds subprojects of the given project.
121123
// For example, the subprojects of a library are its example sketches.
122124
func findSubprojects(superproject Type, apexSuperprojectType projecttype.Type) []Type {
123-
subprojectFolderNames := []string{}
125+
subprojectsFolderNames := []string{}
124126
var subProjectType projecttype.Type
125127
var searchPathsRecursively bool
126128

@@ -130,11 +132,11 @@ func findSubprojects(superproject Type, apexSuperprojectType projecttype.Type) [
130132
// Sketches don't have subprojects
131133
return nil
132134
case projecttype.Library:
133-
subprojectFolderNames = append(subprojectFolderNames, library.ExamplesFolderSupportedNames()...)
135+
subprojectsFolderNames = append(subprojectsFolderNames, library.ExamplesFolderSupportedNames()...)
134136
subProjectType = projecttype.Sketch
135137
searchPathsRecursively = true // Examples sketches can be under nested subfolders
136138
case projecttype.Platform:
137-
subprojectFolderNames = append(subprojectFolderNames, platform.BundledLibrariesFolderNames()...)
139+
subprojectsFolderNames = append(subprojectsFolderNames, platform.BundledLibrariesFolderNames()...)
138140
subProjectType = projecttype.Library
139141
searchPathsRecursively = false // Bundled libraries must be in the root of the libraries folder
140142
case projecttype.PackageIndex:
@@ -146,9 +148,19 @@ func findSubprojects(superproject Type, apexSuperprojectType projecttype.Type) [
146148

147149
// Search the subproject paths for projects.
148150
var immediateSubprojects []Type
149-
for _, subprojectFolderName := range subprojectFolderNames {
150-
subprojectPath := superproject.Path.Join(subprojectFolderName)
151-
immediateSubprojects = append(immediateSubprojects, findProjectsUnderPath(subprojectPath, subProjectType, searchPathsRecursively)...)
151+
for _, subprojectsFolderName := range subprojectsFolderNames {
152+
subprojectsPath := superproject.Path.Join(subprojectsFolderName)
153+
if subprojectsPath.Exist() {
154+
directoryListing, err := subprojectsPath.ReadDir()
155+
if err != nil {
156+
panic(err)
157+
}
158+
directoryListing.FilterDirs()
159+
160+
for _, subprojectPath := range directoryListing {
161+
immediateSubprojects = append(immediateSubprojects, findProjectsUnderPath(subprojectPath, subProjectType, searchPathsRecursively)...)
162+
}
163+
}
152164
}
153165

154166
var allSubprojects []Type

0 commit comments

Comments
 (0)