@@ -8,22 +8,17 @@ module Obelisk.Command.Deploy where
88
99import Control.Lens
1010import Control.Monad
11−import Control.Monad.Catch (Exception (displayException), MonadThrow, throwM, try, bracket_)
11+import Control.Monad.Catch (Exception (displayException), MonadThrow, bracket, throwM, try)
1212import Control.Monad.IO.Class (liftIO)
1313import Data.Aeson (FromJSON, ToJSON, encode, eitherDecode)
1414import Data.Bits
1515import qualified Data.ByteString.Lazy as BSL
1616import Data.Default
17−import qualified Data.HashMap.Strict as HM
1817import qualified Data.Map as Map
19−import Data.Text (Text)
2018import qualified Data.Set as Set
2119import qualified Data.Text as T
2220import qualified Data.Text.IO as T
2321import GHC.Generics
24−import Nix.Pretty
25−import Nix.String (principledMakeNixStringWithoutContext)
26−import Nix.Value
2722import System.Directory
2823import System.FilePath
2924import System.IO
@@ -172,20 +167,6 @@ deployPush deployPath getNixBuilders = do
172167deployUpdate :: MonadObelisk m => FilePath -> m ()
173168deployUpdate deployPath = updateThunkToLatest (ThunkUpdateConfig Nothing (ThunkConfig Nothing)) (deployPath </> "src")
174169
175−keytoolToAndroidConfig :: KeytoolConfig -> HM.HashMap Text (NValue t Identity m)
176−keytoolToAndroidConfig conf =
177− let path = nvPath $ _keytoolConfig_keystore conf
178− fromStr = nvStr . principledMakeNixStringWithoutContext . T.pack
179− storepass = fromStr $ _keytoolConfig_storepass conf
180− alias = fromStr $ _keytoolConfig_alias conf
181− keypass = fromStr $ _keytoolConfig_keypass conf
182− in HM.fromList
183− [ ("storeFile", path)
184− , ("storePassword", storepass)
185− , ("keyAlias", alias)
186− , ("keyPassword", keypass)
187− ]
188−
189170data PlatformDeployment = Android | IOS
190171 deriving (Show, Eq)
191172
@@ -200,7 +181,7 @@ deployMobile platform mobileArgs = withProjectRoot "." $ \root -> do
200181 configDir = root </> "config"
201182 exists <- liftIO $ doesDirectoryExist srcDir
202183 unless exists $ failWith "ob test should be run inside of a deploy directory"
203− nixBuildTarget <- case platform of
184+ (nixBuildTarget, extraArgs) <- case platform of
204185 Android -> do
205186 let keystorePath = root </> "android_keystore.jks"
206187 keytoolConfPath = root </> "android_keytool_config.json"
@@ -224,43 +205,53 @@ deployMobile platform mobileArgs = withProjectRoot "." $ \root -> do
224205 checkKeytoolConfExist <- liftIO $ doesFileExist keytoolConfPath
225206 unless checkKeytoolConfExist $ failWith "Missing android KeytoolConfig"
226207 keytoolConfContents <- liftIO $ BSL.readFile keytoolConfPath
227− liftIO $ print keytoolConfContents
228− releaseKey :: String <- case eitherDecode keytoolConfContents :: Either String KeytoolConfig of
208+ keyArgs <- case eitherDecode keytoolConfContents :: Either String KeytoolConfig of
229209 Left err -> failWith $ T.pack err
230− Right conf -> return $
231− (printNix :: MonadObelisk m => NValue t Identity m -> String) $
232− nvSet (keytoolToAndroidConfig conf) HM.empty
233− let expr = mconcat
234− [ "with (import ", srcDir, " {});"
235− , "android.frontend.override (drv: { "
236− , "releaseKey = (if builtins.isNull drv.releaseKey then {} else drv.releaseKey) // " <> releaseKey <> "; "
210+ Right conf -> pure [ "--sign"
211+ , "--store-file", _keytoolConfig_keystore conf
212+ , "--store-password", _keytoolConfig_storepass conf
213+ , "--key-alias", _keytoolConfig_alias conf
214+ , "--key-password", _keytoolConfig_keypass conf]
215+ let expr = unwords
216+ [ "with (import ", toNixPath srcDir, " {});"
217+ , "android.frontend.override (drv: {"
218+ , "isRelease = true;"
219+ , "staticSrc = (passthru.__androidWithConfig ", configDir, ").frontend.staticSrc;"
237220 , "assets = (passthru.__androidWithConfig ", configDir, ").frontend.assets;"
238221 , "})"
239222 ]
240− return $ Target
223+ return (Target
241224 { _target_path = Nothing
242225 , _target_attr = Nothing
243226 , _target_expr = Just expr
244− }
227+ }, keyArgs)
245228 IOS -> do
246229 let expr = mconcat
247− [ "with (import ", srcDir, " {});"
248− , "ios.frontend.override (_: { staticSrc = (passthru.__iosWithConfig ", configDir, ").frontend.staticSrc; })"
230+ [ "with (import ", toNixPath srcDir, " {});"
231+ , "ios.frontend.override (_: { staticSrc = (passthru.__iosWithConfig ", toNixPath configDir, ").frontend.staticSrc; })"
249232 ]
250− return $ Target
233+ return (Target
251234 { _target_path = Nothing
252235 , _target_attr = Nothing
253236 , _target_expr = Just expr
254− }
237+ }, [])
255238 result <- nixCmd $ NixCmd_Build $ def
256239 & nixBuildConfig_outLink .~ OutLink_None
257240 & nixCmdConfig_target .~ nixBuildTarget
258− putLog Notice $ T.pack $ "Your recently built android apk can be found at the following path: " <> show result
259− callProcessAndLogOutput (Notice, Error) $ proc (result </> "bin" </> "deploy") mobileArgs
241+ let mobileArtifact = case platform of
242+ IOS -> "iOS App"
243+ Android -> "Android APK"
244+ putLog Notice $ T.pack $ unwords ["Your recently built", mobileArtifact, "can be found at the following path:", show result]
245+ callProcessAndLogOutput (Notice, Error) $ proc (result </> "bin" </> "deploy") (mobileArgs ++ extraArgs)
260246 where
261− withEcho showEcho f = do
262− prevEcho <- hGetEcho stdin
263− bracket_ (hSetEcho stdin showEcho) (hSetEcho stdin prevEcho) f
247+ withEcho showEcho f = bracket
248+ (do
249+ prevEcho <- hGetEcho stdin
250+ hSetEcho stdin showEcho
251+ pure prevEcho
252+ )
253+ (hSetEcho stdin)
254+ (const f)
264255
265256data KeytoolConfig = KeytoolConfig
266257 { _keytoolConfig_keystore :: FilePath
@@ -275,7 +266,7 @@ instance ToJSON KeytoolConfig
275266createKeystore :: MonadObelisk m => FilePath -> KeytoolConfig -> m ()
276267createKeystore root config = do
277268 let expr = "with (import " <> toImplDir root <> ").reflex-platform.nixpkgs; pkgs.mkShell { buildInputs = [ pkgs.jdk ]; }"
278− callProcessAndLogOutput (Notice,Notice) $ setCwd (Just root) $ proc "nix-shell" ["default.nix", "-E" , expr, "--run" , keytoolCmd]
269+ callProcessAndLogOutput (Notice,Notice) $ setCwd (Just root) $ proc "nix-shell" ["-E" , expr, "--run" , keytoolCmd]
279270 where
280271 keytoolCmd = processToShellString "keytool"
281272 [ "-genkeypair", "-noprompt"