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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/moonshard.rb +22 -24
  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: bb2e17a8f3e7a1f56fe38a9d86b0b501cbd501b8aacff719a971722c81b48a17
4
- data.tar.gz: 6eb0a0623a262704722ab450d4fcc85abd03aeed0618069ffdbb4e75ee784406
3
+ metadata.gz: d6f6a34f5d9df3e406dc7deb5abe08c9b3286d73f5a252096a22f717cb691539
4
+ data.tar.gz: 48376f7947cadd24a7066e23575c449fea4977662a3ba337f0c5ff05f1bda040
5
5
  SHA512:
6
- metadata.gz: fdf59f4c2fdf7ddbb026e4d786935d1b222bf248a925a06a80a6ba9f4d0ac9206ec91222374e5e3af4eaff7d3a11795fcab9cf4cc1d9a9dd923cf9399f21c65c
7
- data.tar.gz: 0a75156a8a73c972c75fca4fef0227f5aad82cfebcf8da8d1ea2ecd3f22cc24a2918d24cca4c3063a07f3c8f3d25316731c03a69145a86ca94edcc15ef0b0079
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 binary so arbitrary files don't cause
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
- # ##{ is ASCII, so this check is safe even for binary data.
66
- next unless content.include?("##{")
65
+ # Only consider files containing the ##{ marker.
66
+ next unless content.include?(PLACEHOLDER)
67
67
 
68
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)
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
- rescue => e
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
- 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
+ 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
- # to every non-empty line.
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "moonshard"
3
- spec.version = "0.2.0"
3
+ spec.version = "0.3.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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy Jeff