1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
module CPM.Diff.API
( compareModulesFromPackages
, compareModulesFromPackageAndDir
, compareModulesInDirs
, compareApiModule
, getBaseTemp
, Differences
, Difference (..)
, showDifferences
) where
import System.Directory ( getTemporaryDirectory )
import System.FilePath ( (</>) )
import Data.List ( nub )
import Data.Maybe ( listToMaybe, catMaybes )
import Prelude hiding ( empty )
import AbstractCurry.Types ( CurryProg (..), CFuncDecl (..), CTypeDecl (..)
, COpDecl (..), QName, CFixity (..)
, CVisibility (..))
import AbstractCurry.Pretty
import AbstractCurry.Select ( functions, funcName, types, typeName )
import Text.Pretty ( pPrint, text, (<+>), vcat, empty, red, ($$) )
import CPM.AbstractCurry ( readAbstractCurryFromPackagePath )
import CPM.Config ( Config )
import CPM.ErrorLogger
import CPM.FileUtil ( copyDirectory, recreateDirectory, tempDir )
import CPM.Package ( Package, Version, packageId, loadPackageSpec
, exportedModules)
import CPM.PackageCache.Global as GC
import CPM.PackageCopy ( resolveAndCopyDependencies )
import CPM.Repository ( Repository )
getBaseTemp :: IO String
getBaseTemp = do
tmpDir <- tempDir
let tmp = tmpDir </> "diff"
recreateDirectory tmp
return tmp
compareModulesFromPackages :: Config -> Repository -> GC.GlobalCache -> String
-> Version -> String -> Version -> Maybe [String]
-> ErrorLogger [(String, Differences)]
compareModulesFromPackages cfg repo gc nameA verA nameB verB onlyMods = do
baseTmp <- liftIOEL getBaseTemp
pkgA <- GC.tryFindPackage gc nameA verA
pkgB <- GC.tryFindPackage gc nameB verB
GC.copyPackage cfg pkgA baseTmp
GC.copyPackage cfg pkgB baseTmp
compareModulesInDirs cfg repo gc (baseTmp </> packageId pkgA)
(baseTmp </> packageId pkgB) onlyMods
compareModulesFromPackageAndDir :: Config -> Repository -> GC.GlobalCache
-> String -> String -> Version -> Maybe [String]
-> ErrorLogger [(String, Differences)]
compareModulesFromPackageAndDir cfg repo gc dirA nameB verB onlyMods = do
baseTmp <- liftIOEL getBaseTemp
pkgB <- GC.tryFindPackage gc nameB verB
pkgA <- loadPackageSpec dirA
GC.copyPackage cfg pkgB baseTmp
liftIOEL $ copyDirectory dirA (baseTmp </> packageId pkgA)
compareModulesInDirs cfg repo gc (baseTmp </> packageId pkgA)
(baseTmp </> packageId pkgB) onlyMods
compareModulesInDirs :: Config -> Repository -> GC.GlobalCache -> String
-> String -> Maybe [String]
-> ErrorLogger [(String, Differences)]
compareModulesInDirs cfg repo gc dirA dirB onlyMods =
loadPackageSpec dirA >>= \pkgA ->
loadPackageSpec dirB >>= \pkgB ->
resolveAndCopyDependencies cfg repo gc dirA >>= \depsA ->
resolveAndCopyDependencies cfg repo gc dirB >>= \depsB ->
let cmpmods = nub (exportedModules pkgA ++ exportedModules pkgB) in
if null cmpmods
then logInfo "No exported modules to compare" >> return []
else do diffs <- liftIOEL $ mapM (compareApiModule
pkgA dirA depsA pkgB dirB depsB) cmpmods
let modsWithDiffs = zip cmpmods diffs
return $ case onlyMods of
Nothing -> modsWithDiffs
Just ms -> filter ((`elem` ms) . fst) modsWithDiffs
compareApiModule :: Package -> String -> [Package] -> Package -> String
-> [Package] -> String -> IO Differences
compareApiModule pkgA dirA depsA pkgB dirB depsB mod =
if mod `elem` exportedModules pkgA
then
if mod `elem` exportedModules pkgB
then
readAbstractCurryFromPackagePath pkgA dirA depsA mod
>>= \prog1 ->
readAbstractCurryFromPackagePath pkgB dirB depsB mod
>>= \prog2 ->
let funcDiffs = diffFuncsFiltered funcIsPublic prog1 prog2
typeDiffs = diffTypesFiltered typeIsPublic prog1 prog2
opDiffs = diffOpsFiltered (\_ _ -> True) prog1 prog2
in return $ (Nothing, funcDiffs, typeDiffs, opDiffs)
else return $ (Just $ Addition mod, [], [], [])
else return $ (Just $ Removal mod, [], [], [])
type Differences = ( Maybe (Difference String)
, [Difference CFuncDecl]
, [Difference CTypeDecl]
, [Difference COpDecl]
)
data Difference a = Addition a
| Removal a
| Change a a
showDifferences :: [Differences] -> Version -> Version -> String
showDifferences diffs verA verB = pPrint $
vcat (map showDifferences' diffs)
where
jump = versionJump verA verB
showDifferences' (modDiff, funcDiffs, typeDiffs, opDiffs) =
(modText modDiff)
$$ (vcat $ funcTexts funcDiffs)
$$ (vcat $ typeTexts typeDiffs)
$$ (vcat $ opTexts opDiffs)
showViolation (Addition _) = if jump == Patch
then red $ text "Adding features in a patch version is a violation of semantic versioning."
else empty
showViolation (Removal _) = if jump /= Major
then red $ text "Removing features in a patch or minor version is a violation of semantic versioning."
else empty
showViolation (Change _ _) = if jump /= Major
then red $ text "Changing APIs in a patch or minor version is a violation of semantic versioning."
else empty
funcTexts funcDiffs =
map (\f -> (text $ showFuncDifference f) <+> (showViolation f)) funcDiffs
typeTexts typeDiffs =
map (\f -> (text $ showTypeDifference f) <+> (showViolation f)) typeDiffs
opTexts opDiffs =
map (\f -> (text $ showOpDifference f) <+> (showViolation f)) opDiffs
modText modDiff = case modDiff of
Nothing -> empty
Just d -> case d of
Addition m -> (text $ "Added module " ++ m) <+> (showViolation d)
Removal m -> (text $ "Removed module " ++ m) <+> (showViolation d)
Change _ _ -> text $ "This should not appear"
data VersionJump = Major | Minor | Patch | None
deriving Eq
versionJump :: Version -> Version -> VersionJump
versionJump (majA, minA, patA, _) (majB, minB, patB, _) =
if majA /= majB
then Major
else if minA /= minB
then Minor
else if patA /= patB
then Patch
else None
showFuncDifference :: Difference CFuncDecl -> String
showFuncDifference (Addition f) = "Added " ++ (showFuncDecl f)
showFuncDifference (Removal f) = "Removed " ++ (showFuncDecl f)
showFuncDifference (Change a b) = "Change " ++ (showFuncDecl a) ++ " to " ++ (showFuncDecl b)
showFuncDecl :: CFuncDecl -> String
showFuncDecl (CFunc (_, n) _ _ t _) =
n ++ " :: " ++ (pPrint $ ppCQualTypeExpr defaultOptions t)
showFuncDecl (CmtFunc _ (_, n) _ _ t _) =
n ++ " :: " ++ (pPrint $ ppCQualTypeExpr defaultOptions t)
showTypeDifference :: Difference CTypeDecl -> String
showTypeDifference (Addition f) = "Added " ++ (showTypeDecl f)
showTypeDifference (Removal f) = "Removed " ++ (showTypeDecl f)
showTypeDifference (Change a b) = "Changed " ++ (showTypeDecl a) ++ " to " ++ (showTypeDecl b)
showTypeDecl :: CTypeDecl -> String
showTypeDecl (CType (_, n) _ _ cs _) =
"data " ++ n ++ " (" ++ (show $ length cs) ++ " constructors)"
showTypeDecl (CTypeSyn (_, n) _ _ t) =
"type " ++ n ++ " = " ++ (pPrint $ ppCTypeExpr defaultOptions t)
showTypeDecl (CNewType (_, n) _ _ _ _) = "newtype " ++ n
showOpDifference :: Difference COpDecl -> String
showOpDifference (Addition f) = "Added " ++ showOpDecl f
showOpDifference (Removal f) = "Removed " ++ showOpDecl f
showOpDifference (Change a b) = "Changed " ++ showOpDecl a ++ " to " ++
showOpDecl b
showOpDecl :: COpDecl -> String
showOpDecl (COp (_, n) CInfixOp a) = "infix " ++ (show a) ++ " " ++ n
showOpDecl (COp (_, n) CInfixlOp a) = "infixl " ++ (show a) ++ " " ++ n
showOpDecl (COp (_, n) CInfixrOp a) = "infixr " ++ (show a) ++ " " ++ n
diffFuncsFiltered :: (CurryProg -> CFuncDecl -> Bool) -> CurryProg -> CurryProg
-> [Difference CFuncDecl]
diffFuncsFiltered = mkDiff funcEq functions funcName
diffTypesFiltered :: (CurryProg -> CTypeDecl -> Bool) -> CurryProg -> CurryProg
-> [Difference CTypeDecl]
diffTypesFiltered = mkDiff typeEq types typeName
diffOpsFiltered :: (CurryProg -> COpDecl -> Bool) -> CurryProg -> CurryProg
-> [Difference COpDecl]
diffOpsFiltered = mkDiff opEq ops opName
funcIsPublic :: CurryProg -> CFuncDecl -> Bool
funcIsPublic _ (CFunc _ _ Public _ _) = True
funcIsPublic _ (CFunc _ _ Private _ _) = False
funcIsPublic _ (CmtFunc _ _ _ Public _ _) = True
funcIsPublic _ (CmtFunc _ _ _ Private _ _) = False
typeIsPublic :: CurryProg -> CTypeDecl -> Bool
typeIsPublic _ (CType _ Public _ _ _) = True
typeIsPublic _ (CType _ Private _ _ _) = False
typeIsPublic _ (CTypeSyn _ Public _ _) = True
typeIsPublic _ (CTypeSyn _ Private _ _) = False
typeIsPublic _ (CNewType _ Public _ _ _) = True
typeIsPublic _ (CNewType _ Private _ _ _) = False
mkDiff :: (a -> a -> Bool)
-> (CurryProg -> [a])
-> (a -> QName)
-> ((CurryProg -> a -> Bool) -> CurryProg -> CurryProg -> [Difference a])
mkDiff eq selector name =
\p b a -> let
as = filter (p a) $ selector a
bs = filter (p b) $ selector b
findDifference f = case listToMaybe $ filter ((== (name f)) . name) bs of
Nothing -> Just $ Removal f
Just f' -> if f `eq` f'
then Nothing
else Just $ Change f f'
additions = filter (not . (flip elem) (map name as) . name) bs
in
catMaybes (map findDifference as) ++ (map Addition additions)
funcEq :: CFuncDecl -> CFuncDecl -> Bool
funcEq (CFunc _ a1 v1 t1 _) (CFunc _ a2 v2 t2 _) = a1 == a2 && v1 == v2 && t1 == t2
funcEq (CmtFunc _ _ a1 v1 t1 _) (CmtFunc _ _ a2 v2 t2 _) = a1 == a2 && v1 == v2 && t1 == t2
funcEq (CFunc _ a1 v1 t1 _) (CmtFunc _ _ a2 v2 t2 _) = a1 == a2 && v1 == v2 && t1 == t2
funcEq (CmtFunc _ _ a1 v1 t1 _) (CFunc _ a2 v2 t2 _) = a1 == a2 && v1 == v2 && t1 == t2
typeEq :: CTypeDecl -> CTypeDecl -> Bool
typeEq (CType _ v1 tvs1 cs1 _) (CType _ v2 tvs2 cs2 _) =
v1 == v2 && tvs1 == tvs2 && cs1 == cs2
typeEq (CTypeSyn _ v1 tvs1 e1) (CTypeSyn _ v2 tvs2 e2) =
v1 == v2 && tvs1 == tvs2 && e1 == e2
typeEq (CNewType _ v1 tvs1 c1 _) (CNewType _ v2 tvs2 c2 _) =
v1 == v2 && tvs1 == tvs2 && c1 == c2
typeEq (CType _ _ _ _ _) (CTypeSyn _ _ _ _) = False
typeEq (CType _ _ _ _ _) (CNewType _ _ _ _ _) = False
typeEq (CTypeSyn _ _ _ _) (CType _ _ _ _ _) = False
typeEq (CTypeSyn _ _ _ _) (CNewType _ _ _ _ _) = False
typeEq (CNewType _ _ _ _ _) (CType _ _ _ _ _) = False
typeEq (CNewType _ _ _ _ _) (CTypeSyn _ _ _ _) = False
opEq :: COpDecl -> COpDecl -> Bool
opEq (COp _ f1 a1) (COp _ f2 a2) = f1 == f2 && a1 == a2
ops :: CurryProg -> [COpDecl]
ops (CurryProg _ _ _ _ _ _ _ os) = os
opName :: COpDecl -> QName
opName (COp n _ _) = n
|