Class: Lich::Util::Update::CustomRepos

Inherits:
Object
  • Object
show all
Defined in:
documented/common/update/custom_repos.rb

Overview

Manages user-registered custom (3rd-party) script repositories.

Users can register arbitrary GitHub repos and track individual .lic files from them. Custom repo scripts are synced into per-repo subdirectories under SCRIPT_DIR/custom.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allHash

Retrieves all registered custom repositories.

Returns:

  • (Hash)

    a hash of custom repositories, where keys are owner/repo strings



59
60
61
# File 'documented/common/update/custom_repos.rb', line 59

def self.all
  UserVars.custom_repos || {}
end

.build_config(owner_repo, registration) ⇒ Hash

Builds the configuration hash for a custom repository.

Parameters:

  • owner_repo (String)

    the GitHub repository in owner/repo format

  • registration (Hash)

    the registration details for the repository

Returns:

  • (Hash)

    the configuration hash for the custom repository



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'documented/common/update/custom_repos.rb', line 41

def self.build_config(owner_repo, registration)
  branch = registration[:branch] || registration['branch'] || 'main'
  {
    display_name: "Custom: #{owner_repo}",
    api_url: "https://api.github.com/repos/#{owner_repo}/git/trees/#{branch}?recursive=1",
    raw_base_url: "https://raw.githubusercontent.com/#{owner_repo}/#{branch}",
    tracking_mode: :explicit,
    script_pattern: /^[^\/]+\.lic$/,
    game_filter: nil,
    default_tracked: [],
    subdirs: {},
    custom: true,
    dest_dir: dest_dir(owner_repo)
  }
end

.dest_dir(owner_repo) ⇒ String

Constructs the destination directory path for the custom repository.

Parameters:

  • owner_repo (String)

    the GitHub repository in owner/repo format

Returns:

  • (String)

    the full path to the destination directory



33
34
35
# File 'documented/common/update/custom_repos.rb', line 33

def self.dest_dir(owner_repo)
  File.join(SCRIPT_DIR, 'custom', repo_dir_name(owner_repo))
end

.repo_dir_name(owner_repo) ⇒ String

Converts the owner/repo format into a directory-friendly name.

Parameters:

  • owner_repo (String)

    the GitHub repository in owner/repo format

Returns:

  • (String)

    the formatted directory name



26
27
28
# File 'documented/common/update/custom_repos.rb', line 26

def self.repo_dir_name(owner_repo)
  owner_repo.gsub('/', '-')
end

Instance Method Details

#add_custom_repo(owner_repo, branch = nil) ⇒ void

This method returns an undefined value.

Adds a custom repository to the user's registered list.

Parameters:

  • owner_repo (String)

    the GitHub repository in owner/repo format

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

    the branch to track (defaults to 'main')



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'documented/common/update/custom_repos.rb', line 67

def add_custom_repo(owner_repo, branch = nil)
  unless owner_repo =~ %r{^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$}
    StatusReporter.respond_mono("[lich5-update: Invalid format '#{owner_repo}'. Use owner/repo (e.g. MahtraDR/dr-scripts).]")
    return
  end

  UserVars.custom_repos ||= {}
  if UserVars.custom_repos[owner_repo]
    StatusReporter.respond_mono("[lich5-update: '#{owner_repo}' is already registered.]")
    return
  end

  UserVars.custom_repos[owner_repo] = {
    'branch'   => branch || 'main',
    'added_at' => Time.now.strftime('%Y-%m-%d')
  }
  Vars.save
  StatusReporter.respond_mono("[lich5-update: Registered custom repo '#{owner_repo}' (branch: #{branch || 'main'}).]")
end

#list_custom_reposvoid

This method returns an undefined value.

Lists all registered custom repositories in a formatted table.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'documented/common/update/custom_repos.rb', line 114

def list_custom_repos
  repos = self.class.all
  if repos.empty?
    StatusReporter.respond_mono("[lich5-update: No custom repos registered. Use --add-custom=owner/repo to add one.]")
    return
  end

  table_rows = []
  table_rows << ['Repository', 'Branch', 'Added', 'Scripts']
  table_rows << :separator

  repos.each do |owner_repo, reg|
    branch = reg[:branch] || reg['branch'] || 'main'
    added = reg[:added_at] || reg['added_at'] || '?'
    tracked = (UserVars.tracked_scripts&.dig(owner_repo) || []).length
    dest = self.class.dest_dir(owner_repo)
    installed = File.directory?(dest) ? Dir.children(dest).count { |f| f.end_with?('.lic') } : 0
    table_rows << [owner_repo, branch, added, "#{tracked} tracked, #{installed} installed"]
  end

  table = Terminal::Table.new(title: 'Custom Repositories', rows: table_rows)
  StatusReporter.respond_mono(table.to_s)
end

#remove_custom_repo(owner_repo) ⇒ void

This method returns an undefined value.

Removes a custom repository from the user's registered list.

Parameters:

  • owner_repo (String)

    the GitHub repository in owner/repo format



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'documented/common/update/custom_repos.rb', line 90

def remove_custom_repo(owner_repo)
  UserVars.custom_repos ||= {}
  unless UserVars.custom_repos.delete(owner_repo)
    StatusReporter.respond_mono("[lich5-update: '#{owner_repo}' is not a registered custom repo.]")
    return
  end

  # Clean up tracked scripts for this repo
  UserVars.tracked_scripts&.delete(owner_repo)
  Vars.save

  dest = self.class.dest_dir(owner_repo)
  if File.directory?(dest)
    files = Dir.children(dest).select { |f| f.end_with?('.lic') }
    if files.any?
      StatusReporter.respond_mono("[lich5-update: Note: #{files.length} script(s) still installed in #{dest}. Delete manually if no longer needed.]")
    end
  end

  StatusReporter.respond_mono("[lich5-update: Removed custom repo '#{owner_repo}'.]")
end