Skip to content

Commit 28b350a

Browse files
committed
[GR-14868] Remove launcher-only options.
PullRequest: truffleruby/761
2 parents 80897a0 + 16b5f13 commit 28b350a

File tree

16 files changed

+99
-218
lines changed

16 files changed

+99
-218
lines changed

spec/mspec/lib/mspec/runner/object.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class Object
1111
MSpec.describe mod, msg, &block
1212
end
1313

14-
private def it(msg, &block)
15-
MSpec.current.it msg, &block
14+
private def it(desc, &block)
15+
MSpec.current.it desc, &block
1616
end
1717

1818
private def it_should_behave_like(desc)

spec/tags/truffle/launcher_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,4 @@ fails:The launcher 'gem' runs as an -S command
106106
fails:The launcher 'gem' in `RbConfig::CONFIG['bindir']` directory runs
107107
fails:The launcher 'gem' in `RbConfig::CONFIG['extra_bindirs'][0]` directory runs
108108
fails:The launcher 'gem' in `RbConfig::CONFIG['extra_bindirs'][1]` directory runs
109+
slow:The launcher prints the version with --version

spec/truffle/launcher_spec.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,22 @@ def should_print_full_java_command(options, env: {})
272272
end
273273
end
274274

275-
it "understands ruby polyglot options" do
276-
out = ruby_exe(nil, options: "--ruby.show_version=true --ruby.to_execute=p:b --ruby.execution_action=INLINE")
275+
it "prints the version with --version" do
276+
out = ruby_exe(nil, options: "--version")
277277
$?.success?.should == true
278278
out.should include(RUBY_DESCRIPTION)
279-
out.should include(':b')
279+
end
280+
281+
it "understands ruby polyglot options" do
282+
out = ruby_exe("p Truffle::Boot.get_option('rubygems')", options: "--ruby.rubygems=false")
283+
$?.success?.should == true
284+
out.should include('false')
280285
end
281286

282287
it "understands ruby polyglot options without ruby. prefix" do
283-
out = ruby_exe(nil, options: "--show_version=true --to_execute=p:b --execution_action=INLINE")
288+
out = ruby_exe("p Truffle::Boot.get_option('rubygems')", options: "--rubygems=false")
284289
$?.success?.should == true
285-
out.should include(RUBY_DESCRIPTION)
286-
out.should include(':b')
290+
out.should include('false')
287291
end
288292

289293
it "does not print a Java backtrace for an -S file that's not found" do

src/shared/java/org/truffleruby/shared/options/CommandLineOptions.java renamed to src/launcher/java/org/truffleruby/launcher/CommandLineOptions.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,30 @@
2626
* the provisions above, a recipient may use your version of this file under
2727
* the terms of any one of the EPL, the GPL or the LGPL.
2828
***** END LICENSE BLOCK *****/
29-
package org.truffleruby.shared.options;
29+
package org.truffleruby.launcher;
3030

3131
import org.graalvm.options.OptionDescriptor;
3232
import org.graalvm.options.OptionType;
33+
import org.truffleruby.shared.options.RubyOptionTypes;
34+
import org.truffleruby.shared.options.StringArrayOptionType;
3335

3436
import java.util.ArrayList;
3537
import java.util.List;
3638
import java.util.Map;
3739

3840
public class CommandLineOptions {
3941

42+
// Options which are only meaningful when using the TruffleRuby launcher (not when using the Context API)
43+
boolean showVersion = false;
44+
boolean showCopyright = false;
45+
ShowHelp showHelp = ShowHelp.NONE;
46+
/** What should be done after context is created */
47+
ExecutionAction executionAction = ExecutionAction.NONE;
48+
/** What should be done when no action is set */
49+
DefaultExecutionAction defaultExecutionAction = DefaultExecutionAction.IRB;
50+
/** A thing to be executed: a file, inline script, etc. Used by executionAction when applicable. */
51+
String toExecute = "";
52+
4053
private Map<String, String> options;
4154
private String[] arguments;
4255
private final List<String> unknownArguments;

src/launcher/java/org/truffleruby/launcher/CommandLineParser.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@
3535
package org.truffleruby.launcher;
3636

3737
import org.graalvm.options.OptionDescriptor;
38-
import org.truffleruby.shared.options.CommandLineOptions;
39-
import org.truffleruby.shared.options.DefaultExecutionAction;
40-
import org.truffleruby.shared.options.ExecutionAction;
4138
import org.truffleruby.shared.options.OptionsCatalog;
42-
import org.truffleruby.shared.options.ShowHelp;
4339
import org.truffleruby.shared.options.Verbosity;
4440

4541
import java.io.File;
@@ -92,11 +88,11 @@ public void processArguments() throws CommandLineException {
9288
}
9389
assert lastInterpreterArgumentIndex >= 0;
9490

95-
if (config.getOption(OptionsCatalog.EXECUTION_ACTION) == ExecutionAction.UNSET) {
91+
if (config.executionAction == ExecutionAction.UNSET) {
9692
if (argumentIndex < arguments.size()) {
97-
config.setOption(OptionsCatalog.EXECUTION_ACTION, ExecutionAction.FILE);
93+
config.executionAction = ExecutionAction.FILE;
9894
//consume the file name
99-
config.setOption(OptionsCatalog.TO_EXECUTE, getCurrentArgument());
95+
config.toExecute = getCurrentArgument();
10096
argumentIndex++;
10197
}
10298
}
@@ -165,8 +161,8 @@ private void processArgument() throws CommandLineException {
165161
// sole "-" means read from stdin and pass remaining args as ARGV
166162
lastInterpreterArgumentIndex = argumentIndex;
167163

168-
if (config.getOption(OptionsCatalog.EXECUTION_ACTION) == ExecutionAction.UNSET) {
169-
config.setOption(OptionsCatalog.EXECUTION_ACTION, ExecutionAction.STDIN);
164+
if (config.executionAction == ExecutionAction.UNSET) {
165+
config.executionAction = ExecutionAction.STDIN;
170166
} else {
171167
// if other action is set then ignore '-', just threat it as a first script argument,
172168
// and stop option parsing
@@ -220,10 +216,10 @@ private void processArgument() throws CommandLineException {
220216
disallowedInRubyOpts(argument);
221217
final String nextArgument = grabValue(getArgumentError(" -e must be followed by an expression to report"));
222218

223-
final ExecutionAction currentExecutionAction = config.getOption(OptionsCatalog.EXECUTION_ACTION);
219+
final ExecutionAction currentExecutionAction = config.executionAction;
224220
if (currentExecutionAction == ExecutionAction.UNSET || currentExecutionAction == ExecutionAction.INLINE) {
225-
config.setOption(OptionsCatalog.EXECUTION_ACTION, ExecutionAction.INLINE);
226-
config.appendOptionValue(OptionsCatalog.TO_EXECUTE, nextArgument + "\n");
221+
config.executionAction = ExecutionAction.INLINE;
222+
config.toExecute += nextArgument + "\n";
227223
} else {
228224
// ignore option
229225
}
@@ -237,9 +233,9 @@ private void processArgument() throws CommandLineException {
237233
throw notImplemented("-F");
238234
case 'h':
239235
disallowedInRubyOpts(argument);
240-
config.setOption(OptionsCatalog.SHOW_HELP, ShowHelp.SHORT);
236+
config.showHelp = ShowHelp.SHORT;
241237
// cancel other execution actions
242-
config.setOption(OptionsCatalog.EXECUTION_ACTION, ExecutionAction.NONE);
238+
config.executionAction = ExecutionAction.NONE;
243239
break;
244240
case 'i':
245241
disallowedInRubyOpts(argument);
@@ -321,9 +317,9 @@ private void processArgument() throws CommandLineException {
321317

322318
String scriptName = grabValue("provide a bin script to execute");
323319

324-
if (config.getOption(OptionsCatalog.EXECUTION_ACTION) == ExecutionAction.UNSET) {
325-
config.setOption(OptionsCatalog.EXECUTION_ACTION, ExecutionAction.PATH);
326-
config.setOption(OptionsCatalog.TO_EXECUTE, scriptName);
320+
if (config.executionAction == ExecutionAction.UNSET) {
321+
config.executionAction = ExecutionAction.PATH;
322+
config.toExecute = scriptName;
327323
} else {
328324
// ignore the option
329325
}
@@ -336,8 +332,8 @@ private void processArgument() throws CommandLineException {
336332
break;
337333
case 'v':
338334
config.setOption(OptionsCatalog.VERBOSITY, Verbosity.TRUE);
339-
config.setOption(OptionsCatalog.SHOW_VERSION, true);
340-
config.setOption(OptionsCatalog.DEFAULT_EXECUTION_ACTION, DefaultExecutionAction.NONE);
335+
config.showVersion = true;
336+
config.defaultExecutionAction = DefaultExecutionAction.NONE;
341337
break;
342338
case 'w':
343339
config.setOption(OptionsCatalog.VERBOSITY, Verbosity.TRUE);
@@ -375,9 +371,9 @@ private void processArgument() throws CommandLineException {
375371
case '-':
376372
if (argument.equals("--copyright")) {
377373
disallowedInRubyOpts(argument);
378-
config.setOption(OptionsCatalog.SHOW_COPYRIGHT, true);
374+
config.showCopyright = true;
379375
// cancel other execution actions
380-
config.setOption(OptionsCatalog.EXECUTION_ACTION, ExecutionAction.NONE);
376+
config.executionAction = ExecutionAction.NONE;
381377
break FOR;
382378
} else if (argument.startsWith("--encoding")) {
383379
if (argument.equals("--encoding")) {
@@ -413,9 +409,9 @@ private void processArgument() throws CommandLineException {
413409
break FOR;
414410
} else if (argument.equals("--version")) {
415411
disallowedInRubyOpts(argument);
416-
config.setOption(OptionsCatalog.SHOW_VERSION, true);
412+
config.showVersion = true;
417413
// cancel other execution actions
418-
config.setOption(OptionsCatalog.EXECUTION_ACTION, ExecutionAction.NONE);
414+
config.executionAction = ExecutionAction.NONE;
419415
break FOR;
420416
} else if (argument.equals("--debug-frozen-string-literal")) {
421417
warnInternalDebugTool(argument);

src/shared/java/org/truffleruby/shared/options/DefaultExecutionAction.java renamed to src/launcher/java/org/truffleruby/launcher/DefaultExecutionAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* GNU General Public License version 2, or
88
* GNU Lesser General Public License version 2.1.
99
*/
10-
package org.truffleruby.shared.options;
10+
package org.truffleruby.launcher;
1111

1212
public enum DefaultExecutionAction {
1313
NONE,

src/shared/java/org/truffleruby/shared/options/ExecutionAction.java renamed to src/launcher/java/org/truffleruby/launcher/ExecutionAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* GNU General Public License version 2, or
88
* GNU Lesser General Public License version 2.1.
99
*/
10-
package org.truffleruby.shared.options;
10+
package org.truffleruby.launcher;
1111

1212
public enum ExecutionAction {
1313
UNSET,

src/launcher/java/org/truffleruby/launcher/RubyLauncher.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@
1616
import org.graalvm.polyglot.Engine;
1717
import org.graalvm.polyglot.PolyglotException;
1818
import org.graalvm.polyglot.Source;
19-
import org.truffleruby.shared.options.CommandLineOptions;
20-
import org.truffleruby.shared.options.DefaultExecutionAction;
21-
import org.truffleruby.shared.options.ExecutionAction;
2219
import org.truffleruby.shared.options.OptionsCatalog;
2320
import org.truffleruby.shared.TruffleRuby;
2421
import org.truffleruby.shared.Metrics;
25-
import org.truffleruby.shared.options.ShowHelp;
2622

27-
import java.io.IOException;
2823
import java.io.PrintStream;
2924
import java.lang.reflect.Method;
3025
import java.util.ArrayList;
@@ -68,7 +63,7 @@ protected List<String> preprocessArguments(List<String> args, Map<String, String
6863
config = new CommandLineOptions(polyglotOptions);
6964

7065
try {
71-
config.setOption(OptionsCatalog.EXECUTION_ACTION, ExecutionAction.UNSET);
66+
config.executionAction = ExecutionAction.UNSET;
7267

7368
final CommandLineParser argumentCommandLineParser = new CommandLineParser(args, config, true, false);
7469
argumentCommandLineParser.processArguments();
@@ -187,28 +182,48 @@ protected AbortException abortUnrecognizedArgument(String argument) {
187182
}
188183

189184
private int runRubyMain(Context.Builder contextBuilder, CommandLineOptions config) {
190-
if (config.getOption(OptionsCatalog.EXECUTION_ACTION) == ExecutionAction.NONE) {
191-
return 0;
185+
if (config.executionAction == ExecutionAction.UNSET) {
186+
switch (config.defaultExecutionAction) {
187+
case NONE:
188+
return 0;
189+
case STDIN:
190+
config.executionAction = ExecutionAction.STDIN;
191+
break;
192+
case IRB:
193+
config.executionAction = ExecutionAction.PATH;
194+
if (System.console() != null) {
195+
System.err.println("[ruby] WARNING: truffleruby starts IRB when stdin is a TTY instead of reading from stdin, use '-' to read from stdin");
196+
config.executionAction = ExecutionAction.PATH;
197+
config.toExecute = "irb";
198+
} else {
199+
config.executionAction = ExecutionAction.STDIN;
200+
}
201+
break;
202+
}
192203
}
193204

194-
if (config.getOption(OptionsCatalog.EXECUTION_ACTION) == ExecutionAction.UNSET &&
195-
config.getOption(OptionsCatalog.DEFAULT_EXECUTION_ACTION) == DefaultExecutionAction.NONE) {
205+
if (config.executionAction == ExecutionAction.NONE) {
196206
return 0;
197207
}
198208

199209
try (Context context = createContext(contextBuilder, config)) {
200210
Metrics.printTime("before-run");
201-
final Source source;
202-
try {
203-
source = Source.newBuilder(
204-
TruffleRuby.LANGUAGE_ID,
211+
212+
if (config.executionAction == ExecutionAction.PATH) {
213+
String path = context.eval(TruffleRuby.LANGUAGE_ID,
205214
// language=ruby
206-
"Truffle::Boot.main",
207-
TruffleRuby.BOOT_SOURCE_NAME).internal(true).build();
208-
} catch (IOException e) {
209-
throw new RuntimeException(e);
215+
"-> name { Truffle::Boot.find_s_file(name) }").execute(config.toExecute).asString();
216+
config.executionAction = ExecutionAction.FILE;
217+
config.toExecute = path;
210218
}
211-
final int exitCode = context.eval(source).asInt();
219+
220+
final Source source = Source.newBuilder(TruffleRuby.LANGUAGE_ID,
221+
// language=ruby
222+
"-> kind, to_execute { Truffle::Boot.main(kind, to_execute) }",
223+
TruffleRuby.BOOT_SOURCE_NAME).internal(true).buildLiteral();
224+
225+
final String kind = config.executionAction.name();
226+
final int exitCode = context.eval(source).execute(kind, config.toExecute).asInt();
212227
Metrics.printTime("after-run");
213228
return exitCode;
214229
} catch (PolyglotException e) {
@@ -271,15 +286,15 @@ private String setRubyLauncher() {
271286
}
272287

273288
private static void printPreRunInformation(CommandLineOptions config) {
274-
if ((boolean) config.getOption(OptionsCatalog.SHOW_VERSION)) {
289+
if (config.showVersion) {
275290
System.out.println(TruffleRuby.getVersionString(getImplementationNameFromEngine(), isAOT()));
276291
}
277292

278-
if ((boolean) config.getOption(OptionsCatalog.SHOW_COPYRIGHT)) {
293+
if (config.showCopyright) {
279294
System.out.println(TruffleRuby.RUBY_COPYRIGHT);
280295
}
281296

282-
switch ((ShowHelp) config.getOption(OptionsCatalog.SHOW_HELP)) {
297+
switch (config.showHelp) {
283298
case NONE:
284299
break;
285300
case SHORT:

src/shared/java/org/truffleruby/shared/options/ShowHelp.java renamed to src/launcher/java/org/truffleruby/launcher/ShowHelp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* GNU General Public License version 2, or
88
* GNU Lesser General Public License version 2.1.
99
*/
10-
package org.truffleruby.shared.options;
10+
package org.truffleruby.launcher;
1111

1212
public enum ShowHelp {
1313
NONE,

0 commit comments

Comments
 (0)