@@ -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
@@ -180,20 +175,6 @@ deployPush deployPath getNixBuilders = do
180175deployUpdate :: MonadObelisk m => FilePath -> m ()
181176deployUpdate deployPath = updateThunkToLatest (ThunkUpdateConfig Nothing (ThunkConfig Nothing)) (deployPath </> "src")
182177
183−keytoolToAndroidConfig :: KeytoolConfig -> HM.HashMap Text (NValue t Identity m)
184−keytoolToAndroidConfig conf =
185− let path = nvPath $ _keytoolConfig_keystore conf
186− fromStr = nvStr . principledMakeNixStringWithoutContext . T.pack
187− storepass = fromStr $ _keytoolConfig_storepass conf
188− alias = fromStr $ _keytoolConfig_alias conf
189− keypass = fromStr $ _keytoolConfig_keypass conf
190− in HM.fromList
191− [ ("storeFile", path)
192− , ("storePassword", storepass)
193− , ("keyAlias", alias)
194− , ("keyPassword", keypass)
195− ]
196−
197178data PlatformDeployment = Android | IOS
198179 deriving (Show, Eq)
199180
@@ -208,7 +189,7 @@ deployMobile platform mobileArgs = withProjectRoot "." $ \root -> do
208189 configDir = root </> "config"
209190 exists <- liftIO $ doesDirectoryExist srcDir
210191 unless exists $ failWith "ob test should be run inside of a deploy directory"
211− nixBuildTarget <- case platform of
192+ (nixBuildTarget, extraArgs) <- case platform of
212193 Android -> do
213194 let keystorePath = root </> "android_keystore.jks"
214195 keytoolConfPath = root </> "android_keytool_config.json"
@@ -232,43 +213,53 @@ deployMobile platform mobileArgs = withProjectRoot "." $ \root -> do
232213 checkKeytoolConfExist <- liftIO $ doesFileExist keytoolConfPath
233214 unless checkKeytoolConfExist $ failWith "Missing android KeytoolConfig"
234215 keytoolConfContents <- liftIO $ BSL.readFile keytoolConfPath
235− liftIO $ print keytoolConfContents
236− releaseKey :: String <- case eitherDecode keytoolConfContents :: Either String KeytoolConfig of
216+ keyArgs <- case eitherDecode keytoolConfContents :: Either String KeytoolConfig of
237217 Left err -> failWith $ T.pack err
238− Right conf -> return $
239− (printNix :: MonadObelisk m => NValue t Identity m -> String) $
240− nvSet (keytoolToAndroidConfig conf) HM.empty
241− let expr = mconcat
242− [ "with (import ", srcDir, " {});"
243− , "android.frontend.override (drv: { "
244− , "releaseKey = (if builtins.isNull drv.releaseKey then {} else drv.releaseKey) // " <> releaseKey <> "; "
218+ Right conf -> pure [ "--sign"
219+ , "--store-file", _keytoolConfig_keystore conf
220+ , "--store-password", _keytoolConfig_storepass conf
221+ , "--key-alias", _keytoolConfig_alias conf
222+ , "--key-password", _keytoolConfig_keypass conf]
223+ let expr = unwords
224+ [ "with (import ", toNixPath srcDir, " {});"
225+ , "android.frontend.override (drv: {"
226+ , "isRelease = true;"
227+ , "staticSrc = (passthru.__androidWithConfig ", configDir, ").frontend.staticSrc;"
245228 , "assets = (passthru.__androidWithConfig ", configDir, ").frontend.assets;"
246229 , "})"
247230 ]
248− return $ Target
231+ return (Target
249232 { _target_path = Nothing
250233 , _target_attr = Nothing
251234 , _target_expr = Just expr
252− }
235+ }, keyArgs)
253236 IOS -> do
254237 let expr = mconcat
255− [ "with (import ", srcDir, " {});"
256− , "ios.frontend.override (_: { staticSrc = (passthru.__iosWithConfig ", configDir, ").frontend.staticSrc; })"
238+ [ "with (import ", toNixPath srcDir, " {});"
239+ , "ios.frontend.override (_: { staticSrc = (passthru.__iosWithConfig ", toNixPath configDir, ").frontend.staticSrc; })"
257240 ]
258− return $ Target
241+ return (Target
259242 { _target_path = Nothing
260243 , _target_attr = Nothing
261244 , _target_expr = Just expr
262− }
245+ }, [])
263246 result <- nixCmd $ NixCmd_Build $ def
264247 & nixBuildConfig_outLink .~ OutLink_None
265248 & nixCmdConfig_target .~ nixBuildTarget
266− putLog Notice $ T.pack $ "Your recently built android apk can be found at the following path: " <> show result
267− callProcessAndLogOutput (Notice, Error) $ proc (result </> "bin" </> "deploy") mobileArgs
249+ let mobileArtifact = case platform of
250+ IOS -> "iOS App"
251+ Android -> "Android APK"
252+ putLog Notice $ T.pack $ unwords ["Your recently built", mobileArtifact, "can be found at the following path:", show result]
253+ callProcessAndLogOutput (Notice, Error) $ proc (result </> "bin" </> "deploy") (mobileArgs ++ extraArgs)
268254 where
269− withEcho showEcho f = do
270− prevEcho <- hGetEcho stdin
271− bracket_ (hSetEcho stdin showEcho) (hSetEcho stdin prevEcho) f
255+ withEcho showEcho f = bracket
256+ (do
257+ prevEcho <- hGetEcho stdin
258+ hSetEcho stdin showEcho
259+ pure prevEcho
260+ )
261+ (hSetEcho stdin)
262+ (const f)
272263
273264data KeytoolConfig = KeytoolConfig
274265 { _keytoolConfig_keystore :: FilePath
@@ -283,7 +274,7 @@ instance ToJSON KeytoolConfig
283274createKeystore :: MonadObelisk m => FilePath -> KeytoolConfig -> m ()
284275createKeystore root config = do
285276 let expr = "with (import " <> toImplDir root <> ").reflex-platform.nixpkgs; pkgs.mkShell { buildInputs = [ pkgs.jdk ]; }"
286− callProcessAndLogOutput (Notice,Notice) $ setCwd (Just root) $ proc "nix-shell" ["default.nix", "-E" , expr, "--run" , keytoolCmd]
277+ callProcessAndLogOutput (Notice,Notice) $ setCwd (Just root) $ proc "nix-shell" ["-E" , expr, "--run" , keytoolCmd]
287278 where
288279 keytoolCmd = processToShellString "keytool"
289280 [ "-genkeypair", "-noprompt"