Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Cabal helper loader #11

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ tags
TAGS
codex.tags
.vim


## test specific ignores
test/testdata/**/stack.yaml
49 changes: 32 additions & 17 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}

module Main where
Expand All @@ -14,24 +15,38 @@ import Hie.Locate
import Hie.Yaml
import System.Directory
import System.Directory.Internal
import System.FilePath.Posix
import System.FilePath
import Hie.CabalHelper
import Distribution.Helper (Ex (..))

main :: IO ()
main = do
pwd <- getCurrentDirectory
files <- listDirectory pwd
let name =
if | any (("dist-newstyle" ==) . takeFileName) files -> "cabal"
| any ((".stack-work" ==) . takeFileName) files -> "stack"
| any (("cabal.project" ==) . takeFileName) files -> "cabal"
| any (("stack.yaml" ==) . takeFileName) files -> "stack"
| otherwise -> "cabal"
cfs <- runMaybeT $ case name of
"cabal" -> cabalPkgs pwd
_ -> stackYamlPkgs pwd
when (null cfs) $ error $
"No .cabal files found under"
<> pwd
<> "\n You may need to run stack build."
pkgs <- catMaybes <$> mapM (nestedPkg pwd) (concat cfs)
putStr <$> hieYaml name $ fmtPkgs name pkgs
projM <- findCabalHelperEntryPoint $ pwd </> "Foo.hs"
Copy link
Owner

@Avi-D-coder Avi-D-coder May 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is okay, but should be optional and behind the cabal-helper and command line flag.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, it is a show-case, it should also not remove the old behaviour as it is waaaaaaaaayyy faster and should be the default mode. So, adding a cli would be sensible, imo, via optparse-applicative

case projM of
Nothing -> error "could not find project context"
Just proj@(Ex p) -> do
env <- initialiseEnvironment p
packages <- loadPackages env
pkgs <- toHieYaml env packages
let name = if | isCabalProject proj -> "cabal"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the logic of isCabalProject compare to | any (("dist-newstyle" ==)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is something to worry about for later. In this case, the project is decided based on the found files, e.g. cabal.project, stack.yaml and *.cabal files. In this order, a project type is selected.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point, we should take build directory into account first so the cabal-helper implicit logic marches the default logic and builds with the users preferred tool.

| isStackProject proj -> "stack"
| otherwise -> error "Neither stack nor cabal project"
putStr $ hieYaml name $ fmtPkgs name pkgs

-- files <- listDirectory pwd
-- let name =
-- if | any (("dist-newstyle" ==) . takeFileName) files -> "cabal"
-- | any ((".stack-work" ==) . takeFileName) files -> "stack"
-- | any (("cabal.project" ==) . takeFileName) files -> "cabal"
-- | any (("stack.yaml" ==) . takeFileName) files -> "stack"
-- | otherwise -> "cabal"
-- cfs <- runMaybeT $ case name of
-- "cabal" -> cabalPkgs pwd
-- _ -> stackYamlPkgs pwd
-- when (null cfs) $ error $
-- "No .cabal files found under"
-- <> pwd
-- <> "\n You may need to run stack build."
-- pkgs <- catMaybes <$> mapM (nestedPkg pwd) (concat cfs)
-- putStr <$> hieYaml name $ fmtPkgs name pkgs
6 changes: 6 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ cradle:

- path: "test"
component: "implicit-hie:test:implicit-hie-test"

- path: test/utils
component: "implicit-hie:test:unit-tests"

- path: test/unit
component: "implicit-hie:test:unit-tests"
69 changes: 58 additions & 11 deletions implicit-hie.cabal
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
cabal-version: 2.0

-- This file has been generated from package.yaml by hpack version 0.33.0.
--
-- see: https://github.com/sol/hpack
--
-- hash: 18f92037a7863d121ac45e847f7dc6177adf0ebc7951dfa1588340f86e64456b

cabal-version: 2.4
name: implicit-hie
version: 0.1.0.0
description: Auto generate a stack or cabal multi component hie.yaml file
Expand All @@ -16,41 +9,68 @@ bug-reports: https://github.com/Avi-D-coder/implicit-hie/issues
author: Avi Dessauer
maintainer: avi.the.coder@gmail.com
copyright: 2020
license: BSD3
license: BSD-3-Clause
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
ChangeLog.md
test/testdata/cabal-helper/**/*.hs
test/testdata/cabal-helper/**/*.cabal
test/testdata/cabal-helper/**/*.project

source-repository head
type: git
location: https://github.com/Avi-D-coder/implicit-hie

flag cabalHelper
Description: Enable Cabal-Helper dependencies
Default: True
Manual: True

common cabal-helper
if flag(cabalHelper)
cpp-options: -DCABAL_HELPER_SUPPORT

library
import: cabal-helper
exposed-modules:
Hie.Cabal.Parser
Hie.Locate
Hie.Logger
Hie.Yaml

if flag(cabalHelper)
exposed-modules:
Hie.CabalHelper
build-depends:
cabal-helper >= 1.1 && <1.2

other-modules:
Paths_implicit_hie
autogen-modules:
Paths_implicit_hie
hs-source-dirs:
src
ghc-options: -Wall -Wincomplete-record-updates -Wincomplete-uni-patterns -fno-warn-unused-imports -fno-warn-unused-binds -fno-warn-name-shadowing -fwarn-redundant-constraints
ghc-options: -Wall -Wincomplete-record-updates -Wincomplete-uni-patterns -fwarn-unused-imports -fwarn-unused-binds -fwarn-name-shadowing -fwarn-redundant-constraints
build-depends:
attoparsec >= 0.13
aeson
, attoparsec >= 0.13
, base >=4.7 && <5
, containers
, directory >= 1.3
, filepath >= 1.4
, filepattern >= 0.1
, hie-bios >=0.5 && <0.6
, hslogger
, text >= 1.2
, transformers >= 0.5
, process
, yaml >= 0.5
default-language: Haskell2010

executable gen-hie
import: cabal-helper
main-is: Main.hs
other-modules:
Paths_implicit_hie
Expand All @@ -62,16 +82,19 @@ executable gen-hie
build-depends:
attoparsec
, base >=4.7 && <5
, cabal-helper
, directory
, filepath
, filepattern
, implicit-hie
, optparse-applicative
, text
, transformers
, yaml
default-language: Haskell2010

test-suite implicit-hie-test
import: cabal-helper
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Expand All @@ -94,3 +117,27 @@ test-suite implicit-hie-test
, transformers
, yaml
default-language: Haskell2010

test-suite unit-tests
import: cabal-helper
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_implicit_hie, TestUtils
if flag(cabalHelper)
other-modules:
CabalHelperSpec
hs-source-dirs:
test/unit, test/utils
ghc-options: -Wall -Wincomplete-record-updates -Wincomplete-uni-patterns -fno-warn-unused-imports -fno-warn-unused-binds -fno-warn-name-shadowing -fwarn-redundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
aeson
, base
, directory
, filepath
, hie-bios
, hspec
, implicit-hie
, text
, yaml
default-language: Haskell2010
2 changes: 1 addition & 1 deletion src/Hie/Cabal/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Indent = Int
data Package = Package Name [Component]
deriving (Show, Eq, Ord)

data CompType = Lib | Exe | Test | Bench
data CompType = Lib | Exe | Test | Bench | FLib
deriving (Show, Eq, Ord)

data Component
Expand Down
Loading