Skip to content

Commit 4665c67

Browse files
committed
Add Android Export Handler
- exportAndroid handler which initializes new Cordova project Add a new handler, `exportAndroid`. This takes the source as a parameter, and performs the same steps that the compile route performs, and calls `compileIfNeeded`. After compilation, the `initCordovaProject` function is invoked, which merely creates a stub cordova project, by making a subprocess call to the `cordova` executable. At this commit, it does not build the project.
1 parent 7ee57b6 commit 4665c67

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
{-
4+
Copyright 2017 The CodeWorld Authors. All rights reserved.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-}
18+
19+
module AndroidExport where
20+
21+
import System.Process
22+
import System.Directory
23+
import System.FilePath
24+
25+
import Util
26+
27+
initCordovaProject :: BuildMode -> ProgramId ->IO ()
28+
initCordovaProject mode programId = do
29+
putStrLn $ androidRootDir mode
30+
checkIfBuildExists <- doesDirectoryExist $ androidRootDir mode </> sourceBase programId
31+
case checkIfBuildExists of
32+
True -> do
33+
putStrLn "Build Exists"
34+
False -> do
35+
createDirectory $ androidRootDir mode </> sourceBaseDir programId
36+
readProcess "cordova" ["create", (androidRootDir mode </> sourceBase programId)] ""
37+
putStrLn "Build Doesn't Exist"

codeworld-server/src/Main.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
module Main where
2323

2424
import Compile
25+
import AndroidExport
2526
import Control.Applicative
2627
import Control.Monad
2728
import Control.Monad.Trans
@@ -120,6 +121,7 @@ site clientId =
120121
("shareContent", shareContentHandler clientId),
121122
("moveProject", moveProjectHandler clientId),
122123
("compile", compileHandler),
124+
("exportAndroid", exportAndroidHandler),
123125
("saveXMLhash", saveXMLHashHandler),
124126
("loadXML", loadXMLHandler),
125127
("loadSource", loadSourceHandler),
@@ -354,6 +356,21 @@ runHandler = do
354356
modifyResponse $ setContentType "text/javascript"
355357
serveFile (buildRootDir mode </> targetFile programId)
356358

359+
exportAndroidHandler :: Snap()
360+
exportAndroidHandler = do
361+
mode <- getBuildMode
362+
Just source <- getParam "source"
363+
let programId = sourceToProgramId source
364+
deployId = sourceToDeployId source
365+
success <- liftIO $ do
366+
ensureProgramDir mode programId
367+
B.writeFile (buildRootDir mode </> sourceFile programId) source
368+
writeDeployLink mode deployId programId
369+
compileIfNeeded mode programId
370+
unless success $ modifyResponse $ setResponseCode 500
371+
modifyResponse $ setContentType "text/plain"
372+
liftIO $ initCordovaProject mode programId
373+
357374
runMessageHandler :: Snap ()
358375
runMessageHandler = do
359376
mode <- getBuildMode

codeworld-server/src/Util.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import System.Posix.Files
4141

4242
import Model
4343

44-
newtype BuildMode = BuildMode String deriving Eq
4544
newtype ProgramId = ProgramId { unProgramId :: Text } deriving Eq
45+
newtype BuildMode = BuildMode String deriving Eq
4646
newtype ProjectId = ProjectId { unProjectId :: Text } deriving Eq
4747
newtype DeployId = DeployId { unDeployId :: Text } deriving Eq
4848
newtype DirId = DirId { unDirId :: Text} deriving Eq
@@ -57,6 +57,9 @@ clientIdPath = "web/clientId.txt"
5757
buildRootDir :: BuildMode -> FilePath
5858
buildRootDir (BuildMode m) = "data" </> m </> "user"
5959

60+
androidRootDir :: BuildMode -> FilePath
61+
androidRootDir (BuildMode m) = "data" </> m </> "android"
62+
6063
shareRootDir :: BuildMode -> FilePath
6164
shareRootDir (BuildMode m) = "data" </> m </> "share"
6265

@@ -66,6 +69,9 @@ projectRootDir (BuildMode m) = "data" </> m </> "projects"
6669
deployRootDir :: BuildMode -> FilePath
6770
deployRootDir (BuildMode m) = "data" </> m </> "deploy"
6871

72+
sourceBaseDir :: ProgramId -> FilePath
73+
sourceBaseDir (ProgramId p) = let s = T.unpack p in take 3 s
74+
6975
sourceBase :: ProgramId -> FilePath
7076
sourceBase (ProgramId p) = let s = T.unpack p in take 3 s </> s
7177

0 commit comments

Comments
 (0)