Sha256: ed272b1e55f5d7f716f8fbbdffd2ae2b0fe561600ada23c9379fb58f5bb42bc1

Contents?: true

Size: 1000 Bytes

Versions: 2

Compression:

Stored size: 1000 Bytes

Contents

require 'elm/compiler/exceptions'
require 'open3'
require 'tempfile'
require 'mkmf'

module Elm
  class Compiler
    class << self
      def compile(elm_files, output_path = nil)
        fail ExecutableNotFound unless elm_executable_exists?

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

      private

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

      def compile_to_string(elm_files)
        output = ''

        Tempfile.open(['elm', '.js']) do |tempfile|
          elm_make(elm_files, tempfile.path)
          output = 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|
          fail CompileError, stderr.gets if wait_thr.value.exitstatus != 0
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
elm-compiler-0.1.1 lib/elm/compiler.rb
elm-compiler-0.1.0 lib/elm/compiler.rb