@@ -6,11 +6,10 @@ module Strategy.Fortran.FpmToml (
6
6
FpmDependency (.. ),
7
7
FpmPathDependency (.. ),
8
8
FpmGitDependency (.. ),
9
+ FpmTomlExecutables (.. ),
9
10
buildGraph ,
10
- fpmTomlCodec ,
11
11
) where
12
12
13
- import Control.Applicative (Alternative ((<|>) ))
14
13
import Control.Effect.Diagnostics (Diagnostics , context )
15
14
import Data.Foldable (asum )
16
15
import Data.Map (Map , elems )
@@ -25,35 +24,68 @@ import DepTypes (
25
24
import Effect.ReadFS (Has , ReadFS , readContentsToml )
26
25
import Graphing (Graphing , directs , induceJust )
27
26
import Path
28
- import Toml (TomlCodec , (.=) )
29
27
import Toml qualified
28
+ import Toml.Schema qualified
30
29
31
30
-- | Represents the content of the fpm manifest.
32
- -- Reference: https://github.com/ fortran-lang/fpm/blob/main/ manifest-reference.md
31
+ -- Reference: https://fpm. fortran-lang.org/spec/ manifest.html
33
32
data FpmToml = FpmToml
34
33
{ fpmDependencies :: Map Text FpmDependency
35
34
, fpmDevDependencies :: Map Text FpmDependency
36
- , fpmExecutables :: [Map Text FpmDependency ]
35
+ , fpmExecutables :: [FpmTomlExecutables ]
37
36
}
38
37
deriving (Eq , Ord , Show )
39
38
40
- fpmTomlCodec :: TomlCodec FpmToml
41
- fpmTomlCodec =
42
- FpmToml
43
- <$> Toml. tableMap Toml. _KeyText fpmDependenciesCodec " dependencies" .= fpmDependencies
44
- <*> Toml. tableMap Toml. _KeyText fpmDependenciesCodec " dev-dependencies" .= fpmDevDependencies
45
- <*> Toml. list fpmExecutableDependenciesCodec " executable" .= fpmExecutables
39
+ instance Toml.Schema. FromValue FpmToml where
40
+ fromValue =
41
+ Toml.Schema. parseTableFromValue $
42
+ FpmToml
43
+ <$> Toml.Schema. pickKey [Toml.Schema. Key " dependencies" Toml.Schema. fromValue, Toml.Schema. Else $ pure mempty ]
44
+ <*> Toml.Schema. pickKey [Toml.Schema. Key " dev-dependencies" Toml.Schema. fromValue, Toml.Schema. Else $ pure mempty ]
45
+ <*> Toml.Schema. pickKey [Toml.Schema. Key " executable" Toml.Schema. fromValue, Toml.Schema. Else $ pure [] ]
46
+
47
+ newtype FpmTomlExecutables = FpmTomlExecutables
48
+ { fpmExecutableDependencies :: Map Text FpmDependency
49
+ }
50
+ deriving (Eq , Ord , Show )
51
+
52
+ instance Toml.Schema. FromValue FpmTomlExecutables where
53
+ fromValue =
54
+ Toml.Schema. parseTableFromValue $
55
+ FpmTomlExecutables
56
+ <$> Toml.Schema. pickKey [Toml.Schema. Key " dependencies" Toml.Schema. fromValue, Toml.Schema. Else $ pure mempty ]
46
57
47
58
data FpmDependency
48
59
= FpmGitDep FpmGitDependency
49
60
| FpmPathDep FpmPathDependency
61
+ | FpmMetaDep Text
62
+ deriving (Eq , Ord , Show )
63
+
64
+ instance Toml.Schema. FromValue FpmDependency where
65
+ fromValue v@ (Toml.Schema. Table' l t) =
66
+ Toml.Schema. parseTable
67
+ ( Toml.Schema. pickKey
68
+ [ Toml.Schema. Key " git" (const (FpmGitDep <$> Toml.Schema. fromValue v))
69
+ , Toml.Schema. Key " path" (const (FpmPathDep <$> Toml.Schema. fromValue v))
70
+ , Toml.Schema. Else (Toml.Schema. failAt (Toml. valueAnn v) " Expected either 'git' or 'path' key got: " )
71
+ ]
72
+ )
73
+ l
74
+ t
75
+ fromValue (Toml.Schema. Text' _ t) = pure $ FpmMetaDep t
76
+ fromValue v = Toml.Schema. failAt (Toml. valueAnn v) " Invalid dependency value, expected a table or a string"
77
+
78
+ newtype FpmMetaDependency = FpmMetaDependency Text
50
79
deriving (Eq , Ord , Show )
51
80
52
81
newtype FpmPathDependency = FpmPathDependency
53
82
{ pathOf :: Text
54
83
}
55
84
deriving (Eq , Ord , Show )
56
85
86
+ instance Toml.Schema. FromValue FpmPathDependency where
87
+ fromValue = Toml.Schema. parseTableFromValue $ FpmPathDependency <$> Toml.Schema. reqKey " path"
88
+
57
89
data FpmGitDependency = FpmGitDependency
58
90
{ url :: Text
59
91
, branch :: Maybe Text
@@ -62,32 +94,14 @@ data FpmGitDependency = FpmGitDependency
62
94
}
63
95
deriving (Eq , Ord , Show )
64
96
65
- fpmExecutableDependenciesCodec :: TomlCodec (Map Text FpmDependency )
66
- fpmExecutableDependenciesCodec = Toml. tableMap Toml. _KeyText fpmDependenciesCodec " dependencies"
67
-
68
- fpmDependenciesCodec :: Toml. Key -> TomlCodec FpmDependency
69
- fpmDependenciesCodec key =
70
- Toml. dimatch matchFpmPathDep FpmPathDep (Toml. table fpmPathDependencyCodec key)
71
- <|> Toml. dimatch matchFpmGitDep FpmGitDep (Toml. table fpmGitDependencyCodec key)
72
- where
73
- matchFpmPathDep :: FpmDependency -> Maybe FpmPathDependency
74
- matchFpmPathDep (FpmPathDep pathDep) = Just pathDep
75
- matchFpmPathDep _ = Nothing
76
-
77
- matchFpmGitDep :: FpmDependency -> Maybe FpmGitDependency
78
- matchFpmGitDep (FpmGitDep gitDep) = Just gitDep
79
- matchFpmGitDep _ = Nothing
80
-
81
- fpmPathDependencyCodec :: TomlCodec FpmPathDependency
82
- fpmPathDependencyCodec = FpmPathDependency <$> Toml. text " path" .= pathOf
83
-
84
- fpmGitDependencyCodec :: TomlCodec FpmGitDependency
85
- fpmGitDependencyCodec =
97
+ instance Toml.Schema. FromValue FpmGitDependency where
98
+ fromValue =
99
+ Toml.Schema. parseTableFromValue $
86
100
FpmGitDependency
87
- <$> Toml. text " git" .= url
88
- <*> Toml. dioptional ( Toml. text " branch" ) .= branch
89
- <*> Toml. dioptional ( Toml. text " tag" ) .= tag
90
- <*> Toml. dioptional ( Toml. text " rev" ) .= rev
101
+ <$> Toml.Schema. reqKey " git"
102
+ <*> Toml.Schema. optKey " branch"
103
+ <*> Toml.Schema. optKey " tag"
104
+ <*> Toml.Schema. optKey " rev"
91
105
92
106
buildGraph :: FpmToml -> Graphing Dependency
93
107
buildGraph fpmToml = induceJust $ foldMap directs [deps, execDeps, devDeps]
@@ -96,7 +110,7 @@ buildGraph fpmToml = induceJust $ foldMap directs [deps, execDeps, devDeps]
96
110
deps = map toProdDependency (elems $ fpmDependencies fpmToml)
97
111
98
112
execDeps :: [Maybe Dependency ]
99
- execDeps = map toProdDependency (foldMap elems $ fpmExecutables fpmToml)
113
+ execDeps = map toProdDependency (foldMap ( elems . fpmExecutableDependencies) ( fpmExecutables fpmToml) )
100
114
101
115
devDeps :: [Maybe Dependency ]
102
116
devDeps = map toDevDependency (elems $ fpmDevDependencies fpmToml)
@@ -109,6 +123,7 @@ buildGraph fpmToml = induceJust $ foldMap directs [deps, execDeps, devDeps]
109
123
110
124
toDependency :: Maybe DepEnvironment -> FpmDependency -> Maybe Dependency
111
125
toDependency _ (FpmPathDep _) = Nothing
126
+ toDependency _ (FpmMetaDep _) = Nothing
112
127
toDependency env (FpmGitDep dep) =
113
128
Just $
114
129
Dependency
@@ -128,5 +143,5 @@ analyzeFpmToml ::
128
143
Path Abs File ->
129
144
m (Graphing Dependency )
130
145
analyzeFpmToml tomlFile = do
131
- fpmTomlContent <- readContentsToml fpmTomlCodec tomlFile
146
+ fpmTomlContent <- readContentsToml tomlFile
132
147
context " Building dependency graph from fpm.toml" $ pure $ buildGraph fpmTomlContent
0 commit comments