Skip to content

Commit 7e7baca

Browse files
committed
moon_json: dependencies
1 parent 170fcf5 commit 7e7baca

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

src/moon_json/dependencies.cr

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# -----------------------------------------------------------------------
2+
# This file is part of MoonScript
3+
#
4+
# MoonSript is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# MoonSript is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with MoonSript. If not, see <https://www.gnu.org/licenses/>.
16+
#
17+
# Copyright (C) 2025 Krisna Pranav, MoonScript Developers
18+
# -----------------------------------------------------------------------
19+
20+
module MoonScript
21+
class MoonJson
22+
class Parser
23+
def parse_dependencies : Array(Installer::Dependency)
24+
@parser.location.try do |location|
25+
dependencies = [] of Installer::Dependency
26+
27+
@parser.read_object do |key|
28+
dependencies << parse_dependency(key)
29+
end
30+
31+
error! :dependencies_empty do
32+
block do
33+
text "The"
34+
bold "dependencies"
35+
text "field lists all the dependencies for the application."
36+
end
37+
38+
block do
39+
text "The"
40+
bold "dependencies"
41+
text "object should not be empty! "
42+
end
43+
44+
snippet snippet_data(location)
45+
end if dependencies.empty?
46+
47+
dependencies
48+
end
49+
rescue JSON::ParseException
50+
error! :dependencies_invalid do
51+
block do
52+
text "The"
53+
bold "dependencies"
54+
text "field lists all the dependencies for the application."
55+
end
56+
57+
snippet "It should be an object!", snippet_data
58+
end
59+
end
60+
61+
def parse_dependency(package : String) : Installer::Dependency
62+
repository, constraint = nil, nil
63+
64+
@parser.read_object do |key|
65+
case key
66+
when "repository"
67+
repository = parse_dependency_repository
68+
when "constraint"
69+
constraint = parse_dependency_constraint
70+
else
71+
error! :dependency_invalid_key do
72+
snippet "A dependency object has an invalid key:", key
73+
snippet "It is here:", snippet_data
74+
end
75+
end
76+
end
77+
78+
error! :dependency_missing_repository do
79+
block do
80+
text "A"
81+
bold "dependency object"
82+
text "is missing the"
83+
bold "repository"
84+
end
85+
86+
snippet snippet_data
87+
end unless repository
88+
89+
error! :dependency_missing_constraint do
90+
block do
91+
text "A"
92+
bold "dependency object"
93+
text "is missing the"
94+
bold "constraint"
95+
end
96+
97+
snippet snippet_data
98+
end unless constraint
99+
100+
Installer::Dependency.new package, repository, constraint
101+
rescue JSON::ParseException
102+
error! :dependency_invalid do
103+
snippet "A dependency must be an object!", snippet_data
104+
end
105+
end
106+
107+
def parse_dependency_repository : String
108+
@parser.read_string
109+
rescue JSON::ParseException
110+
error! :dependency_repository_invalid do
111+
block do
112+
text "The"
113+
bold "repository"
114+
text "field of a depencency must be an string!"
115+
end
116+
117+
snippet snippet_data
118+
end
119+
end
120+
121+
def parse_dependency_constraint : Installer::Constraint
122+
location =
123+
@parser.location
124+
125+
raw =
126+
@parser.read_string
127+
128+
match =
129+
raw.match(/(\d+\.\d+\.\d+)\s*<=\s*v\s*<\s*(\d+\.\d+\.\d+)/)
130+
131+
constraint =
132+
if match
133+
lower =
134+
Installer::Semver.parse?(match[1])
135+
136+
upper =
137+
Installer::Semver.parse?(match[2])
138+
139+
Installer::SimpleConstraint.new(lower, upper) if upper && lower
140+
else
141+
match =
142+
raw.match(/(.*?):(\d+\.\d+\.\d+)/)
143+
144+
if match
145+
version =
146+
Installer::Semver.parse?(match[2])
147+
148+
target =
149+
match[1]
150+
151+
Installer::FixedConstraint.new(version, target) if version
152+
end
153+
end
154+
155+
error! :dependency_constraint_bad do
156+
block "The constraint of a dependency is either in this format:"
157+
snippet "0.0.0 <= v < 1.0.0"
158+
159+
block "or a git tag / commit / branch followed by the version:"
160+
snippet "master:0.1.0"
161+
162+
snippet "could not able to find either:", snippet_data(location)
163+
end unless constraint
164+
165+
constraint
166+
rescue JSON::ParseException
167+
error! :dependency_constraint_invalid do
168+
block do
169+
text "The"
170+
bold "constraint"
171+
text "field of a depencency must be an string"
172+
end
173+
174+
snippet snippet_data
175+
end
176+
end
177+
end
178+
end
179+
end

0 commit comments

Comments
 (0)