Class: RubyNext::Commands::Nextify

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby-next/commands/nextify.rb

Defined Under Namespace

Classes: Stats

Instance Attribute Summary collapse

Attributes inherited from Base

#dry_run

Instance Method Summary collapse

Methods inherited from Base

#base_parser, #initialize, #log, run

Constructor Details

This class inherits a constructor from RubyNext::Commands::Base

Instance Attribute Details

#lib_pathObject (readonly)

Returns the value of attribute lib_path.



45
46
47
# File 'lib/ruby-next/commands/nextify.rb', line 45

def lib_path
  @lib_path
end

#min_versionObject (readonly)

Returns the value of attribute min_version.



45
46
47
# File 'lib/ruby-next/commands/nextify.rb', line 45

def min_version
  @min_version
end

#out_pathObject (readonly)

Returns the value of attribute out_path.



45
46
47
# File 'lib/ruby-next/commands/nextify.rb', line 45

def out_path
  @out_path
end

#overwriteObject (readonly) Also known as: overwrite?

Returns the value of attribute overwrite.



45
46
47
# File 'lib/ruby-next/commands/nextify.rb', line 45

def overwrite
  @overwrite
end

#pathsObject (readonly)

Returns the value of attribute paths.



45
46
47
# File 'lib/ruby-next/commands/nextify.rb', line 45

def paths
  @paths
end

#single_versionObject (readonly)

Returns the value of attribute single_version.



45
46
47
# File 'lib/ruby-next/commands/nextify.rb', line 45

def single_version
  @single_version
end

#specified_rewritersObject (readonly)

Returns the value of attribute specified_rewriters.



45
46
47
# File 'lib/ruby-next/commands/nextify.rb', line 45

def specified_rewriters
  @specified_rewriters
end

#statsObject (readonly)

Returns the value of attribute stats.



47
48
49
# File 'lib/ruby-next/commands/nextify.rb', line 47

def stats
  @stats
end

Instance Method Details

#parse!(args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ruby-next/commands/nextify.rb', line 71

def parse!(args)
  print_help = false
  print_rewriters = false
  rewriter_names = []
  custom_rewriters = []
  @single_version = false
  @overwrite = false

  optparser = base_parser do |opts|
    opts.banner = "Usage: ruby-next nextify DIRECTORY_OR_FILE [options]"

    opts.on("-o", "--output=OUTPUT", "Specify output directory or file or stdout") do |val|
      @out_path = val
    end

    opts.on("--min-version=VERSION", "Specify the minimum Ruby version to support") do |val|
      @min_version = Gem::Version.new(val)
    end

    opts.on("--single-version", "Only create one version of a file (for the earliest Ruby version)") do
      @single_version = true
    end

    opts.on("--overwrite", "Overwrite original file") do
      @overwrite = true
    end

    opts.on("--edge", "Enable edge (master) Ruby features") do |val|
      ENV["RUBY_NEXT_EDGE"] = val.to_s
      require "ruby-next/language/rewriters/edge"
    end

    opts.on("--proposed", "Enable proposed/experimental Ruby features") do |val|
      ENV["RUBY_NEXT_PROPOSED"] = val.to_s
      require "ruby-next/language/rewriters/proposed"
    end

    opts.on(
      "--transpile-mode=MODE",
      "Transpiler mode (ast or rewrite). Default: rewrite"
    ) do |val|
      Language.mode = val.to_sym
    end

    opts.on("--[no-]refine", "Do not inject `using RubyNext`") do |val|
      Core.strategy = :core_ext unless val
    end

    opts.on("--list-rewriters", "List available rewriters") do |val|
      print_rewriters = true
    end

    opts.on("--rewrite=REWRITERS...", "Specify particular Ruby features to rewrite") do |val|
      rewriter_names << val
    end

    opts.on("--import-rewriter=REWRITERS...", "Specify paths to load custom rewritiers") do |val|
      custom_rewriters << val
    end

    opts.on("-h", "--help", "Print help") do
      print_help = true
    end
  end

  optparser.parse!(args)

  @lib_path = args[0]

  if print_help
    $stdout.puts optparser.help
    exit 0
  end

  # Load custom rewriters
  custom_rewriters.each do |path|
    Kernel.load path
  end

  if print_rewriters
    Language.rewriters.each do |rewriter|
      $stdout.puts "#{rewriter::NAME} (\"#{rewriter::SYNTAX_PROBE}\")#{rewriter.unsupported_syntax? ? " (unsupported)" : ""}"
    end
    exit 0
  end

  unless lib_path&.then(&File.method(:exist?))
    $stdout.puts "Path not found: #{lib_path}"
    $stdout.puts optparser.help
    exit 2
  end

  if rewriter_names.any? && min_version
    $stdout.puts "--rewrite cannot be used with --min-version simultaneously"
    exit 2
  end

  @specified_rewriters =
    if rewriter_names.any?
      begin
        Language.select_rewriters(*rewriter_names)
      rescue Language::RewriterNotFoundError => error
        $stdout.puts error.message
        $stdout.puts "Try --list-rewriters to see list of available rewriters"
        exit 2
      end
    end

  if overwrite? && !single_version?
    $stdout.puts "--overwrite only works with --single-version or explcit rewritires specified (via --rewrite)"
    exit 2
  end

  @paths =
    if File.directory?(lib_path)
      Dir[File.join(lib_path, "**/*.rb")]
    elsif File.file?(lib_path)
      [lib_path].tap do |_|
        @lib_path = File.dirname(lib_path)
      end
    end
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby-next/commands/nextify.rb', line 49

def run
  @stats = Stats.new

  log "RubyNext core strategy: #{RubyNext::Core.strategy}"
  log "RubyNext transpile mode: #{RubyNext::Language.mode}"

  remove_rbnext!

  @min_version ||= MIN_SUPPORTED_VERSION

  paths.each do |path|
    stats.file!

    contents = File.read(path)
    transpile path, contents
  end

  ensure_rbnext!

  log stats.report
end