@@ -136,6 +136,7 @@ findPackageType rootDir = do
136
136
return $ fromMaybe NoPackage $ asum [HpackPackage <$> mHpack, CabalPackage <$> mCabal]
137
137
138
138
-- | Edit a hpack package to add the given package to the package.yaml.
139
+ -- If package.yaml is not in an expected format, will fail fatally.
139
140
--
140
141
-- Currently does not preserve format.
141
142
-- Keep an eye out on this other GSOC project!
@@ -155,19 +156,29 @@ editHpackPackage fp modulePath pkgName = do
155
156
156
157
case Y. decodeThrow contents :: Maybe Object of
157
158
Just obj -> do
159
+ -- Map over all major components, such as "executable", "executables",
160
+ -- "tests" and "benchmarks". Note, that "library" is a major component,
161
+ -- but its structure is different and can not be mapped over in the same way.
162
+ --
163
+ -- Only adds the package if the declared "source-dirs" field is part of the
164
+ -- module path, or if no "source-dirs" is declared.
158
165
let compsMapped = mapComponentTypes (ensureObject $ mapComponents (ensureObject $ mapCompDependencies addDep)) obj
159
166
160
- let addDepToMainLib = fromMaybe True $ do
161
- Object lib <- HM. lookup " library " compsMapped
162
- sourceDirs <- HM. lookup " source-dirs " lib
163
- return $ isInSourceDir sourceDirs
167
+ -- Is there a global "dependencies" yaml object?
168
+ let addDepToMainDep = fromMaybe False $ do
169
+ Array _ <- HM. lookup " dependencies " compsMapped
170
+ return True
164
171
165
- let newPkg = if addDepToMainLib
166
- then mapMainDependencies addDep compsMapped
167
- else compsMapped
172
+ -- Either add the package to only the top-level "dependencies",
173
+ -- or to all main components of which the given module is part of.
174
+ let newPkg
175
+ | addDepToMainDep = mapMainDependencies addDep obj
176
+ -- Map over the library component at last, since it has different structure.
177
+ | otherwise = mapLibraryDependency addDep compsMapped
168
178
169
- newPkgText = T. decodeUtf8 $ Y. encode newPkg
179
+ let newPkgText = T. decodeUtf8 $ Y. encode newPkg
170
180
181
+ -- Construct the WorkSpaceEdit
171
182
let numOldLines = length $ T. lines $ T. decodeUtf8 contents
172
183
range = J. Range (J. Position 0 0 ) (J. Position numOldLines 0 )
173
184
textEdit = J. TextEdit range newPkgText
@@ -187,8 +198,14 @@ editHpackPackage fp modulePath pkgName = do
187
198
mapMainDependencies :: (Value -> Value ) -> Object -> Object
188
199
mapMainDependencies f o =
189
200
let g :: T. Text -> Value -> Value
190
- g " dependencies" x = f x
191
- g " library" (Y. Object o') = Y. Object (mapMainDependencies f o')
201
+ g " dependencies" = f
202
+ g _ = id
203
+ in HM. mapWithKey g o
204
+
205
+ mapLibraryDependency :: (Value -> Value ) -> Object -> Object
206
+ mapLibraryDependency f o =
207
+ let g :: T. Text -> Value -> Value
208
+ g " library" (Y. Object o') = Y. Object (mapCompDependencies f o')
192
209
g _ x = x
193
210
in HM. mapWithKey g o
194
211
0 commit comments