Skip to content

Commit

Permalink
Basic implementation of stack new #137
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Jun 18, 2015
1 parent a95a57d commit 282034a
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 6 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

* Fall back to cabal dependency solver when a snapshot can't be found
* Basic implementation of `stack new` [#137](https://github.com/commercialhaskell/stack/issues/137)

## 0.0.3

Expand Down
30 changes: 30 additions & 0 deletions new-template/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright (c) 2015

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Your name here nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions new-template/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
6 changes: 6 additions & 0 deletions new-template/app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main where

import Lib

main :: IO ()
main = someFunc
41 changes: 41 additions & 0 deletions new-template/new-template.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: new-template
version: 0.1.0.0
synopsis: Initial project template from stack
description: Please see README.md
homepage: http://github.com/name/project
license: BSD3
license-file: LICENSE
author: Your name here
maintainer: [email protected]
-- copyright:
category: Web
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10

library
hs-source-dirs: src
exposed-modules: Lib
build-depends: base >= 4.7 && < 5
default-language: Haskell2010

executable new-template-exe
hs-source-dirs: app
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base
, new-template
default-language: Haskell2010

test-suite new-template-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
build-depends: base
, new-template
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010

source-repository head
type: git
location: https://github.com/name/project
6 changes: 6 additions & 0 deletions new-template/src/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Lib
( someFunc
) where

someFunc :: IO ()
someFunc = putStrLn "someFunc"
2 changes: 2 additions & 0 deletions new-template/test/Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main :: IO ()
main = putStrLn "Test suite not yet implemented"
40 changes: 37 additions & 3 deletions src/Stack/New.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Stack.New
( newProject
) where

import Control.Monad.IO.Class
import Control.Monad (filterM, forM_, unless)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Logger (MonadLogger, logInfo)
import Data.ByteString (ByteString)
import qualified Data.ByteString as S
import Data.FileEmbed (embedDir)
import Data.Map (Map)
import qualified Data.Map as Map
import qualified Data.Text as T
import System.Directory (createDirectoryIfMissing,
doesFileExist)
import System.FilePath (takeDirectory)

newProject :: MonadIO m => m ()
newProject = error "new command not yet implemented, check out https://github.com/commercialhaskell/stack/issues/137 for status and to get involved"
newProject :: (MonadIO m, MonadLogger m)
=> m ()
newProject = do
$logInfo "NOTE: Currently stack new functionality is very rudimentary"
$logInfo "There are plans to make this feature more useful in the future"
$logInfo "For more information, see: https://github.com/commercialhaskell/stack/issues/137"
$logInfo "For now, we'll just be generating a basic project structure in your current directory"

exist <- filterM (liftIO . doesFileExist) (Map.keys files)
unless (null exist) $
error $ unlines
$ "The following files already exist, refusing to overwrite:"
: map ("- " ++) exist

$logInfo ""
forM_ (Map.toList files) $ \(fp, bs) -> do
$logInfo $ T.pack $ "Writing: " ++ fp
liftIO $ do
createDirectoryIfMissing True $ takeDirectory fp
S.writeFile fp bs

files :: Map FilePath ByteString
files = Map.fromList $(embedDir "new-template")
14 changes: 12 additions & 2 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ main =
buildOpts
addCommand "new"
"Create a brand new project"
(\_ _ -> newProject)
(pure ())
newCmd
initOptsParser
addCommand "init"
"Initialize a stack project based on one or more cabal packages"
initCmd
Expand Down Expand Up @@ -610,3 +610,13 @@ initCmd initOpts go@GlobalOpts{..} = do
Docker.rerunWithOptionalContainer (lcConfig lc) (lcProjectRoot lc) $
runStackT manager globalLogLevel (lcConfig lc) globalTerminal $
initProject globalResolver initOpts

-- | Project creation
newCmd :: InitOpts -> GlobalOpts -> IO ()
newCmd initOpts go@GlobalOpts{..} = do
(manager,lc) <- loadConfigWithOpts go
runStackLoggingT manager globalLogLevel globalTerminal $
Docker.rerunWithOptionalContainer (lcConfig lc) (lcProjectRoot lc) $
runStackT manager globalLogLevel (lcConfig lc) globalTerminal $ do
newProject
initProject globalResolver initOpts
10 changes: 9 additions & 1 deletion stack.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ maintainer: [email protected]
category: Development
build-type: Simple
cabal-version: >=1.10
extra-source-files: README.md ChangeLog.md
homepage: https://github.com/commercialhaskell/stack
extra-source-files: README.md ChangeLog.md

-- Glob would be nice, but apparently Cabal doesn't support it:
-- cabal: filepath wildcard 'test/package-dump/*.txt' does not match any files.
-- Happened during cabal sdist
test/package-dump/ghc-7.8.txt
test/package-dump/ghc-7.10.txt

new-template/src/Lib.hs
new-template/new-template.cabal
new-template/test/Spec.hs
new-template/app/Main.hs
new-template/LICENSE
new-template/Setup.hs

library
hs-source-dirs: src/
ghc-options: -Wall
Expand Down Expand Up @@ -136,6 +143,7 @@ library
, yaml >= 0.8.10.1
, zlib >= 0.5.4.2
, deepseq
, file-embed
if !os(windows)
build-depends: unix >= 2.7.0.1
default-language: Haskell2010
Expand Down

0 comments on commit 282034a

Please sign in to comment.