Skip to content

Commit

Permalink
Merge pull request #7938 from bacchanalia/better-script-exe-names
Browse files Browse the repository at this point in the history
Improve names of generated executables for scripts
  • Loading branch information
mergify[bot] authored Jan 31, 2022
2 parents 4879ae1 + e13d214 commit 73a343b
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 50 deletions.
21 changes: 19 additions & 2 deletions cabal-install/src/Distribution/Client/ScriptUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ import Control.Exception
( bracket )
import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Lazy ()
import qualified Data.Set as S
import System.Directory
( canonicalizePath, doesFileExist, getTemporaryDirectory, removeDirectoryRecursive )
import System.FilePath
( (</>) )
( (</>), takeFileName )
import qualified Text.Parsec as P

-- A note on multi-module script support #6787:
Expand Down Expand Up @@ -255,9 +256,15 @@ updateContextAndWriteProjectFile ctx scriptPath scriptExecutable = do

absScript <- canonicalizePath scriptPath
let
-- Replace characters which aren't allowed in the executable component name with '_'
-- Prefix with "cabal-script-" to make it clear to end users that the name may be mangled
scriptExeName = "cabal-script-" ++ map censor (takeFileName scriptPath)
censor c | c `S.member` ccNamecore = c
| otherwise = '_'

sourcePackage = fakeProjectSourcePackage projectRoot
& lSrcpkgDescription . L.condExecutables
.~ [("script", CondNode executable (targetBuildDepends $ buildInfo executable) [])]
.~ [(fromString scriptExeName, CondNode executable (targetBuildDepends $ buildInfo executable) [])]
executable = scriptExecutable
& L.modulePath .~ absScript

Expand Down Expand Up @@ -354,3 +361,13 @@ lSrcpkgDescription f s = fmap (\x -> s { srcpkgDescription = x }) (f (srcpkgDesc
lLocalPackages :: Lens' ProjectBaseContext [PackageSpecifier UnresolvedSourcePackage]
lLocalPackages f s = fmap (\x -> s { localPackages = x }) (f (localPackages s))
{-# inline lLocalPackages #-}

-- Character classes
-- Transcribed from "templates/Lexer.x"
ccSpace, ccCtrlchar, ccPrintable, ccSymbol', ccParen, ccNamecore :: Set Char
ccSpace = S.fromList " "
ccCtrlchar = S.fromList $ [chr 0x0 .. chr 0x1f] ++ [chr 0x7f]
ccPrintable = S.fromList [chr 0x0 .. chr 0xff] S.\\ ccCtrlchar
ccSymbol' = S.fromList ",=<>+*&|!$%^@#?/\\~"
ccParen = S.fromList "()[]"
ccNamecore = ccPrintable S.\\ S.unions [ccSpace, S.fromList ":\"{}", ccParen, ccSymbol']
2 changes: 1 addition & 1 deletion cabal-testsuite/PackageTests/ListBin/Script/cabal.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
Building executable 'cabal-script-script.hs' for fake-package-0..
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
Building executable 'cabal-script-script.hs' for fake-package-0..
# cabal v2-repl
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (ephemeral targets)
- fake-package-0 (exe:cabal-script-script.hs) (ephemeral targets)
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
Building executable 'cabal-script-script.hs' for fake-package-0..
# cabal v2-run
Up to date
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
Building executable 'cabal-script-script.hs' for fake-package-0..
# cabal v2-build
Up to date
12 changes: 6 additions & 6 deletions cabal-testsuite/PackageTests/NewBuild/CmdClean/Keep/cabal.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
Building executable 'cabal-script-script.hs' for fake-package-0..
# cabal v2-build
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (configuration changed)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script2.hs) (first run)
Configuring executable 'cabal-script-script2.hs' for fake-package-0..
Building executable 'cabal-script-script2.hs' for fake-package-0..
# cabal v2-clean
12 changes: 6 additions & 6 deletions cabal-testsuite/PackageTests/NewBuild/CmdClean/Orphan/cabal.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
Building executable 'cabal-script-script.hs' for fake-package-0..
# cabal v2-build
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (configuration changed)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script2.hs) (first run)
Configuring executable 'cabal-script-script2.hs' for fake-package-0..
Building executable 'cabal-script-script2.hs' for fake-package-0..
# cabal v2-clean
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
Building executable 'cabal-script-script.hs' for fake-package-0..
# cabal v2-clean
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
# cabal v2-repl
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
- fake-package-0 (exe:cabal-script-script.hs) (first run)
6 changes: 3 additions & 3 deletions cabal-testsuite/PackageTests/NewBuild/CmdRun/Script/cabal.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
Building executable 'cabal-script-script.hs' for fake-package-0..
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.lhs) (first run)
Configuring executable 'cabal-script-script.lhs' for fake-package-0..
Building executable 'cabal-script-script.lhs' for fake-package-0..
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-with_sp) (first run)
Configuring executable 'cabal-script-with_sp' for fake-package-0..
Building executable 'cabal-script-with_sp' for fake-package-0..
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Test.Cabal.Prelude

main = cabalTest . void $ do
cabal' "v2-run" ["script with spaces"] >>= assertOutputContains "Hello World"
cabal' "v2-run" ["with sp"] >>= assertOutputContains "Hello World"
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
- fake-package-0 (exe:cabal-script-script.hs) (first run)
Configuring executable 'cabal-script-script.hs' for fake-package-0..
Building executable 'cabal-script-script.hs' for fake-package-0..
# cabal v2-run
Up to date
4 changes: 3 additions & 1 deletion changelog.d/pr-7851
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
synopsis: Better support for scripts
packages: cabal-install
prs: #7851 #7925
prs: #7851 #7925 #7938
issues: #7842 #7073 #6354 #6149

description: {
Expand All @@ -12,5 +12,7 @@ description: {
- `cabal clean script` added. It will clean the cache for script.
- `cabal clean` will now remove script caches for which there is no marching script.
- `cabal list-bin` now works with scripts
- The name of the generated script executable has been changed from "script" to
"cabal-script-<your-sanitized-script-name>" for easier process management.

}

0 comments on commit 73a343b

Please sign in to comment.