Class: GameMachine::Codeblocks::Compiler

Inherits:
Object
  • Object
show all
Defined in:
server/lib/game_machine/codeblocks/compiler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, code) ⇒ Compiler


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 29

def initialize(username,code)
  @username = username
  @classname = extract_classname(code)
  if classname.nil?
    raise "classname is nil"
  end

  package_part = 'A' + Digest::SHA256.hexdigest(username + code + Time.now.to_i.to_s)
  @outdir = File.join(GameMachine.app_root,'tmp',package_part)
  @package = "user.codeblocks.#{package_part}"
  @code = remove_package(code)
  @code = "package #{@package};\n\n#{@code}"
end

Instance Attribute Details

#classnameObject (readonly)

Returns the value of attribute classname


13
14
15
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 13

def classname
  @classname
end

#codeObject (readonly)

Returns the value of attribute code


13
14
15
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 13

def code
  @code
end

#outdirObject (readonly)

Returns the value of attribute outdir


13
14
15
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 13

def outdir
  @outdir
end

#packageObject (readonly)

Returns the value of attribute package


13
14
15
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 13

def package
  @package
end

#usernameObject (readonly)

Returns the value of attribute username


13
14
15
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 13

def username
  @username
end

Class Method Details

.fixture_pathObject


16
17
18
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 16

def fixture_path
  File.join(ENV['APP_ROOT'],'spec','fixtures')
end

.testcode_failObject


20
21
22
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 20

def testcode_fail
  File.read(File.join(fixture_path,'codeblocks_fail.java'))
end

.testcode_passObject


24
25
26
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 24

def testcode_pass
  File.read(File.join(fixture_path,'codeblocks_pass.java'))
end

Instance Method Details

#bin_dirObject


51
52
53
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 51

def bin_dir
  File.join(outdir,package_path)
end

#classfile_for(classname) ⇒ Object


47
48
49
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 47

def classfile_for(classname)
  File.join(bin_dir,"#{classname}.class")
end

#classpathObject


70
71
72
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 70

def classpath
  File.join(GameMachine.java_root,'lib',"game_machine-#{GameMachine::VERSION}.jar")
end

#compileObject


74
75
76
77
78
79
80
81
82
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 74

def compile
  result = JavaLib::CodeblockCompiler.compile(classpath,code,"#{package}.#{classname}")
  if result.is_compiled
    JavaLib::CodeblockCompiler.load(result)
  else
    result.get_errors.each {|error| puts error}
    nil
  end
end

#extract_classname(code) ⇒ Object


59
60
61
62
63
64
65
66
67
68
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 59

def extract_classname(code)
  klass = nil
  io = StringIO.new(code)
  io.each_line do |line|
    if line.match(/^\s*?public\s+?class\s+?(\w+?)\s+?implements\s+?Codeblock/)
      klass = $1
    end
  end
  klass
end

#package_pathObject


43
44
45
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 43

def package_path
  package.split('.').join('/')
end

#remove_package(code) ⇒ Object


55
56
57
# File 'server/lib/game_machine/codeblocks/compiler.rb', line 55

def remove_package(code)
  code.lines.select {|line| !line.match(/^\s*?package/)}.join('')
end