Class: PhpPrecompileHook

Inherits:
PhpFileHook
  • Object
show all
Defined in:
lib/precompile_hook.rb

Constant Summary collapse

BATCH_SEPARATOR =
"\n/* ---Mumuki-Batch-Separator--- */\n"
RESULTS_SEPARATOR =
"\n/* ---Mumuki-Results-Separator--- */\n"

Instance Method Summary collapse

Methods inherited from PhpFileHook

#tempfile_extension

Instance Method Details

#add_php_tag(content) ⇒ Object



56
57
58
# File 'lib/precompile_hook.rb', line 56

def add_php_tag(content)
  "<?php\n#{content}"
end

#add_php_tags(files) ⇒ Object



52
53
54
# File 'lib/precompile_hook.rb', line 52

def add_php_tags(files)
  Hash[files.map{|name, content| [name, add_php_tag(content)] } ]
end

#command_line(*filenames) ⇒ Object



5
6
7
# File 'lib/precompile_hook.rb', line 5

def command_line(*filenames)
  "run-tests-and-get-ast #{filenames.join(' ')}"
end

#compile(request) ⇒ Object



9
10
11
12
13
14
# File 'lib/precompile_hook.rb', line 9

def compile(request)
  return request unless request[:query].nil?

  files = super request
  struct request.to_h.merge result: run!(files)
end

#compile_file_content(request) ⇒ Object



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

def compile_file_content(request)
  test_content = compile_test_content request

  if has_files?(request)
    ast_content = request.content.values.join("\n")
    add_php_tags(test_content).merge('submission_ast.json' => add_php_tag(ast_content))
  else
    add_php_tag(test_content) + BATCH_SEPARATOR + add_php_tag(request.content)
  end
end

#compile_test_content(request) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/precompile_hook.rb', line 33

def compile_test_content(request)
  test = <<-EOF
declare(strict_types=1);

#{request.extra}
#{has_files?(request) ? '' : request.content}

use PHPUnit\\Framework\\TestCase;

final class #{PhpTestHook::TEST_NAME}Test extends TestCase {
#{request.test.lines.map {|it| '  ' + it}.join}
}
  EOF

  has_files?(request) ?
    files_of(request).merge("#{PhpTestHook::TEST_NAME.downcase}.php" => test) :
    test
end

#post_process_file(_file, result, status) ⇒ Object



27
28
29
30
31
# File 'lib/precompile_hook.rb', line 27

def post_process_file(_file, result, status)
  parts = result.split RESULTS_SEPARATOR

  { test: parts.first, ast: parts.last }
end