Skip to content

Commit 6e677f5

Browse files
theobatmpilgrem
authored andcommitted
refactoring: use cabal's unqualcompname underneath stack newtype
1 parent 3b503f5 commit 6e677f5

22 files changed

Lines changed: 190 additions & 138 deletions

src/Stack/Build/Cache.hs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import Stack.Types.EnvConfig
7474
import Stack.Types.GhcPkgId ( ghcPkgIdString )
7575
import Stack.Types.Installed
7676
(InstalledLibraryInfo (..), foldOnGhcPkgId' )
77-
import Stack.Types.NamedComponent ( NamedComponent (..) )
77+
import Stack.Types.NamedComponent ( NamedComponent (..), componentCachePath )
7878
import Stack.Types.SourceMap ( smRelDir )
7979
import System.PosixCompat.Files
8080
( modificationTime, getFileStatus, setFileTimes )
@@ -135,13 +135,7 @@ buildCacheFile dir component = do
135135
smh <- view $ envConfigL . to (.sourceMapHash)
136136
smDirName <- smRelDir smh
137137
let nonLibComponent prefix name = prefix <> "-" <> T.unpack name
138-
cacheFileName <- parseRelFile $ case component of
139-
CLib -> "lib"
140-
CSubLib name -> nonLibComponent "sub-lib" name
141-
CFlib name -> nonLibComponent "flib" name
142-
CExe name -> nonLibComponent "exe" name
143-
CTest name -> nonLibComponent "test" name
144-
CBench name -> nonLibComponent "bench" name
138+
cacheFileName <- parseRelFile $ componentCachePath component
145139
pure $ cachesDir </> smDirName </> cacheFileName
146140

147141
-- | Try to read the dirtiness cache for the given package directory.

src/Stack/Build/ConstructPlan.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import Stack.Types.CompCollection ( collectionMember )
5959
import Stack.Types.Compiler ( WhichCompiler (..) )
6060
import Stack.Types.CompilerPaths
6161
( CompilerPaths (..), HasCompiler (..) )
62+
import Stack.Types.ComponentUtils ( unqualCompFromText )
6263
import Stack.Types.Config ( Config (..), HasConfig (..), stackRootL )
6364
import Stack.Types.ConfigureOpts ( BaseConfigOpts (..) )
6465
import qualified Stack.Types.ConfigureOpts as ConfigureOpts
@@ -1182,7 +1183,7 @@ checkAndWarnForUnknownTools p = do
11821183
-- From Cabal 1.12, build-tools can specify another executable in the same
11831184
-- package.
11841185
notPackageExe toolName =
1185-
MaybeT $ skipIf $ collectionMember toolName p.executables
1186+
MaybeT $ skipIf $ collectionMember (unqualCompFromText toolName) p.executables
11861187
warn name = MaybeT . pure . Just $ ToolWarning (ExeName name) p.name
11871188
skipIf p' = pure $ if p' then Nothing else Just ()
11881189

src/Stack/Build/ExecutePackage.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ import Stack.Types.CompilerPaths
109109
, cpWhich, getGhcPkgExe
110110
)
111111
import qualified Stack.Types.Component as Component
112+
import Stack.Types.ComponentUtils
113+
( StackUnqualCompName, unqualCompToText, unqualCompToString, toCabalName )
112114
import Stack.Types.Config ( Config (..), HasConfig (..) )
113115
import Stack.Types.ConfigureOpts
114116
( BaseConfigOpts (..), ConfigureOpts (..) )
@@ -958,7 +960,7 @@ checkForUnlistedFiles TTRemotePackage{} _ = pure []
958960
-- coverage reports if coverage is enabled.
959961
singleTest :: HasEnvConfig env
960962
=> TestOpts
961-
-> [Text]
963+
-> [StackUnqualCompName]
962964
-> ActionContext
963965
-> ExecuteEnv
964966
-> Task
@@ -1013,7 +1015,7 @@ singleTest topts testsToRun ac ee task installedMap = do
10131015
]
10141016

10151017
errs <- fmap Map.unions $ forM suitesToRun $ \(testName, suiteInterface) -> do
1016-
let stestName = T.unpack testName
1018+
let stestName = unqualCompToString testName
10171019
(testName', isTestTypeLib) <-
10181020
case suiteInterface of
10191021
C.TestSuiteLibV09{} -> pure (stestName ++ "Stub", True)
@@ -1110,7 +1112,7 @@ singleTest topts testsToRun ac ee task installedMap = do
11101112
<> T.intercalate " " (map showProcessArgDebug args)
11111113
announce $
11121114
"test (suite: "
1113-
<> display testName
1115+
<> display (unqualCompToText testName)
11141116
<> display argsDisplay
11151117
<> ")"
11161118

@@ -1153,7 +1155,7 @@ singleTest topts testsToRun ac ee task installedMap = do
11531155
$ BL.fromStrict
11541156
$ encodeUtf8 $ fromString $
11551157
show ( logPath
1156-
, mkUnqualComponentName (T.unpack testName)
1158+
, toCabalName testName
11571159
)
11581160
else do
11591161
isTerminal <- view $ globalOptsL . to (.terminal)
@@ -1186,7 +1188,7 @@ singleTest topts testsToRun ac ee task installedMap = do
11861188
let announceResult result =
11871189
announce $
11881190
"Test suite "
1189-
<> display testName
1191+
<> display (unqualCompToText testName)
11901192
<> " "
11911193
<> result
11921194
case mec of
@@ -1210,15 +1212,15 @@ singleTest topts testsToRun ac ee task installedMap = do
12101212
(package.buildType == C.Simple)
12111213
exeName
12121214
(packageNameString package.name)
1213-
(T.unpack testName)
1215+
(unqualCompToString testName)
12141216
pure emptyResult
12151217

12161218
when needHpc $ do
12171219
let testsToRun' = map f testsToRun
12181220
f tName =
12191221
case (.interface) <$> mComponent of
1220-
Just C.TestSuiteLibV09{} -> tName <> "Stub"
1221-
_ -> tName
1222+
Just C.TestSuiteLibV09{} -> unqualCompToText tName <> "Stub"
1223+
_ -> unqualCompToText tName
12221224
where
12231225
mComponent = collectionLookup tName package.testSuites
12241226
generateHpcReport pkgDir package testsToRun'

src/Stack/Build/Source.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import qualified Pantry.SHA256 as SHA256
2525
import Stack.Build.Cache ( tryGetBuildCache )
2626
import Stack.Build.Haddock ( shouldHaddockDeps )
2727
import Stack.Package
28-
( buildableBenchmarks, buildableExes, buildableTestSuites
28+
( buildableBenchmarksComp, buildableExesComp, buildableTestSuitesComp
2929
, hasBuildableMainLibrary, resolvePackage
3030
)
3131
import Stack.PackageFile ( getPackageFile )
@@ -361,17 +361,17 @@ loadLocalPackage pp = do
361361
let (_s, e, t, b) = splitComponents $ Set.toList comps
362362
in (e, t, b)
363363
Just (TargetAll _packageType) ->
364-
( buildableExes pkg
364+
( buildableExesComp pkg
365365
, if bopts.tests
366366
&& maybe True (Set.notMember name . (.skipTest)) mcurator
367-
then buildableTestSuites pkg
367+
then buildableTestSuitesComp pkg
368368
else Set.empty
369369
, if bopts.benchmarks
370370
&& maybe
371371
True
372372
(Set.notMember name . (.skipBenchmark))
373373
mcurator
374-
then buildableBenchmarks pkg
374+
then buildableBenchmarksComp pkg
375375
else Set.empty
376376
)
377377
Nothing -> mempty
@@ -466,9 +466,9 @@ loadLocalPackage pp = do
466466
-- through component parsing, but the components aren't present, then they
467467
-- must not be buildable.
468468
, unbuildable = toComponents
469-
(exes `Set.difference` buildableExes pkg)
470-
(tests `Set.difference` buildableTestSuites pkg)
471-
(benches `Set.difference` buildableBenchmarks pkg)
469+
(exes `Set.difference` buildableExesComp pkg)
470+
(tests `Set.difference` buildableTestSuitesComp pkg)
471+
(benches `Set.difference` buildableBenchmarksComp pkg)
472472
}
473473

474474
-- | Compare the current filesystem state to the cached information, and

src/Stack/Build/Target.hs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import Stack.Prelude
8080
import Stack.Types.BuildConfig
8181
( BuildConfig (..), HasBuildConfig (..) )
8282
import Stack.Types.BuildOptsCLI ( BuildOptsCLI (..) )
83+
import Stack.Types.ComponentUtils ( unqualCompFromText )
8384
import Stack.Types.Config ( Config (..) )
8485
import Stack.Types.NamedComponent
8586
( NamedComponent (..), renderComponent )
@@ -226,9 +227,9 @@ parseRawTarget t =
226227

227228
parseCompType t' =
228229
case t' of
229-
"exe" -> Just CExe
230-
"test" -> Just CTest
231-
"bench" -> Just CBench
230+
"exe" -> Just (CExe . unqualCompFromText)
231+
"test" -> Just (CTest . unqualCompFromText)
232+
"bench" -> Just (CBench . unqualCompFromText)
232233
_ -> Nothing
233234

234235
--------------------------------------------------------------------------------
@@ -263,11 +264,14 @@ resolveRawTarget sma allLocs (rawInput, rt) =
263264
-- 'ComponentName'
264265
isCompNamed :: ComponentName -> NamedComponent -> Bool
265266
isCompNamed _ CLib = False
266-
isCompNamed t1 (CSubLib t2) = t1 == t2
267-
isCompNamed t1 (CExe t2) = t1 == t2
268-
isCompNamed t1 (CFlib t2) = t1 == t2
269-
isCompNamed t1 (CTest t2) = t1 == t2
270-
isCompNamed t1 (CBench t2) = t1 == t2
267+
isCompNamed t1 t2 = case t2 of
268+
(CSubLib t2') -> t1' == t2'
269+
(CExe t2') -> t1' == t2'
270+
(CFlib t2') -> t1' == t2'
271+
(CTest t2') -> t1' == t2'
272+
(CBench t2') -> t1' == t2'
273+
where t1' = unqualCompFromText t1
274+
271275

272276
go (RTComponent cname) = do
273277
-- Associated list from component name to package that defines it. We use an

src/Stack/Component.hs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ module Stack.Component
2222
, stackBenchmarkFromCabal
2323
, stackTestFromCabal
2424
, foldOnNameAndBuildInfo
25-
, stackUnqualToQual
2625
, componentDependencyMap
2726
, fromCabalName
2827
) where
@@ -47,13 +46,6 @@ import Stack.Types.Component
4746
)
4847
import Stack.Types.ComponentUtils ( fromCabalName )
4948
import Stack.Types.Dependency ( cabalExeToStackDep, cabalToStackDep )
50-
import Stack.Types.NamedComponent ( NamedComponent )
51-
52-
stackUnqualToQual ::
53-
(Text -> NamedComponent)
54-
-> StackUnqualCompName
55-
-> NamedComponent
56-
stackUnqualToQual c (StackUnqualCompName n) = c n
5749

5850
foldOnNameAndBuildInfo ::
5951
( HasField "buildInfo" a StackBuildInfo

src/Stack/ComponentFile.hs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import Stack.Types.Component
5555
, StackExecutable (..), StackLibrary (..)
5656
, StackTestSuite (..), StackUnqualCompName (..)
5757
)
58+
import Stack.Types.ComponentUtils ( unqualCompToString, emptyCompName )
5859
import Stack.Types.Config
5960
( Config (..), HasConfig (..), prettyStackDevL )
6061
import Stack.Types.NamedComponent ( NamedComponent (..) )
@@ -77,7 +78,7 @@ stackBenchmarkFiles ::
7778
StackBenchmark
7879
-> RIO GetPackageFileContext (NamedComponent, ComponentFile)
7980
stackBenchmarkFiles bench =
80-
resolveComponentFiles (CBench bench.name.unqualCompToText) build names
81+
resolveComponentFiles (CBench bench.name) build names
8182
where
8283
names = bnames <> exposed
8384
exposed =
@@ -92,7 +93,7 @@ stackTestSuiteFiles ::
9293
StackTestSuite
9394
-> RIO GetPackageFileContext (NamedComponent, ComponentFile)
9495
stackTestSuiteFiles test =
95-
resolveComponentFiles (CTest test.name.unqualCompToText) build names
96+
resolveComponentFiles (CTest test.name) build names
9697
where
9798
names = bnames <> exposed
9899
exposed =
@@ -108,7 +109,7 @@ stackExecutableFiles ::
108109
StackExecutable
109110
-> RIO GetPackageFileContext (NamedComponent, ComponentFile)
110111
stackExecutableFiles exe =
111-
resolveComponentFiles (CExe exe.name.unqualCompToText) build names
112+
resolveComponentFiles (CExe exe.name) build names
112113
where
113114
build = exe.buildInfo
114115
names =
@@ -122,9 +123,9 @@ stackLibraryFiles ::
122123
stackLibraryFiles lib =
123124
resolveComponentFiles componentName build names
124125
where
125-
componentRawName = lib.name.unqualCompToText
126+
componentRawName = lib.name
126127
componentName
127-
| componentRawName == mempty = CLib
128+
| componentRawName == emptyCompName = CLib
128129
| otherwise = CSubLib componentRawName
129130
build = lib.buildInfo
130131
names = bnames ++ exposed
@@ -341,7 +342,7 @@ componentOutputDir namedComponent distDir =
341342
CBench name -> makeTmp name
342343
where
343344
makeTmp name =
344-
buildDir distDir </> componentNameToDir (name <> "/" <> name <> "-tmp")
345+
buildDir distDir </> componentNameToDirNormOrTmp True name
345346

346347
-- | Try to resolve the list of base names in the given directory by
347348
-- looking for unique instances of base names applied with the given
@@ -545,10 +546,15 @@ buildDir distDir = distDir </> relDirBuild
545546

546547
-- NOTE: don't export this, only use it for valid paths based on
547548
-- component names.
548-
componentNameToDir :: Text -> Path Rel Dir
549-
componentNameToDir name =
550-
fromMaybe (throw $ ComponentNotParsedBug sName) (parseRelDir sName)
551-
where sName = T.unpack name
549+
componentNameToDir :: StackUnqualCompName -> Path Rel Dir
550+
componentNameToDir = componentNameToDirNormOrTmp False
551+
552+
componentNameToDirNormOrTmp :: Bool -> StackUnqualCompName -> Path Rel Dir
553+
componentNameToDirNormOrTmp isTemp name =
554+
fromMaybe (throw $ ComponentNotParsedBug sName) (parseRelDir fullName)
555+
where
556+
fullName = if isTemp then sName <> "/" <> sName <> "-tmp" else sName
557+
sName = unqualCompToString name
552558

553559
-- | See 'Distribution.Simple.LocalBuildInfo.componentBuildDir'
554560
componentBuildDir :: NamedComponent -> Path Abs Dir -> Path Abs Dir

src/Stack/Coverage.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import Stack.Types.BuildConfig
5757
import Stack.Types.Compiler ( getGhcVersion )
5858
import Stack.Types.CompilerPaths ( getGhcPkgExe )
5959
import Stack.Types.CompCollection ( getBuildableSetText )
60+
import Stack.Types.ComponentUtils ( unqualCompToString )
6061
import Stack.Types.BuildOptsCLI
6162
( BuildOptsCLI (..), defaultBuildOptsCLI )
6263
import Stack.Types.EnvConfig
@@ -392,11 +393,12 @@ generateHpcReportForTargets opts tixFiles targetNames = do
392393
\case
393394
CTest testName -> (pkgPath </>) <$>
394395
parseRelFile
395-
( T.unpack testName
396+
( testName'
396397
++ "/"
397-
++ T.unpack testName
398+
++ testName'
398399
++ ".tix"
399400
)
401+
where testName' = unqualCompToString testName
400402
_ -> prettyThrowIO $ NonTestSuiteTarget name
401403
TargetAll PTProject -> do
402404
pkgPath <- hpcPkgPath name

src/Stack/Exec.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Stack.Types.BuildOptsCLI
3131
( BuildOptsCLI (..), defaultBuildOptsCLI )
3232
import Stack.Types.CompilerPaths
3333
( CompilerPaths (..), HasCompiler (..), getGhcPkgExe )
34+
import Stack.Types.ComponentUtils ( unqualCompFromString, unqualCompToText )
3435
import Stack.Types.Config ( Config (..), HasConfig (..) )
3536
import Stack.Types.EnvConfig ( EnvConfig )
3637
import Stack.Types.EnvSettings ( EnvSettings (..) )
@@ -183,15 +184,16 @@ execCmd opts =
183184
let executables = concatMap (filter isCExe . Set.toList) pkgComponents
184185
let (exe, args') = case args of
185186
[] -> (firstExe, args)
186-
x:xs -> case L.find (\y -> y == CExe (T.pack x)) executables of
187+
x:xs -> case L.find (\y -> y == CExe (unqualCompFromString x)) executables of
187188
Nothing -> (firstExe, args)
188189
argExe -> (argExe, xs)
189190
where
190191
firstExe = listToMaybe executables
191192
case exe of
192193
Just (CExe exe') -> do
193-
withNewLocalBuildTargets [T.cons ':' exe'] $ build Nothing
194-
pure (T.unpack exe', args')
194+
let textExeName = unqualCompToText exe'
195+
withNewLocalBuildTargets [T.cons ':' textExeName] $ build Nothing
196+
pure (T.unpack textExeName, args')
195197
_ -> prettyThrowIO ExecutableToRunNotFound
196198

197199
getGhcCmd pkgs args = do

src/Stack/Ghci.hs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ import Stack.Ghci.Script
4646
, scriptToLazyByteString
4747
)
4848
import Stack.Package
49-
( buildableExes, buildableForeignLibs, getPackageOpts
49+
( buildableForeignLibsComp, buildableSubLibsComp, buildableExesComp
50+
, buildableTestSuitesComp, buildableBenchmarksComp, getPackageOpts
5051
, hasBuildableMainLibrary, listOfPackageDeps
5152
, packageFromPackageDescription, readDotBuildinfo
5253
, resolvePackageDescription, topSortPackageComponent
@@ -63,7 +64,6 @@ import qualified Stack.Types.BuildOpts as BenchmarkOpts ( BenchmarkOpts (..) )
6364
import qualified Stack.Types.BuildOpts as TestOpts ( TestOpts (..) )
6465
import Stack.Types.BuildOptsCLI
6566
( ApplyCLIFlag (..), BuildOptsCLI (..), defaultBuildOptsCLI )
66-
import Stack.Types.CompCollection ( getBuildableListText )
6767
import Stack.Types.CompilerPaths
6868
( CompilerPaths (..), HasCompiler (..) )
6969
import Stack.Types.Config ( Config (..), HasConfig (..), buildOptsL )
@@ -951,21 +951,21 @@ makeGhciPkgInfo installMap installedMap locals addPkgs mfileTargets pkgDesc = do
951951
-- (differently).
952952
wantedPackageComponents :: BuildOpts -> Target -> Package -> Set NamedComponent
953953
wantedPackageComponents _ (TargetComps cs) _ = cs
954-
wantedPackageComponents bopts (TargetAll PTProject) pkg = S.fromList $
954+
wantedPackageComponents bopts (TargetAll PTProject) pkg =
955955
( if hasBuildableMainLibrary pkg
956-
then CLib : map CSubLib buildableForeignLibs'
957-
else []
956+
then S.insert CLib (S.mapMonotonic CSubLib buildableForeignLibs')
957+
else S.empty
958958
)
959-
<> map CExe buildableExes'
960-
<> map CSubLib buildableSubLibs
961-
<> (if bopts.tests then map CTest buildableTestSuites else [])
962-
<> (if bopts.benchmarks then map CBench buildableBenchmarks else [])
959+
<> S.mapMonotonic CExe buildableExes'
960+
<> S.mapMonotonic CSubLib buildableSubLibs
961+
<> (if bopts.tests then S.mapMonotonic CTest buildableTestSuites else S.empty)
962+
<> (if bopts.benchmarks then S.mapMonotonic CBench buildableBenchmarks else S.empty)
963963
where
964-
buildableForeignLibs' = S.toList $ buildableForeignLibs pkg
965-
buildableSubLibs = getBuildableListText pkg.subLibraries
966-
buildableExes' = S.toList $ buildableExes pkg
967-
buildableTestSuites = getBuildableListText pkg.testSuites
968-
buildableBenchmarks = getBuildableListText pkg.benchmarks
964+
buildableForeignLibs' = buildableForeignLibsComp pkg
965+
buildableSubLibs = buildableSubLibsComp pkg
966+
buildableExes' = buildableExesComp pkg
967+
buildableTestSuites = buildableTestSuitesComp pkg
968+
buildableBenchmarks = buildableBenchmarksComp pkg
969969
wantedPackageComponents _ _ _ = S.empty
970970

971971
checkForIssues :: HasTerm env => [GhciPkgInfo] -> RIO env ()

0 commit comments

Comments
 (0)