Module: CrystalRuby::Compilation

Defined in:
lib/crystalruby/compilation.rb

Defined Under Namespace

Classes: CompilationFailedError

Class Method Summary collapse

Class Method Details

.build_compile_command(verbose:, debug:, lib:, src:) ⇒ String

Builds the command to compile the Crystal source file

Parameters:

  • verbose (Boolean)

    whether to print the compiler output

  • debug (Boolean)

    whether to compile in debug mode

  • lib (String)

    path to the library file

  • src (String)

    path to the source file

Returns:

  • (String)

    the command to compile the Crystal source file



34
35
36
37
38
39
40
# File 'lib/crystalruby/compilation.rb', line 34

def self.build_compile_command(verbose:, debug:, lib:, src:)
  verbose_flag = verbose ? "--verbose --progress" : ""
  debug_flag = debug ? "" : "--release --no-debug"
  redirect_output = " > /dev/null " unless verbose
  lib, src = [lib, src].map(&Shellwords.method(:escape))
  %(crystal build #{verbose_flag} #{debug_flag} --single-module --link-flags "-shared" -o #{lib} #{src}#{redirect_output})
end

.compile!(src:, lib:, verbose: CrystalRuby.config.verbose, debug: CrystalRuby.config.debug) ⇒ void

This method returns an undefined value.

Simple wrapper around invocation of the Crystal compiler

Parameters:

  • src (String)

    path to the source file

  • lib (String)

    path to the library file

  • verbose (Boolean) (defaults to: CrystalRuby.config.verbose)

    whether to print the compiler output

  • debug (Boolean) (defaults to: CrystalRuby.config.debug)

    whether to compile in debug mode

Raises:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/crystalruby/compilation.rb', line 16

def self.compile!(
  src:,
  lib:,
  verbose: CrystalRuby.config.verbose,
  debug: CrystalRuby.config.debug
)
  compile_command = build_compile_command(verbose: verbose, debug: debug, lib: lib, src: src)
  CrystalRuby.log_debug "Compiling Crystal code #{verbose ? ": #{compile_command}" : ""}"
  IO.popen(compile_command, chdir: File.dirname(src), &:read)
  raise CompilationFailedError, "Compilation failed in #{src}" unless $?&.success?
end

.install_shards!(src_dir) ⇒ Object

Trigger the shards install command in the given source directory



43
44
45
46
47
48
# File 'lib/crystalruby/compilation.rb', line 43

def self.install_shards!(src_dir)
  CrystalRuby.log_debug "Running shards install inside #{src_dir}"
  output = IO.popen("shards update", chdir: src_dir, &:read)
  CrystalRuby.log_debug output if CrystalRuby.config.verbose
  raise CompilationFailedError, "Shards install failed" unless $?&.success?
end

.shard_check?(src_dir) ⇒ Boolean

Return whether the shards check command succeeds in the given source directory

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/crystalruby/compilation.rb', line 51

def self.shard_check?(src_dir)
  IO.popen("shards check", chdir: src_dir, &:read)
  $?&.success?
end