github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/internal/orbs/ruby/ruby.go (about)

     1  // Copyright 2022 Harness, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ruby
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"strings"
    21  
    22  	circle "github.com/drone/go-convert/convert/circle/yaml"
    23  	harness "github.com/drone/spec/dist/go"
    24  )
    25  
    26  // Convert converts an Orb to a Harness step.
    27  func Convert(command string, step *circle.Custom) *harness.Step {
    28  	switch command {
    29  	case "install":
    30  		return nil
    31  	case "install-deps":
    32  		return convertInstallDeps(step)
    33  	case "rspec-test":
    34  		return convertRspecTest(step)
    35  	case "rubocop-check":
    36  		return convertRubocopCheck(step)
    37  	default:
    38  		return nil
    39  	}
    40  }
    41  
    42  // helper function converts a ruby/rspec-test
    43  // orb to a run step.
    44  func convertRspecTest(step *circle.Custom) *harness.Step {
    45  	outpath := "/tmp/test-results/rspec"
    46  	if s, _ := step.Params["path"].(string); s != "" {
    47  		outpath = s
    48  	}
    49  
    50  	var script []string
    51  	script = append(script, fmt.Sprintf(`mkdir -p %q`, outpath))
    52  	script = append(script, fmt.Sprintf(
    53  		`bundle exec rspec --profile 10 --format RspecJunitFormatter --out %s/results.xml --format progress`,
    54  		outpath,
    55  	),
    56  	)
    57  
    58  	return &harness.Step{
    59  		Name: "rspec_test",
    60  		Type: "script",
    61  		Spec: &harness.StepExec{
    62  			Run: strings.Join(script, "\n"),
    63  		},
    64  	}
    65  }
    66  
    67  // helper function converts a ruby/install-deps
    68  // orb to a run step.
    69  func convertInstallDeps(step *circle.Custom) *harness.Step {
    70  	path := "./vendor/bundle"
    71  	clean := false
    72  	gemfile := "Gemfile"
    73  	if s, _ := step.Params["path"].(string); s != "" {
    74  		path = s
    75  	}
    76  	if s, _ := step.Params["gemfile"].(string); s != "" {
    77  		gemfile = s
    78  	}
    79  	if s, _ := step.Params["clean-bundle"].(bool); s {
    80  		clean = s
    81  	}
    82  
    83  	var script []string
    84  	if path == "./vendor/bundle" {
    85  		script = append(script, `bundle config deployment 'true'`)
    86  	}
    87  
    88  	script = append(script, fmt.Sprintf(`bundle config gemfile %q`, gemfile))
    89  	script = append(script, fmt.Sprintf(`bundle config path %q`, path))
    90  
    91  	if clean {
    92  		script = append(script, `bundle check || (bundle install && bundle clean --force)`)
    93  	} else {
    94  		script = append(script, `bundle check || bundle install`)
    95  	}
    96  
    97  	return &harness.Step{
    98  		Name: "install_deps",
    99  		Type: "script",
   100  		Spec: &harness.StepExec{
   101  			Run: strings.Join(script, "\n"),
   102  		},
   103  	}
   104  }
   105  
   106  // helper function converts a ruby/rubocop-check
   107  // orb to a run step.
   108  func convertRubocopCheck(step *circle.Custom) *harness.Step {
   109  	checkpath := "."
   110  	format := "progress"
   111  	outpath := "/tmp/rubocop-results"
   112  	parallel := false
   113  
   114  	if s, _ := step.Params["check-path"].(string); s != "" {
   115  		checkpath = s
   116  	}
   117  	if s, _ := step.Params["out-path"].(string); s != "" {
   118  		outpath = s
   119  	}
   120  	if s, _ := step.Params["parallel"].(bool); s {
   121  		parallel = s
   122  	}
   123  
   124  	var buf bytes.Buffer
   125  	buf.WriteString(fmt.Sprintf(`mkdir -p %q`, outpath))
   126  	buf.WriteString("\n")
   127  
   128  	if !parallel {
   129  		buf.WriteString(
   130  			fmt.Sprintf(
   131  				`bundle exec rubocop %s --out %s/check-results.xml --format %s`,
   132  				checkpath,
   133  				outpath,
   134  				format,
   135  			),
   136  		)
   137  	} else {
   138  		buf.WriteString(
   139  			fmt.Sprintf(
   140  				`bundle exec rubocop %s --out %s/check-results.xml --format %s --parallel`,
   141  				checkpath,
   142  				outpath,
   143  				format,
   144  			),
   145  		)
   146  	}
   147  
   148  	return &harness.Step{
   149  		Name: "rubocop_check",
   150  		Type: "script",
   151  		Spec: &harness.StepExec{
   152  			Run: buf.String(),
   153  		},
   154  	}
   155  }