moonshard 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/moonshard.rb +32 -68
- data/moonshard.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d18c56c8d60d654600cf9681405d9abf4420e430c98eaff816861c7eadece4ae
|
|
4
|
+
data.tar.gz: 3da4545e60a4fdefd3a2be759eadacae9d9fa6e415eba6f899ed69472709bf1e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8eafa7bd31a734ba77b4a5b1d3fe46c268c3eef1b188c23f0b748ca3e9da923b57e4e721869b31b46b25ce79628ec8faa15532d2938621819324dcff29185be9
|
|
7
|
+
data.tar.gz: 949cde414a606d182d34955bf719621684f4e00b44cb066e343a23d9d48d63d7870b5aa071a696adcb5dbe76378ebcb220450cc7c52c0679ef16425de114cfeb
|
data/lib/moonshard.rb
CHANGED
|
@@ -2,7 +2,6 @@ require "json"
|
|
|
2
2
|
|
|
3
3
|
module Moonshard
|
|
4
4
|
DEFAULT_BACKUP_FILE = "original_files.json"
|
|
5
|
-
PLACEHOLDER = '##{'
|
|
6
5
|
|
|
7
6
|
class Error < StandardError
|
|
8
7
|
end
|
|
@@ -30,7 +29,7 @@ module Moonshard
|
|
|
30
29
|
|
|
31
30
|
original_files.each do |file, content|
|
|
32
31
|
begin
|
|
33
|
-
File.
|
|
32
|
+
File.write(file, content)
|
|
34
33
|
puts "Restored: #{file}"
|
|
35
34
|
rescue => e
|
|
36
35
|
warn "Warning: cannot restore #{file}: #{e.message}"
|
|
@@ -51,90 +50,55 @@ module Moonshard
|
|
|
51
50
|
Dir.glob(File.join(root_dir, "**", "*")).each do |file|
|
|
52
51
|
next unless File.file?(file)
|
|
53
52
|
|
|
54
|
-
# Don't process the backup JSON itself
|
|
53
|
+
# Don't process the backup JSON itself
|
|
55
54
|
next if File.expand_path(file) == backup_file
|
|
56
55
|
|
|
57
56
|
begin
|
|
58
|
-
|
|
59
|
-
content = File.binread(file)
|
|
57
|
+
content = File.read(file)
|
|
60
58
|
rescue => e
|
|
61
59
|
warn "Warning: cannot read #{file}: #{e.message}"
|
|
62
60
|
next
|
|
63
61
|
end
|
|
64
62
|
|
|
65
|
-
# Only
|
|
66
|
-
next unless content.
|
|
63
|
+
# Only process files containing ##{...}
|
|
64
|
+
next unless content.match?(/##\{[^}]+\}/)
|
|
67
65
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
# Skip binary / invalid UTF-8 files.
|
|
72
|
-
unless text.valid_encoding?
|
|
73
|
-
warn "Skipping binary/non-UTF-8 file: #{file}"
|
|
74
|
-
next
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
file_dir = File.dirname(file)
|
|
78
|
-
|
|
79
|
-
replaced = text.gsub(/^([ \t]*)##\{([^}]+)\}[ \t]*$/) do
|
|
80
|
-
indentation = Regexp.last_match(1)
|
|
81
|
-
referenced_file = Regexp.last_match(2)
|
|
82
|
-
|
|
83
|
-
referenced_path = File.expand_path(
|
|
84
|
-
referenced_file,
|
|
85
|
-
file_dir
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
unless File.file?(referenced_path)
|
|
89
|
-
warn "Warning: file not found: #{referenced_path} " \
|
|
90
|
-
"(referenced from #{file})"
|
|
66
|
+
# Save original content before replacement
|
|
67
|
+
original_files[file] = content
|
|
91
68
|
|
|
92
|
-
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
begin
|
|
96
|
-
inserted_content = File.binread(referenced_path)
|
|
97
|
-
inserted_content.force_encoding(Encoding::UTF_8)
|
|
69
|
+
file_dir = File.dirname(file)
|
|
98
70
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
71
|
+
replaced = content.gsub(/^([ \t]*)##\{([^}]+)\}[ \t]*$/) do
|
|
72
|
+
indentation = Regexp.last_match(1)
|
|
73
|
+
referenced_file = Regexp.last_match(2)
|
|
102
74
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
next Regexp.last_match(0)
|
|
108
|
-
end
|
|
75
|
+
referenced_path = File.expand_path(
|
|
76
|
+
referenced_file,
|
|
77
|
+
file_dir
|
|
78
|
+
)
|
|
109
79
|
|
|
110
|
-
|
|
111
|
-
#
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
# Apply the indentation from the ##{...} line to
|
|
115
|
-
# every non-empty line of the referenced file.
|
|
116
|
-
inserted_content.lines.map do |line|
|
|
117
|
-
if line.strip.empty?
|
|
118
|
-
line
|
|
119
|
-
else
|
|
120
|
-
indentation + line
|
|
121
|
-
end
|
|
122
|
-
end.join
|
|
80
|
+
unless File.file?(referenced_path)
|
|
81
|
+
warn "Warning: file not found: #{referenced_path} (referenced from #{file})"
|
|
82
|
+
next Regexp.last_match(0)
|
|
123
83
|
end
|
|
124
84
|
|
|
125
|
-
|
|
85
|
+
inserted_content = File.read(referenced_path).chomp
|
|
126
86
|
|
|
127
|
-
#
|
|
128
|
-
|
|
87
|
+
# Apply the indentation of ##{...}
|
|
88
|
+
# to every non-empty line.
|
|
89
|
+
inserted_content.lines.map do |line|
|
|
90
|
+
if line.strip.empty?
|
|
91
|
+
line
|
|
92
|
+
else
|
|
93
|
+
indentation + line
|
|
94
|
+
end
|
|
95
|
+
end.join
|
|
96
|
+
end
|
|
129
97
|
|
|
130
|
-
|
|
98
|
+
next if replaced == content
|
|
131
99
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
warn "Warning: cannot process #{file}: #{e.message}"
|
|
135
|
-
rescue => e
|
|
136
|
-
warn "Warning: cannot process #{file}: #{e.message}"
|
|
137
|
-
end
|
|
100
|
+
File.write(file, replaced)
|
|
101
|
+
puts "Updated: #{file}"
|
|
138
102
|
end
|
|
139
103
|
|
|
140
104
|
File.write(
|
data/moonshard.gemspec
CHANGED