Class: NeocitiesRed::Services::Site::Differencer
- Inherits:
-
Object
- Object
- NeocitiesRed::Services::Site::Differencer
- Defined in:
- lib/neocities_red/services/site/differencer.rb
Overview
Compares local files against the remote Neocities site.
Identifies three categories of differences:
- Added — files present locally but not on the server
- Modified — files present in both but with different SHA1 hashes
- Removed — files present on the server but not locally
Results are color-coded: green for added, yellow for modified, red for removed.
Instance Method Summary collapse
-
#initialize(client, path: ".", detail: false, ignore_dotfiles: false, exclude: []) ⇒ Differencer
constructor
A new instance of Differencer.
-
#show ⇒ Array(Array<String>, Array<String>, Array<String>)
Computes the diff between local and remote files.
Constructor Details
#initialize(client, path: ".", detail: false, ignore_dotfiles: false, exclude: []) ⇒ Differencer
Returns a new instance of Differencer.
32 33 34 35 36 37 38 39 |
# File 'lib/neocities_red/services/site/differencer.rb', line 32 def initialize(client, path: ".", detail: false, ignore_dotfiles: false, exclude: []) @client = client @path = path @detail = detail || false @ignore_dotfiles = ignore_dotfiles || false @exclude = exclude || [] @pastel = Pastel.new(eachline: "\n") end |
Instance Method Details
#show ⇒ Array(Array<String>, Array<String>, Array<String>)
Computes the diff between local and remote files.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 |
# File 'lib/neocities_red/services/site/differencer.rb', line 46 def show server_files = Services::File::List.new(@client, nil, @detail).show root_path = Pathname(@path) added_paths = [] removed_paths = [] modified_paths = [] Dir.chdir(root_path) do paths = Dir.glob(::File.join("**", "*"), ::File::FNM_DOTMATCH) server_file_entries = server_files.reject { |file| file[:is_directory] } server_paths = server_file_entries.map { |file| file[:path] } server_file_map = server_file_entries.to_h do |file| [file[:path], file[:sha1_hash]] end local_paths = paths if @ignore_dotfiles server_paths = server_paths.reject { |path| path.start_with?(".") } local_paths = local_paths.reject { |path| path.start_with?(".") } end local_files = local_paths.select { |path| ::File.file?(path) }.map do |path| { path: path, sha1_hash: Digest::SHA1.file(path).hexdigest } end if @exclude.any? normalized_exclude = @exclude.map { |path| Pathname.new(path).cleanpath.to_s } server_paths -= normalized_exclude local_paths -= normalized_exclude end removed_paths = server_paths - local_paths removed_paths.map! { |file| @pastel.red(file) } added_paths = local_paths - server_paths added_paths.map! { |file| @pastel.green(file) } modified_paths = local_files.select do |file| server_hash = server_file_map[file[:path]] server_hash && server_hash != file[:sha1_hash] end modified_paths.map! { |file| @pastel.yellow(file[:path]) } end [ added_paths, modified_paths, removed_paths ] end |