Class: Sxn::Core::SessionConfig

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

Overview

Manages .sxnrc session configuration files

Constant Summary collapse

FILENAME =
".sxnrc"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_path) ⇒ SessionConfig

Returns a new instance of SessionConfig.



13
14
15
16
# File 'lib/sxn/core/session_config.rb', line 13

def initialize(session_path)
  @session_path = session_path
  @config_path = File.join(session_path, FILENAME)
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



11
12
13
# File 'lib/sxn/core/session_config.rb', line 11

def config_path
  @config_path
end

#session_pathObject (readonly)

Returns the value of attribute session_path.



11
12
13
# File 'lib/sxn/core/session_config.rb', line 11

def session_path
  @session_path
end

Class Method Details

.find_from_path(start_path) ⇒ Object

Class method: Find .sxnrc by walking up directory tree



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sxn/core/session_config.rb', line 77

def self.find_from_path(start_path)
  current = File.expand_path(start_path)

  while current != "/" && current != File.dirname(current)
    config_path = File.join(current, FILENAME)
    return new(current) if File.exist?(config_path)

    current = File.dirname(current)
  end

  nil
end

.in_session?(path = Dir.pwd) ⇒ Boolean

Class method: Check if path is within a session

Returns:

  • (Boolean)


91
92
93
# File 'lib/sxn/core/session_config.rb', line 91

def self.in_session?(path = Dir.pwd)
  !find_from_path(path).nil?
end

Instance Method Details

#create(parent_sxn_path:, default_branch:, session_name:, template_id: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sxn/core/session_config.rb', line 18

def create(parent_sxn_path:, default_branch:, session_name:, template_id: nil)
  config = {
    "version" => 1,
    "parent_sxn_path" => parent_sxn_path,
    "default_branch" => default_branch,
    "session_name" => session_name,
    "created_at" => Time.now.iso8601
  }
  config["template_id"] = template_id if template_id
  File.write(@config_path, YAML.dump(config))
  config
end

#default_branchObject



47
48
49
# File 'lib/sxn/core/session_config.rb', line 47

def default_branch
  read&.dig("default_branch")
end

#exists?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/sxn/core/session_config.rb', line 31

def exists?
  File.exist?(@config_path)
end

#parent_sxn_pathObject



43
44
45
# File 'lib/sxn/core/session_config.rb', line 43

def parent_sxn_path
  read&.dig("parent_sxn_path")
end

#project_rootObject



59
60
61
62
63
64
65
# File 'lib/sxn/core/session_config.rb', line 59

def project_root
  parent_path = parent_sxn_path
  return nil unless parent_path

  # parent_sxn_path points to .sxn folder, project root is its parent
  File.dirname(parent_path)
end

#readObject



35
36
37
38
39
40
41
# File 'lib/sxn/core/session_config.rb', line 35

def read
  return nil unless exists?

  YAML.safe_load_file(@config_path) || {}
rescue Psych::SyntaxError
  nil
end

#session_nameObject



51
52
53
# File 'lib/sxn/core/session_config.rb', line 51

def session_name
  read&.dig("session_name")
end

#template_idObject



55
56
57
# File 'lib/sxn/core/session_config.rb', line 55

def template_id
  read&.dig("template_id")
end

#update(updates) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/sxn/core/session_config.rb', line 67

def update(updates)
  config = read || {}
  updates.each do |key, value|
    config[key.to_s] = value
  end
  File.write(@config_path, YAML.dump(config))
  config
end