Class: Shai::InstalledProjects

Inherits:
Object
  • Object
show all
Defined in:
lib/shai/installed_projects.rb

Constant Summary collapse

FILENAME =
".shai-installed"
CURRENT_VERSION =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path) ⇒ InstalledProjects

Returns a new instance of InstalledProjects.



13
14
15
16
# File 'lib/shai/installed_projects.rb', line 13

def initialize(base_path)
  @base_path = File.expand_path(base_path)
  @data = load_data
end

Instance Attribute Details

#base_path ⇒ Object (readonly)

Returns the value of attribute base_path.



11
12
13
# File 'lib/shai/installed_projects.rb', line 11

def base_path
  @base_path
end

Instance Method Details

#add_project(slug, files) ⇒ Object

Add a new project with its files



78
79
80
81
82
83
84
# File 'lib/shai/installed_projects.rb', line 78

def add_project(slug, files)
  @data["projects"][slug] = {
    "installed_at" => Time.now.iso8601,
    "files" => files.sort
  }
  save!
end

#all_installed_files ⇒ Object



54
55
56
# File 'lib/shai/installed_projects.rb', line 54

def all_installed_files
  projects.values.flat_map { |p| p["files"] || [] }
end

#delete! ⇒ Object



114
115
116
# File 'lib/shai/installed_projects.rb', line 114

def delete!
  File.delete(file_path) if exists?
end

#empty? ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/shai/installed_projects.rb', line 34

def empty?
  projects.empty?
end

#exists? ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/shai/installed_projects.rb', line 22

def exists?
  File.exist?(file_path)
end

#file_path ⇒ Object



18
19
20
# File 'lib/shai/installed_projects.rb', line 18

def file_path
  File.join(base_path, FILENAME)
end

#files_for_project(slug) ⇒ Object



50
51
52
# File 'lib/shai/installed_projects.rb', line 50

def files_for_project(slug)
  projects.dig(slug, "files") || []
end

#find_conflicts(new_files) ⇒ Object

Check for conflicts between new files and already installed projects Returns hash of { file_path => owning_project_slug }



68
69
70
71
72
73
74
75
# File 'lib/shai/installed_projects.rb', line 68

def find_conflicts(new_files)
  conflicts = {}
  new_files.each do |file|
    owner = project_for_file(file)
    conflicts[file] = owner if owner
  end
  conflicts
end

#get_project(slug) ⇒ Object



46
47
48
# File 'lib/shai/installed_projects.rb', line 46

def get_project(slug)
  projects[slug]
end

#has_project?(slug) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/shai/installed_projects.rb', line 42

def has_project?(slug)
  projects.key?(slug)
end

#project_count ⇒ Object



38
39
40
# File 'lib/shai/installed_projects.rb', line 38

def project_count
  projects.size
end

#project_for_file(file_path) ⇒ Object

Find which project owns a specific file



59
60
61
62
63
64
# File 'lib/shai/installed_projects.rb', line 59

def project_for_file(file_path)
  projects.each do |slug, data|
    return slug if (data["files"] || []).include?(file_path)
  end
  nil
end

#project_slugs ⇒ Object



30
31
32
# File 'lib/shai/installed_projects.rb', line 30

def project_slugs
  projects.keys
end

#projects ⇒ Object



26
27
28
# File 'lib/shai/installed_projects.rb', line 26

def projects
  @data["projects"] || {}
end

#remove_files_from_project(slug, files_to_remove) ⇒ Object

Remove specific files from a project (when being overwritten)



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/shai/installed_projects.rb', line 94

def remove_files_from_project(slug, files_to_remove)
  return unless @data["projects"][slug]

  current_files = @data["projects"][slug]["files"] || []
  @data["projects"][slug]["files"] = current_files - files_to_remove

  # If no files left, remove the project entirely
  if @data["projects"][slug]["files"].empty?
    @data["projects"].delete(slug)
  end

  save!
end

#remove_project(slug) ⇒ Object

Remove a project and return its files



87
88
89
90
91
# File 'lib/shai/installed_projects.rb', line 87

def remove_project(slug)
  project = @data["projects"].delete(slug)
  save! if project
  project&.dig("files") || []
end

#save! ⇒ Object



108
109
110
111
112
# File 'lib/shai/installed_projects.rb', line 108

def save!
  content = YAML.dump(@data)
  header = "# Installed by shai - do not edit manually\n"
  File.write(file_path, header + content)
end