Module: Clacky::Utils::ScriptsManager

Defined in:
lib/clacky/utils/scripts_manager.rb

Overview

Manages user-space shell scripts in ~/.clacky/scripts/.

On first use, bundled scripts are copied from the gem’s scripts/ directory into ~/.clacky/scripts/. The user-space copy is always used so users can customise scripts without modifying the gem.

Bundled scripts are re-copied when the gem is upgraded (detected via gem version stamp in ~/.clacky/scripts/.version).

Constant Summary collapse

SCRIPTS_DIR =
File.expand_path("~/.clacky/scripts").freeze
DEFAULT_SCRIPTS_DIR =
File.expand_path("../../../scripts", __dir__).freeze
VERSION_FILE =
File.join(SCRIPTS_DIR, ".version").freeze
SCRIPTS =
%w[
  install_browser.sh
  install_system_deps.sh
  install_rails_deps.sh
].freeze

Class Method Summary collapse

Class Method Details

.path_for(name) ⇒ String?

Returns the full path to a managed script.

Parameters:

  • name (String)

    script filename, e.g. “install_browser.sh”

Returns:

  • (String, nil)


53
54
55
56
# File 'lib/clacky/utils/scripts_manager.rb', line 53

def self.path_for(name)
  dest = File.join(SCRIPTS_DIR, name)
  File.exist?(dest) ? dest : nil
end

.setup!Object

Copy bundled scripts to ~/.clacky/scripts/ if missing or outdated. Called once at agent startup — fast (no-op after first run).



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/clacky/utils/scripts_manager.rb', line 28

def self.setup!
  FileUtils.mkdir_p(SCRIPTS_DIR)

  current_version = Clacky::VERSION
  stored_version  = File.exist?(VERSION_FILE) ? File.read(VERSION_FILE).strip : nil

  SCRIPTS.each do |script|
    dest = File.join(SCRIPTS_DIR, script)
    src  = File.join(DEFAULT_SCRIPTS_DIR, script)
    next unless File.exist?(src)

    # Copy if missing or gem was upgraded
    if !File.exist?(dest) || stored_version != current_version
      FileUtils.cp(src, dest)
      FileUtils.chmod(0o755, dest)
    end
  end

  # Write version stamp after successful copy
  File.write(VERSION_FILE, current_version)
end