Class: Shellfie::SessionConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/shellfie/session_config.rb

Constant Summary collapse

MAX_BYTES =
1_048_576
MAX_DURATION =
86_400
MAX_COUNT =
10_000
MAX_CAPTURES =
100
MAX_PATTERN_LENGTH =
512
MAX_INCLUDE_FILES =
100
MAX_TOTAL_BYTES =
10 * MAX_BYTES
ACTIONS =
%i[run type key sleep wait expect capture hide show].freeze
STEP_OPTION_KEYS =
{
  run: %i[visibility timeout async cwd], type: %i[speed], key: %i[count async timeout delay],
  sleep: [], wait: %i[timeout], expect: [], capture: [], hide: [], show: []
}.freeze
TOP_LEVEL_KEYS =
%i[version mode title theme terminal requires steps outputs render redact vars step_sets].freeze
TERMINAL_KEYS =
%i[shell columns rows cwd cwd_policy env env_allowlist timeout total_timeout prompt].freeze
OUTPUT_KEYS =
%i[path format animate scale shadow transparent capture].freeze
OUTPUT_FORMATS =
%w[png gif svg svg-raster webp apng mp4 webm png-sequence html txt ansi json asciicast cast].freeze
RENDER_KEYS =
%i[window font animation headless].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, path: nil, source_paths: nil) ⇒ SessionConfig

Returns a new instance of SessionConfig.

Raises:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/shellfie/session_config.rb', line 137

def initialize(raw, path: nil, source_paths: nil)
  raise ValidationError, "Session configuration must be a YAML mapping" unless raw.is_a?(Hash)

  unknown = raw.keys - TOP_LEVEL_KEYS
  raise_unknown_keys!(unknown, TOP_LEVEL_KEYS, "session")
  variables = validate_variables(raw[:vars] || {})
  raw = interpolate_variables(raw.reject { |key, _value| key == :vars }, variables)
  raise ValidationError, "Session config version must be 2" unless raw[:version] == 2
  raise ValidationError, "Session config must contain steps" unless raw.key?(:steps)
  raise ValidationError, "Session title must be a string" if raw.key?(:title) && !raw[:title].is_a?(String)

  @path = path
  @source_paths = Array(source_paths || path).compact.freeze
  @mode = (raw[:mode] || "run").to_s
  @title = (raw[:title] || "Terminal Session").to_s
  @theme = (raw[:theme] || "macos").to_s
  @terminal = defaults.merge(symbolize_hash(raw[:terminal] || {}))
  @requires = Array(raw[:requires])
  raise ValidationError, "steps must be an array" unless raw[:steps].is_a?(Array)
  @steps = expand_steps(raw[:steps], validate_step_sets(raw[:step_sets] || {}))
  @outputs = Array(raw[:outputs]).map { |output| symbolize_hash(output) }
  @render = symbolize_hash(raw[:render] || {})
  %i[window font animation].each do |key|
    @render[key] = symbolize_hash(@render[key]) if @render[key].is_a?(Hash)
  end
  @redactions = Array(raw[:redact])
  validate!
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def mode
  @mode
end

#outputsObject (readonly)

Returns the value of attribute outputs.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def outputs
  @outputs
end

#pathObject (readonly)

Returns the value of attribute path.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def path
  @path
end

#redactionsObject (readonly)

Returns the value of attribute redactions.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def redactions
  @redactions
end

#renderObject (readonly)

Returns the value of attribute render.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def render
  @render
end

#requiresObject (readonly)

Returns the value of attribute requires.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def requires
  @requires
end

#source_pathsObject (readonly)

Returns the value of attribute source_paths.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def source_paths
  @source_paths
end

#stepsObject (readonly)

Returns the value of attribute steps.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def steps
  @steps
end

#terminalObject (readonly)

Returns the value of attribute terminal.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def terminal
  @terminal
end

#themeObject (readonly)

Returns the value of attribute theme.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def theme
  @theme
end

#titleObject (readonly)

Returns the value of attribute title.



32
33
34
# File 'lib/shellfie/session_config.rb', line 32

def title
  @title
end

Class Method Details

.parse(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shellfie/session_config.rb', line 34

def self.parse(path)
  source_path = File.realpath(path)
  state = { files: 0, bytes: 0 }
  raw, documents, provenance = load_included(source_path, root: File.dirname(source_path), stack: [], state: state)
  new(raw, path: source_path, source_paths: documents.map(&:first).uniq)
rescue ValidationError => e
  raise YamlSafety.annotate_validation_error(e, documents || [], provenance: provenance || {})
rescue Psych::Exception => e
  raise ParseError, "Invalid session YAML syntax: #{e.message}"
rescue Errno::ENOENT
  raise ParseError, "Session file not found: #{path}"
end

Instance Method Details

#base_dirObject



166
167
168
# File 'lib/shellfie/session_config.rb', line 166

def base_dir
  path ? File.dirname(path) : Dir.pwd
end

#to_hObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/shellfie/session_config.rb', line 170

def to_h
  {
    version: 2,
    mode: mode,
    title: title,
    theme: theme,
    terminal: terminal,
    requires: requires,
    steps: steps,
    outputs: outputs,
    render: render,
    redact: redactions
  }
end