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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c81db104c04d28b06832166c2402793f552a433
4
- data.tar.gz: 662092aba1aed2ef10960947908bd56ed5a26e79
3
+ metadata.gz: 73c222a1089089b9c0b127a5cd17f2bd071a5d81
4
+ data.tar.gz: 18a92267b8d4d60d73a582c62fddefd7ee124f72
5
5
  SHA512:
6
- metadata.gz: a84ee6b7cf5811fa101781f9358cf6d6606569ab6b7af7fe296fac6ca37d859d1c746cfa94e2dc8263182b44bd097893f7fdb5a2696ed60c027f03246e616d01
7
- data.tar.gz: 3bc5bc648382e248c9d3ebb159338157089ec5823f6262fe9b6e124ac8442f5f60aa1a323371780b6d24f04fee36c836bb92dcd48f5cf791a1cc6860dd57f30d
6
+ metadata.gz: b29d404e2a0c6644372bd27ecc3c822596bad20d7ebe14c9f52c7d33c12b067f4e28bf273ad6b639a33480252419f6e5351e3f2cf83dc9e0b2c6ab50f5ca1f75
7
+ data.tar.gz: 726df1464886ee70ce8fba545593ec88790b404293e88bbb7b46fb2783afa39727ed9b55c21b31618e39b0c8e6b808374aa98b7bdfabe9c78fa7df6360cac4ae
@@ -3,29 +3,30 @@ require 'tempfile'
3
3
 
4
4
  module Malachite
5
5
  class Client
6
- def initialize(file_path)
7
- @file_path = file_path
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 call(args)
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(@file_path, @name).compile
29
+ Malachite::Compiler.new.compile
29
30
  end
30
31
  end
31
32
  end
@@ -1,13 +1,11 @@
1
1
  module Malachite
2
2
  class Compiler
3
- def initialize(file_path, name)
4
- @file_path = file_path
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 path_to_tmp_file if File.exist?(path_to_tmp_file)
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
- unless system('go', 'build', '-buildmode=c-shared', '-o', path_to_tmp_file.to_s, @go_file.to_s)
19
- fail Malachite::ConfigError, "Unable to Build Shared Library for #{@file_path}"
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
- path_to_tmp_file
20
+ path_to_compiled_file
22
21
  end
23
22
 
24
23
  def copy_boilerplate_to_tmp
25
- File.open(@go_file, 'w') do |file|
26
- file.puts "package main\n"
27
- file.puts "import \"encoding/json\"\n"
28
- file.puts "import \"C\"\n"
29
- file.puts munged_source
30
- file.puts munged_boilerplate
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 munged_boilerplate
35
- boiler = File.read(File.expand_path('../boilerplate.go.tmpl', __FILE__))
36
- boiler.gsub(/XXXXXX/, "#{handler_type(munged_source)}{}")
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 munged_source
40
- source_go = File.read(@file_path)
41
- source_go.gsub(/package main/, '')
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 handler_type(handler_code)
45
- match = /^func handler\(\w+ (.*?)\)/.match(handler_code)
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
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].each do |task|
2
+ load task
3
+ end
@@ -1,3 +1,3 @@
1
1
  module Malachite
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/malachite.rb CHANGED
@@ -19,7 +19,7 @@ module Malachite
19
19
  end
20
20
 
21
21
  if defined?(::Rails::Engine)
22
- require 'malachite/rails'
22
+ require 'malachite/railtie'
23
23
  else
24
- require 'malachite/ruby'
24
+ fail "Malachite #{Malachite::VERSION} is a Rails-only Gem"
25
25
  end
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.1
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-17 00:00:00.000000000 Z
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/rails.rb
69
- - lib/malachite/ruby.rb
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:
@@ -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