@@ -109,22 +109,44 @@ cfgCmdSet cmd = do
109
109
newValue' = T. stripEnd $
110
110
decodeUtf8With lenientDecode $ Yaml. encode newValue -- Text
111
111
file = toFilePath configFilePath -- String
112
- newYamlLines <- case inConfig config cmdKeys of
113
- Nothing -> do
112
+ hits = catMaybes $ NE. toList $ NE. map (inConfig config) cmdKeys
113
+ primaryCmdKey = NE. last $ NE. head cmdKeys
114
+ newYamlLines <- case hits of
115
+ [] -> do
114
116
prettyInfoL
115
117
[ pretty configFilePath
116
118
, flow " has been extended."
117
119
]
118
- pure $ writeLines yamlLines " " cmdKeys newValue'
119
- Just oldValue -> if oldValue == newValue
120
+ pure $ writeLines yamlLines " " ( NE. head cmdKeys) newValue'
121
+ [(cmdKey, oldValue)] -> if oldValue == newValue && cmdKey == primaryCmdKey
120
122
then do
121
123
prettyInfoL
122
124
[ pretty configFilePath
123
125
, flow " already contained the intended configuration and remains \
124
126
\unchanged."
125
127
]
126
128
pure yamlLines
127
- else switchLine configFilePath (NE. last cmdKeys) newValue' [] yamlLines
129
+ else do
130
+ when (cmdKey /= primaryCmdKey) $
131
+ prettyWarn $
132
+ fillSep
133
+ [ pretty configFilePath
134
+ , flow " contained a synonym for"
135
+ , style Target (fromString $ T. unpack primaryCmdKey)
136
+ , parens (style Current (fromString $ T. unpack cmdKey))
137
+ , flow " which has been replaced."
138
+ ]
139
+ <> line
140
+ switchLine configFilePath cmdKey primaryCmdKey newValue' [] yamlLines
141
+ _ -> do
142
+ -- In practice, this warning should not be encountered because with
143
+ -- snapshot and resolver present, Stack will not parse the YAML file.
144
+ prettyWarnL
145
+ [ pretty configFilePath
146
+ , flow " contains more than one possible existing configuration and, \
147
+ \consequently, remains unchanged."
148
+ ]
149
+ pure yamlLines
128
150
liftIO $ writeFileUtf8 file (T. unlines newYamlLines)
129
151
where
130
152
-- This assumes that if the key does not exist, the lines that can be
@@ -145,14 +167,15 @@ cfgCmdSet cmd = do
145
167
146
168
inConfig v cmdKeys = case v of
147
169
Yaml. Object obj ->
148
- case KeyMap. lookup (Key. fromText (NE. head cmdKeys)) obj of
149
- Nothing -> Nothing
150
- Just v' -> case nonEmpty $ NE. tail cmdKeys of
151
- Nothing -> Just v'
152
- Just ks -> inConfig v' ks
170
+ let cmdKey = NE. head cmdKeys
171
+ in case KeyMap. lookup (Key. fromText cmdKey) obj of
172
+ Nothing -> Nothing
173
+ Just v' -> case nonEmpty $ NE. tail cmdKeys of
174
+ Nothing -> Just (cmdKey, v')
175
+ Just ks -> inConfig v' ks
153
176
_ -> Nothing
154
177
155
- switchLine file cmdKey _ searched [] = do
178
+ switchLine file cmdKey _ _ searched [] = do
156
179
prettyWarnL
157
180
[ style Current (fromString $ T. unpack cmdKey)
158
181
, flow " not found in YAML file"
@@ -161,11 +184,11 @@ cfgCmdSet cmd = do
161
184
\supported."
162
185
]
163
186
pure $ reverse searched
164
- switchLine file cmdKey newValue searched (oldLine: rest) =
187
+ switchLine file cmdKey cmdKey' newValue searched (oldLine: rest) =
165
188
case parseOnly (parseLine cmdKey) oldLine of
166
- Left _ -> switchLine file cmdKey newValue (oldLine: searched) rest
189
+ Left _ -> switchLine file cmdKey cmdKey' newValue (oldLine: searched) rest
167
190
Right (kt, spaces1, spaces2, spaces3, comment) -> do
168
- let newLine = spaces1 <> renderKey cmdKey kt <> spaces2 <>
191
+ let newLine = spaces1 <> renderKey cmdKey' kt <> spaces2 <>
169
192
" :" <> spaces3 <> newValue <> comment
170
193
prettyInfoL
171
194
[ pretty file
@@ -246,13 +269,13 @@ snapshotValue root snapshot = do
246
269
void $ loadSnapshot =<< completeSnapshotLocation concreteSnapshot
247
270
pure (Yaml. toJSON concreteSnapshot)
248
271
249
- cfgCmdSetKeys :: ConfigCmdSet -> NonEmpty Text
250
- cfgCmdSetKeys (ConfigCmdSetSnapshot _) = [" snapshot" ]
251
- cfgCmdSetKeys (ConfigCmdSetResolver _) = [" resolver" ]
252
- cfgCmdSetKeys (ConfigCmdSetSystemGhc _ _) = [configMonoidSystemGHCName]
253
- cfgCmdSetKeys (ConfigCmdSetInstallGhc _ _) = [configMonoidInstallGHCName]
272
+ cfgCmdSetKeys :: ConfigCmdSet -> NonEmpty ( NonEmpty Text )
273
+ cfgCmdSetKeys (ConfigCmdSetSnapshot _) = [[ " snapshot" ], [ " resolver " ] ]
274
+ cfgCmdSetKeys (ConfigCmdSetResolver _) = [[ " resolver" ], [ " snapshot " ] ]
275
+ cfgCmdSetKeys (ConfigCmdSetSystemGhc _ _) = [[ configMonoidSystemGHCName] ]
276
+ cfgCmdSetKeys (ConfigCmdSetInstallGhc _ _) = [[ configMonoidInstallGHCName] ]
254
277
cfgCmdSetKeys (ConfigCmdSetDownloadPrefix _ _) =
255
- [" package-index" , " download-prefix" ]
278
+ [[ " package-index" , " download-prefix" ] ]
256
279
257
280
cfgCmdName :: String
258
281
cfgCmdName = " config"
@@ -284,7 +307,8 @@ configCmdSetParser =
284
307
( OA. metavar " SNAPSHOT"
285
308
<> OA. help " E.g. \" nightly\" or \" lts-22.8\" " ))
286
309
( OA. progDesc
287
- " Change the resolver key of the current project." ))
310
+ " Change the snapshot of the current project, using the \
311
+ \resolver key." ))
288
312
, OA. command (T. unpack configMonoidSystemGHCName)
289
313
( OA. info
290
314
( ConfigCmdSetSystemGhc
0 commit comments