Module: Moonshard

Defined in:
lib/moonshard.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

DEFAULT_BACKUP_FILE =
"original_files.json"

Class Method Summary collapse

Class Method Details

.replace(root_dir) ⇒ Object



44
45
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
103
104
105
106
107
108
109
110
111
112
# File 'lib/moonshard.rb', line 44

def self.replace(root_dir)
  root_dir = File.expand_path(root_dir)
  backup_file = File.expand_path(DEFAULT_BACKUP_FILE)

  original_files = {}

  Dir.glob(File.join(root_dir, "**", "*")).each do |file|
    next unless File.file?(file)

    # Don't process the backup JSON itself
    next if File.expand_path(file) == backup_file

    begin
      content = File.read(file)
    rescue => e
      warn "Warning: cannot read #{file}: #{e.message}"
      next
    end

    # Only process files containing ##{...}
    next unless content.match?(/##\{[^}]+\}/)

    # Save original content before replacement
    original_files[file] = content

    file_dir = File.dirname(file)

    replaced = content.gsub(/^([ \t]*)##\{([^}]+)\}[ \t]*$/) do
      indentation = Regexp.last_match(1)
      referenced_file = Regexp.last_match(2)

      referenced_path = File.expand_path(
        referenced_file,
        file_dir
      )

      unless File.file?(referenced_path)
        warn "Warning: file not found: #{referenced_path} (referenced from #{file})"
        next Regexp.last_match(0)
      end

      inserted_content = File.read(referenced_path).chomp

      # Apply the indentation of ##{...}
      # to every non-empty line.
      inserted_content.lines.map do |line|
        if line.strip.empty?
          line
        else
          indentation + line
        end
      end.join
    end

    next if replaced == content

    File.write(file, replaced)
    puts "Updated: #{file}"
  end

  File.write(
    backup_file,
    JSON.pretty_generate(original_files)
  )

  puts "Backup saved to: #{backup_file}"

  true
end

.restore(backup_file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/moonshard.rb', line 17

def self.restore(backup_file)
  unless File.file?(backup_file)
    warn "Error: backup file not found: #{backup_file}"
    return false
  end

  begin
    original_files = JSON.parse(File.read(backup_file))
  rescue JSON::ParserError => e
    warn "Error: invalid JSON file: #{e.message}"
    return false
  end

  original_files.each do |file, content|
    begin
      File.write(file, content)
      puts "Restored: #{file}"
    rescue => e
      warn "Warning: cannot restore #{file}: #{e.message}"
    end
  end

  puts "Restore completed."

  true
end

.run(args) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/moonshard.rb', line 9

def self.run(args)
  if args[0] == "-r"
    restore(args[1] || DEFAULT_BACKUP_FILE)
  else
    replace(args[0] || ".")
  end
end