malachite 0.0.5 → 0.0.6

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: 926d38044dda1a9e4c467ed3538e03cb58aaeb44
4
- data.tar.gz: 7e720cad471a0a7039b7d0817ae6a3681d364d12
3
+ metadata.gz: c98f3940a23926e32f06eec874ec4ac63e14b4c6
4
+ data.tar.gz: 65d45419c6d50ab9ade97f966d1994645683a844
5
5
  SHA512:
6
- metadata.gz: 75aca51ffd362de4e8f32282d71efd9b201fa248ae839e91c796bf70ba58ba59d75bce93800a8d0e36e2edf4c508003a5a2bfa705428f49fe0616b747fa15336
7
- data.tar.gz: d0c72131ebfc6256fc76b9ce5475ce6eb3f324b7d5dd97898854c8964879691a5354f3d1da3ff774a2d9e4d013dab371dcefc72f51d2981db1a72d599158029f
6
+ metadata.gz: 2d12dd1b59b31bced169bba5330107d9a3af9fb18cd78eac1df6305db07ba92215b7a038afe4099b7c7eb66685fe7503b75a2b188c7632e6e30a59476a87c77d
7
+ data.tar.gz: a0ec6db840c2a26d80223b8695b50bdb3ccaffb8554f41991bb32b715a249db8710acff918b8eb986cef47f42c35afe9f21b91875df4e88e58596458827dac1b
@@ -14,7 +14,6 @@ module Malachite
14
14
  Malachite.load_json(ptr.to_s)
15
15
  end
16
16
 
17
- # Malachite::Client.upcase(["foo", "bar"])
18
17
  def self.method_missing(name, args)
19
18
  new.call_method(name, args)
20
19
  end
@@ -23,6 +22,8 @@ module Malachite
23
22
 
24
23
  def open_dlib
25
24
  Fiddle.dlopen(shared_object_path)
25
+ rescue Fiddle::DLError
26
+ raise Malachite::DLError
26
27
  end
27
28
 
28
29
  def shared_object_path
@@ -12,24 +12,16 @@ module Malachite
12
12
  private
13
13
 
14
14
  def compile!
15
- copy_boilerplate_to_tmp
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'
15
+ modify_source_files_in_tmp
16
+ unless system('go', 'build', '-buildmode=c-shared', '-o', @compiled_file, *modified_go_files)
17
+ fail Malachite::BuildError, 'Unable to Build Shared Library'
19
18
  end
20
19
  path_to_compiled_file
21
20
  end
22
21
 
23
- def copy_boilerplate_to_tmp
24
- source_files = Dir["#{Rails.root.join('app', 'go')}/*.go"].reject { |f| f['test'] }
22
+ def modify_source_files_in_tmp
25
23
  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
24
+ Malachite::FileCompiler.new(f).compile
33
25
  end
34
26
  File.open(Rails.root.join('tmp', 'main.go').to_s, 'w') do |file|
35
27
  file.puts main_boilerplate
@@ -37,30 +29,16 @@ module Malachite
37
29
  end
38
30
  end
39
31
 
40
- def source_file(f)
41
- source = File.read(f)
42
- source.gsub(/package main/, '')
32
+ def modified_go_files
33
+ Dir["#{Rails.root.join('tmp')}/*.go"]
43
34
  end
44
35
 
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)
36
+ def source_files
37
+ Dir["#{Rails.root.join('app', 'go')}/*.go"].reject { |f| f['test'] }
53
38
  end
54
39
 
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]]
59
- end
60
-
61
- def path_to_tmp_file(file)
62
- basename = File.basename(file, '.go')
63
- Rails.root.join('tmp', "#{basename}.go").to_s
40
+ def main_boilerplate
41
+ File.read(File.expand_path('../main.go.tmpl', __FILE__))
64
42
  end
65
43
 
66
44
  def path_to_compiled_file
@@ -1,3 +1,4 @@
1
1
  module Malachite
2
- class ConfigError < StandardError; end
2
+ class BuildError < StandardError; end
3
+ class DLError < StandardError; end
3
4
  end
@@ -0,0 +1,41 @@
1
+ module Malachite
2
+ class FileCompiler
3
+ def initialize(file)
4
+ @file = file
5
+ end
6
+
7
+ def compile
8
+ File.open(path_to_tmp_file(@file), 'w') do |file|
9
+ file.puts 'package main'
10
+ file.puts "import \"C\""
11
+ file.puts source_file(@file)
12
+ file.puts exporter_boilerplate(@file)
13
+ file.close
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def source_file(f)
20
+ source = File.read(f)
21
+ source.gsub(/package main/, '')
22
+ end
23
+
24
+ def exporter_boilerplate(file)
25
+ exporter = File.read(File.expand_path('../exporter.go.tmpl', __FILE__))
26
+ method_name, method_type = extract_method_and_type(file)
27
+ exporter.gsub(/YYYYYY/, "#{method_type}{}").gsub(/XXXXXX/, method_name)
28
+ end
29
+
30
+ def extract_method_and_type(source_file_path)
31
+ handler_code = File.read(source_file_path)
32
+ match = /^func Handle(.*?)\(\w+ (.*?)\)/.match(handler_code)
33
+ [match[1], match[2]]
34
+ end
35
+
36
+ def path_to_tmp_file(file)
37
+ basename = File.basename(file, '.go')
38
+ Rails.root.join('tmp', "#{basename}.go").to_s
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  namespace :malachite do
2
- desc 'run Go tests'
2
+ desc 'runs all Go tests in app/go'
3
3
  task :test do
4
4
  test_files = Dir["#{Rails.root.join('app', 'go')}/*.go"]
5
5
  system('go', 'test', *test_files)
@@ -1,3 +1,3 @@
1
1
  module Malachite
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
data/lib/malachite.rb CHANGED
@@ -21,5 +21,5 @@ end
21
21
  if defined?(::Rails::Engine)
22
22
  require 'malachite/railtie'
23
23
  else
24
- fail "Malachite #{Malachite::VERSION} is a Rails-only Gem"
24
+ fail "Malachite #{Malachite::VERSION} requires Rails"
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: malachite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zack Hubert
@@ -68,6 +68,7 @@ files:
68
68
  - lib/malachite/compiler.rb
69
69
  - lib/malachite/errors.rb
70
70
  - lib/malachite/exporter.go.tmpl
71
+ - lib/malachite/file_compiler.rb
71
72
  - lib/malachite/main.go.tmpl
72
73
  - lib/malachite/railtie.rb
73
74
  - lib/malachite/tasks.rb