Merge: nitrpg: Move `nitrpg` to its own repository
[nit.git] / src / doc / commands / commands_ini.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 module commands_ini
16
17 import commands_model
18
19 # Cmd that finds the ini file related to an `mentity`
20 abstract class CmdIni
21 super CmdEntity
22
23 # Ini file
24 var ini: nullable IniFile = null
25
26 redef fun init_command do
27 var res = super
28 if not res isa CmdSuccess then return res
29 var mentity = self.mentity.as(not null)
30
31 if not mentity isa MPackage then return new WarningNoIni(mentity)
32
33 var ini = mentity.ini
34 if ini == null then return new WarningNoIni(mentity)
35
36 self.ini = ini
37
38 return res
39 end
40 end
41
42 # No ini file for `mentity`
43 class WarningNoIni
44 super CmdWarning
45
46 # MEntity provided
47 var mentity: MEntity
48
49 redef fun to_s do return "No ini file for `{mentity.full_name}`"
50 end
51
52 # Cmd that finds the ini description related to an `mentity`
53 class CmdIniDescription
54 super CmdIni
55
56 # Ini description
57 var desc: nullable String = null
58
59 redef fun init_command do
60 var res = super
61 if not res isa CmdSuccess then return res
62
63 var mentity = self.mentity.as(not null)
64 var ini = self.ini.as(not null)
65
66 var desc = ini["package.desc"]
67 if desc == null then return new WarningNoDescription(mentity)
68 self.desc = desc
69
70 return res
71 end
72 end
73
74 # No git clone url for `mentity`
75 class WarningNoDescription
76 super CmdWarning
77
78 # MEntity provided
79 var mentity: MEntity
80
81 redef fun to_s do return "No description for `{mentity.full_name}`"
82 end
83
84 # Cmd that finds the clone url related to an `mentity`
85 class CmdIniGitUrl
86 super CmdIni
87
88 # Clone url
89 var url: nullable String = null
90
91 redef fun init_command do
92 var res = super
93 if not res isa CmdSuccess then return res
94
95 var mentity = self.mentity.as(not null)
96 var ini = self.ini.as(not null)
97
98 var url = ini["upstream.git"]
99 if url == null then return new WarningNoGitUrl(mentity)
100 self.url = url
101
102 return res
103 end
104 end
105
106 # No git url for `mentity`
107 class WarningNoGitUrl
108 super CmdWarning
109
110 # MEntity provided
111 var mentity: MEntity
112
113 redef fun to_s do return "No git url for `{mentity.full_name}`"
114 end
115
116 # Cmd that finds the git clone command related to an `mentity`
117 class CmdIniCloneCommand
118 super CmdIniGitUrl
119
120 # Clone command
121 var command: nullable String is lazy do
122 var url = self.url
123 if url == null then return null
124 return "git clone {url}"
125 end
126 end
127
128 # Cmd that finds the issues link related to an `mentity`
129 class CmdIniIssuesUrl
130 super CmdIni
131
132 # Issues url
133 var url: nullable String = null
134
135 redef fun init_command do
136 var res = super
137 if not res isa CmdSuccess then return res
138
139 var mentity = self.mentity.as(not null)
140 var ini = self.ini.as(not null)
141
142 var url = ini["upstream.issues"]
143 if url == null then return new WarningNoIssuesUrl(mentity)
144 self.url = url
145
146 return res
147 end
148 end
149
150 # No issues url for `mentity`
151 class WarningNoIssuesUrl
152 super CmdWarning
153
154 # MEntity provided
155 var mentity: MEntity
156
157 redef fun to_s do return "No issues url for `{mentity.full_name}`"
158 end
159
160 # Cmd that finds the maintainer name of an `mentity`
161 class CmdIniMaintainer
162 super CmdIni
163
164 # Maintainer name
165 var maintainer: nullable String = null
166
167 redef fun init_command do
168 var res = super
169 if not res isa CmdSuccess then return res
170
171 var mentity = self.mentity.as(not null)
172 var ini = self.ini.as(not null)
173
174 var maintainer = ini["package.maintainer"]
175 if maintainer == null then return new WarningNoMaintainer(mentity)
176 self.maintainer = maintainer
177
178 return res
179 end
180 end
181
182 # No maintainer for `mentity`
183 class WarningNoMaintainer
184 super CmdWarning
185
186 # MEntity provided
187 var mentity: MEntity
188
189 redef fun to_s do return "No maintainer for `{mentity.full_name}`"
190 end
191
192 # Cmd that finds the contributors list of an `mentity`
193 class CmdIniContributors
194 super CmdIni
195
196 # Contributors list
197 var contributors: nullable Array[String] = null
198
199 redef fun init_command do
200 var res = super
201 if not res isa CmdSuccess then return res
202
203 var mentity = self.mentity.as(not null)
204 var ini = self.ini.as(not null)
205
206 var names = ini["package.more_contributors"]
207 if names == null then return new WarningNoContributor(mentity)
208
209 var contributors = new Array[String]
210 for name in names.split(",") do
211 contributors.add name.trim
212 end
213 if contributors.is_empty then return new WarningNoContributor(mentity)
214 self.contributors = contributors
215
216 return res
217 end
218 end
219
220 # No contributor for `mentity`
221 class WarningNoContributor
222 super CmdWarning
223
224 # MEntity provided
225 var mentity: MEntity
226
227 redef fun to_s do return "No contributor for `{mentity.full_name}`"
228 end
229
230 # Cmd that finds the license related to an `mentity`
231 class CmdIniLicense
232 super CmdIni
233
234 # License string
235 var license: nullable String = null
236
237 redef fun init_command do
238 var res = super
239 if not res isa CmdSuccess then return res
240
241 var mentity = self.mentity.as(not null)
242 var ini = self.ini.as(not null)
243
244 var license = ini["package.license"]
245 if license == null then return new WarningNoLicense(mentity)
246 self.license = license
247
248 return res
249 end
250 end
251
252 # No ini license string for `mentity`
253 class WarningNoLicense
254 super CmdWarning
255
256 # MEntity provided
257 var mentity: MEntity
258
259 redef fun to_s do return "No license for `{mentity.full_name}`"
260 end
261
262 abstract class CmdEntityFile
263 super CmdEntity
264
265 # File path
266 var file: nullable String = null is writable
267
268 # Accepted file names
269 fun file_names: Array[String] is abstract
270
271 # Init file related data
272 fun init_file: CmdMessage do
273 var mentity = self.mentity.as(not null)
274
275 var source_file = mentity.location.file
276 if source_file == null then return throw_warning
277
278 for file_name in file_names do
279 var file = source_file.filename / file_name
280 if not file.file_exists then continue
281 self.file = file
282 break
283 end
284
285 if file == null then return throw_warning
286
287 return new CmdSuccess
288 end
289
290 redef fun init_command do
291 var res = super
292 if not res isa CmdSuccess then return res
293 return init_file
294 end
295
296 fun throw_warning: CmdWarning is abstract
297 end
298
299 abstract class CmdEntityFileContent
300 super CmdEntityFile
301
302 # File content
303 var content: nullable String = null is writable
304
305 redef fun init_file do
306 var res = super
307 if not res isa CmdSuccess then return res
308
309 var file = self.file.as(not null)
310 content = file.to_path.read_all
311 return res
312 end
313 end
314
315 # Cmd that finds the license file related to an `mentity`
316 class CmdLicenseFile
317 super CmdEntityFile
318
319 redef var file_names = ["LICENSE", "LICENSE.md", "LICENSE.txt"]
320 redef fun throw_warning do return new WarningNoLicenseFile(mentity.as(not null))
321 end
322
323 # Cmd that finds the license file content related to an `mentity`
324 class CmdLicenseFileContent
325 super CmdEntityFileContent
326 super CmdLicenseFile
327 end
328
329 # No license file for `mentity`
330 class WarningNoLicenseFile
331 super CmdWarning
332
333 # MEntity provided
334 var mentity: MEntity
335
336 redef fun to_s do return "No license file for `{mentity.full_name}`"
337 end
338
339 # Cmd that finds the contributing file related to an `mentity`
340 class CmdContribFile
341 super CmdEntityFile
342
343 redef var file_names = ["CONTRIBUTING", "CONTRIBUTING.md", "CONTRIBUTING.txt"]
344 redef fun throw_warning do return new WarningNoContribFile(mentity.as(not null))
345 end
346
347 # Cmd that finds the contrib file content related to an `mentity`
348 class CmdContribFileContent
349 super CmdEntityFileContent
350 super CmdContribFile
351 end
352
353 # No license file for `mentity`
354 class WarningNoContribFile
355 super CmdWarning
356
357 # MEntity provided
358 var mentity: MEntity
359
360 redef fun to_s do return "No contributing file for `{mentity.full_name}`"
361 end