Class: Kward::StarterPackInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/starter_pack_installer.rb

Overview

Installs Kward's starter prompt/instruction files into the user config dir.

Defined Under Namespace

Classes: Result

Constant Summary collapse

VERSION =
"v1.0.0"
ARCHIVE_URL =
"https://codeload.github.com/kaiwood/kward-starter-pack/tar.gz/refs/tags/#{VERSION}".freeze
ALLOWED_FILES =
["AGENTS.md"].freeze
ALLOWED_PREFIXES =
["prompts/", "skills/"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_dir: ConfigFiles.config_dir, archive_url: ARCHIVE_URL, downloader: nil) ⇒ StarterPackInstaller

Returns a new instance of StarterPackInstaller.



23
24
25
26
27
# File 'lib/kward/starter_pack_installer.rb', line 23

def initialize(config_dir: ConfigFiles.config_dir, archive_url: ARCHIVE_URL, downloader: nil)
  @config_dir = File.expand_path(config_dir)
  @archive_url = archive_url
  @downloader = downloader || method(:download)
end

Class Method Details

.install(**kwargs) ⇒ Object



19
20
21
# File 'lib/kward/starter_pack_installer.rb', line 19

def self.install(**kwargs)
  new(**kwargs).install
end

Instance Method Details

#installObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kward/starter_pack_installer.rb', line 29

def install
  archive = @downloader.call(@archive_url)
  installed = []
  skipped = []

  Dir.mktmpdir("kward-starter-pack") do |dir|
    files = extract_allowed_files(archive, dir)
    files.each do |relative_path, source_path|
      destination = destination_path(relative_path)
      if File.exist?(destination)
        skipped << relative_path
        next
      end

      FileUtils.mkdir_p(File.dirname(destination), mode: 0o700)
      File.open(destination, File::WRONLY | File::CREAT | File::EXCL, 0o600) do |file|
        file.write(File.binread(source_path))
      end
      installed << relative_path
    end
  end

  Result.new(installed: installed, skipped: skipped)
end