Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing Ruby during react-native Podfile detection #227

Merged
merged 2 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions scanners/ios/podfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ end

absPodfilePth, err := filepath.Abs(podfileParser.podfilePth)
if err != nil {
return map[string]string{}, fmt.Errorf("failed to expand path (%s), error: %s", podfileParser.podfilePth, err)
return map[string]string{}, fmt.Errorf("failed to expand path (%s): %s", podfileParser.podfilePth, err)
}

envs := []string{fmt.Sprintf("PODFILE_PATH=%s", absPodfilePth)}
podfileDir := filepath.Dir(absPodfilePth)

out, err := runRubyScriptForOutput(rubyScriptContent, gemfileContent, podfileDir, envs)
out, err := runRubyScriptForOutput(rubyScriptContent, gemfileContent, envs)
if err != nil {
return map[string]string{}, fmt.Errorf("ruby script failed, error: %s", err)
return map[string]string{}, fmt.Errorf("ruby script failed: %s", err)
}

if out == "" {
Expand All @@ -87,11 +86,11 @@ end

var targetDefinitionOutput targetDefinitionOutputModel
if err := json.Unmarshal([]byte(out), &targetDefinitionOutput); err != nil {
return map[string]string{}, fmt.Errorf("failed to parse target definition output, error: %s", err)
return map[string]string{}, fmt.Errorf("failed to parse target definition output: %s", err)
}

if podfileParser.shouldRaiseReadDefinitionError(targetDefinitionOutput.Error) {
return map[string]string{}, fmt.Errorf("failed to read target defintion map, error: %s", targetDefinitionOutput.Error)
return map[string]string{}, fmt.Errorf("failed to read target definition map: %s", targetDefinitionOutput.Error)
}

return targetDefinitionOutput.Data, nil
Expand All @@ -115,7 +114,7 @@ func (podfileParser podfileParser) shouldRaiseReadDefinitionError(err string) bo
func (podfileParser podfileParser) getUserDefinedProjectRelavtivePath(cocoapodsVersion string) (string, error) {
targetProjectMap, err := podfileParser.getTargetDefinitionProjectMap(cocoapodsVersion)
if err != nil {
return "", fmt.Errorf("failed to get target definition map, error: %s", err)
return "", fmt.Errorf("failed to get target definition map: %s", err)
}

for target, project := range targetProjectMap {
Expand Down Expand Up @@ -157,15 +156,14 @@ end
`
absPodfilePth, err := filepath.Abs(podfileParser.podfilePth)
if err != nil {
return "", fmt.Errorf("failed to expand path (%s), error: %s", podfileParser.podfilePth, err)
return "", fmt.Errorf("failed to expand path (%s): %s", podfileParser.podfilePth, err)
}

envs := []string{fmt.Sprintf("PODFILE_PATH=%s", absPodfilePth)}
podfileDir := filepath.Dir(absPodfilePth)

out, err := runRubyScriptForOutput(rubyScriptContent, gemfileContent, podfileDir, envs)
out, err := runRubyScriptForOutput(rubyScriptContent, gemfileContent, envs)
if err != nil {
return "", fmt.Errorf("ruby script failed, error: %s", err)
return "", fmt.Errorf("ruby script failed: %s", err)
}

if out == "" {
Expand All @@ -179,11 +177,11 @@ end

var workspacePathOutput workspacePathOutputModel
if err := json.Unmarshal([]byte(out), &workspacePathOutput); err != nil {
return "", fmt.Errorf("failed to parse workspace path output, error: %s", err)
return "", fmt.Errorf("failed to parse workspace path output: %s", err)
}

if podfileParser.shouldRaiseReadDefinitionError(workspacePathOutput.Error) {
return "", fmt.Errorf("failed to read workspace path, error: %s", workspacePathOutput.Error)
return "", fmt.Errorf("failed to read workspace path: %s", workspacePathOutput.Error)
}

return workspacePathOutput.Data, nil
Expand Down Expand Up @@ -213,34 +211,34 @@ func (podfileParser podfileParser) GetWorkspaceProjectMap(projects []string) (ma

projectRelPth, err := podfileParser.getUserDefinedProjectRelavtivePath(cocoapodsVersion)
if err != nil {
return map[string]string{}, fmt.Errorf("failed to get user defined project path, error: %s", err)
return map[string]string{}, fmt.Errorf("failed to get user defined project path: %s", err)
}

if projectRelPth == "" {
projects, err := pathutil.FilterPaths(projects, pathutil.InDirectoryFilter(podfileDir, true))
if err != nil {
return map[string]string{}, fmt.Errorf("failed to filter projects, error: %s", err)
return map[string]string{}, fmt.Errorf("failed to filter projects: %s", err)
}

if len(projects) == 0 {
return map[string]string{}, errors.New("failed to determin workspace - project mapping: no explicit project specified and no project found in the Podfile's directory")
return map[string]string{}, errors.New("failed to determine workspace - project mapping: no explicit project specified and no project found in the Podfile's directory")
} else if len(projects) > 1 {
return map[string]string{}, errors.New("failed to determin workspace - project mapping: no explicit project specified and more than one project found in the Podfile's directory")
return map[string]string{}, errors.New("failed to determine workspace - project mapping: no explicit project specified and more than one project found in the Podfile's directory")
}

projectRelPth = filepath.Base(projects[0])
}
projectPth := filepath.Join(podfileDir, projectRelPth)

if exist, err := pathutil.IsPathExists(projectPth); err != nil {
return map[string]string{}, fmt.Errorf("failed to check if path (%s) exists, error: %s", projectPth, err)
return map[string]string{}, fmt.Errorf("failed to check if path (%s) exists: %s", projectPth, err)
} else if !exist {
return map[string]string{}, fmt.Errorf("project not found at: %s", projectPth)
}

workspaceRelPth, err := podfileParser.getUserDefinedWorkspaceRelativePath(cocoapodsVersion)
if err != nil {
return map[string]string{}, fmt.Errorf("failed to get user defined workspace path, error: %s", err)
return map[string]string{}, fmt.Errorf("failed to get user defined workspace path: %s", err)
}

if workspaceRelPth == "" {
Expand Down Expand Up @@ -286,7 +284,7 @@ func (podfileParser podfileParser) cocoapodsVersion(podfileLockPth string) (stri
func (podfileParser podfileParser) fixPodfileQuotation(podfilePth string) error {
podfileContent, err := fileutil.ReadStringFromFile(podfilePth)
if err != nil {
return fmt.Errorf("failed to read podfile (%s), error: %s", podfilePth, err)
return fmt.Errorf("failed to read podfile (%s): %s", podfilePth, err)
}

podfileContent = strings.Replace(podfileContent, `‘`, `'`, -1)
Expand All @@ -295,7 +293,7 @@ func (podfileParser podfileParser) fixPodfileQuotation(podfilePth string) error
podfileContent = strings.Replace(podfileContent, `”`, `"`, -1)

if err := fileutil.WriteStringToFile(podfilePth, podfileContent); err != nil {
return fmt.Errorf("failed to apply Podfile quotation fix, error: %s", err)
return fmt.Errorf("failed to apply Podfile quotation fix: %s", err)
}

return nil
Expand Down
13 changes: 5 additions & 8 deletions scanners/ios/rubyscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/bitrise-io/go-utils/pathutil"
)

func runRubyScriptForOutput(scriptContent, gemfileContent, inDir string, withEnvs []string) (string, error) {
func runRubyScriptForOutput(scriptContent, gemfileContent string, withEnvs []string) (string, error) {
tmpDir, err := pathutil.NormalizedOSTempDirPath("__bitrise-init__")
if err != nil {
return "", err
Expand All @@ -32,10 +32,7 @@ func runRubyScriptForOutput(scriptContent, gemfileContent, inDir string, withEnv
}

cmd := command.New("bundle", "install")

if inDir != "" {
cmd.SetDir(inDir)
}
cmd.SetDir(tmpDir)

withEnvs = append(withEnvs, "BUNDLE_GEMFILE="+gemfilePth)
cmd.AppendEnvs(withEnvs...)
Expand All @@ -62,9 +59,9 @@ func runRubyScriptForOutput(scriptContent, gemfileContent, inDir string, withEnv
cmd = command.New("ruby", rubyScriptPth)
}

if inDir != "" {
cmd.SetDir(inDir)
}
// Set the temp dir as working dir, so the project defined `.ruby-version` does not cause ruby resolution to fail:
// [ ... ] ruby script failed, error: rbenv: version `2.7.4' is not installed (set by /[ ... ]/MyTestApp/.ruby-version)
cmd.SetDir(tmpDir)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directory is still changed inside the Cocoapod resolution script (podfile.go):

Dir.chdir(File.dirname(podfile_path))


if len(withEnvs) > 0 {
cmd.AppendEnvs(withEnvs...)
Expand Down
2 changes: 1 addition & 1 deletion scanners/ios/rubyscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ puts "#{{ :test_key => 'test_value' }.to_json}"
`

expectedOut := "{\"test_key\":\"test_value\"}"
actualOut, err := runRubyScriptForOutput(rubyScriptContent, gemfileContent, "", []string{})
actualOut, err := runRubyScriptForOutput(rubyScriptContent, gemfileContent, []string{})
require.NoError(t, err)
require.Equal(t, expectedOut, actualOut)
}