39037e21e19342a6296524d1e2e186e9f476c418
[nit.git] / src / android_annotations.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Annotations to gather metadata on Android projects. Get the metadata
18 # by calling `ModelBuilder::android_project_for`.
19 module android_annotations
20
21 import parser_util
22 import modelbuilder
23 import modelize_property
24 import literal
25 import typing
26
27 # Metadata associated to an Android project
28 class AndroidProject
29 # Name of the resulting application
30 var name: nullable String = null
31
32 # Java package used to identify the APK
33 var java_package: nullable String = null
34
35 # Version of the Android application and APK
36 var version: nullable String = null
37
38 # Numerical version code of the Android application and APK
39 var version_code: Int = 0
40
41 # Custom lines to add to the AndroidManifest.xml in the <manifest> node
42 var manifest_lines = new Array[String]
43
44 # Custom lines to add to the AndroidManifest.xml in the <application> node
45 var manifest_application_lines = new Array[String]
46
47 # Minimum API level required for the application to run
48 var min_sdk: nullable Int = null
49
50 # Build target API level
51 var target_sdk: nullable Int = null
52
53 # Maximum API level on which the application will be allowed to run
54 var max_sdk: nullable Int = null
55
56 redef fun to_s do return """
57 name: {{{name or else "null"}}}
58 namespace: {{{java_package or else "null"}}}
59 version: {{{version or else "null"}}}"""
60 end
61
62 redef class ModelBuilder
63 # Get the `AndroidProject` gathered from `mmodule` and its importations
64 fun android_project_for(mmodule: MModule): AndroidProject
65 do
66 var project = new AndroidProject
67
68 var annot = priority_annotation_on_modules("app_name", mmodule)
69 if annot != null then project.name = annot.arg_as_string(self)
70
71 annot = priority_annotation_on_modules("app_version", mmodule)
72 if annot != null then project.version = annot.as_version(self)
73
74 annot = priority_annotation_on_modules("java_package", mmodule)
75 if annot != null then project.java_package = annot.arg_as_string(self)
76
77 var annots = collect_annotations_on_modules("min_sdk_version", mmodule)
78 for an in annots do project.min_sdk = an.arg_as_int(self)
79
80 annots = collect_annotations_on_modules("max_sdk_version", mmodule)
81 for an in annots do project.max_sdk = an.arg_as_int(self)
82
83 annots = collect_annotations_on_modules("target_sdk_version", mmodule)
84 for an in annots do project.target_sdk = an.arg_as_int(self)
85
86 annots = collect_annotations_on_modules("android_manifest", mmodule)
87 for an in annots do project.manifest_lines.add an.arg_as_string(self)
88
89 annots = collect_annotations_on_modules("android_manifest_application", mmodule)
90 for an in annots do project.manifest_application_lines.add an.arg_as_string(self)
91
92 # Get the date and time (down to the minute) as string
93 var local_time = new Tm.localtime
94 var local_time_s = local_time.strftime("%y%m%d%H%M")
95 project.version_code = local_time_s.to_i
96
97 toolcontext.check_errors
98
99 return project
100 end
101
102 # Recursively collect all annotations by name in `mmodule` and its importations (direct and indirect)
103 private fun collect_annotations_on_modules(name: String, mmodule: MModule): Array[AAnnotation]
104 do
105 var annotations = new Array[AAnnotation]
106 for mmod in mmodule.in_importation.greaters do
107 if not mmodule2nmodule.keys.has(mmod) then continue
108 var amod = mmodule2nmodule[mmod]
109 var module_decl = amod.n_moduledecl
110 if module_decl == null then continue
111 var aas = module_decl.collect_annotations_by_name(name)
112 annotations.add_all aas
113 end
114 return annotations
115 end
116
117 # Get an annotation by name from `mmodule` and its super modules. Will recursively search
118 # in imported module to find the "latest" declaration and detects priority conflicts.
119 private fun priority_annotation_on_modules(name: String, mmodule: MModule): nullable AAnnotation
120 do
121 if mmodule2nmodule.keys.has(mmodule) then
122 var amod = mmodule2nmodule[mmodule]
123 var module_decl = amod.n_moduledecl
124 if module_decl != null then
125 var annotations = module_decl.collect_annotations_by_name(name)
126 if annotations.length == 1 then
127 return annotations.first
128 else if annotations.length > 1 then
129 toolcontext.error(mmodule.location,
130 "Multiple declaration of annotation {name}, it must be defined only once.")
131 end
132 end
133 end
134
135 var sources = new Array[MModule]
136 var annotations = null
137 for mmod in mmodule.in_importation.direct_greaters do
138 var res = priority_annotation_on_modules(name, mmod)
139 if res != null then
140 sources.add mmod
141 annotations = res
142 end
143 end
144 if sources.length > 1 then
145 toolcontext.error(mmodule.location,
146 "Priority conflict on annotation {name}, it has been defined in: {sources.join(", ")}")
147 return null
148 end
149 return annotations
150 end
151 end
152
153 redef class AAnnotation
154 # Get the single argument of `self` as a `String`. Raise error on any inconsistency.
155 private fun arg_as_string(modelbuilder: ModelBuilder): String
156 do
157 var annotation_name = n_atid.n_id.text
158 var format_error = "Annotation error: \"{annotation_name}\" expects a single String as argument."
159
160 var args = n_args
161 var platform_name
162 if args.length != 1 then
163 modelbuilder.error(self, format_error)
164 return ""
165 else
166 var arg = args.first
167
168 if not arg isa AExprAtArg then
169 modelbuilder.error(self, format_error)
170 return ""
171 end
172
173 var expr = arg.n_expr
174 if not expr isa AStringFormExpr then
175 modelbuilder.error(self, format_error)
176 return ""
177 end
178 return expr.value.as(not null)
179 end
180 end
181
182 # Get the single argument of `self` as an `Int`. Raise error on any inconsistency.
183 private fun arg_as_int(modelbuilder: ModelBuilder): nullable Int
184 do
185 var annotation_name = n_atid.n_id.text
186 var format_error = "Annotation error: \"{annotation_name}\" expects a single Int as argument."
187
188 var args = n_args
189 var platform_name
190 if args.length != 1 then
191 modelbuilder.error(self, format_error)
192 return null
193 else
194 var arg = args.first
195
196 if not arg isa AExprAtArg then
197 modelbuilder.error(self, format_error)
198 return null
199 end
200
201 var expr = arg.n_expr
202 if not expr isa AIntExpr then
203 modelbuilder.error(self, format_error)
204 return null
205 end
206 return expr.value.as(not null)
207 end
208 end
209
210 # Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`.
211 #
212 # The user can enter as many fields as needed. The call to `git_revision` will be replaced by the short
213 # revision number. If the working tree is dirty, it will append another field with "d" for dirty.
214 private fun as_version(modelbuilder: ModelBuilder): String
215 do
216 var annotation_name = n_atid.n_id.text
217 var version_fields = new Array[Object]
218
219 var args = n_args
220 var platform_name
221 if args.length < 1 then
222 modelbuilder.error(self, "Annotation error: \"{annotation_name}\" expects at least a single argument.")
223 return ""
224 else
225 for arg in args do
226 var format_error = "Annotation error: \"{annotation_name}\" expects its arguments to be of type Int or a call to `git_revision`"
227
228 if not arg isa AExprAtArg then
229 modelbuilder.error(self, format_error)
230 return ""
231 end
232
233 var expr = arg.n_expr
234 if expr isa AIntExpr then
235 var value = expr.value
236 assert value != null
237 version_fields.add value
238 else if expr isa AStringFormExpr then
239 version_fields.add expr.value.as(not null)
240 else if expr isa ACallExpr then
241 # We support calls to "git" only
242 var exec_args = expr.n_args.to_a
243 if expr.n_id.text != "git_revision" or not exec_args.is_empty then
244 modelbuilder.error(self,
245 "Annotation error: \"{annotation_name}\" accepts only calls to `git_revision` with the command as arguments.")
246 return ""
247 end
248
249 # Get Git short revision
250 var proc = new IProcess("git", "rev-parse", "--short", "HEAD")
251 proc.wait
252 assert proc.status == 0
253 var lines = proc.read_all
254 var revision = lines.split("\n").first
255
256 # Is it dirty?
257 # If not, the return of `git diff --shortstat` is an empty line
258 proc = new IProcess("git", "diff-index", "--quiet", "HEAD")
259 proc.wait
260 var dirty = proc.status != 0
261 if dirty then revision += ".d"
262
263 version_fields.add revision
264 else
265 modelbuilder.error(self, format_error)
266 return ""
267 end
268 end
269 end
270
271 return version_fields.join(".")
272 end
273 end