Skip to content

Commit

Permalink
[#246] Put common parts of the project inside common stanza (#408)
Browse files Browse the repository at this point in the history
* [#246] Put common parts of the project inside `common` stanza

Resolves #246

* Make HLint happy

* Update README.md

Co-Authored-By: Dmitrii Kovanikov <kovanikov@gmail.com>

Co-authored-by: Dmitrii Kovanikov <kovanikov@gmail.com>
  • Loading branch information
vrom911 and chshersh authored Feb 20, 2020
1 parent 1954789 commit 55e4ed3
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 134 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ The changelog is available [on GitHub][2].
(by [@chshersh](https://github.com/chshersh))
* __#TUI__ Allow `brick-0.52`.
(by [@vrom911](https://github.com/vrom911))
* [#246](https://github.com/kowainik/summoner/issues/246):
Put common fields into common stanza `common-options`.
* Bump default `cabal` version to `2.4` in the generated project.

## 1.4.0.0 – Dec 25, 2019 🎅

Expand Down
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Features related to the structure and content of the generated projects.

+ Support for Cabal and Stack build tools.
+ Ability to pick stanzas (library, executable, test-suite, benchmark).
+ Usage of [common stanza](https://vrom911.github.io/blog/common-stanzas) to reduce `.cabal` file's boilerplate.
+ Option to include an alternative prelude, if desired. The project would then
use [`base-noprelude` technique](http://hackage.haskell.org/package/Prelude),
and the `Prelude` module would be added to the library target.
Expand Down Expand Up @@ -189,7 +190,7 @@ To start using Summoner make sure that you have the following tools installed on

We also have minimal version requirements for build tools:

* [Cabal >= 2.0](https://www.haskell.org/cabal/)
* [Cabal >= 2.4](https://www.haskell.org/cabal/)
* [Stack >= 2.1](http://haskellstack.org)

However, it is always recommended to use the newest versions of build tools.
Expand Down Expand Up @@ -223,8 +224,8 @@ mv summon-cli-linux ~/.local/bin/summon
Using `cabal`:

```shell
cabal new-update
cabal new-install summoner-tui
cabal v2-update
cabal v2-install summoner-tui
```

Using `stack`:
Expand Down Expand Up @@ -256,7 +257,7 @@ cd summoner
Build and install using `cabal`:

```shell
cabal new-install summoner-tui:exe:summon-tui
cabal v2-install summoner-tui:exe:summon-tui
```

Build and install using `stack`:
Expand All @@ -283,8 +284,8 @@ mv summon-cli-linux ~/.local/bin/summon
Using `cabal`:

```shell
cabal new-update
cabal new-install summoner
cabal v2-update
cabal v2-install summoner
```

Using `stack`:
Expand Down Expand Up @@ -316,7 +317,7 @@ cd summoner
Build and install using `cabal`:

```shell
cabal new-install summoner-cli:exe:summon
cabal v2-install summoner-cli:exe:summon
```

Build and install using `stack`:
Expand Down Expand Up @@ -617,7 +618,7 @@ On Linux, to build the `summoner-tui` you'll need to have `libtinfo` installed.
To build the project you can use the following commands:

```shell=
cabal new-build all
cabal v2-build all
```

for Cabal and
Expand All @@ -635,7 +636,7 @@ Summoner uses the golden tests technique for assuring the correctness of the gen
To actually run tests you need to run:

```shell
cabal new-test all
cabal v2-test all
```

or, if using Stack
Expand All @@ -649,8 +650,8 @@ stack test
Building Summoner with the recommended method creates two executables: `summon` and `summon-tui`. To run the compiled executable you can use the following commands:

```shell
cabal new-exec summon -- SOME_COMMAND
cabal new-exec summon-tui -- SOME_COMMAND
cabal v2-exec summon -- SOME_COMMAND
cabal v2-exec summon-tui -- SOME_COMMAND
```

or, if using Stack
Expand Down
2 changes: 1 addition & 1 deletion summoner-cli/src/Summoner/Default.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defaultGHC = maxBound

-- | Default version of the Cabal.
defaultCabal :: Text
defaultCabal = "2.0"
defaultCabal = "2.4"

defaultTomlFile :: FilePath
defaultTomlFile = ".summoner.toml"
Expand Down
114 changes: 55 additions & 59 deletions summoner-cli/src/Summoner/Template/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ cabalFile Settings{..} = File (toString settingsRepo ++ ".cabal") cabalFileConte
cabalFileContent = T.concat
[ cabalHeader
, memptyIfFalse settingsGitHub sourceRepository
, memptyIfFalse settingsIsLib libraryStanza
, memptyIfFalse settingsIsExe $ executableStanza $ memptyIfFalse settingsIsLib $ ", " <> settingsRepo
, memptyIfFalse settingsTest $ testSuiteStanza $ memptyIfFalse settingsIsLib $ ", " <> settingsRepo
, memptyIfFalse settingsBench $ benchmarkStanza $ memptyIfFalse settingsIsLib $ ", " <> settingsRepo
, commonStanza
, memptyIfFalse settingsIsLib libraryStanza
, memptyIfFalse settingsIsExe executableStanza
, memptyIfFalse settingsTest testSuiteStanza
, memptyIfFalse settingsBench $ benchmarkStanza $ memptyIfFalse settingsIsLib $ ", " <> settingsRepo
]

-- TODO: do something to not have empty lines
Expand All @@ -58,7 +59,7 @@ cabalFile Settings{..} = File (toString settingsRepo ++ ".cabal") cabalFileConte
[ "category: " <> settingsCategories | "" /= settingsCategories ] ++
[ "build-type: Simple"
, "extra-doc-files: README.md"
, " , CHANGELOG.md"
, " CHANGELOG.md"
, "tested-with: " <> testedGhcs
]

Expand All @@ -72,7 +73,7 @@ cabalFile Settings{..} = File (toString settingsRepo ++ ".cabal") cabalFileConte

testedGhcs :: Text
testedGhcs = intercalateMap
("\n" <> T.replicate 19 " " <> ", ")
("\n" <> T.replicate 21 " ")
(mappend "GHC == " . showGhcVer)
settingsTestedVersions

Expand All @@ -85,6 +86,21 @@ cabalFile Settings{..} = File (toString settingsRepo ++ ".cabal") cabalFileConte
location: ${githubUrl}.git
|]


commonStanza :: Text
commonStanza =
[text|
$endLine
common common-options
build-depends: $settingsBaseType $baseBounds
$commaPreludeLibrary

ghc-options: -Wall
$ghcOptions

default-language: Haskell2010
|] <> defaultExtensions

baseBounds :: Text
baseBounds = cabalBaseVersions settingsTestedVersions

Expand All @@ -93,85 +109,65 @@ cabalFile Settings{..} = File (toString settingsRepo ++ ".cabal") cabalFileConte
[text|
$endLine
library
import: common-options
hs-source-dirs: src
exposed-modules: $libModuleName
$preludeMod
|]

build-depends: $settingsBaseType $baseBounds
$commaPreludeLibrary

ghc-options: -Wall
$ghcOptions

default-language: Haskell2010
|] <> defaultExtensions

executableStanza :: Text -> Text
executableStanza commaRepo =
executableStanza :: Text
executableStanza =
[text|
$endLine
executable $settingsRepo
import: common-options
hs-source-dirs: app
main-is: Main.hs
$buildDepends
$rtsOptions
|]

build-depends: $settingsBaseType $baseBounds
$commaRepo
$commaPreludeLibrary

ghc-options: -Wall
-threaded
-rtsopts
-with-rtsopts=-N
$ghcOptions

default-language: Haskell2010
|] <> defaultExtensions

testSuiteStanza :: Text -> Text
testSuiteStanza commaRepo =
testSuiteStanza :: Text
testSuiteStanza =
[text|
$endLine
test-suite ${settingsRepo}-test
import: common-options
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs

build-depends: $settingsBaseType $baseBounds
$commaRepo
$commaPreludeLibrary

ghc-options: -Wall
-threaded
-rtsopts
-with-rtsopts=-N
$ghcOptions

default-language: Haskell2010
|] <> defaultExtensions
$buildDepends
$rtsOptions
|]

benchmarkStanza :: Text -> Text
benchmarkStanza commaRepo =
[text|
$endLine
benchmark ${settingsRepo}-benchmark
import: common-options
type: exitcode-stdio-1.0
hs-source-dirs: benchmark
main-is: Main.hs

build-depends: $settingsBaseType $baseBounds
, gauge
build-depends: gauge
$commaRepo
$commaPreludeLibrary

ghc-options: -Wall
-threaded
-rtsopts
-with-rtsopts=-N
$ghcOptions

default-language: Haskell2010
|] <> defaultExtensions

$rtsOptions
|]

-- | @build-depends@ for the repo, only if the library is on.
buildDepends :: Text
buildDepends =
if settingsIsLib
then "build-depends: " <> settingsRepo
else ""

rtsOptions :: Text
rtsOptions =
[text|
ghc-options: -threaded
-rtsopts
-with-rtsopts=-N
|]

preludeMod, commaPreludeLibrary :: Text
(preludeMod, commaPreludeLibrary) = case settingsPrelude of
Expand Down
75 changes: 22 additions & 53 deletions summoner-cli/test/golden/fullProject/fullProject.cabal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cabal-version: 2.0
cabal-version: 2.4
name: fullProject
version: 0.0.0.0
synopsis: Full test project
Expand All @@ -13,22 +13,18 @@ copyright: 2018 Kowainik
category: Testing
build-type: Simple
extra-doc-files: README.md
, CHANGELOG.md
CHANGELOG.md
tested-with: GHC == 8.0.2
, GHC == 8.2.2
, GHC == 8.4.4
, GHC == 8.6.5
, GHC == 8.8.2
GHC == 8.2.2
GHC == 8.4.4
GHC == 8.6.5
GHC == 8.8.2

source-repository head
type: git
location: https://github.com/kowainik/fullProject.git

library
hs-source-dirs: src
exposed-modules: FullProject
Prelude

common common-options
build-depends: base-noprelude >= 4.9.1.0 && < 4.14
, relude

Expand All @@ -41,65 +37,38 @@ library
LambdaCase
OverloadedStrings

library
import: common-options
hs-source-dirs: src
exposed-modules: FullProject
Prelude

executable fullProject
import: common-options
hs-source-dirs: app
main-is: Main.hs

build-depends: base-noprelude >= 4.9.1.0 && < 4.14
, fullProject
, relude

ghc-options: -Wall
-threaded
build-depends: fullProject
ghc-options: -threaded
-rtsopts
-with-rtsopts=-N
-Wcompat
-Widentities

default-language: Haskell2010
default-extensions: ConstraintKinds
LambdaCase
OverloadedStrings

test-suite fullProject-test
import: common-options
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs

build-depends: base-noprelude >= 4.9.1.0 && < 4.14
, fullProject
, relude

ghc-options: -Wall
-threaded
build-depends: fullProject
ghc-options: -threaded
-rtsopts
-with-rtsopts=-N
-Wcompat
-Widentities

default-language: Haskell2010
default-extensions: ConstraintKinds
LambdaCase
OverloadedStrings

benchmark fullProject-benchmark
import: common-options
type: exitcode-stdio-1.0
hs-source-dirs: benchmark
main-is: Main.hs

build-depends: base-noprelude >= 4.9.1.0 && < 4.14
, gauge
build-depends: gauge
, fullProject
, relude

ghc-options: -Wall
-threaded
ghc-options: -threaded
-rtsopts
-with-rtsopts=-N
-Wcompat
-Widentities

default-language: Haskell2010
default-extensions: ConstraintKinds
LambdaCase
OverloadedStrings
Loading

0 comments on commit 55e4ed3

Please sign in to comment.