-
Notifications
You must be signed in to change notification settings - Fork 103
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
Omit false conds #404
Omit false conds #404
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,25 @@ at the Singapore Haskell meetup: http://typeful.net/talks/hpack | |
|
||
## Documentation | ||
|
||
### Handling of `Paths_` modules | ||
|
||
Cabal generates a `Paths_` module for every package. By default Hpack adds | ||
that module to `other-modules` when generating a `.cabal` file. This is | ||
sometimes useful and most of the time not harmful. | ||
|
||
However, there are situations when this can lead to compilation errors (e.g | ||
when using a custom `Prelude`). | ||
|
||
To prevent Hpack from adding the `Paths_` module to `other-modules` add the | ||
following to `package.yaml`: | ||
|
||
```yaml | ||
library: | ||
when: | ||
- condition: false | ||
other-modules: Paths_name # substitute name with the package name | ||
``` | ||
|
||
### Quick-reference | ||
|
||
#### Top-level fields | ||
|
@@ -376,7 +395,6 @@ Conditionals with an else branch: | |
- Must have a `condition` field | ||
- Must have a `then` field, itself an object containing any number of other fields | ||
- Must have a `else` field, itself an object containing any number of other fields | ||
- All other top-level fields are ignored | ||
|
||
For example, | ||
|
||
|
@@ -394,6 +412,8 @@ becomes | |
else | ||
ghc-options: -O0 | ||
|
||
**Note:** Conditionals with `condition: false` are omitted from the generated | ||
`.cabal` file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It also gives us cleaner There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems safe to me! |
||
|
||
### <a name="file-globbing"></a>File globbing | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ module Hpack.Config ( | |
, Library(..) | ||
, Executable(..) | ||
, Conditional(..) | ||
, Cond(..) | ||
, Flag(..) | ||
, SourceRepository(..) | ||
, BuildType(..) | ||
|
@@ -70,7 +71,6 @@ module Hpack.Config ( | |
, renameDependencies | ||
, Empty(..) | ||
, pathsModuleFromPackageName | ||
, Cond(..) | ||
|
||
, LibrarySection(..) | ||
, fromLibrarySectionInConditional | ||
|
@@ -468,17 +468,16 @@ hasKey key (Object o) = HashMap.member key o | |
hasKey _ _ = False | ||
|
||
newtype Condition = Condition { | ||
_conditionCondition :: Cond | ||
conditionCondition :: Cond | ||
} deriving (Eq, Show, Generic, FromValue) | ||
|
||
newtype Cond = Cond String | ||
data Cond = CondBool Bool | CondExpression String | ||
deriving (Eq, Show) | ||
|
||
instance FromValue Cond where | ||
fromValue v = case v of | ||
String s -> return (Cond $ T.unpack s) | ||
Bool True -> return (Cond "true") | ||
Bool False -> return (Cond "false") | ||
String c -> return (CondExpression $ T.unpack c) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this means someone could avoid this new filtering behavior by explicitly passing a string. Like this: when:
- condition: 'false' That's probably fine, and may in fact be desired as a workaround. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, exactly, that's what I thought. |
||
Bool c -> return (CondBool c) | ||
_ -> typeMismatch "Boolean or String" v | ||
|
||
data ThenElse cSources cxxSources jsSources a = ThenElse { | ||
|
@@ -955,7 +954,7 @@ data Section a = Section { | |
} deriving (Eq, Show, Functor, Foldable, Traversable) | ||
|
||
data Conditional a = Conditional { | ||
conditionalCondition :: String | ||
conditionalCondition :: Cond | ||
, conditionalThen :: a | ||
, conditionalElse :: Maybe a | ||
} deriving (Eq, Show, Functor, Foldable, Traversable) | ||
|
@@ -1277,6 +1276,13 @@ getMentionedLibraryModules (LibrarySection _ _ exposedModules generatedExposedMo | |
listModules :: FilePath -> Section a -> IO [Module] | ||
listModules dir Section{..} = concat <$> mapM (getModules dir) sectionSourceDirs | ||
|
||
removeConditionalsThatAreAlwaysFalse :: Section a -> Section a | ||
removeConditionalsThatAreAlwaysFalse sect = sect { | ||
sectionConditionals = filter p $ sectionConditionals sect | ||
} | ||
where | ||
p = (/= CondBool False) . conditionalCondition | ||
|
||
inferModules :: | ||
FilePath | ||
-> String | ||
|
@@ -1286,7 +1292,7 @@ inferModules :: | |
-> ([Module] -> a -> b) | ||
-> Section a | ||
-> IO (Section b) | ||
inferModules dir packageName_ getMentionedModules getInferredModules fromData fromConditionals = traverseSectionAndConditionals | ||
inferModules dir packageName_ getMentionedModules getInferredModules fromData fromConditionals = fmap removeConditionalsThatAreAlwaysFalse . traverseSectionAndConditionals | ||
(fromConfigSection fromData [pathsModuleFromPackageName packageName_]) | ||
(fromConfigSection (\ [] -> fromConditionals) []) | ||
[] | ||
|
@@ -1427,7 +1433,7 @@ toSection packageName_ executableNames = go | |
ThenElseConditional (Product (ThenElse then_ else_) c) -> conditional c <$> (go then_) <*> (Just <$> go else_) | ||
FlatConditional (Product sect c) -> conditional c <$> (go sect) <*> pure Nothing | ||
where | ||
conditional (Condition (Cond c)) = Conditional c | ||
conditional = Conditional . conditionCondition | ||
|
||
type SystemBuildTool = (String, VersionConstraint) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include a note here about how Cabal generates these modules names? I think it replaces hyphens with underscores. I'm not sure if it does anything else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the only thing I'm aware of. Ideally, I would like to link to the Cabal docs on
Path_
, however, I couldn't find anything.@phadej do you know if the Cabal docs cover
Paths_
anywhere?