diff --git a/hs/Setup.hs b/hs/Setup.hs index 80b4740..e0c5053 100644 --- a/hs/Setup.hs +++ b/hs/Setup.hs @@ -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' diff --git a/hs/buildjs b/hs/buildjs new file mode 100755 index 0000000..68e9ebe --- /dev/null +++ b/hs/buildjs @@ -0,0 +1,2 @@ +#!/bin/sh +cabal configure --ghcjs && node dist/setup/setup.jsexe/all.js build diff --git a/hs/rb/enums.rb b/hs/rb/enums.rb index 5401915..5fd0566 100644 --- a/hs/rb/enums.rb +++ b/hs/rb/enums.rb @@ -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" diff --git a/hs/rb/records.rb b/hs/rb/records.rb index a1689cc..f8df415 100644 --- a/hs/rb/records.rb +++ b/hs/rb/records.rb @@ -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" diff --git a/hs/src/lib/System/Cordova/Camera/CameraOptions.erb b/hs/src/lib/System/Cordova/Camera/CameraOptions.erb index f4398d6..7a96c3b 100644 --- a/hs/src/lib/System/Cordova/Camera/CameraOptions.erb +++ b/hs/src/lib/System/Cordova/Camera/CameraOptions.erb @@ -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', diff --git a/hs/src/lib/System/Cordova/Camera/DestinationType.erb b/hs/src/lib/System/Cordova/Camera/DestinationType.erb index 2dcc061..0214eec 100644 --- a/hs/src/lib/System/Cordova/Camera/DestinationType.erb +++ b/hs/src/lib/System/Cordova/Camera/DestinationType.erb @@ -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'), diff --git a/hs/src/lib/System/Cordova/Camera/Direction.erb b/hs/src/lib/System/Cordova/Camera/Direction.erb index 4599670..57b444f 100644 --- a/hs/src/lib/System/Cordova/Camera/Direction.erb +++ b/hs/src/lib/System/Cordova/Camera/Direction.erb @@ -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.') %> diff --git a/hs/src/lib/System/Cordova/Camera/EncodingType.erb b/hs/src/lib/System/Cordova/Camera/EncodingType.erb index 983ddde..039fd59 100644 --- a/hs/src/lib/System/Cordova/Camera/EncodingType.erb +++ b/hs/src/lib/System/Cordova/Camera/EncodingType.erb @@ -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.') %> diff --git a/hs/src/lib/System/Cordova/Camera/MediaType.erb b/hs/src/lib/System/Cordova/Camera/MediaType.erb index 37cbbf8..3b7ed37 100644 --- a/hs/src/lib/System/Cordova/Camera/MediaType.erb +++ b/hs/src/lib/System/Cordova/Camera/MediaType.erb @@ -1,7 +1,7 @@ <% require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb') %> -<%= makeEnumModule(__FILE__, 'MediaType', %w{ +<%= makeEnumModule('MediaType', %w{ Picture Video AllMedia diff --git a/hs/src/lib/System/Cordova/Camera/PopoverArrowDirection.erb b/hs/src/lib/System/Cordova/Camera/PopoverArrowDirection.erb index 41fb669..93be672 100644 --- a/hs/src/lib/System/Cordova/Camera/PopoverArrowDirection.erb +++ b/hs/src/lib/System/Cordova/Camera/PopoverArrowDirection.erb @@ -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'), diff --git a/hs/src/lib/System/Cordova/Camera/PopoverOptions.erb b/hs/src/lib/System/Cordova/Camera/PopoverOptions.erb index 1ab9b6e..69d2978 100644 --- a/hs/src/lib/System/Cordova/Camera/PopoverOptions.erb +++ b/hs/src/lib/System/Cordova/Camera/PopoverOptions.erb @@ -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'), diff --git a/hs/src/lib/System/Cordova/Camera/SourceType.erb b/hs/src/lib/System/Cordova/Camera/SourceType.erb index 2277bd2..1c83d76 100644 --- a/hs/src/lib/System/Cordova/Camera/SourceType.erb +++ b/hs/src/lib/System/Cordova/Camera/SourceType.erb @@ -1,7 +1,7 @@ <% require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb') %> -<%= makeEnumModule(__FILE__, 'SourceType', %w{ +<%= makeEnumModule('SourceType', %w{ PhotoLibrary Camera SavedPhotoAlbum diff --git a/hs/src/lib/System/Cordova/FileSystem/FileErrorCode.erb b/hs/src/lib/System/Cordova/FileSystem/FileErrorCode.erb index d158657..b0e3c3c 100644 --- a/hs/src/lib/System/Cordova/FileSystem/FileErrorCode.erb +++ b/hs/src/lib/System/Cordova/FileSystem/FileErrorCode.erb @@ -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 diff --git a/hs/src/lib/System/Cordova/FileSystem/Storage.erb b/hs/src/lib/System/Cordova/FileSystem/Storage.erb index b585094..cab204d 100644 --- a/hs/src/lib/System/Cordova/FileSystem/Storage.erb +++ b/hs/src/lib/System/Cordova/FileSystem/Storage.erb @@ -1,7 +1,7 @@ <% require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb') %> -<%= makeEnumModule(__FILE__, 'Storage', %w{ +<%= makeEnumModule('Storage', %w{ Temporary Persistent }, 'window.') %> diff --git a/hs/src/lib/System/Cordova/NetworkInformation/Connection.erb b/hs/src/lib/System/Cordova/NetworkInformation/Connection.erb index 9d31065..8e3837f 100644 --- a/hs/src/lib/System/Cordova/NetworkInformation/Connection.erb +++ b/hs/src/lib/System/Cordova/NetworkInformation/Connection.erb @@ -1,7 +1,7 @@ <% require File.expand_path(File.dirname(__FILE__) + '/../../../../../rb/enums.rb') %> -<%= makeEnumModule(__FILE__, 'Connection', [ +<%= makeEnumModule('Connection', [ 'Unknown', 'Ethernet', 'Wifi',