Class: Sxn::Commands::Init

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/sxn/commands/init.rb

Overview

Initialize sxn in a project folder

Constant Summary collapse

SHELL_MARKER =

Shell integration marker - used to identify sxn shell functions

"# sxn shell integration"
SHELL_MARKER_END =
"# end sxn shell integration"
SHELL_FUNCTION =

Shell function that gets installed

<<~SHELL.freeze
  #{SHELL_MARKER}
  sxn-enter() {
    local cmd
    cmd="$(sxn enter 2>/dev/null)"
    if [ $? -eq 0 ] && [ -n "$cmd" ]; then
      eval "$cmd"
    else
      sxn enter
    fi
  }
  sxn-up() {
    local cmd
    cmd="$(sxn up 2>/dev/null)"
    if [ $? -eq 0 ] && [ -n "$cmd" ]; then
      eval "$cmd"
    else
      sxn up
    fi
  }
  #{SHELL_MARKER_END}
SHELL

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV, local_options = {}, config = {}) ⇒ Init

Returns a new instance of Init.



44
45
46
47
48
49
# File 'lib/sxn/commands/init.rb', line 44

def initialize(args = ARGV, local_options = {}, config = {})
  super
  @ui = Sxn::UI::Output.new
  @prompt = Sxn::UI::Prompt.new
  @config_manager = Sxn::Core::ConfigManager.new
end

Instance Method Details

#init(folder = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sxn/commands/init.rb', line 51

def init(folder = nil)
  @ui.section("Sxn Initialization")

  # Check if already initialized
  if @config_manager.initialized? && !options[:force]
    @ui.warning("Project already initialized")
    @ui.info("Use --force to reinitialize")
    return
  end

  # Get sessions folder
  sessions_folder = determine_sessions_folder(folder)

  begin
    # Initialize configuration
    @ui.progress_start("Creating configuration")
    result_folder = @config_manager.initialize_project(sessions_folder, force: options[:force])
    @ui.progress_done

    @ui.success("Initialized sxn in #{result_folder}")

    # Auto-detect projects if enabled
    auto_detect_projects if options[:auto_detect] && !options[:quiet]

    display_next_steps
  rescue Sxn::Error => e
    @ui.error("Initialization failed: #{e.message}")
    exit(e.exit_code)
  rescue StandardError => e
    @ui.error("Unexpected error: #{e.message}")
    @ui.debug(e.backtrace.join("\n")) if ENV["SXN_DEBUG"]
    exit(1)
  end
end

#install_shellObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sxn/commands/init.rb', line 90

def install_shell
  @ui.section("Shell Integration")

  shell_type = detect_shell_type
  rc_file = shell_rc_file(shell_type)

  unless rc_file
    @ui.error("Could not determine shell configuration file")
    @ui.info("Supported shells: bash, zsh")
    exit(1)
  end

  if options[:uninstall]
    uninstall_shell_integration(rc_file, shell_type)
  else
    install_shell_integration(rc_file, shell_type)
  end
end