1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Annotations to gather metadata on Android projects. Get the metadata
18 # by calling `ModelBuilder::android_project_for`.
19 module android_annotations
23 import modelize_property
27 # Metadata associated to an Android project
29 # Name of the resulting application
30 var name
: nullable String = null
32 # Java package used to identify the APK
33 var java_package
: nullable String = null
35 # Version of the Android application and APK
36 var version
: nullable String = null
38 # Numerical version code of the Android application and APK
39 var version_code
: Int = 0
41 # Custom lines to add to the AndroidManifest.xml in the <manifest> node
42 var manifest_lines
= new Array[String]
44 # Custom lines to add to the AndroidManifest.xml in the <application> node
45 var manifest_application_lines
= new Array[String]
47 # Minimum API level required for the application to run
48 var min_sdk
: nullable Int = null
50 # Build target API level
51 var target_sdk
: nullable Int = null
53 # Maximum API level on which the application will be allowed to run
54 var max_sdk
: nullable Int = null
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"}}}"""
62 redef class ModelBuilder
63 # Get the `AndroidProject` gathered from `mmodule` and its importations
64 fun android_project_for
(mmodule
: MModule): AndroidProject
66 var project
= new AndroidProject
68 var annot
= priority_annotation_on_modules
("app_name", mmodule
)
69 if annot
!= null then project
.name
= annot
.arg_as_string
(self)
71 annot
= priority_annotation_on_modules
("app_version", mmodule
)
72 if annot
!= null then project
.version
= annot
.as_version
(self)
74 annot
= priority_annotation_on_modules
("java_package", mmodule
)
75 if annot
!= null then project
.java_package
= annot
.arg_as_string
(self)
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)
80 annots
= collect_annotations_on_modules
("max_sdk_version", mmodule
)
81 for an
in annots
do project
.max_sdk
= an
.arg_as_int
(self)
83 annots
= collect_annotations_on_modules
("target_sdk_version", mmodule
)
84 for an
in annots
do project
.target_sdk
= an
.arg_as_int
(self)
86 annots
= collect_annotations_on_modules
("android_manifest", mmodule
)
87 for an
in annots
do project
.manifest_lines
.add an
.arg_as_string
(self)
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)
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
97 toolcontext
.check_errors
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]
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
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
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.")
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
)
144 if sources
.length
> 1 then
145 toolcontext
.error
(mmodule
.location
,
146 "Priority conflict on annotation {name}, it has been defined in: {sources.join(", ")}")
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
157 var annotation_name
= n_atid
.n_id
.text
158 var format_error
= "Annotation error: \"{annotation_name}\
" expects a single String as argument."
162 if args
.length
!= 1 then
163 modelbuilder
.error
(self, format_error
)
168 if not arg
isa AExprAtArg then
169 modelbuilder
.error
(self, format_error
)
173 var expr
= arg
.n_expr
174 if not expr
isa AStringFormExpr then
175 modelbuilder
.error
(self, format_error
)
178 return expr
.value
.as(not null)
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
185 var annotation_name
= n_atid
.n_id
.text
186 var format_error
= "Annotation error: \"{annotation_name}\
" expects a single Int as argument."
190 if args
.length
!= 1 then
191 modelbuilder
.error
(self, format_error
)
196 if not arg
isa AExprAtArg then
197 modelbuilder
.error
(self, format_error
)
201 var expr
= arg
.n_expr
202 if not expr
isa AIntExpr then
203 modelbuilder
.error
(self, format_error
)
206 return expr
.value
.as(not null)
210 # Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`.
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
216 var annotation_name
= n_atid
.n_id
.text
217 var version_fields
= new Array[Object]
221 if args
.length
< 1 then
222 modelbuilder
.error
(self, "Annotation error: \"{annotation_name}\
" expects at least a single argument.")
226 var format_error
= "Annotation error: \"{annotation_name}\
" expects its arguments to be of type Int or a call to `git_revision`"
228 if not arg
isa AExprAtArg then
229 modelbuilder
.error
(self, format_error
)
233 var expr
= arg
.n_expr
234 if expr
isa AIntExpr then
235 var value
= expr
.value
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.")
249 # Get Git short revision
250 var proc
= new IProcess("git", "rev-parse", "--short", "HEAD")
252 assert proc
.status
== 0
253 var lines
= proc
.read_all
254 var revision
= lines
.split
("\n").first
257 # If not, the return of `git diff --shortstat` is an empty line
258 proc
= new IProcess("git", "diff-index", "--quiet", "HEAD")
260 var dirty
= proc
.status
!= 0
261 if dirty
then revision
+= ".d"
263 version_fields
.add revision
265 modelbuilder
.error
(self, format_error
)
271 return version_fields
.join
(".")