Skip to content

Commit

Permalink
Merge pull request #3360 from Matthew-Mosior/issue-3232-init-doesnt-c…
Browse files Browse the repository at this point in the history
…heck-the-name-of-a-package

`idris2 --init` doesnt check the name of a package
  • Loading branch information
andrevidela authored Jul 31, 2024
2 parents 3f76bfd + 6bda3d5 commit 72241a4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ This CHANGELOG describes the merged but unreleased changes. Please see [CHANGELO
installed will be ignored by the compiler when it tries to use that library as
a dependency for some other package.

* The `idris2 --init` command now ensures that package names are
valid Idris2 identifiers.

### Building/Packaging changes

* The Nix flake's `buildIdris` function now returns a set with `executable` and
Expand Down
3 changes: 2 additions & 1 deletion src/Idris/Package.idr
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,8 @@ processPackage : {auto c : Ref Ctxt Defs} ->
processPackage opts (cmd, mfile)
= withCtxt . withSyn . withROpts $ case cmd of
Init =>
do pkg <- coreLift interactive
do Just pkg <- coreLift interactive
| Nothing => coreLift (exitWith (ExitFailure 1))
let fp = fromMaybe (pkg.name ++ ".ipkg") mfile
False <- coreLift (exists fp)
| _ => throw (GenericMsg emptyFC ("File " ++ fp ++ " already exists"))
Expand Down
30 changes: 27 additions & 3 deletions src/Idris/Package/Init.idr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Idris.Package.Types
import System.Directory
import Control.App.FileIO

import Libraries.Text.Lexer
import Libraries.Utils.Path
import Libraries.System.Directory.Tree

Expand Down Expand Up @@ -67,9 +68,11 @@ prompt p = putStr p >> fflush stdout >> getLine

export
covering
interactive : IO PkgDesc
interactive : IO (Maybe PkgDesc)
interactive = do
pname <- prompt "Package name: "
pname <- prompt "Package name: "
let True = checkPackageName $ fastUnpack pname
| False => pure Nothing
pauthors <- prompt "Package authors: "
poptions <- prompt "Package options: "
psource <- prompt "Source directory: "
Expand All @@ -81,11 +84,32 @@ interactive = do
, modules := modules
, sourcedir := sourcedir
} (initPkgDesc (fromMaybe "project" (mstring pname)))
pure pkg
pure $ Just pkg

where

mstring : String -> Maybe String
mstring str = case trim str of
"" => Nothing
str => Just str

isIdentStart : Char -> Bool
isIdentStart '_' = True
isIdentStart x = isUpper x ||
isAlpha x ||
x > chr 160

isIdentTrailing : List Char -> Bool
isIdentTrailing [] = True
isIdentTrailing (x::xs) = case isAlphaNum x ||
x > chr 160 ||
x == '-' ||
x == '_' ||
x == '\'' of
False => False
True => isIdentTrailing xs

checkPackageName : List Char -> Bool
checkPackageName [] = True
checkPackageName (x::xs) = isIdentStart x &&
isIdentTrailing xs

0 comments on commit 72241a4

Please sign in to comment.