Class: Rufio::CommandMode

Inherits:
Object
  • Object
show all
Defined in:
lib/rufio/command_mode.rb

Overview

コマンドモード - DSLコマンドを実行するための統一インターフェースすべてのコマンドはDslCommandとして扱われる

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(background_executor = nil) ⇒ CommandMode

Returns a new instance of CommandMode.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rufio/command_mode.rb', line 12

def initialize(background_executor = nil)
  @commands = {}
  @background_executor = background_executor
  @script_runner = nil
  @script_path_manager = nil
  @job_manager = nil
  @local_script_scanner = LocalScriptScanner.new
  @rakefile_parser = RakefileParser.new
  load_builtin_commands
  load_dsl_commands
end

Instance Attribute Details

#background_executorObject

Returns the value of attribute background_executor.



9
10
11
# File 'lib/rufio/command_mode.rb', line 9

def background_executor
  @background_executor
end

#script_path_managerObject (readonly)

Returns the value of attribute script_path_manager.



10
11
12
# File 'lib/rufio/command_mode.rb', line 10

def script_path_manager
  @script_path_manager
end

#script_runnerObject (readonly)

Returns the value of attribute script_runner.



10
11
12
# File 'lib/rufio/command_mode.rb', line 10

def script_runner
  @script_runner
end

Instance Method Details

#available_commandsObject

利用可能なコマンドのリストを取得



112
113
114
# File 'lib/rufio/command_mode.rb', line 112

def available_commands
  @commands.keys
end

#command_info(command_name) ⇒ Object

コマンドの情報を取得



117
118
119
120
121
122
123
124
125
126
# File 'lib/rufio/command_mode.rb', line 117

def command_info(command_name)
  command = @commands[command_name]
  return nil unless command

  {
    name: command_name,
    plugin: command[:source] || "dsl",
    description: command[:command].description
  }
end

#complete_rake_task(prefix) ⇒ Array<String>

rakeタスク名を補完する

Parameters:

  • prefix (String)

    入力中の文字列(rake:を含まない)

Returns:

  • (Array<String>)

    補完候補(rake:付き)



172
173
174
# File 'lib/rufio/command_mode.rb', line 172

def complete_rake_task(prefix)
  @rakefile_parser.complete(prefix).map { |name| "rake:#{name}" }
end

#complete_script(prefix) ⇒ Array<String>

スクリプト名を補完する

Parameters:

  • prefix (String)

    入力中の文字列(@を含む)

Returns:

  • (Array<String>)

    補完候補(@付き)



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rufio/command_mode.rb', line 151

def complete_script(prefix)
  # @を除去して検索
  search_prefix = prefix.sub(/^@/, '')

  candidates = []

  # ScriptRunnerからの候補
  if @script_runner
    candidates += @script_runner.complete(search_prefix)
  end

  # ローカルスクリプトからの候補
  candidates += @local_script_scanner.complete(search_prefix)

  # 重複排除してソート、@付きで返す
  candidates.uniq.sort.map { |name| "@#{name}" }
end

#execute(command_string, working_dir: nil) ⇒ Object

コマンドを実行する

Parameters:

  • command_string (String)

    コマンド文字列

  • working_dir (String, nil) (defaults to: nil)

    作業ディレクトリ(スクリプト実行時に使用)



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rufio/command_mode.rb', line 60

def execute(command_string, working_dir: nil)
  # 空のコマンドは無視
  return nil if command_string.nil? || command_string.strip.empty?

  # スクリプト実行 (@ で始まる場合)
  if command_string.strip.start_with?('@')
    return execute_script(command_string.strip[1..-1], working_dir)
  end

  # rakeタスク実行 (rake: で始まる場合)
  if command_string.strip.start_with?('rake:')
    task_name = command_string.strip[5..-1]
    return execute_rake_task(task_name, working_dir)
  end

  # シェルコマンドの実行 (! で始まる場合)
  if command_string.strip.start_with?('!')
    shell_command = command_string.strip[1..-1]

    # バックグラウンドエグゼキュータが利用可能な場合は非同期実行
    if @background_executor
      if @background_executor.execute_async(shell_command)
        return "🔄 Running in background: #{shell_command.split.first}"
      else
        return "⚠️  Command already running"
      end
    else
      # バックグラウンドエグゼキュータがない場合は同期実行
      return execute_shell_command(shell_command)
    end
  end

  # コマンド名を取得 (前後の空白を削除)
  command_name = command_string.strip.to_sym

  # 統一されたコマンドストアから検索
  command = @commands[command_name]
  if command
    # 内部コマンドを実行
    return execute_unified_command(command_name, command)
  end

  # 内部コマンドが見つからない場合、スクリプトパスから検索
  if @script_path_manager || @script_runner
    script_result = try_execute_script_from_paths(command_string.strip, working_dir)
    return script_result if script_result
  end

  "⚠️  コマンドが見つかりません: #{command_name}"
end

#load_dsl_commands(paths = nil) ⇒ Object

DSLコマンドをロードする

Parameters:

  • paths (Array<String>, nil) (defaults to: nil)

    設定ファイルのパス配列(nilの場合はデフォルトパス)



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rufio/command_mode.rb', line 130

def load_dsl_commands(paths = nil)
  loader = DslCommandLoader.new

  commands = if paths
               loader.load_from_paths(paths)
             else
               loader.load
             end

  # ユーザーDSLコマンドは既存のコマンドを上書きする(優先度が高い)
  commands.each do |cmd|
    @commands[cmd.name.to_sym] = {
      command: cmd,
      source: "dsl"
    }
  end
end

#setup_script_path_manager(config_file:, job_manager:) ⇒ Object

ScriptPathManagerを設定する(設定ファイルベース)

Parameters:

  • config_file (String)

    設定ファイルのパス

  • job_manager (JobManager)

    ジョブマネージャー



39
40
41
42
43
44
45
46
47
48
# File 'lib/rufio/command_mode.rb', line 39

def setup_script_path_manager(config_file:, job_manager:)
  @job_manager = job_manager
  @script_path_manager = ScriptPathManager.new(config_file)
  # ScriptRunnerも設定(ScriptPathManagerのパスを使用)
  @script_runner = ScriptRunner.new(
    script_paths: @script_path_manager.paths,
    job_manager: job_manager,
    command_logger: @background_executor&.command_logger
  )
end

#setup_script_runner(script_paths:, job_manager:) ⇒ Object

ScriptRunnerを設定する

Parameters:

  • script_paths (Array<String>)

    スクリプトパス

  • job_manager (JobManager)

    ジョブマネージャー



27
28
29
30
31
32
33
34
# File 'lib/rufio/command_mode.rb', line 27

def setup_script_runner(script_paths:, job_manager:)
  @job_manager = job_manager
  @script_runner = ScriptRunner.new(
    script_paths: script_paths,
    job_manager: job_manager,
    command_logger: @background_executor&.command_logger
  )
end

#update_browsing_directory(directory) ⇒ Object

閲覧中ディレクトリを更新

Parameters:

  • directory (String)

    現在の閲覧ディレクトリ



52
53
54
55
# File 'lib/rufio/command_mode.rb', line 52

def update_browsing_directory(directory)
  @local_script_scanner.update_directory(directory)
  @rakefile_parser.update_directory(directory)
end