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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/moonshard.rb +32 -68
  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: d6f6a34f5d9df3e406dc7deb5abe08c9b3286d73f5a252096a22f717cb691539
4
- data.tar.gz: 48376f7947cadd24a7066e23575c449fea4977662a3ba337f0c5ff05f1bda040
3
+ metadata.gz: d18c56c8d60d654600cf9681405d9abf4420e430c98eaff816861c7eadece4ae
4
+ data.tar.gz: 3da4545e60a4fdefd3a2be759eadacae9d9fa6e415eba6f899ed69472709bf1e
5
5
  SHA512:
6
- metadata.gz: a5047124bee729f517f154d913281e551bc4b15bbf7e024a35de3c83c748c38bcec2f2e2391c4a58cee1d3fb2bf7ad35c9bee3c2ee34fc4e18e9c9bd10285983
7
- data.tar.gz: c5af09fde7932cd6d6f2e838741f1f8f07117fe56f5e0eb294eb8889ea04b080ddb544be59f4fc1bfe29ff23b4a9984198c567b0c11721ffd2b34e66fff2266f
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.binwrite(file, content)
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
- # Read as raw bytes so binary files don't cause UTF-8 errors.
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 consider files containing the ##{ marker.
66
- next unless content.include?(PLACEHOLDER)
63
+ # Only process files containing ##{...}
64
+ next unless content.match?(/##\{[^}]+\}/)
67
65
 
68
- begin
69
- text = content.dup.force_encoding(Encoding::UTF_8)
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
- next Regexp.last_match(0)
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
- unless inserted_content.valid_encoding?
100
- warn "Warning: referenced file is not valid UTF-8: " \
101
- "#{referenced_path}"
71
+ replaced = content.gsub(/^([ \t]*)##\{([^}]+)\}[ \t]*$/) do
72
+ indentation = Regexp.last_match(1)
73
+ referenced_file = Regexp.last_match(2)
102
74
 
103
- next Regexp.last_match(0)
104
- end
105
- rescue => e
106
- warn "Warning: cannot read #{referenced_path}: #{e.message}"
107
- next Regexp.last_match(0)
108
- end
75
+ referenced_path = File.expand_path(
76
+ referenced_file,
77
+ file_dir
78
+ )
109
79
 
110
- # Remove the final newline because the placeholder line
111
- # already provides the newline.
112
- inserted_content = inserted_content.chomp
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
- next if replaced == text
85
+ inserted_content = File.read(referenced_path).chomp
126
86
 
127
- # Save the original content before writing.
128
- original_files[file] = text
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
- File.binwrite(file, replaced)
98
+ next if replaced == content
131
99
 
132
- puts "Updated: #{file}"
133
- rescue ArgumentError => e
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "moonshard"
3
- spec.version = "0.3.0"
3
+ spec.version = "0.4.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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy Jeff