Skip to content

Commit

Permalink
REPLMode: Define PackageToken as abstract type
Browse files Browse the repository at this point in the history
Previously, `PackageToken` was a Union of multiple struct types. This
commit turns `PackageToken` into an abstract type with multiple
subtypes.

The original constructor for `PackageToken` was replaced with a function
`packagetoken`.
  • Loading branch information
barucden committed Nov 21, 2022
1 parent d55ab57 commit 1b0e16e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
50 changes: 27 additions & 23 deletions src/REPLMode/argument_parsers.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import ..isdir_nothrow, ..Registry.RegistrySpec, ..isurl

abstract type PackageToken end

struct PackageIdentifier <: PackageToken
val::String
end

struct VersionToken <: PackageToken
version::String
end

struct Rev <: PackageToken
rev::String
end

struct Subdir <: PackageToken
dir::String
end

packagetoken(word::String)::PackageToken =
first(word) == '@' ? VersionToken(word[2:end]) :
first(word) == '#' ? Rev(word[2:end]) :
first(word) == ':' ? Subdir(word[2:end]) :
PackageIdentifier(word)

###############
# PackageSpec #
###############
Expand All @@ -17,26 +41,11 @@ function parse_package(args::Vector{QString}, options; add_or_dev=false)::Vector
push!(words, word)
end
end
args = PackageToken[PackageToken(pkgword) for pkgword in words]
args = PackageToken[packagetoken(pkgword) for pkgword in words]

return parse_package_args(args; add_or_dev=add_or_dev)
end

struct VersionToken
version::String
end

struct Rev
rev::String
end

struct Subdir
dir::String
end

const PackageIdentifier = String
const PackageToken = Union{PackageIdentifier, VersionToken, Rev, Subdir}

# Match a git repository URL. This includes uses of `@` and `:` but
# requires that it has `.git` at the end.
let url = raw"((git|ssh|http(s)?)|(git@[\w\-\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git$)(/)?",
Expand Down Expand Up @@ -78,12 +87,6 @@ function package_lex(qwords::Vector{QString})::Vector{String}
return words
end

PackageToken(word::String)::PackageToken =
first(word) == '@' ? VersionToken(word[2:end]) :
first(word) == '#' ? Rev(word[2:end]) :
first(word) == ':' ? Subdir(word[2:end]) :
String(word)

function parse_package_args(args::Vector{PackageToken}; add_or_dev=false)::Vector{PackageSpec}
# check for and apply PackageSpec modifier (e.g. `#foo` or `@v1.0.2`)
function apply_modifier!(pkg::PackageSpec, args::Vector{PackageToken})
Expand Down Expand Up @@ -131,7 +134,8 @@ let uuid = raw"(?i)[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}(
end
# packages can be identified through: uuid, name, or name+uuid
# additionally valid for add/develop are: local path, url
function parse_package_identifier(word::AbstractString; add_or_develop=false)::PackageSpec
function parse_package_identifier(pkg_id::PackageIdentifier; add_or_develop=false)::PackageSpec
word = pkg_id.val
if add_or_develop
if isurl(word)
return PackageSpec(; url=word)
Expand Down
5 changes: 3 additions & 2 deletions test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,9 @@ temp_pkg_dir() do project_path
end

@testset "parse package url win" begin
@test typeof(Pkg.REPLMode.parse_package_identifier("https://github.com/abc/ABC.jl";
add_or_develop=true)) == Pkg.Types.PackageSpec
pkg_id = Pkg.REPLMode.PackageIdentifier("https://github.com/abc/ABC.jl")
pkg_spec = Pkg.REPLMode.parse_package_identifier(pkg_id; add_or_develop=true)
@test typeof(pkg_spec) == Pkg.Types.PackageSpec
end

@testset "parse git url (issue #1935) " begin
Expand Down

0 comments on commit 1b0e16e

Please sign in to comment.