Module: Dependabot::Devbox::Helpers

Extended by:
T::Sig
Defined in:
lib/dependabot/devbox/helpers.rb

Constant Summary collapse

JSONC_TOKEN =

Matches either a JSON string literal (with escapes), a line comment, a block comment, or a trailing comma. The alternation lets gsub preserve strings while stripping the JSONC-only constructs, so e.g. "//" inside a URL value is not mistaken for the start of a comment.

T.let(
  %r{
    ("(?:\\.|[^"\\])*")    # JSON string literal
    | //[^\n]*             # line comment
    | /\*.*?\*/            # block comment
    | ,(?=\s*[\}\]])       # trailing comma
  }mx,
  Regexp
)

Class Method Summary collapse

Class Method Details

.parse_json_or_jsonc(content) ⇒ Object

Raises:

  • (JSON::ParserError)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dependabot/devbox/helpers.rb', line 29

def self.parse_json_or_jsonc(content)
  return {} unless content

  cleaned = content.gsub(JSONC_TOKEN) { ::Regexp.last_match(1) || "" }

  parsed = JSON.parse(cleaned)
  # A devbox.json must be a JSON object. Guard here so a malformed manifest
  # (e.g. a top-level array) surfaces as a clear parse error rather than an
  # opaque sorbet-runtime type error at the call site.
  raise JSON::ParserError, "Expected a JSON object, got #{parsed.class}" unless parsed.is_a?(Hash)

  parsed
end

.run_devbox_command(*args, dir:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dependabot/devbox/helpers.rb', line 54

def self.run_devbox_command(*args, dir:)
  Dependabot::SharedHelpers.run_shell_command(
    "devbox #{args.join(' ')}",
    cwd: dir,
    env: {
      "DEVBOX_CACHE" => File.join(dir, ".devbox_cache"),
      "XDG_CACHE_HOME" => File.join(dir, ".cache"),
      "HOME" => dir
    }
  )
end