Skip to content

Commit

Permalink
Merge pull request #4 from fbonetti/custom_elm_path
Browse files Browse the repository at this point in the history
Custom elm path
  • Loading branch information
fbonetti committed May 20, 2016
2 parents 6d1c5e2 + deec60a commit 1151c9f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 28 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
/spec/reports/
/tmp/
/elm-stuff/
/elm-package.json
/elm.js
*.gem
*.gem
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ cache: bundler
rvm:
- 2.2.3
sudo: false
before_install: gem install bundler -v 1.11.2
before_install:
- gem install bundler -v 1.11.2
install:
- bundle install
- npm install elm
- npm install -g elm@0.17
- elm-package install --yes
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ Or install it yourself as:

## Usage

> NOTE: Make sure [Elm](https://elm-lang.org/install) is installed. If the `elm-make` executable can't be found in the current `PATH`, the exception `Elm::Compiler::ExecutableNotFound` will be thrown.
> NOTE: Make sure [Elm](https://elm-lang.org/install) is installed. If the `elm-make` executable can't be found in the current `PATH` or via the `elm_make_path` option, the exception `Elm::Compiler::ExecutableNotFound` will be thrown.
```ruby
Elm::Compiler.compile(elm_files, output_path = nil)
Elm::Compiler.compile(elm_files, output_path: nil, elm_make_path: nil)
```

* `elm_files`: Accepts a single file path or an array of file paths.
* `output_path`: Path to the output file. If left blank, the compiled Javascript will be returned as a string.
* `elm_make_path`: Path to the `elm-make` executable. If left blank, the executable will be looked up in the current `PATH`.



Expand All @@ -56,13 +57,13 @@ Elm::Compiler.compile(["Clock.elm", "Counter.elm"])
Compile to file:

```ruby
Elm::Compiler.compile("Clock.elm", "elm.js")
Elm::Compiler.compile("Clock.elm", output_path: "elm.js")
```

Compile multiple files to file:

```ruby
Elm::Compiler.compile(["Clock.elm", "Counter.elm"], "elm.js")
Elm::Compiler.compile(["Clock.elm", "Counter.elm"], output_path: "elm.js")
```

## Contributing
Expand Down
15 changes: 15 additions & 0 deletions elm-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "1.0.0",
"summary": "elm-package.json for rspec test suite",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "4.0.0 <= v < 5.0.0",
"elm-lang/html": "1.0.0 <= v < 2.0.0"
},
"elm-version": "0.17.0 <= v < 0.18.0"
}
27 changes: 12 additions & 15 deletions lib/elm/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,32 @@
module Elm
class Compiler
class << self
def compile(elm_files, output_path = nil)
fail ExecutableNotFound unless elm_executable_exists?
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)

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

private

def elm_executable_exists?
!find_executable0('elm-make').nil?
def elm_executable_exists?(elm_executable)
File.executable?(elm_executable)
end

def compile_to_string(elm_files)
output = ''

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

output
end

def elm_make(elm_files, output_path)
Open3.popen3('elm-make', *elm_files, '--yes', '--output', output_path) do |_stdin, _stdout, stderr, wait_thr|
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|
fail CompileError, stderr.gets(nil) if wait_thr.value.exitstatus != 0
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/elm/compiler/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Elm
class Compiler
VERSION = '0.1.2'
VERSION = '0.2.0'
end
end
18 changes: 16 additions & 2 deletions spec/elm/compiler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'mkmf'

describe Elm::Compiler do
let(:test_file) { 'spec/fixtures/Test.elm' }
Expand Down Expand Up @@ -38,16 +39,29 @@
end

it 'should write to the given output_path' do
output = Elm::Compiler.compile(test_file, 'elm.js')
output = Elm::Compiler.compile(test_file, output_path: 'elm.js')
expect(output).to be_nil
expect(File.exist?('elm.js')).to be(true)
end

it 'should accept a string or an array of strings' do
output = Elm::Compiler.compile([test_file], 'elm.js')
output = Elm::Compiler.compile([test_file], output_path: 'elm.js')
expect(output).to be_nil
expect(File.exist?('elm.js')).to be(true)
end
end

context 'elm_make_path given' do
it 'should raise ExecutableNoFound if path is bad' do
code = proc { Elm::Compiler.compile(test_file, elm_make_path: "/dev/null") }
expect(&code).to raise_exception(Elm::Compiler::ExecutableNotFound)
end

it 'should work if path is good' do
output = Elm::Compiler.compile(test_file, elm_make_path: find_executable0('elm-make'))
expect(output).to be_instance_of(String)
expect(output.empty?).to be(false)
end
end
end
end
5 changes: 3 additions & 2 deletions spec/fixtures/Test.elm
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Graphics.Element exposing (show)
import Html exposing (text)

main = show 3
main =
text "test"

0 comments on commit 1151c9f

Please sign in to comment.