Class: NeocitiesRed::Services::Site::Differencer

Inherits:
Object
  • Object
show all
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.

Examples:

differencer = NeocitiesRed::Services::Site::Differencer.new(
  client, path: ".", detail: false, ignore_dotfiles: false, exclude: []
)
added, modified, removed = differencer.show

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(client, path: ".", detail: false, ignore_dotfiles: false, exclude: []) ⇒ Differencer

Returns a new instance of Differencer.

Parameters:

  • client (NeocitiesRed::Client)

    authenticated API client

  • path (String) (defaults to: ".")

    local directory path to compare (default: ".")

  • detail (Boolean) (defaults to: false)

    whether to show detailed output (default: false)

  • ignore_dotfiles (Boolean) (defaults to: false)

    when true, ignores files starting with "."

  • exclude (Array<String>) (defaults to: [])

    local paths to exclude from comparison



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

#showArray(Array<String>, Array<String>, Array<String>)

Computes the diff between local and remote files.

Returns:

  • (Array(Array<String>, Array<String>, Array<String>))

    a tuple of [added, modified, removed], where each element is an array of color-coded file path strings



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