Skip to content

Commit

Permalink
port_ruby_to_crystal' Initial commit. Date: 2022-11-02 18:53:05'
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Nov 2, 2022
0 parents commit 36c0ded
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 0 deletions.
Empty file added .gitignore
Empty file.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
a Regex script for port ruby code to crystal, reduce friction.

Include only the easiest, most obvious, and safest parts.

only change the most easy part with minimum effort.

with minimal effort and cost.

**Note:** Run this code at your own risk.

# How to use it

Entrying the projet folder, you should have renamed the extension .rb file
to .cr for those in-place files.

Before this step, it's a good idea to use rubocop to make some style changes.
e.g. change all single-quoted strings to double-quoted strings.

```sh
$: ./port_ruby_to_crystal
```

# Run test

```sh
╰─ $ ./test.sh
Running ruby test.rb successful.
Running crystal run test.cr successful.
```
31 changes: 31 additions & 0 deletions port_ruby_to_crystal
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/env ruby

def replace(code)
code.gsub!(/\bFile\.exist\?(\s*)/, 'File.exists?\1')
code.gsub!(/\bFile\.readlines\b/, 'File.read_lines')
code.gsub!(/\bYAML\.load\b/, 'YAML.parse')
code.gsub!(/require (?:'|")fileutils(?:'|")(\s*)/, 'require "file_utils"\1')
code.gsub!(/\.respond_to\?(\s*)/, '.responds_to?\1')
code.gsub!(/\brequire_relative\b/, 'require')
code.gsub!(/\.include\?(\s*)/, '.includes?\1')
code.gsub!(/\.end_with\?(\s*)/, '.ends_with?\1')
code.gsub!(/\.start_with\?(\s*)/, '.starts_with?\1')
code.gsub!(/(\w+\[.*?\]) \|\|/, '\1? ||')
code.gsub!(/\battr_accessor\b/, 'property')
code.gsub!(/\battr_reader\b/, 'getter')
code.gsub!(/\battr_writer\b/, 'setter')
code.gsub!("__dir__", '__DIR__')
code.gsub!(/(\()?(&):(\w+)(\))?/, ' \2.\3')
end

if ARGV[0] == '--test'
code = File.read('test/test.rb')
replace(code)
File.write('test/test.cr', code)
else
Dir.glob("**/*.cr").each do |cr_file|
code = File.read(cr_file)
replace(code)
File.write(cr_file, code)
end
end
8 changes: 8 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env sh

set -e

ruby test/test.rb && echo "Running ruby test.rb successful."
chmod +x ./port_ruby_to_crystal
./port_ruby_to_crystal --test
crystal run test/test.cr && echo "Running crystal run test.cr successful."
Empty file added test/empty.cr
Empty file.
Empty file added test/empty.rb
Empty file.
27 changes: 27 additions & 0 deletions test/test.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
File.exists?("ruby")
"hello".responds_to? :size
require "./empty"

arr = {"world" => 100}
arr["hello"]? || "world"

["hello", "world"].includes? "hello"

class Test
property :test1
getter :test2
setter :test3
end

__DIR__

["a", "b"].map &.upcase
["a", "b"].map &.upcase

"hello".ends_with? "lo"
"hello".starts_with? "he"

File.read_lines("./test/test.rb")

require "file_utils"
require "file_utils"
27 changes: 27 additions & 0 deletions test/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
File.exist?("ruby")
"hello".respond_to? :size
require_relative "./empty"

arr = {"world" => 100}
arr["hello"] || "world"

["hello", "world"].include? "hello"

class Test
attr_accessor :test1
attr_reader :test2
attr_writer :test3
end

__dir__

["a", "b"].map(&:upcase)
["a", "b"].map &:upcase

"hello".end_with? "lo"
"hello".start_with? "he"

File.readlines("./test/test.rb")

require 'fileutils'
require "fileutils"

0 comments on commit 36c0ded

Please sign in to comment.