Class: Kamal::Dev::DevcontainerParser

Inherits:
Object
  • Object
show all
Defined in:
lib/kamal/dev/devcontainer_parser.rb

Overview

Parses VS Code devcontainer.json specifications into Docker configuration

Handles JSON with comments (// and /* */), extracts container properties, and transforms them into a standardized configuration hash for Docker deployment.

Examples:

Basic usage

parser = DevcontainerParser.new(".devcontainer/devcontainer.json")
config = parser.parse
#=> {image: "ruby:3.2", ports: [3000], workspace: "/workspace", ...}

Defined Under Namespace

Classes: ValidationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ DevcontainerParser

Initialize parser with devcontainer.json file path



25
26
27
# File 'lib/kamal/dev/devcontainer_parser.rb', line 25

def initialize(file_path)
  @file_path = file_path
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



20
21
22
# File 'lib/kamal/dev/devcontainer_parser.rb', line 20

def file_path
  @file_path
end

Instance Method Details

#compose_file_pathString?

Get path to compose file if present

Returns path relative to .devcontainer/ directory



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/kamal/dev/devcontainer_parser.rb', line 79

def compose_file_path
  return nil unless uses_compose?

  content = File.read(@file_path)
  clean_content = strip_comments(content)
  devcontainer_json = JSON.parse(clean_content)

  compose_file = devcontainer_json["dockerComposeFile"]
  return nil unless compose_file

  # Handle array of compose files (use first one)
  compose_file = compose_file.first if compose_file.is_a?(Array)

  # Resolve path relative to .devcontainer directory
  devcontainer_dir = File.dirname(@file_path)
  File.join(devcontainer_dir, compose_file)
rescue
  nil
end

#parseHash

Parse devcontainer.json and return standardized config hash

Raises:

  • (Errno::ENOENT)

    if file doesn't exist

  • (JSON::ParserError)

    if JSON is malformed

  • (ValidationError)

    if required properties are missing



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kamal/dev/devcontainer_parser.rb', line 43

def parse
  content = File.read(@file_path)
  clean_content = strip_comments(content)
  devcontainer_json = JSON.parse(clean_content)

  validate_required_properties!(devcontainer_json)

  {
    image: extract_image(devcontainer_json),
    ports: extract_ports(devcontainer_json),
    mounts: extract_mounts(devcontainer_json),
    env: extract_env(devcontainer_json),
    options: extract_options(devcontainer_json),
    user: extract_user(devcontainer_json),
    workspace: extract_workspace(devcontainer_json)
  }
end

#uses_compose?Boolean

Check if devcontainer uses Docker Compose



64
65
66
67
68
69
70
71
72
# File 'lib/kamal/dev/devcontainer_parser.rb', line 64

def uses_compose?
  content = File.read(@file_path)
  clean_content = strip_comments(content)
  devcontainer_json = JSON.parse(clean_content)

  devcontainer_json.key?("dockerComposeFile")
rescue
  false
end