ruby

package
v0.0.0-...-9ce4fc3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package ruby provides a backend for Ruby using Bundler.

Index

Constants

This section is empty.

Variables

View Source
var RubyBackend = api.LanguageBackend{
	Name:             "ruby-bundler",
	Specfile:         "Gemfile",
	Lockfile:         "Gemfile.lock",
	IsAvailable:      bundlerIsAvailable,
	FilenamePatterns: []string{"*.rb"},
	Quirks:           api.QuirksAddRemoveAlsoLocks,
	GetPackageDir: func() string {
		path := string(util.GetCmdOutput([]string{
			"bundle", "config", "--parseable", "path"}))
		path = strings.TrimSuffix(path, "\n")
		path = strings.TrimPrefix(path, "path=")
		if path == "" {
			return ".bundle"
		} else {
			return path
		}
	},
	Search: func(query string) []api.PkgInfo {
		endpoint := "https://rubygems.org/api/v1/search.json"
		queryParams := "?query=" + url.QueryEscape(query)

		resp, err := api.HttpClient.Get(endpoint + queryParams)
		if err != nil {
			util.DieNetwork("RubyGems: %s", err)
		}
		defer resp.Body.Close()

		body, err := io.ReadAll(resp.Body)
		if err != nil {
			util.DieProtocol("RubyGems: %s", err)
		}

		var outputStructs []rubygemsInfo
		if err := json.Unmarshal(body, &outputStructs); err != nil {
			util.DieProtocol("RubyGems response: %s", err)
		}

		results := []api.PkgInfo{}
		for _, s := range outputStructs {
			deps := []string{}
			for _, group := range s.Dependencies {
				for _, dep := range group {
					deps = append(deps, dep.Name)
				}
			}
			results = append(results, api.PkgInfo{
				Name:             s.Name,
				Description:      s.Info,
				Version:          s.Version,
				HomepageURL:      s.HomepageURI,
				DocumentationURL: s.DocumentationURI,
				SourceCodeURL:    s.SourceCodeURI,
				BugTrackerURL:    s.BugTrackerURI,
				Author:           s.Authors,
				License:          strings.Join(s.Licenses, ", "),
				Dependencies:     deps,
			})
		}
		return results
	},
	Info: func(name api.PkgName) api.PkgInfo {
		endpoint := "https://rubygems.org/api/v1/gems/"
		path := url.QueryEscape(string(name)) + ".json"

		resp, err := api.HttpClient.Get(endpoint + path)
		if err != nil {
			util.DieNetwork("RubyGems: %s", err)
		}
		defer resp.Body.Close()

		switch resp.StatusCode {
		case 200:
			break
		case 404:
			return api.PkgInfo{}
		default:
			util.DieNetwork("RubyGems: HTTP status %d", resp.StatusCode)
		}

		body, err := io.ReadAll(resp.Body)
		if err != nil {
			util.DieProtocol("RubyGems: %s", err)
		}

		var s rubygemsInfo
		if err := json.Unmarshal(body, &s); err != nil {
			util.DieProtocol("RubyGems response: %s", err)
		}
		deps := []string{}
		for _, group := range s.Dependencies {
			for _, dep := range group {
				deps = append(deps, dep.Name)
			}
		}
		return api.PkgInfo{
			Name:             s.Name,
			Description:      s.Info,
			Version:          s.Version,
			HomepageURL:      s.HomepageURI,
			DocumentationURL: s.DocumentationURI,
			SourceCodeURL:    s.SourceCodeURI,
			BugTrackerURL:    s.BugTrackerURI,
			Author:           s.Authors,
			License:          strings.Join(s.Licenses, ", "),
			Dependencies:     deps,
		}
	},
	Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName string) {

		span, ctx := tracer.StartSpanFromContext(ctx, "bundle (init) add")
		defer span.Finish()
		if !util.Exists("Gemfile") {
			util.RunCmd([]string{"bundle", "init"})
		}
		args := []string{}
		for name, spec := range pkgs {
			if spec == "" {
				args = append(args, string(name))
			}
		}
		if len(args) > 0 {

			util.RunCmd(append([]string{
				"bundle", "add", "--skip-install"}, args...))
		}
		for name, spec := range pkgs {
			if spec != "" {
				nameArg := string(name)
				versionArg := "--version=" + string(spec)
				util.RunCmd([]string{"bundle", "add", nameArg, versionArg})
			}
		}
	},
	Remove: func(ctx context.Context, pkgs map[api.PkgName]bool) {

		span, ctx := tracer.StartSpanFromContext(ctx, "bundle remove")
		defer span.Finish()
		cmd := []string{"bundle", "remove", "--skip-install"}
		for name := range pkgs {
			cmd = append(cmd, string(name))
		}
		util.RunCmd(cmd)
	},
	Lock: func(ctx context.Context) {

		span, ctx := tracer.StartSpanFromContext(ctx, "bundle lock")
		defer span.Finish()
		util.RunCmd([]string{"bundle", "lock"})
	},
	Install: func(ctx context.Context) {

		span, ctx := tracer.StartSpanFromContext(ctx, "bundle install")
		defer span.Finish()

		util.RunCmd([]string{"bundle", "config", "set", "--local", "clean", "true"})
		if path := getPath(); path != "" {
			util.RunCmd([]string{"bundle", "config", "set", "--local", "path", path})
		}
		util.RunCmd([]string{"bundle", "install"})
	},
	ListSpecfile: func(mergeAllGroups bool) map[api.PkgName]api.PkgSpec {
		outputB := util.GetCmdOutput([]string{
			"ruby", "-e", util.GetResource("/ruby/list-specfile.rb"),
		})
		results := map[api.PkgName]api.PkgSpec{}
		if err := json.Unmarshal(outputB, &results); err != nil {
			util.DieProtocol("ruby: %s", err)
		}
		return results
	},
	ListLockfile: func() map[api.PkgName]api.PkgVersion {
		outputB := util.GetCmdOutput([]string{
			"ruby", "-e", util.GetResource("/ruby/list-lockfile.rb"),
		})
		results := map[api.PkgName]api.PkgVersion{}
		if err := json.Unmarshal(outputB, &results); err != nil {
			util.DieProtocol("ruby: %s", err)
		}
		return results
	},
	GuessRegexps: util.Regexps([]string{
		`require\s*['"]([^'"]+)['"]`,
	}),
	Guess: func(ctx context.Context) (map[string][]api.PkgName, bool) {

		span, ctx := tracer.StartSpanFromContext(ctx, "guess-gems.rb")
		defer span.Finish()
		guessedGems := util.GetCmdOutput([]string{
			"ruby", "-e", util.GetResource("/ruby/guess-gems.rb"),
		})
		results := map[string][]api.PkgName{}
		if err := json.Unmarshal(guessedGems, &results); err != nil {
			util.DieProtocol("ruby: %s", err)
		}
		return results, true
	},
	InstallReplitNixSystemDependencies: nix.DefaultInstallReplitNixSystemDependencies,
}

RubyBackend is a UPM language backend for Ruby using Bundler.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL