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
21 private import parser_util
25 private import annotation
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 # Custom lines to add to AndroidManifest.xml as attributes inside the <activity> node
48 var manifest_activity_attributes
= new Array[String]
50 # Minimum API level required for the application to run
51 var min_api
: nullable Int = null
53 # Build target API level
54 var target_api
: nullable Int = null
56 # Maximum API level on which the application will be allowed to run
57 var max_api
: nullable Int = null
59 # Activities to declare in the manifest
60 var activities
= new Array[String]
62 redef fun to_s
do return """
63 name: {{{name or else "null"}}}
64 namespace: {{{java_package or else "null"}}}
65 version: {{{version or else "null"}}}"""
68 redef class ModelBuilder
69 # Get the `AndroidProject` gathered from `mmodule` and its importations
70 fun android_project_for
(mmodule
: MModule): AndroidProject
72 var project
= new AndroidProject
74 var annot
= lookup_annotation_on_modules
("app_name", mmodule
)
75 if annot
!= null then project
.name
= annot
.arg_as_string
(self)
77 annot
= lookup_annotation_on_modules
("app_version", mmodule
)
78 if annot
!= null then project
.version
= annot
.as_version
(self)
80 annot
= lookup_annotation_on_modules
("java_package", mmodule
)
81 if annot
!= null then project
.java_package
= annot
.arg_as_string
(self)
83 var annots
= collect_annotations_on_modules
("min_api_version", mmodule
)
84 if not annots
.is_empty
then
85 var i
= annots
.pop
.arg_as_int
(self)
86 if i
== null then i
= 0
89 i
= an
.arg_as_int
(self)
90 if i
== null then continue
91 project
.min_api
= project
.min_api
.max
(i
)
95 annots
= collect_annotations_on_modules
("max_api_version", mmodule
)
96 if not annots
.is_empty
then
97 var i
= annots
.pop
.arg_as_int
(self)
98 if i
== null then i
= 0
101 i
= an
.arg_as_int
(self)
102 if i
== null then continue
103 project
.max_api
= project
.max_api
.min
(i
)
107 annot
= lookup_annotation_on_modules
("target_api_version", mmodule
)
108 if annot
!= null then project
.target_api
= annot
.arg_as_int
(self) or else 0
110 annots
= collect_annotations_on_modules
("android_manifest", mmodule
)
111 for an
in annots
do project
.manifest_lines
.add an
.arg_as_string
(self) or else ""
113 annots
= collect_annotations_on_modules
("android_manifest_application", mmodule
)
114 for an
in annots
do project
.manifest_application_lines
.add an
.arg_as_string
(self) or else ""
116 annots
= collect_annotations_on_modules
("android_manifest_activity", mmodule
)
117 for an
in annots
do project
.manifest_activity_attributes
.add an
.arg_as_string
(self) or else ""
119 annots
= collect_annotations_on_modules
("android_activity", mmodule
)
121 var activity
= an
.arg_as_string
(self)
122 if activity
!= null then project
.activities
.add activity
125 # Get the date and time (down to the minute) as string
126 var local_time
= new Tm.localtime
127 var local_time_s
= local_time
.strftime
("%y%m%d%H%M")
128 project
.version_code
= local_time_s
.to_i
130 toolcontext
.check_errors
136 redef class AAnnotation
137 # Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`.
139 # The user can enter as many fields as needed. The call to `git_revision` will be replaced by the short
140 # revision number. If the working tree is dirty, it will append another field with "d" for dirty.
141 private fun as_version
(modelbuilder
: ModelBuilder): String
143 var version_fields
= new Array[Object]
146 if args
.length
< 1 then
147 modelbuilder
.error
(self, "Annotation error: \"{name}\
" expects at least a single argument.")
151 var format_error
= "Annotation error: \"{name}\
" expects its arguments to be of type Int or a call to `git_revision`"
155 if value
!= null then
156 version_fields
.add value
160 value
= arg
.as_string
161 if value
!= null then
162 version_fields
.add value
166 if value
== "git_revision" then
167 # Get Git short revision
168 var proc
= new ProcessReader("git", "rev-parse", "--short", "HEAD")
170 assert proc
.status
== 0
171 var lines
= proc
.read_all
172 var revision
= lines
.split
("\n").first
175 # If not, the return of `git diff --shortstat` is an empty line
176 proc
= new ProcessReader("git", "diff-index", "--quiet", "HEAD")
178 var dirty
= proc
.status
!= 0
179 if dirty
then revision
+= ".d"
181 version_fields
.add revision
185 modelbuilder
.error
(self, format_error
)
190 return version_fields
.join
(".")