File tree Expand file tree Collapse file tree 5 files changed +110
-3
lines changed Expand file tree Collapse file tree 5 files changed +110
-3
lines changed Original file line number Diff line number Diff line change 22
22
" readlines" ,
23
23
" rmtree" ,
24
24
" rubocop" ,
25
- " Takeshi"
25
+ " Takeshi" ,
26
+ " utime"
26
27
],
27
28
"rbs-helper.rbs-inline-on-save" : true ,
28
29
"rbs-helper.rbs-inline-signature-directory" : " sig/"
Original file line number Diff line number Diff line change 3
3
require "language_server-protocol"
4
4
require "ruby_lsp/addon"
5
5
6
+ require_relative "file_writer"
6
7
require_relative "logger"
7
8
8
9
module RubyLsp
@@ -125,7 +126,7 @@ def generate_path_helpers_signature #: void
125
126
rbs_path . dirname . mkpath
126
127
127
128
sig = ::RbsRails ::PathHelpers . generate
128
- rbs_path . write sig
129
+ FileWriter . new ( rbs_path ) . write sig
129
130
logger . info ( "Updated RBS signature: #{ rbs_path } " )
130
131
end
131
132
@@ -139,7 +140,7 @@ def generate_signature0(klass) #: void
139
140
rbs_path . dirname . mkpath
140
141
141
142
sig = ::RbsRails ::ActiveRecord . class_to_rbs ( klass )
142
- rbs_path . write sig
143
+ FileWriter . new ( rbs_path ) . write sig
143
144
logger . info ( "Updated RBS signature: #{ rbs_path } " )
144
145
end
145
146
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLsp
4
+ module RbsRails
5
+ # To avoid unnecessary type reloading by type checkers and other utilities,
6
+ # FileWriter modifies the target file only if its content has been changed.
7
+ #
8
+ # See https://github.com/pocke/rbs_rails/pull/346
9
+ class FileWriter
10
+ attr_reader :path #: Pathname
11
+
12
+ # @rbs path: Pathname
13
+ def initialize ( path ) #: void
14
+ @path = path
15
+ end
16
+
17
+ def write ( content ) #: void
18
+ original_content = begin
19
+ path . read
20
+ rescue StandardError
21
+ nil
22
+ end
23
+
24
+ return unless original_content != content
25
+
26
+ path . write ( content )
27
+ end
28
+ end
29
+ end
30
+ end
Original file line number Diff line number Diff line change
1
+ # Generated from lib/ruby_lsp/rbs_rails/file_writer.rb with RBS::Inline
2
+
3
+ module RubyLsp
4
+ module RbsRails
5
+ # To avoid unnecessary type reloading by type checkers and other utilities,
6
+ # FileWriter modifies the target file only if its content has been changed.
7
+ #
8
+ # See https://github.com/pocke/rbs_rails/pull/346
9
+ class FileWriter
10
+ attr_reader path: Pathname
11
+
12
+ # @rbs path: Pathname
13
+ def initialize : (Pathname path) -> void
14
+
15
+ def write : (untyped content) -> void
16
+ end
17
+ end
18
+ end
Original file line number Diff line number Diff line change
1
+ RSpec . describe RubyLsp ::RbsRails ::FileWriter do
2
+ describe "#write" do
3
+ subject { file_writer . write ( content ) }
4
+
5
+ after do
6
+ tmpdir . rmtree
7
+ end
8
+
9
+ let ( :file_writer ) { RubyLsp ::RbsRails ::FileWriter . new ( path ) }
10
+ let ( :path ) { tmpdir / "test_file.rbs" }
11
+ let ( :tmpdir ) { Pathname . new ( Dir . mktmpdir ( "file_writer_test" ) ) }
12
+
13
+ context "when the file does not exist" do
14
+ it "creates the file with the given content" do
15
+ expect ( path ) . not_to exist
16
+
17
+ file_writer . write ( "class NewClass; end" )
18
+
19
+ expect ( path ) . to exist
20
+ expect ( path . read ) . to eq ( "class NewClass; end" )
21
+ end
22
+ end
23
+
24
+ context "when the file exists" do
25
+ before do
26
+ path . write ( old_content )
27
+ end
28
+
29
+ let ( :old_content ) { "class ExistingClass; end" }
30
+
31
+ context "when the content is different" do
32
+ it "updates the file with the new content" do
33
+ file_writer . write ( "class NewClass; end" )
34
+
35
+ expect ( path ) . to exist
36
+ expect ( path . read ) . to eq ( "class NewClass; end" )
37
+ end
38
+ end
39
+
40
+ context "when the content is the same" do
41
+ before do
42
+ path . utime ( mtime , mtime )
43
+ end
44
+
45
+ let ( :mtime ) { Time . now - 60 }
46
+
47
+ it "does not modify the file" do
48
+ file_writer . write ( old_content )
49
+
50
+ expect ( path ) . to exist
51
+ expect ( path . read ) . to eq ( old_content )
52
+ expect ( path . mtime ) . to eq ( mtime )
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
You can’t perform that action at this time.
0 commit comments