moonshard 0.1.0 → 0.2.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 +69 -31
- 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: bb2e17a8f3e7a1f56fe38a9d86b0b501cbd501b8aacff719a971722c81b48a17
|
|
4
|
+
data.tar.gz: 6eb0a0623a262704722ab450d4fcc85abd03aeed0618069ffdbb4e75ee784406
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fdf59f4c2fdf7ddbb026e4d786935d1b222bf248a925a06a80a6ba9f4d0ac9206ec91222374e5e3af4eaff7d3a11795fcab9cf4cc1d9a9dd923cf9399f21c65c
|
|
7
|
+
data.tar.gz: 0a75156a8a73c972c75fca4fef0227f5aad82cfebcf8da8d1ea2ecd3f22cc24a2918d24cca4c3063a07f3c8f3d25316731c03a69145a86ca94edcc15ef0b0079
|
data/lib/moonshard.rb
CHANGED
|
@@ -29,7 +29,7 @@ module Moonshard
|
|
|
29
29
|
|
|
30
30
|
original_files.each do |file, content|
|
|
31
31
|
begin
|
|
32
|
-
File.
|
|
32
|
+
File.binwrite(file, content)
|
|
33
33
|
puts "Restored: #{file}"
|
|
34
34
|
rescue => e
|
|
35
35
|
warn "Warning: cannot restore #{file}: #{e.message}"
|
|
@@ -54,51 +54,89 @@ module Moonshard
|
|
|
54
54
|
next if File.expand_path(file) == backup_file
|
|
55
55
|
|
|
56
56
|
begin
|
|
57
|
-
|
|
57
|
+
# Read as binary so arbitrary files don't cause
|
|
58
|
+
# invalid UTF-8 errors.
|
|
59
|
+
content = File.binread(file)
|
|
58
60
|
rescue => e
|
|
59
61
|
warn "Warning: cannot read #{file}: #{e.message}"
|
|
60
62
|
next
|
|
61
63
|
end
|
|
62
64
|
|
|
63
|
-
#
|
|
64
|
-
next unless content.
|
|
65
|
+
# ##{ is ASCII, so this check is safe even for binary data.
|
|
66
|
+
next unless content.include?("##{")
|
|
65
67
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
begin
|
|
69
|
+
# The replacement syntax is text-based.
|
|
70
|
+
# Skip files that aren't valid UTF-8.
|
|
71
|
+
text = content.force_encoding(Encoding::UTF_8)
|
|
72
|
+
|
|
73
|
+
unless text.valid_encoding?
|
|
74
|
+
warn "Skipping binary/non-UTF-8 file: #{file}"
|
|
75
|
+
next
|
|
76
|
+
end
|
|
68
77
|
|
|
69
|
-
|
|
78
|
+
# Save original content before replacement.
|
|
79
|
+
original_files[file] = text
|
|
70
80
|
|
|
71
|
-
|
|
72
|
-
indentation = Regexp.last_match(1)
|
|
73
|
-
referenced_file = Regexp.last_match(2)
|
|
81
|
+
file_dir = File.dirname(file)
|
|
74
82
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
)
|
|
83
|
+
replaced = text.gsub(/^([ \t]*)##\{([^}]+)\}[ \t]*$/) do
|
|
84
|
+
indentation = Regexp.last_match(1)
|
|
85
|
+
referenced_file = Regexp.last_match(2)
|
|
79
86
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
87
|
+
referenced_path = File.expand_path(
|
|
88
|
+
referenced_file,
|
|
89
|
+
file_dir
|
|
90
|
+
)
|
|
84
91
|
|
|
85
|
-
|
|
92
|
+
unless File.file?(referenced_path)
|
|
93
|
+
warn "Warning: file not found: #{referenced_path} " \
|
|
94
|
+
"(referenced from #{file})"
|
|
86
95
|
|
|
87
|
-
|
|
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
|
|
96
|
+
next Regexp.last_match(0)
|
|
94
97
|
end
|
|
95
|
-
end.join
|
|
96
|
-
end
|
|
97
98
|
|
|
98
|
-
|
|
99
|
+
begin
|
|
100
|
+
inserted_content = File.binread(referenced_path)
|
|
101
|
+
rescue => e
|
|
102
|
+
warn "Warning: cannot read #{referenced_path}: #{e.message}"
|
|
103
|
+
next Regexp.last_match(0)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
inserted_content = inserted_content.force_encoding(
|
|
107
|
+
Encoding::UTF_8
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
unless inserted_content.valid_encoding?
|
|
111
|
+
warn "Warning: referenced file is not valid UTF-8: " \
|
|
112
|
+
"#{referenced_path}"
|
|
99
113
|
|
|
100
|
-
|
|
101
|
-
|
|
114
|
+
next Regexp.last_match(0)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
inserted_content = inserted_content.chomp
|
|
118
|
+
|
|
119
|
+
# Apply the indentation from ##{...}
|
|
120
|
+
# to every non-empty line.
|
|
121
|
+
inserted_content.lines.map do |line|
|
|
122
|
+
if line.strip.empty?
|
|
123
|
+
line
|
|
124
|
+
else
|
|
125
|
+
indentation + line
|
|
126
|
+
end
|
|
127
|
+
end.join
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
next if replaced == text
|
|
131
|
+
|
|
132
|
+
File.binwrite(file, replaced)
|
|
133
|
+
|
|
134
|
+
puts "Updated: #{file}"
|
|
135
|
+
rescue ArgumentError => e
|
|
136
|
+
warn "Warning: cannot process #{file}: #{e.message}"
|
|
137
|
+
rescue => e
|
|
138
|
+
warn "Warning: cannot process #{file}: #{e.message}"
|
|
139
|
+
end
|
|
102
140
|
end
|
|
103
141
|
|
|
104
142
|
File.write(
|
data/moonshard.gemspec
CHANGED