@@ -3,23 +3,34 @@
33{-# LANGUAGE OverloadedStrings #-}
44{-# LANGUAGE ScopedTypeVariables #-}
55{-# LANGUAGE ViewPatterns #-}
6+{-# LANGUAGE DeriveGeneric #-}
7+{-# LANGUAGE TypeApplications #-}
68module Obelisk.Command.Deploy where
79
810import Control.Lens
911import Control.Monad
10−import Control.Monad.Catch (Exception (displayException), MonadThrow, throwM, try)
12+import Control.Monad.Catch (Exception (displayException), MonadThrow, throwM, try, bracket_)
1113import Control.Monad.IO.Class (liftIO)
14+import Data.Aeson (FromJSON, ToJSON, encode, eitherDecode)
1215import Data.Bits
16+import qualified Data.ByteString.Lazy as BSL
1317import Data.Default
18+import qualified Data.HashMap.Strict as HM
1419import qualified Data.Map as Map
20+import Data.Text (Text)
1521import qualified Data.Set as Set
1622import qualified Data.Text as T
1723import qualified Data.Text.IO as T
24+import GHC.Generics
25+import Nix.Convert
26+import Nix.Pretty
27+import Nix.Value
1828import System.Directory
1929import System.Environment (getEnvironment)
2030import System.FilePath
31+import System.IO
2132import System.Posix.Files
22−import System.Process (delegate_ctlc, env, proc)
33+import System.Process (delegate_ctlc, env, proc, cwd)
2334import Text.URI (URI)
2435import qualified Text.URI as URI
2536import Text.URI.Lens
@@ -157,25 +168,119 @@ deployPush deployPath getNixBuilders = do
157168deployUpdate :: MonadObelisk m => FilePath -> m ()
158169deployUpdate deployPath = updateThunkToLatest $ deployPath </> "src"
159170
160−deployMobile :: MonadObelisk m => String -> [String] -> m ()
171+keytoolToAndroidConfig :: KeytoolConfig -> HM.HashMap Text (NValueNF Identity)
172+keytoolToAndroidConfig conf = runIdentity $ do
173+ path <- toValue $ Path $ _keytoolConfig_keystore conf
174+ storepass <- toValue $ T.pack $ _keytoolConfig_storepass conf
175+ alias <- toValue $ T.pack $ _keytoolConfig_alias conf
176+ keypass <- toValue $ T.pack $ _keytoolConfig_keypass conf
177+ return $ HM.fromList
178+ [ ("storeFile", path)
179+ , ("storePassword", storepass)
180+ , ("keyAlias", alias)
181+ , ("keyPassword", keypass)
182+ ]
183+
184+data PlatformDeployment = Android | IOS
185+ deriving (Show, Eq)
186+
187+renderPlatformDeployment :: PlatformDeployment -> String
188+renderPlatformDeployment = \case
189+ Android -> "android"
190+ IOS -> "ios"
191+
192+deployMobile :: MonadObelisk m => PlatformDeployment -> [String] -> m ()
161193deployMobile platform mobileArgs = withProjectRoot "." $ \root -> do
162194 let srcDir = root </> "src"
163195 configDir = root </> "config"
164196 exists <- liftIO $ doesDirectoryExist srcDir
165197 unless exists $ failWith "ob test should be run inside of a deploy directory"
166− -- result <- nixBuildAttrWithCache srcDir $ platform <> ".frontend"
167− let expr = mconcat
168− [ "with (import ", srcDir, " {});"
169− -- , "android.frontend.override (_: { staticSrc = (passthru.__android ", configDir, ").frontend.staticSrc; })"
170− , "ios.frontend.override (_: { staticSrc = (passthru.__ios ", configDir, ").frontend.staticSrc; })"
171− ]
172− target = Target
198+ nixBuildTarget <- case platform of
199+ Android -> do
200+ let keystorePath = root </> "android_keystore.jks"
201+ keytoolConfPath = root </> "android_keytool_config.json"
202+ hasKeystore <- liftIO $ doesFileExist keystorePath
203+ when (not hasKeystore) $ do
204+ -- TODO log instructions for how to modify the keystore
205+ putLog Notice $ "Creating keystore: " <> T.pack keystorePath
206+ putLog Notice "Enter a keystore password: "
207+ keyStorePassword <- liftIO $ withEcho False getLine
208+ putLog Notice "Re-enter the keystore password: "
209+ keyStorePassword' <- liftIO $ withEcho False getLine
210+ unless (keyStorePassword' == keyStorePassword) $ failWith "passwords do not match"
211+ let keyToolConf = KeytoolConfig
212+ { _keytoolConfig_keystore = keystorePath
213+ , _keytoolConfig_alias = "obelisk"
214+ , _keytoolConfig_storepass = keyStorePassword
215+ , _keytoolConfig_keypass = keyStorePassword
216+ }
217+ createKeystore root $ keyToolConf
218+ liftIO $ BSL.writeFile keytoolConfPath $ encode keyToolConf
219+ checkKeytoolConfExist <- liftIO $ doesFileExist keytoolConfPath
220+ unless checkKeytoolConfExist $ failWith "Missing android KeytoolConfig"
221+ keytoolConfContents <- liftIO $ BSL.readFile keytoolConfPath
222+ liftIO $ putStrLn $ show keytoolConfContents
223+ releaseKey <- case eitherDecode keytoolConfContents of
224+ Left err -> failWith $ T.pack err
225+ Right conf -> do
226+ let nvset = toValue @(HM.HashMap Text (NValueNF Identity)) @Identity @(NValueNF Identity) $ keytoolToAndroidConfig conf
227+ return $ printNix $ runIdentity nvset
228+ let expr = mconcat
229+ [ "with (import ", srcDir, " {});"
230+ , "android.frontend.override (drv: { "
231+ , "releaseKey = (if builtins.isNull drv.releaseKey then {} else drv.releaseKey) // " <> releaseKey <> "; "
232+ , "staticSrc = (passthru.__android ", configDir, ").frontend.staticSrc;"
233+ , "})"
234+ ]
235+ return $ Target
236+ { _target_path = Nothing
237+ , _target_attr = Nothing
238+ , _target_expr = Just expr
239+ }
240+ IOS -> do
241+ let expr = mconcat
242+ [ "with (import ", srcDir, " {});"
243+ , "ios.frontend.override (_: { staticSrc = (passthru.__ios ", configDir, ").frontend.staticSrc; })"
244+ ]
245+ return $ Target
173246 { _target_path = Nothing
174247 , _target_attr = Nothing
175248 , _target_expr = Just expr
176249 }
177− result <- nixCmd $ NixCmd_Build $ def & nixBuildConfig_common .~ (def & nixCmdConfig_target .~ target)
250+ result <- nixCmd $ NixCmd_Build $ def
251+ & nixBuildConfig_outLink .~ OutLink_None
252+ & nixCmdConfig_target .~ nixBuildTarget
253+ putLog Notice $ T.pack $ "Your recently built android apk can be found at the following path: " <> (show result)
178254 callProcessAndLogOutput (Notice, Error) $ proc (result </> "bin" </> "deploy") mobileArgs
255+ where
256+ withEcho showEcho f = do
257+ prevEcho <- hGetEcho stdin
258+ bracket_ (hSetEcho stdin showEcho) (hSetEcho stdin prevEcho) f
259+
260+data KeytoolConfig = KeytoolConfig
261+ { _keytoolConfig_keystore :: FilePath
262+ , _keytoolConfig_alias :: String
263+ , _keytoolConfig_storepass :: String
264+ , _keytoolConfig_keypass :: String
265+ } deriving (Show, Generic)
266+
267+instance FromJSON KeytoolConfig
268+instance ToJSON KeytoolConfig
269+
270+createKeystore :: MonadObelisk m => FilePath -> KeytoolConfig -> m ()
271+createKeystore root config = do
272+ let expr = "with (import " <> toImplDir root <> ").reflex-platform.nixpkgs; pkgs.mkShell { buildInputs = [ pkgs.jdk ]; }"
273+ callProcessAndLogOutput (Notice,Notice) $ (proc "nix-shell" ["-E" , expr, "--run" , keytoolCmd]) { cwd = Just root }
274+ where
275+ keytoolCmd = processToShellString "keytool"
276+ [ "-genkeypair", "-noprompt"
277+ , "-keystore", _keytoolConfig_keystore config
278+ , "-keyalg", "RSA", "-keysize", "2048"
279+ , "-validity", "1000000000"
280+ , "-storepass", _keytoolConfig_storepass config
281+ , "-alias", _keytoolConfig_alias config
282+ , "-keypass", _keytoolConfig_keypass config
283+ ]
179284
180285-- | Simplified deployment configuration mechanism. At one point we may revisit this.
181286writeDeployConfig :: MonadObelisk m => FilePath -> FilePath -> String -> m ()