Skip to content

Commit 7c4a594

Browse files
xcode & xamarin: Auto scan directory for project files (#95)
* feat xcode: scan the directory for project files * typo * moved the scanForProjectFiles() to cmd/utils.go * project dir scan for the project files like in the scanner * clean * deps: scanner/xamarin & scanner/ios * xamarin option for auto scan * warning message added about the --file flag usage * PR: IDEType enum renamed to ProjectType; and changed from String to Int; * PR: fix some log typo: solution => workspace / project * ref: scanXamarinProject: move the "search for the soulition file" block in to a separated func; * ref: scanXcodeProject: move the "search for the project file" block in to a separated func; * dep ensure * PR clean * PR: make ProjectType private
1 parent b2286a9 commit 7c4a594

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+19777
-17
lines changed

Gopkg.lock

Lines changed: 58 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
name = "github.com/stretchr/testify"
3232
branch = "master"
3333

34+
[[constraint]]
35+
name = "github.com/bitrise-core/bitrise-init"
36+
branch = "master"
37+
3438
[prune]
3539
go-tests = true
3640
unused-packages = true

cmd/utils.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/bitrise-core/bitrise-init/scanners/ios"
8+
"github.com/bitrise-core/bitrise-init/scanners/xamarin"
9+
"github.com/bitrise-core/bitrise-init/utility"
10+
)
11+
12+
// projectType enum.
13+
// Could be iOSProjectType = 0
14+
// Or xamarinProjectType = 1
15+
type projectType int
16+
17+
const (
18+
iOSProjectType projectType = iota
19+
xamarinProjectType
20+
)
21+
22+
// Scans the root dir for the provided project files
23+
func scanForProjectFiles(projType projectType) ([]string, error) {
24+
searchDir, err := os.Getwd()
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
fileList, err := utility.ListPathInDirSortedByComponents(searchDir, false)
30+
if err != nil {
31+
return nil, fmt.Errorf("failed to search for files in (%s), error: %s", searchDir, err)
32+
}
33+
34+
var paths []string
35+
{
36+
if projType == iOSProjectType {
37+
paths, err = ios.FilterRelevantWorkspaceFiles(fileList)
38+
if err != nil {
39+
return nil, fmt.Errorf("failed to search for workspace files, error: %s", err)
40+
}
41+
42+
if len(paths) == 0 {
43+
paths, err = ios.FilterRelevantProjectFiles(fileList)
44+
if err != nil {
45+
return nil, fmt.Errorf("failed to search for project files, error: %s", err)
46+
}
47+
}
48+
} else if projType == xamarinProjectType {
49+
paths, err = xamarin.FilterSolutionFiles(fileList)
50+
if err != nil {
51+
return nil, fmt.Errorf("failed to search for solution files, error: %s", err)
52+
}
53+
}
54+
55+
}
56+
57+
if len(paths) == 0 {
58+
return nil, fmt.Errorf("no project file found: %s", searchDir)
59+
60+
}
61+
return paths, nil
62+
}

cmd/xamarin.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"encoding/json"
55
"fmt"
6+
"path"
67
"path/filepath"
78
"sort"
89

@@ -75,6 +76,38 @@ func archivableSolutionConfigNames(projectsByID map[string]project.Model) []stri
7576
return archivableSolutionConfigNames
7677
}
7778

79+
// findSolution scans the directory for Xamarin.Solution file first
80+
// If can't find any, ask the user to drag-and-drop the file
81+
func findSolution() (string, error) {
82+
var solutionPth string
83+
solPaths, err := scanForProjectFiles(xamarinProjectType)
84+
if err != nil {
85+
log.Printf("Failed: %s", err)
86+
fmt.Println()
87+
88+
log.Infof("Provide the solution file manually")
89+
askText := `Please drag-and-drop your Xamarin Solution (` + colorstring.Green(".sln") + `) file,
90+
and then hit Enter`
91+
solutionPth, err = goinp.AskForPath(askText)
92+
if err != nil {
93+
return "", fmt.Errorf("failed to read input: %s", err)
94+
}
95+
} else {
96+
if len(solPaths) == 1 {
97+
log.Printf("Found one solution file: %s.", path.Base(solPaths[0]))
98+
solutionPth = solPaths[0]
99+
} else {
100+
log.Printf("Found multiple solution file: %s.", path.Base(solutionPth))
101+
solutionPth, err = goinp.SelectFromStringsWithDefault("Select the solution file you want to scan", 1, solPaths)
102+
if err != nil {
103+
return "", fmt.Errorf("failed to select solution file: %s", err)
104+
}
105+
}
106+
}
107+
108+
return solutionPth, nil
109+
}
110+
78111
func scanXamarinProject(cmd *cobra.Command, args []string) error {
79112
absExportOutputDirPath, err := initExportOutputDir()
80113
if err != nil {
@@ -87,14 +120,17 @@ func scanXamarinProject(cmd *cobra.Command, args []string) error {
87120
// Xamarin Solution Path
88121
xamarinCmd.SolutionFilePath = paramXamarinSolutionFilePath
89122
if xamarinCmd.SolutionFilePath == "" {
90-
askText := `Please drag-and-drop your Xamarin Solution (` + colorstring.Green(".sln") + `) file,
91-
and then hit Enter`
92123
fmt.Println()
93-
projpth, err := goinp.AskForPath(askText)
124+
log.Infof("Scan the directory for solution files")
125+
log.Warnf("You can specify the Xamarin Solution file to scan with the --file flag.")
126+
127+
//
128+
// Scan the directory for Xamarin.Solution file first
129+
// If can't find any, ask the user to drag-and-drop the file
130+
xamarinCmd.SolutionFilePath, err = findSolution()
94131
if err != nil {
95-
return fmt.Errorf("failed to read input: %s", err)
132+
return err
96133
}
97-
xamarinCmd.SolutionFilePath = projpth
98134
}
99135
log.Debugf("xamSolutionPth: %s", xamarinCmd.SolutionFilePath)
100136

cmd/xcode.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"path"
67
"path/filepath"
78
"strings"
89

@@ -61,6 +62,41 @@ func initExportOutputDir() (string, error) {
6162
return absExportOutputDirPath, nil
6263
}
6364

65+
// findProject scans the directory for Xcode Project (.xcworkspace / .xcodeproject) file first
66+
// If can't find any, ask the user to drag-and-drop the file
67+
func findProject() (string, error) {
68+
var projpth string
69+
70+
projPaths, err := scanForProjectFiles(iOSProjectType)
71+
if err != nil {
72+
log.Printf("Failed: %s", err)
73+
fmt.Println()
74+
75+
log.Infof("Provide the project file manually")
76+
askText := `Please drag-and-drop your Xcode Project (` + colorstring.Green(".xcodeproj") + `) or Workspace (` + colorstring.Green(".xcworkspace") + `) file,
77+
the one you usually open in Xcode, then hit Enter.
78+
(Note: if you have a Workspace file you should most likely use that)`
79+
projpth, err = goinp.AskForPath(askText)
80+
if err != nil {
81+
return "", fmt.Errorf("failed to read input: %s", err)
82+
}
83+
return projpth, err
84+
}
85+
86+
if len(projPaths) == 1 {
87+
log.Printf("Found one project file: %s.", path.Base(projPaths[0]))
88+
return projPaths[0], nil
89+
}
90+
91+
log.Printf("Found multiple project file: %s.", path.Base(projpth))
92+
projpth, err = goinp.SelectFromStringsWithDefault("Select the project file you want to scan", 1, projPaths)
93+
if err != nil {
94+
return "", fmt.Errorf("failed to select project file: %s", err)
95+
}
96+
97+
return projpth, nil
98+
}
99+
64100
func scanXcodeProject(cmd *cobra.Command, args []string) error {
65101
absExportOutputDirPath, err := initExportOutputDir()
66102
if err != nil {
@@ -81,14 +117,17 @@ func scanXcodeProject(cmd *cobra.Command, args []string) error {
81117

82118
projectPath := paramXcodeProjectFilePath
83119
if projectPath == "" {
84-
askText := `Please drag-and-drop your Xcode Project (` + colorstring.Green(".xcodeproj") + `) or Workspace (` + colorstring.Green(".xcworkspace") + `) file,
85-
the one you usually open in Xcode, then hit Enter.
86-
(Note: if you have a Workspace file you should most likely use that)`
87-
projpth, err := goinp.AskForPath(askText)
120+
log.Infof("Scan the directory for project files")
121+
log.Warnf("You can specify the Xcode project/workscape file to scan with the --file flag.")
122+
123+
projpth, err := findProject()
88124
if err != nil {
89-
return fmt.Errorf("failed to read input: %s", err)
125+
return err
90126
}
91127

128+
//
129+
// Scan the directory for Xcode Project (.xcworkspace / .xcodeproject) file first
130+
// If can't find any, ask the user to drag-and-drop the file
92131
projectPath = strings.Trim(strings.TrimSpace(projpth), "'\"")
93132
}
94133
log.Debugf("projectPath: %s", projectPath)

0 commit comments

Comments
 (0)