Skip to content

Commit

Permalink
Use env vars to tell erb about hs module
Browse files Browse the repository at this point in the history
Depends on ghcjs/shims#11
  • Loading branch information
mtolly committed Oct 20, 2014
1 parent d08d2e5 commit b232d32
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 39 deletions.
26 changes: 18 additions & 8 deletions hs/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ import Distribution.Simple.Utils
import Distribution.PackageDescription
import Distribution.Simple.LocalBuildInfo
import System.Process (readProcess)
import System.Environment (setEnv)
import System.FilePath (splitPath, isPathSeparator, dropExtension, (</>))

main = defaultMainWithHooks
simpleUserHooks
{ hookedPreProcessors = [("erb", runErb)] }

runErb _ _ =
PreProcessor
{ platformIndependent = True
, runPreProcessor = mkSimplePreProcessor $ \fin fout verbosity -> do
info verbosity $ "erb-processing " ++ fin ++ " to " ++ fout
readProcess "erb" [fin] "" >>= writeFile fout
{ hookedPreProcessors = [("erb", \_ _ -> PreProcessor
{ platformIndependent = True
, runPreProcessor = runErb
})]
}

runErb (din, fin) (dout, fout) verbosity = do
let fin' = din </> fin
fout' = dout </> fout
modName
= filter (not . isPathSeparator)
$ intercalate "."
$ splitPath
$ dropExtension fin
info verbosity $ "erb-processing " ++ fin' ++ " to " ++ fout'
setEnv "ERB_HS_MODULE" modName
readProcess "erb" [fin'] "" >>= writeFile fout'
2 changes: 2 additions & 0 deletions hs/buildjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
cabal configure --ghcjs && node dist/setup/setup.jsexe/all.js build
12 changes: 2 additions & 10 deletions hs/rb/enums.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@ def initialize(nameHs, exprJs = nameHs.upcase)
attr_reader :nameHs, :exprJs
end

def erbFileToModule(erb_file)
parts = erb_file.split('/')
parts.shift until parts[0] == 'lib'
parts.shift
parts[-1] = parts[-1].match(/\.erb$/) { |md| md.pre_match }
parts.join('.')
end

def makeEnumModule(erb_file, name, tags, exprPrefix = '')
def makeEnumModule(name, tags, exprPrefix = '')
tags = tags.map do |t|
t.is_a?(String) ? Tag.new(t) : t
end
lines = []

lines << "module #{erbFileToModule(erb_file)} where"
lines << "module #{ENV['ERB_HS_MODULE']} where"

lines << "import GHCJS.Types"
lines << "import GHCJS.Marshal"
Expand Down
12 changes: 2 additions & 10 deletions hs/rb/records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@ def initialize(type, nameHs, nameJs = nameHs)
attr_reader :type, :nameHs, :nameJs
end

def erbFileToModule(erb_file)
parts = erb_file.split('/')
parts.shift until parts[0] == 'lib'
parts.shift
parts[-1] = parts[-1].match(/\.erb$/) { |md| md.pre_match }
parts.join('.')
end

def makeRecordModule(erb_file, imports, name, fields)
def makeRecordModule(imports, name, fields)
fieldDefs = fields.map do |field|
"#{field.nameHs} :: #{field.type}"
end
lines = []

lines << "module #{erbFileToModule(erb_file)} where"
lines << "module #{ENV['ERB_HS_MODULE']} where"

lines << "import GHCJS.Foreign"
lines << "import GHCJS.Marshal"
Expand Down
2 changes: 1 addition & 1 deletion hs/src/lib/System/Cordova/Camera/CameraOptions.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/records.rb')
%>
<%= makeRecordModule(__FILE__, [
<%= makeRecordModule([
'import System.Cordova.Camera.DestinationType',
'import System.Cordova.Camera.SourceType',
'import System.Cordova.Camera.EncodingType',
Expand Down
2 changes: 1 addition & 1 deletion hs/src/lib/System/Cordova/Camera/DestinationType.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb')
%>
<%= makeEnumModule(__FILE__, 'DestinationType', [
<%= makeEnumModule('DestinationType', [
Tag.new('DataURL', 'DATA_URL'),
Tag.new('FileURI', 'FILE_URI'),
Tag.new('NativeURI', 'NATIVE_URI'),
Expand Down
2 changes: 1 addition & 1 deletion hs/src/lib/System/Cordova/Camera/Direction.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb')
%>
<%= makeEnumModule(__FILE__, 'Direction', %w{
<%= makeEnumModule('Direction', %w{
Back
Front
}, 'Camera.Direction.') %>
2 changes: 1 addition & 1 deletion hs/src/lib/System/Cordova/Camera/EncodingType.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb')
%>
<%= makeEnumModule(__FILE__, 'EncodingType', %w{
<%= makeEnumModule('EncodingType', %w{
JPEG
PNG
}, 'Camera.EncodingType.') %>
2 changes: 1 addition & 1 deletion hs/src/lib/System/Cordova/Camera/MediaType.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb')
%>
<%= makeEnumModule(__FILE__, 'MediaType', %w{
<%= makeEnumModule('MediaType', %w{
Picture
Video
AllMedia
Expand Down
2 changes: 1 addition & 1 deletion hs/src/lib/System/Cordova/Camera/PopoverArrowDirection.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb')
%>
<%= makeEnumModule(__FILE__, 'PopoverArrowDirection', [
<%= makeEnumModule('PopoverArrowDirection', [
Tag.new('ArrowUp', 'ARROW_UP'),
Tag.new('ArrowDown', 'ARROW_DOWN'),
Tag.new('ArrowLeft', 'ARROW_LEFT'),
Expand Down
2 changes: 1 addition & 1 deletion hs/src/lib/System/Cordova/Camera/PopoverOptions.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/records.rb')
%>
<%= makeRecordModule(__FILE__, [], 'PopoverOptions', [
<%= makeRecordModule([], 'PopoverOptions', [
Field.new('Maybe Int', 'popX', 'x'),
Field.new('Maybe Int', 'popY', 'y'),
Field.new('Maybe Int', 'popWidth', 'width'),
Expand Down
2 changes: 1 addition & 1 deletion hs/src/lib/System/Cordova/Camera/SourceType.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb')
%>
<%= makeEnumModule(__FILE__, 'SourceType', %w{
<%= makeEnumModule('SourceType', %w{
PhotoLibrary
Camera
SavedPhotoAlbum
Expand Down
2 changes: 1 addition & 1 deletion hs/src/lib/System/Cordova/FileSystem/FileErrorCode.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb')
%>
<%= makeEnumModule(__FILE__, 'FileErrorCode', %w{
<%= makeEnumModule('FileErrorCode', %w{
NOT_FOUND_ERR
SECURITY_ERR
ABORT_ERR
Expand Down
2 changes: 1 addition & 1 deletion hs/src/lib/System/Cordova/FileSystem/Storage.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb')
%>
<%= makeEnumModule(__FILE__, 'Storage', %w{
<%= makeEnumModule('Storage', %w{
Temporary
Persistent
}, 'window.') %>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb')
%>
<%= makeEnumModule(__FILE__, 'Connection', [
<%= makeEnumModule('Connection', [
'Unknown',
'Ethernet',
'Wifi',
Expand Down

0 comments on commit b232d32

Please sign in to comment.