Class: IRuby::Application

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/iruby/application.rb

Defined Under Namespace

Classes: RegisterParams, UnregisterParams

Constant Summary collapse

DEFAULT_KERNEL_NAME =
"ruby#{ruby_version_info[0]}".freeze
DEFAULT_DISPLAY_NAME =
"Ruby #{ruby_version_info[0]} (iruby kernel)"

Instance Method Summary collapse

Instance Method Details

#__system__Object



200
# File 'lib/iruby/application.rb', line 200

alias __system__ system

#exec_jupyter(sub_cmd, argv) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/iruby/application.rb', line 339

def exec_jupyter(sub_cmd, argv)
  opts = OptionParser.new
  opts.banner = "Usage: #{$PROGRAM_NAME} unregister [options]"

  kernel_name = resolve_installed_kernel_name(DEFAULT_KERNEL_NAME)
  opts.on(
    "--kernel=NAME", String,
    "The name of the default kernel to start."
  ) {|v| kernel_name = v }

  opts.order!(argv)

  opts = ["--kernel=#{kernel_name}"]
  exec("jupyter", "console", *opts)
end

#register_kernel(argv) ⇒ Object



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
# File 'lib/iruby/application.rb', line 139

def register_kernel(argv)
  params = parse_register_command_line(argv)

  if params.name != DEFAULT_KERNEL_NAME
    # `--name` is specified and `--display-name` is not
    # default `params.display_name` to `params.name`
    params.display_name ||= params.name
  end

  check_and_warn_kernel_in_default_ipython_directory(params)

  if installed_kernel_exist?(params.name, params.ipython_dir)
    unless params.force
      $stderr.puts "IRuby kernel named `#{params.name}` already exists!"
      $stderr.puts "Use --force to force register the new kernel."
      exit 1
    end
  end

  Dir.mktmpdir("iruby_kernel") do |tmpdir|
    path = File.join(tmpdir, DEFAULT_KERNEL_NAME)
    FileUtils.mkdir_p(path)

    # Stage assets
    assets_dir = File.expand_path("../assets", __FILE__)
    FileUtils.cp_r(Dir.glob(File.join(assets_dir, "*")), path)

    kernel_dict = {
      "argv" => make_iruby_cmd(),
      "display_name" => params.display_name || DEFAULT_DISPLAY_NAME,
      "language" => "ruby",
      "metadata" => {"debugger": false}
    }

    # TODO: Support params.profile
    # TODO: Support params.env

    kernel_content = JSON.pretty_generate(kernel_dict)
    File.write(File.join(path, "kernel.json"), kernel_content)

    args = ["--name=#{params.name}"]
    args << "--user" if params.user
    args << path

    # TODO: Support params.prefix
    # TODO: Support params.sys_prefix

    system("jupyter", "kernelspec", "install", *args)
  end
end

#runObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/iruby/application.rb', line 100

def run
  case @sub_cmd
  when :register
    register_kernel(@sub_argv)
  when :unregister
    unregister_kernel(@sub_argv)
  when :console
    exec_jupyter(@sub_cmd.to_s, @sub_argv)
  when :kernel
    @sub_app = KernelApplication.new(@sub_argv)
    @sub_app.run
  else
    raise "[IRuby][BUG] Unknown subcommand: #{@sub_cmd}; this must be treated in parse_command_line."
  end
end

#setup(argv = nil) ⇒ Object

Set the application instance up.



15
16
17
18
# File 'lib/iruby/application.rb', line 15

def setup(argv=nil)
  @iruby_executable = File.expand_path($PROGRAM_NAME)
  parse_command_line(argv)
end

#unregister_kernel(argv) ⇒ Object



301
302
303
304
305
306
307
# File 'lib/iruby/application.rb', line 301

def unregister_kernel(argv)
  params = parse_unregister_command_line(argv)
  opts = []
  opts << "-y" if params.yes
  opts << "-f" if params.force
  system("jupyter", "kernelspec", "uninstall", *opts, *params.names)
end