malachite 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/malachite/client.rb +11 -10
- data/lib/malachite/compiler.rb +44 -23
- data/lib/malachite/{rails.rb → railtie.rb} +6 -15
- data/lib/malachite/tasks.rb +3 -0
- data/lib/malachite/version.rb +1 -1
- data/lib/malachite.rb +2 -2
- metadata +4 -4
- data/lib/malachite/ruby.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73c222a1089089b9c0b127a5cd17f2bd071a5d81
|
4
|
+
data.tar.gz: 18a92267b8d4d60d73a582c62fddefd7ee124f72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b29d404e2a0c6644372bd27ecc3c822596bad20d7ebe14c9f52c7d33c12b067f4e28bf273ad6b639a33480252419f6e5351e3f2cf83dc9e0b2c6ab50f5ca1f75
|
7
|
+
data.tar.gz: 726df1464886ee70ce8fba545593ec88790b404293e88bbb7b46fb2783afa39727ed9b55c21b31618e39b0c8e6b808374aa98b7bdfabe9c78fa7df6360cac4ae
|
data/lib/malachite/client.rb
CHANGED
@@ -3,29 +3,30 @@ require 'tempfile'
|
|
3
3
|
|
4
4
|
module Malachite
|
5
5
|
class Client
|
6
|
-
def initialize
|
7
|
-
@
|
8
|
-
@name = library_name
|
9
|
-
@func = Fiddle::Function.new(open_dlib['call'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP)
|
6
|
+
def initialize
|
7
|
+
@dylib = open_dlib
|
10
8
|
end
|
11
9
|
|
12
|
-
def
|
10
|
+
def call_method(name, args)
|
11
|
+
method_name = "call#{name.to_s.camelize}"
|
12
|
+
@func = Fiddle::Function.new(@dylib[method_name], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP)
|
13
13
|
ptr = @func.call(Malachite.dump_json(args))
|
14
14
|
Malachite.load_json(ptr.to_s)
|
15
15
|
end
|
16
16
|
|
17
|
+
# Malachite::Client.upcase(["foo", "bar"])
|
18
|
+
def self.method_missing(name, args)
|
19
|
+
new.call_method(name, args)
|
20
|
+
end
|
21
|
+
|
17
22
|
private
|
18
23
|
|
19
24
|
def open_dlib
|
20
25
|
Fiddle.dlopen(shared_object_path)
|
21
26
|
end
|
22
27
|
|
23
|
-
def library_name
|
24
|
-
File.basename(@file_path, '.go')
|
25
|
-
end
|
26
|
-
|
27
28
|
def shared_object_path
|
28
|
-
Malachite::Compiler.new
|
29
|
+
Malachite::Compiler.new.compile
|
29
30
|
end
|
30
31
|
end
|
31
32
|
end
|
data/lib/malachite/compiler.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
module Malachite
|
2
2
|
class Compiler
|
3
|
-
def initialize
|
4
|
-
@
|
5
|
-
@go_file = path_to_go_file
|
6
|
-
@name = name
|
3
|
+
def initialize
|
4
|
+
@compiled_file = path_to_compiled_file
|
7
5
|
end
|
8
6
|
|
9
7
|
def compile
|
10
|
-
return
|
8
|
+
return @compiled_file if File.exist?(@compiled_file)
|
11
9
|
compile!
|
12
10
|
end
|
13
11
|
|
@@ -15,35 +13,58 @@ module Malachite
|
|
15
13
|
|
16
14
|
def compile!
|
17
15
|
copy_boilerplate_to_tmp
|
18
|
-
|
19
|
-
|
16
|
+
go_files = Dir["#{Rails.root.join('tmp')}/*.go"]
|
17
|
+
unless system('go', 'build', '-buildmode=c-shared', '-o', @compiled_file, *go_files)
|
18
|
+
fail Malachite::ConfigError, 'Unable to Build Shared Library'
|
20
19
|
end
|
21
|
-
|
20
|
+
path_to_compiled_file
|
22
21
|
end
|
23
22
|
|
24
23
|
def copy_boilerplate_to_tmp
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
24
|
+
source_files = Dir["#{Rails.root.join('app', 'go')}/*.go"].reject { |f| f['test'] }
|
25
|
+
source_files.each do |f|
|
26
|
+
File.open(path_to_tmp_file(f), 'w') do |file|
|
27
|
+
file.puts 'package main'
|
28
|
+
file.puts "import \"C\""
|
29
|
+
file.puts source_file(f)
|
30
|
+
file.puts exporter_boilerplate(f)
|
31
|
+
file.close
|
32
|
+
end
|
31
33
|
end
|
34
|
+
File.open(Rails.root.join('tmp', 'main.go').to_s, 'w') do |file|
|
35
|
+
file.puts main_boilerplate
|
36
|
+
file.close
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def source_file(f)
|
41
|
+
source = File.read(f)
|
42
|
+
source.gsub(/package main/, '')
|
43
|
+
end
|
44
|
+
|
45
|
+
def main_boilerplate
|
46
|
+
File.read(File.expand_path('../main.go.tmpl', __FILE__))
|
47
|
+
end
|
48
|
+
|
49
|
+
def exporter_boilerplate(file)
|
50
|
+
exporter = File.read(File.expand_path('../exporter.go.tmpl', __FILE__))
|
51
|
+
method_name, method_type = extract_method_and_type(file)
|
52
|
+
exporter.gsub(/YYYYYY/, "#{method_type}{}").gsub(/XXXXXX/, method_name)
|
32
53
|
end
|
33
54
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
55
|
+
def extract_method_and_type(source_file_path)
|
56
|
+
handler_code = File.read(source_file_path)
|
57
|
+
match = /^func Handle(.*?)\(\w+ (.*?)\)/.match(handler_code)
|
58
|
+
[match[1], match[2]]
|
37
59
|
end
|
38
60
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
61
|
+
def path_to_tmp_file(file)
|
62
|
+
basename = File.basename(file, '.go')
|
63
|
+
Rails.root.join('tmp', "#{basename}.go").to_s
|
42
64
|
end
|
43
65
|
|
44
|
-
def
|
45
|
-
|
46
|
-
match[1]
|
66
|
+
def path_to_compiled_file
|
67
|
+
Rails.root.join('tmp', 'malachite.so').to_s
|
47
68
|
end
|
48
69
|
end
|
49
70
|
end
|
@@ -1,20 +1,8 @@
|
|
1
1
|
module Malachite
|
2
|
-
class Compiler
|
3
|
-
def path_to_tmp_file
|
4
|
-
Rails.root.join('tmp', "#{@name}.so").to_s
|
5
|
-
end
|
6
|
-
|
7
|
-
def path_to_go_file
|
8
|
-
Rails.root.join('tmp', "#{@name}.go").to_s
|
9
|
-
end
|
10
|
-
end
|
11
|
-
class Client
|
12
|
-
def self.method_missing(name, args)
|
13
|
-
path_to_source = Rails.root.join('app', 'go', "#{name}.go")
|
14
|
-
new(path_to_source).call(args)
|
15
|
-
end
|
16
|
-
end
|
17
2
|
class MalachiteRailtie < Rails::Railtie
|
3
|
+
rake_tasks do
|
4
|
+
load 'malachite/tasks/malachite.rake'
|
5
|
+
end
|
18
6
|
initializer 'malachite.configure_rails_initialization' do
|
19
7
|
Dir.glob(Rails.root.join('tmp', '*.so')).each do |file|
|
20
8
|
File.delete(file)
|
@@ -25,6 +13,9 @@ module Malachite
|
|
25
13
|
Dir.glob(Rails.root.join('tmp', '*.h')).each do |file|
|
26
14
|
File.delete(file)
|
27
15
|
end
|
16
|
+
rake_tasks do
|
17
|
+
load 'malachite/tasks/malachite.rake'
|
18
|
+
end
|
28
19
|
end
|
29
20
|
end
|
30
21
|
end
|
data/lib/malachite/version.rb
CHANGED
data/lib/malachite.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: malachite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zack Hubert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -65,8 +65,8 @@ files:
|
|
65
65
|
- lib/malachite/client.rb
|
66
66
|
- lib/malachite/compiler.rb
|
67
67
|
- lib/malachite/errors.rb
|
68
|
-
- lib/malachite/
|
69
|
-
- lib/malachite/
|
68
|
+
- lib/malachite/railtie.rb
|
69
|
+
- lib/malachite/tasks.rb
|
70
70
|
- lib/malachite/version.rb
|
71
71
|
homepage: http://www.zhubert.com
|
72
72
|
licenses:
|
data/lib/malachite/ruby.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
module Malachite
|
2
|
-
class Compiler
|
3
|
-
def path_to_tmp_file
|
4
|
-
Tempfile.new([@name, 'so']).path
|
5
|
-
end
|
6
|
-
|
7
|
-
def path_to_go_file
|
8
|
-
Tempfile.new([@name, 'go']).path
|
9
|
-
end
|
10
|
-
end
|
11
|
-
class Client
|
12
|
-
def self.method_missing(name, args)
|
13
|
-
new("#{name}.go").call(args)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|