Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove mkmf as a dependency #5

Merged
merged 1 commit into from
Jun 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lib/elm/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
require 'elm/compiler/exceptions'
require 'open3'
require 'tempfile'
require 'mkmf'

module Elm
class Compiler
class << self
def compile(elm_files, output_path: nil, elm_make_path: nil)
elm_executable = elm_make_path || find_executable0("elm-make")
fail ExecutableNotFound unless elm_executable_exists?(elm_executable)
def compile(elm_files, output_path: nil, elm_make_path: "elm-make")
fail ExecutableNotFound unless elm_executable_exists?(elm_make_path)

if output_path
elm_make(elm_executable, elm_files, output_path)
elm_make(elm_make_path, elm_files, output_path)
else
compile_to_string(elm_executable, elm_files)
compile_to_string(elm_make_path, elm_files)
end
end

private

def elm_executable_exists?(elm_executable)
File.executable?(elm_executable)
def elm_executable_exists?(elm_make_path)
Open3.popen2(elm_make_path){}.nil?
rescue Errno::ENOENT, Errno::EACCES
false
end

def compile_to_string(elm_executable, elm_files)
def compile_to_string(elm_make_path, elm_files)
Tempfile.open(['elm', '.js']) do |tempfile|
elm_make(elm_executable, elm_files, tempfile.path)
elm_make(elm_make_path, elm_files, tempfile.path)
return File.read tempfile.path
end
end

def elm_make(elm_executable, elm_files, output_path)
Open3.popen3({"LANG" => "en_US.UTF8" }, elm_executable, *elm_files, '--yes', '--output', output_path) do |_stdin, _stdout, stderr, wait_thr|
def elm_make(elm_make_path, elm_files, output_path)
Open3.popen3({"LANG" => "en_US.UTF8" }, elm_make_path, *elm_files, '--yes', '--output', output_path) do |_stdin, _stdout, stderr, wait_thr|
fail CompileError, stderr.gets(nil) if wait_thr.value.exitstatus != 0
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/elm/compiler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'spec_helper'
require 'mkmf'

describe Elm::Compiler do
let(:test_file) { 'spec/fixtures/Test.elm' }
Expand Down Expand Up @@ -58,7 +57,7 @@
end

it 'should work if path is good' do
output = Elm::Compiler.compile(test_file, elm_make_path: find_executable0('elm-make'))
output = Elm::Compiler.compile(test_file, elm_make_path: 'elm-make')
expect(output).to be_instance_of(String)
expect(output.empty?).to be(false)
end
Expand Down