moonshard 0.2.0 → 0.3.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 +22 -24
- 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: d6f6a34f5d9df3e406dc7deb5abe08c9b3286d73f5a252096a22f717cb691539
|
|
4
|
+
data.tar.gz: 48376f7947cadd24a7066e23575c449fea4977662a3ba337f0c5ff05f1bda040
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a5047124bee729f517f154d913281e551bc4b15bbf7e024a35de3c83c748c38bcec2f2e2391c4a58cee1d3fb2bf7ad35c9bee3c2ee34fc4e18e9c9bd10285983
|
|
7
|
+
data.tar.gz: c5af09fde7932cd6d6f2e838741f1f8f07117fe56f5e0eb294eb8889ea04b080ddb544be59f4fc1bfe29ff23b4a9984198c567b0c11721ffd2b34e66fff2266f
|
data/lib/moonshard.rb
CHANGED
|
@@ -2,6 +2,7 @@ require "json"
|
|
|
2
2
|
|
|
3
3
|
module Moonshard
|
|
4
4
|
DEFAULT_BACKUP_FILE = "original_files.json"
|
|
5
|
+
PLACEHOLDER = '##{'
|
|
5
6
|
|
|
6
7
|
class Error < StandardError
|
|
7
8
|
end
|
|
@@ -50,34 +51,29 @@ module Moonshard
|
|
|
50
51
|
Dir.glob(File.join(root_dir, "**", "*")).each do |file|
|
|
51
52
|
next unless File.file?(file)
|
|
52
53
|
|
|
53
|
-
# Don't process the backup JSON itself
|
|
54
|
+
# Don't process the backup JSON itself.
|
|
54
55
|
next if File.expand_path(file) == backup_file
|
|
55
56
|
|
|
56
57
|
begin
|
|
57
|
-
# Read as
|
|
58
|
-
# invalid UTF-8 errors.
|
|
58
|
+
# Read as raw bytes so binary files don't cause UTF-8 errors.
|
|
59
59
|
content = File.binread(file)
|
|
60
60
|
rescue => e
|
|
61
61
|
warn "Warning: cannot read #{file}: #{e.message}"
|
|
62
62
|
next
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
#
|
|
66
|
-
next unless content.include?(
|
|
65
|
+
# Only consider files containing the ##{ marker.
|
|
66
|
+
next unless content.include?(PLACEHOLDER)
|
|
67
67
|
|
|
68
68
|
begin
|
|
69
|
-
|
|
70
|
-
# Skip files that aren't valid UTF-8.
|
|
71
|
-
text = content.force_encoding(Encoding::UTF_8)
|
|
69
|
+
text = content.dup.force_encoding(Encoding::UTF_8)
|
|
72
70
|
|
|
71
|
+
# Skip binary / invalid UTF-8 files.
|
|
73
72
|
unless text.valid_encoding?
|
|
74
73
|
warn "Skipping binary/non-UTF-8 file: #{file}"
|
|
75
74
|
next
|
|
76
75
|
end
|
|
77
76
|
|
|
78
|
-
# Save original content before replacement.
|
|
79
|
-
original_files[file] = text
|
|
80
|
-
|
|
81
77
|
file_dir = File.dirname(file)
|
|
82
78
|
|
|
83
79
|
replaced = text.gsub(/^([ \t]*)##\{([^}]+)\}[ \t]*$/) do
|
|
@@ -98,26 +94,25 @@ module Moonshard
|
|
|
98
94
|
|
|
99
95
|
begin
|
|
100
96
|
inserted_content = File.binread(referenced_path)
|
|
101
|
-
|
|
102
|
-
warn "Warning: cannot read #{referenced_path}: #{e.message}"
|
|
103
|
-
next Regexp.last_match(0)
|
|
104
|
-
end
|
|
97
|
+
inserted_content.force_encoding(Encoding::UTF_8)
|
|
105
98
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
unless inserted_content.valid_encoding?
|
|
111
|
-
warn "Warning: referenced file is not valid UTF-8: " \
|
|
112
|
-
"#{referenced_path}"
|
|
99
|
+
unless inserted_content.valid_encoding?
|
|
100
|
+
warn "Warning: referenced file is not valid UTF-8: " \
|
|
101
|
+
"#{referenced_path}"
|
|
113
102
|
|
|
103
|
+
next Regexp.last_match(0)
|
|
104
|
+
end
|
|
105
|
+
rescue => e
|
|
106
|
+
warn "Warning: cannot read #{referenced_path}: #{e.message}"
|
|
114
107
|
next Regexp.last_match(0)
|
|
115
108
|
end
|
|
116
109
|
|
|
110
|
+
# Remove the final newline because the placeholder line
|
|
111
|
+
# already provides the newline.
|
|
117
112
|
inserted_content = inserted_content.chomp
|
|
118
113
|
|
|
119
|
-
# Apply the indentation from ##{...}
|
|
120
|
-
#
|
|
114
|
+
# Apply the indentation from the ##{...} line to
|
|
115
|
+
# every non-empty line of the referenced file.
|
|
121
116
|
inserted_content.lines.map do |line|
|
|
122
117
|
if line.strip.empty?
|
|
123
118
|
line
|
|
@@ -129,6 +124,9 @@ module Moonshard
|
|
|
129
124
|
|
|
130
125
|
next if replaced == text
|
|
131
126
|
|
|
127
|
+
# Save the original content before writing.
|
|
128
|
+
original_files[file] = text
|
|
129
|
+
|
|
132
130
|
File.binwrite(file, replaced)
|
|
133
131
|
|
|
134
132
|
puts "Updated: #{file}"
|
data/moonshard.gemspec
CHANGED