android: fix priority annotations bug
[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 private import annotation
27
28 # Metadata associated to an Android project
29 class AndroidProject
30 # Name of the resulting application
31 var name: nullable String = null
32
33 # Java package used to identify the APK
34 var java_package: nullable String = null
35
36 # Version of the Android application and APK
37 var version: nullable String = null
38
39 # Numerical version code of the Android application and APK
40 var version_code: Int = 0
41
42 # Custom lines to add to the AndroidManifest.xml in the <manifest> node
43 var manifest_lines = new Array[String]
44
45 # Custom lines to add to the AndroidManifest.xml in the <application> node
46 var manifest_application_lines = new Array[String]
47
48 # Minimum API level required for the application to run
49 var min_api: nullable Int = null
50
51 # Build target API level
52 var target_api: nullable Int = null
53
54 # Maximum API level on which the application will be allowed to run
55 var max_api: nullable Int = null
56
57 redef fun to_s do return """
58 name: {{{name or else "null"}}}
59 namespace: {{{java_package or else "null"}}}
60 version: {{{version or else "null"}}}"""
61 end
62
63 redef class ModelBuilder
64 # Get the `AndroidProject` gathered from `mmodule` and its importations
65 fun android_project_for(mmodule: MModule): AndroidProject
66 do
67 var project = new AndroidProject
68
69 var annot = priority_annotation_on_modules("app_name", mmodule)
70 if annot != null then project.name = annot.arg_as_string(self)
71
72 annot = priority_annotation_on_modules("app_version", mmodule)
73 if annot != null then project.version = annot.as_version(self)
74
75 annot = priority_annotation_on_modules("java_package", mmodule)
76 if annot != null then project.java_package = annot.arg_as_string(self)
77
78 var annots = collect_annotations_on_modules("min_api_version", mmodule)
79 for an in annots do project.min_api = an.arg_as_int(self)
80
81 annots = collect_annotations_on_modules("max_api_version", mmodule)
82 for an in annots do project.max_api = an.arg_as_int(self)
83
84 annots = collect_annotations_on_modules("target_api_version", mmodule)
85 for an in annots do project.target_api = an.arg_as_int(self)
86
87 annots = collect_annotations_on_modules("android_manifest", mmodule)
88 for an in annots do project.manifest_lines.add an.arg_as_string(self) or else ""
89
90 annots = collect_annotations_on_modules("android_manifest_application", mmodule)
91 for an in annots do project.manifest_application_lines.add an.arg_as_string(self) or else ""
92
93 # Get the date and time (down to the minute) as string
94 var local_time = new Tm.localtime
95 var local_time_s = local_time.strftime("%y%m%d%H%M")
96 project.version_code = local_time_s.to_i
97
98 toolcontext.check_errors
99
100 return project
101 end
102
103 # Recursively collect all annotations by name in `mmodule` and its importations (direct and indirect)
104 private fun collect_annotations_on_modules(name: String, mmodule: MModule): Array[AAnnotation]
105 do
106 var annotations = new Array[AAnnotation]
107 for mmod in mmodule.in_importation.greaters do
108 if not mmodule2nmodule.keys.has(mmod) then continue
109 var amod = mmodule2nmodule[mmod]
110 var module_decl = amod.n_moduledecl
111 if module_decl == null then continue
112 var aas = module_decl.collect_annotations_by_name(name)
113 annotations.add_all aas
114 end
115 return annotations
116 end
117
118 # Get an annotation by name from `mmodule` and its super modules. Will recursively search
119 # in imported module to find the "latest" declaration and detects priority conflicts.
120 private fun priority_annotation_on_modules(name: String, mmodule: MModule): nullable AAnnotation
121 do
122 if mmodule2nmodule.keys.has(mmodule) then
123 var amod = mmodule2nmodule[mmodule]
124 var module_decl = amod.n_moduledecl
125 if module_decl != null then
126 var annotations = module_decl.collect_annotations_by_name(name)
127 if annotations.length == 1 then
128 return annotations.first
129 else if annotations.length > 1 then
130 toolcontext.error(mmodule.location,
131 "Multiple declaration of annotation {name}, it must be defined only once.")
132 end
133 end
134 end
135
136 var annotations = new HashSet[AAnnotation]
137 for mmod in mmodule.in_importation.direct_greaters do
138 var res = priority_annotation_on_modules(name, mmod)
139 if res != null then annotations.add res
140 end
141 if annotations.length > 1 then
142 var locs = new Array[Location]
143 for annot in annotations do locs.add(annot.location)
144
145 toolcontext.error(mmodule.location,
146 "Priority conflict on annotation {name}, it has been defined in: {locs.join(", ")}")
147 return null
148 else if annotations.length == 1 then
149 return annotations.first
150 else return null
151 end
152 end
153
154 redef class AAnnotation
155 # Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`.
156 #
157 # The user can enter as many fields as needed. The call to `git_revision` will be replaced by the short
158 # revision number. If the working tree is dirty, it will append another field with "d" for dirty.
159 private fun as_version(modelbuilder: ModelBuilder): String
160 do
161 var annotation_name = n_atid.n_id.text
162 var version_fields = new Array[Object]
163
164 var args = n_args
165 var platform_name
166 if args.length < 1 then
167 modelbuilder.error(self, "Annotation error: \"{name}\" expects at least a single argument.")
168 return ""
169 else
170 for arg in args do
171 var format_error = "Annotation error: \"{name}\" expects its arguments to be of type Int or a call to `git_revision`"
172
173 var value
174 value = arg.as_int
175 if value != null then
176 version_fields.add value
177 continue
178 end
179
180 value = arg.as_string
181 if value != null then
182 version_fields.add value
183 end
184
185 value = arg.as_id
186 if value == "git_revision" then
187 # Get Git short revision
188 var proc = new IProcess("git", "rev-parse", "--short", "HEAD")
189 proc.wait
190 assert proc.status == 0
191 var lines = proc.read_all
192 var revision = lines.split("\n").first
193
194 # Is it dirty?
195 # If not, the return of `git diff --shortstat` is an empty line
196 proc = new IProcess("git", "diff-index", "--quiet", "HEAD")
197 proc.wait
198 var dirty = proc.status != 0
199 if dirty then revision += ".d"
200
201 version_fields.add revision
202 continue
203 end
204
205 modelbuilder.error(self, format_error)
206 return ""
207 end
208 end
209
210 return version_fields.join(".")
211 end
212 end