ruby_crystal_codemod 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7986ec4b8202ba075699a41427f7fbcdd98b468dc08d03cdce440b9677479520
4
- data.tar.gz: 10353a8c14d346e7dec11f6848b88ab3463f93ddb203283c245f47338676e53f
3
+ metadata.gz: 5bdee5c536c92fee24314a9164c4284bf95434d0420aef21fb43cc16a838bce3
4
+ data.tar.gz: f408e7e716bc30bbec7fc22af88755f44831f3e01bf1e1fd314485b08706d491
5
5
  SHA512:
6
- metadata.gz: 101ae069a8d1f6805ef80c7d5140240eb968f8e4633ef8e1ee361bf3cbb4c89c1ea695b4a3b4b524e0c579014f79747a28c86d0cc2f428bff017407e4634b504
7
- data.tar.gz: e5578aefa628eabe37f6cf9ffc2445571bec4466031435ea97c45166cdbac3a06ae6d41651bcc459f8f5f569f28f2b9cd31a054d62c43186ff036939c8534a21
6
+ metadata.gz: 1271dce196b637376838ab7a498cddf460ebcc4282b631dcb3c0b745ed7fc712424d17d6c38e7968fc65bad7b0b35d566c839f6c9cb882e11719fcbc4ed882fb
7
+ data.tar.gz: dbf1c4d4b43162a6f93391aeb7d3d63a84c1566ba9d5b203374359b4967b69a7d0bc8e421df896b8ebc723d0460a99531519a06cf79109f03d6be0954f8873e9
@@ -33,15 +33,6 @@
33
33
  name: Bundle Install
34
34
  command: bundle check || bundle install
35
35
 
36
- # Restore crystal cache
37
- - restore_cache:
38
- keys:
39
- - crystal-shards-{{ checksum "util/post_process_crystal/shard.lock" }}
40
-
41
- - run:
42
- name: Compile ./util/post_process tool
43
- command: ./bin/compile_post_process
44
-
45
36
  - run:
46
37
  name: Run rspec
47
38
  command: |
@@ -58,13 +49,6 @@
58
49
  command: |
59
50
  bundle exec rufo -c lib/ spec/lib/
60
51
 
61
- # Store crystal cache
62
- - save_cache:
63
- key: crystal-shards-{{ checksum "util/post_process_crystal/shard.lock" }}
64
- paths:
65
- - util/post_process_crystal/lib/
66
- - util/post_process_crystal/shard.lock
67
-
68
52
  # Store bundle cache
69
53
  - save_cache:
70
54
  key: bundler-packages-v2-{{ checksum "ruby-version-for-ci.txt" }}-{{ checksum "ruby_crystal_codemod.gemspec" }}
data/README.md CHANGED
@@ -8,7 +8,12 @@ This project is a fork of [Rufo](https://github.com/ruby-formatter/rufo). (Rufo
8
8
 
9
9
  The formatting rules have been modified in an attempt to produce some semi-valid Crystal code. Then you need to add some type annotations and fix any other issues manually. See the [Crystal for Rubyists](https://github.com/crystal-lang/crystal/wiki/Crystal-for-Rubyists) wiki page to learn more about the syntax differences.
10
10
 
11
- > Ruby => Crystal Codemod / Rufo supports all Ruby versions >= 2.4.**5**, due to a bug in Ruby's Ripper parser.
11
+ > Ruby => Crystal Codemod / Rufo supports all Ruby versions >= 2.4.5, due to a bug in Ruby's Ripper parser.
12
+
13
+ ## Requirements
14
+
15
+ * Ruby >= `2.4.5`
16
+ * [Crystal](https://crystal-lang.org/install/) >= `0.31.1`
12
17
 
13
18
  ## Installation
14
19
 
@@ -45,8 +50,6 @@ When transpiling a Ruby file into Crystal, the transpiler will remove all of the
45
50
 
46
51
  The `BEGIN` / `END` comments can start with either `#~#` or `# ~#`. (Code formatters / linters often enforce a space after the `#` character for comments.)
47
52
 
48
- > This comment post-processing step is done by a Crystal program in `./util/post_process_crystal`. Run `./bin/compile_post_process` to compile the binary at `./util/post_process`.
49
-
50
53
  For example, here's how you can define a class that works for both Ruby and Crystal:
51
54
  (Crystal requires type annotations here.)
52
55
 
@@ -17,3 +17,4 @@ require_relative "ruby_crystal_codemod/settings"
17
17
  require_relative "ruby_crystal_codemod/formatter"
18
18
  require_relative "ruby_crystal_codemod/version"
19
19
  require_relative "ruby_crystal_codemod/file_finder"
20
+ require_relative "ruby_crystal_codemod/post_process_crystal"
@@ -20,6 +20,7 @@ class RubyCrystalCodemod::Command
20
20
  @dot_file = RubyCrystalCodemod::DotFile.new
21
21
  @squiggly_warning_files = []
22
22
  @logger = RubyCrystalCodemod::Logger.new(loglevel)
23
+ @post_processor = PostProcessCrystal.new
23
24
  end
24
25
 
25
26
  def exit_code(status_code)
@@ -113,18 +114,24 @@ class RubyCrystalCodemod::Command
113
114
  logger.log("Format: #{filename} => #{crystal_filename}")
114
115
  end
115
116
 
116
- # Run the post-processing command to handle BEGIN and END comments for Ruby / Crystal.
117
- post_process_cmd = File.expand_path(File.join(__dir__, "../../util/post_process"))
118
- unless File.exist?(post_process_cmd)
119
- raise "Please run ./bin/compile_post_process to compile the post-processing command " \
120
- "at: #{post_process_cmd}"
121
- end
122
- stdout, stderr, status = Open3.capture3(post_process_cmd, crystal_filename)
123
- unless status.success?
124
- warn "'./util/post_process' failed with status: #{status.exitstatus}\n\n" \
125
- "stdout: #{stdout}\n\n" \
126
- "stderr: #{stderr}"
127
- end
117
+ # Run the post-processing step to remove Ruby code and uncomment Crystal.
118
+ @post_processor.filename = crystal_filename
119
+ @post_processor.post_process_crystal
120
+ File.write(crystal_filename, @post_processor.contents)
121
+
122
+ # UPDATE: It's difficult to package/compile the Crystal binary in a Ruby gem.
123
+ # But it was really easy to port it to Ruby!
124
+ # post_process_cmd = File.expand_path(File.join(__dir__, "../../util/post_process"))
125
+ # unless File.exist?(post_process_cmd)
126
+ # raise "Please run ./bin/compile_post_process to compile the post-processing command " \
127
+ # "at: #{post_process_cmd}"
128
+ # end
129
+ # stdout, stderr, status = Open3.capture3(post_process_cmd, crystal_filename)
130
+ # unless status.success?
131
+ # warn "'./util/post_process' failed with status: #{status.exitstatus}\n\n" \
132
+ # "stdout: #{stdout}\n\n" \
133
+ # "stderr: #{stderr}"
134
+ # end
128
135
 
129
136
  # Format the Crystal file with the Crystal code formatter
130
137
  stdout, stderr, status = Open3.capture3("crystal", "tool", "format", crystal_filename)
@@ -0,0 +1,46 @@
1
+ class PostProcessCrystal
2
+ attr_reader :filename, :contents
3
+
4
+ def self.file_read_lines(path)
5
+ File.read(path).lines.map(&:chomp)
6
+ end
7
+
8
+ def initialize(filename = "")
9
+ @filename = filename
10
+ @contents = ""
11
+ end
12
+
13
+ def filename=(filename)
14
+ @filename = filename
15
+ @contents = ""
16
+ end
17
+
18
+ def post_process_crystal
19
+ @contents = +""
20
+ lines = self.class.file_read_lines(@filename)
21
+
22
+ current_lang = nil
23
+ regex = /^\s*# ?~# (?<action>(BEGIN|END)) (?<lang>(ruby|crystal))/
24
+ uncomment_regex = /^(?<indent>\s*)# ?/
25
+ lines.each do |line|
26
+ matches = regex.match(line)
27
+ if matches
28
+ case matches["action"]
29
+ when "BEGIN"
30
+ current_lang = matches["lang"]
31
+ when "END"
32
+ current_lang = nil
33
+ end
34
+ next
35
+ end
36
+ case current_lang
37
+ when "ruby"
38
+ next
39
+ when "crystal"
40
+ line = line.sub(uncomment_regex, "\\k<indent>")
41
+ end
42
+
43
+ @contents << line << "\n"
44
+ end
45
+ end
46
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyCrystalCodemod
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_crystal_codemod
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ary Borenszweig
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2019-12-06 00:00:00.000000000 Z
12
+ date: 2019-12-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_print
@@ -170,6 +170,7 @@ files:
170
170
  - lib/ruby_crystal_codemod/file_finder.rb
171
171
  - lib/ruby_crystal_codemod/formatter.rb
172
172
  - lib/ruby_crystal_codemod/logger.rb
173
+ - lib/ruby_crystal_codemod/post_process_crystal.rb
173
174
  - lib/ruby_crystal_codemod/settings.rb
174
175
  - lib/ruby_crystal_codemod/version.rb
175
176
  - rakelib/ruby_crystal_codemod.rake