Skip to content

Commit

Permalink
Refactor IO functions to use Http tasks for file operations and proce…
Browse files Browse the repository at this point in the history
…ss management
  • Loading branch information
decioferreira committed Feb 28, 2025
1 parent d93b5f7 commit fbfef5a
Show file tree
Hide file tree
Showing 8 changed files with 1,161 additions and 1,170 deletions.
587 changes: 283 additions & 304 deletions bin/index.js

Large diffs are not rendered by default.

59 changes: 53 additions & 6 deletions src/Builder/File.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Builder.File exposing
)

import Codec.Archive.Zip as Zip
import Http
import Json.Decode as Decode
import Json.Encode as Encode
import System.IO as IO exposing (IO(..))
Expand Down Expand Up @@ -95,8 +96,8 @@ readBinary decoder path =


writeUtf8 : FilePath -> String -> IO ()
writeUtf8 path content =
IO (\_ s -> ( s, IO.WriteString IO.pure path content ))
writeUtf8 =
IO.writeString



Expand All @@ -105,21 +106,67 @@ writeUtf8 path content =

readUtf8 : FilePath -> IO String
readUtf8 path =
IO (\_ s -> ( s, IO.Read IO.pure path ))
IO
(\_ s ->
( s
, IO.ImpureTask
(Http.task
{ method = "POST"
, headers = []
, url = "read"
, body = Http.stringBody "text/plain" path
, resolver =
Http.stringResolver
(\response ->
case response of
Http.GoodStatus_ _ body ->
Ok (IO.pure body)

_ ->
Ok (IO.pure "")
)
, timeout = Nothing
}
)
)
)


readStdin : IO String
readStdin =
IO (\_ s -> ( s, IO.ReadStdin IO.pure ))
IO
(\_ s ->
( s
, IO.ImpureTask
(Http.task
{ method = "POST"
, headers = []
, url = "readStdin"
, body = Http.emptyBody
, resolver =
Http.stringResolver
(\response ->
case response of
Http.GoodStatus_ _ body ->
Ok (IO.pure body)

_ ->
Ok (IO.pure "")
)
, timeout = Nothing
}
)
)
)



-- WRITE BUILDER


writeBuilder : FilePath -> String -> IO ()
writeBuilder path builder =
IO (\_ s -> ( s, IO.WriteString IO.pure path builder ))
writeBuilder =
IO.writeString



Expand Down
171 changes: 121 additions & 50 deletions src/Builder/Http.elm
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ module Builder.Http exposing
import Basics.Extra exposing (uncurry)
import Codec.Archive.Zip as Zip
import Compiler.Elm.Version as V
import Http
import Json.Decode as Decode
import Json.Encode as Encode
import System.IO as IO exposing (IO(..))
import Url.Builder
import Utils.Crash exposing (crash)
import Utils.Main as Utils exposing (SomeException)


Expand Down Expand Up @@ -94,37 +96,40 @@ type alias Header =

get : Manager -> String -> List Header -> (Error -> e) -> (String -> IO (Result e a)) -> IO (Result e a)
get =
fetch MethodGet
fetch "GET"


post : Manager -> String -> List Header -> (Error -> e) -> (String -> IO (Result e a)) -> IO (Result e a)
post =
fetch MethodPost
fetch "POST"


type Method
= MethodGet
| MethodPost


fetch : Method -> Manager -> String -> List Header -> (Error -> e) -> (String -> IO (Result e a)) -> IO (Result e a)
fetch methodVerb _ url headers _ onSuccess =
fetch : String -> Manager -> String -> List Header -> (Error -> e) -> (String -> IO (Result e a)) -> IO (Result e a)
fetch method _ url headers _ onSuccess =
IO
(\_ s ->
( s
, IO.HttpFetch IO.pure
(case methodVerb of
MethodGet ->
"GET"

MethodPost ->
"POST"
, IO.ImpureTask
(Http.task
{ method = method
, headers = List.map (\( a, b ) -> Http.header a b) (addDefaultHeaders headers)
, url = url
, body = Http.emptyBody
, resolver =
Http.stringResolver
(\response ->
case response of
Http.GoodStatus_ _ body ->
Ok (onSuccess body)

_ ->
Ok (onSuccess "")
)
, timeout = Nothing
}
)
url
(addDefaultHeaders headers)
)
)
|> IO.bind onSuccess


addDefaultHeaders : List Header -> List Header
Expand Down Expand Up @@ -171,8 +176,51 @@ shaToChars =

getArchive : Manager -> String -> (Error -> e) -> e -> (( Sha, Zip.Archive ) -> IO (Result e a)) -> IO (Result e a)
getArchive _ url _ _ onSuccess =
IO (\_ s -> ( s, IO.GetArchive IO.pure "GET" url ))
|> IO.bind (\shaAndArchive -> onSuccess shaAndArchive)
IO
(\_ s ->
( s
, IO.ImpureTask
(Http.task
{ method = "POST"
, headers = []
, url = "getArchive"
, body = Http.stringBody "text/plain" url
, resolver =
Http.stringResolver
(\response ->
case response of
Http.GoodStatus_ _ body ->
let
shaAndArchiveResult =
Decode.decodeString
(Decode.map2 Tuple.pair
(Decode.field "sha" Decode.string)
(Decode.field "archive"
(Decode.list
(Decode.map2 Zip.Entry
(Decode.field "eRelativePath" Decode.string)
(Decode.field "eData" Decode.string)
)
)
)
)
body
in
case shaAndArchiveResult of
Ok shaAndArchive ->
Ok (onSuccess shaAndArchive)

Err _ ->
crash "getArchive"

_ ->
crash "getArchive"
)
, timeout = Nothing
}
)
)
)



Expand All @@ -190,35 +238,58 @@ upload _ url parts =
IO
(\_ s ->
( s
, IO.HttpUpload IO.pure
url
(addDefaultHeaders [])
(List.map
(\part ->
case part of
FilePart name filePath ->
Encode.object
[ ( "type", Encode.string "FilePart" )
, ( "name", Encode.string name )
, ( "filePath", Encode.string filePath )
]

JsonPart name filePath value ->
Encode.object
[ ( "type", Encode.string "JsonPart" )
, ( "name", Encode.string name )
, ( "filePath", Encode.string filePath )
, ( "value", value )
]

StringPart name string ->
Encode.object
[ ( "type", Encode.string "StringPart" )
, ( "name", Encode.string name )
, ( "string", Encode.string string )
]
)
parts
, IO.ImpureTask
(Http.task
{ method = "POST"
, headers = []
, url = "httpUpload"
, body =
Http.jsonBody
(Encode.object
[ ( "urlStr", Encode.string url )
, ( "headers", Encode.object (List.map (Tuple.mapSecond Encode.string) (addDefaultHeaders [])) )
, ( "parts"
, Encode.list
(\part ->
case part of
FilePart name filePath ->
Encode.object
[ ( "type", Encode.string "FilePart" )
, ( "name", Encode.string name )
, ( "filePath", Encode.string filePath )
]

JsonPart name filePath value ->
Encode.object
[ ( "type", Encode.string "JsonPart" )
, ( "name", Encode.string name )
, ( "filePath", Encode.string filePath )
, ( "value", value )
]

StringPart name string ->
Encode.object
[ ( "type", Encode.string "StringPart" )
, ( "name", Encode.string name )
, ( "string", Encode.string string )
]
)
parts
)
]
)
, resolver =
Http.stringResolver
(\response ->
case response of
Http.GoodStatus_ _ _ ->
Ok (IO.pure ())

_ ->
crash "httpUpload"
)
, timeout = Nothing
}
)
)
)
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/Json/Encode.elm
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ writeUgly path value =
{-| FIXME Builder.File.writeBuilder
-}
fileWriteBuilder : String -> String -> IO ()
fileWriteBuilder path value =
IO (\_ s -> ( s, IO.WriteString IO.pure path value ))
fileWriteBuilder =
IO.writeString



Expand Down
15 changes: 14 additions & 1 deletion src/System/Exit.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ module System.Exit exposing
, exitWith
)

import Http
import System.IO as IO exposing (IO(..))
import Utils.Crash exposing (crash)


type ExitCode
Expand All @@ -27,7 +29,18 @@ exitWith exitCode =
ExitFailure int ->
int
in
( s, IO.ExitWith IO.pure code )
( s
, IO.ImpureTask
(Http.task
{ method = "POST"
, headers = []
, url = "exitWith"
, body = Http.stringBody "text/plain" (String.fromInt code)
, resolver = Http.stringResolver (\_ -> crash "exitWith")
, timeout = Nothing
}
)
)
)


Expand Down
Loading

0 comments on commit fbfef5a

Please sign in to comment.