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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/moonshard.rb +69 -31
  3. data/moonshard.gemspec +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93fffd8c4d7bb102f655c8062412d7ac5bfd816a5adfd87d2299551ee030db39
4
- data.tar.gz: 4e6e7961be6fc2b9379bdd3b054d3b9b013b0998c857eb1ac203d8ce471c351f
3
+ metadata.gz: bb2e17a8f3e7a1f56fe38a9d86b0b501cbd501b8aacff719a971722c81b48a17
4
+ data.tar.gz: 6eb0a0623a262704722ab450d4fcc85abd03aeed0618069ffdbb4e75ee784406
5
5
  SHA512:
6
- metadata.gz: 54cc25eb6933812d809e0b27948bf3eb9db825f37b947c4320aa0887dde5ac792aee8dacf6f8d6e0b45822581073258b041e14d22e8cf0a3a19d8b7bdef7623f
7
- data.tar.gz: 74c2cf709fc228d89684d97bdfb4ef69848859374f52e9900e9f57279a07f5dcead3aa07d58fa26cd9c1a84bb00ba397db50657ef8955b72dea3203e50d2db56
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.write(file, content)
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
- content = File.read(file)
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
- # Only process files containing ##{...}
64
- next unless content.match?(/##\{[^}]+\}/)
65
+ # ##{ is ASCII, so this check is safe even for binary data.
66
+ next unless content.include?("##{")
65
67
 
66
- # Save original content before replacement
67
- original_files[file] = content
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
- file_dir = File.dirname(file)
78
+ # Save original content before replacement.
79
+ original_files[file] = text
70
80
 
71
- replaced = content.gsub(/^([ \t]*)##\{([^}]+)\}[ \t]*$/) do
72
- indentation = Regexp.last_match(1)
73
- referenced_file = Regexp.last_match(2)
81
+ file_dir = File.dirname(file)
74
82
 
75
- referenced_path = File.expand_path(
76
- referenced_file,
77
- file_dir
78
- )
83
+ replaced = text.gsub(/^([ \t]*)##\{([^}]+)\}[ \t]*$/) do
84
+ indentation = Regexp.last_match(1)
85
+ referenced_file = Regexp.last_match(2)
79
86
 
80
- unless File.file?(referenced_path)
81
- warn "Warning: file not found: #{referenced_path} (referenced from #{file})"
82
- next Regexp.last_match(0)
83
- end
87
+ referenced_path = File.expand_path(
88
+ referenced_file,
89
+ file_dir
90
+ )
84
91
 
85
- inserted_content = File.read(referenced_path).chomp
92
+ unless File.file?(referenced_path)
93
+ warn "Warning: file not found: #{referenced_path} " \
94
+ "(referenced from #{file})"
86
95
 
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
96
+ next Regexp.last_match(0)
94
97
  end
95
- end.join
96
- end
97
98
 
98
- next if replaced == content
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
- File.write(file, replaced)
101
- puts "Updated: #{file}"
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "moonshard"
3
- spec.version = "0.1.0"
3
+ spec.version = "0.2.0"
4
4
  spec.authors = ["Tommy Jeff"]
5
5
  spec.email = ["threcius@gmail.com"]
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moonshard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy Jeff