src: remove some warnings and do some cleaning
[nit.git] / src / compiler / 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 private import parser_util
22 import modelize
23 import literal
24 import semantize
25 private import annotation
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_api: nullable Int = null
49
50 # Build target API level
51 var target_api: nullable Int = null
52
53 # Maximum API level on which the application will be allowed to run
54 var max_api: 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 = lookup_annotation_on_modules("app_name", mmodule)
69 if annot != null then project.name = annot.arg_as_string(self)
70
71 annot = lookup_annotation_on_modules("app_version", mmodule)
72 if annot != null then project.version = annot.as_version(self)
73
74 annot = lookup_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_api_version", mmodule)
78 if not annots.is_empty then
79 var i = annots.pop.arg_as_int(self)
80 if i == null then i = 0
81 project.min_api = i
82 for an in annots do
83 i = an.arg_as_int(self)
84 if i == null then continue
85 project.min_api = project.min_api.max(i)
86 end
87 end
88
89 annots = collect_annotations_on_modules("max_api_version", mmodule)
90 if not annots.is_empty then
91 var i = annots.pop.arg_as_int(self)
92 if i == null then i = 0
93 project.max_api = i
94 for an in annots do
95 i = an.arg_as_int(self)
96 if i == null then continue
97 project.max_api = project.max_api.min(i)
98 end
99 end
100
101 annot = lookup_annotation_on_modules("target_api_version", mmodule)
102 if annot != null then project.target_api = annot.arg_as_int(self) or else 0
103
104 annots = collect_annotations_on_modules("android_manifest", mmodule)
105 for an in annots do project.manifest_lines.add an.arg_as_string(self) or else ""
106
107 annots = collect_annotations_on_modules("android_manifest_application", mmodule)
108 for an in annots do project.manifest_application_lines.add an.arg_as_string(self) or else ""
109
110 # Get the date and time (down to the minute) as string
111 var local_time = new Tm.localtime
112 var local_time_s = local_time.strftime("%y%m%d%H%M")
113 project.version_code = local_time_s.to_i
114
115 toolcontext.check_errors
116
117 return project
118 end
119 end
120
121 redef class AAnnotation
122 # Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`.
123 #
124 # The user can enter as many fields as needed. The call to `git_revision` will be replaced by the short
125 # revision number. If the working tree is dirty, it will append another field with "d" for dirty.
126 private fun as_version(modelbuilder: ModelBuilder): String
127 do
128 var version_fields = new Array[Object]
129
130 var args = n_args
131 if args.length < 1 then
132 modelbuilder.error(self, "Annotation error: \"{name}\" expects at least a single argument.")
133 return ""
134 else
135 for arg in args do
136 var format_error = "Annotation error: \"{name}\" expects its arguments to be of type Int or a call to `git_revision`"
137
138 var value
139 value = arg.as_int
140 if value != null then
141 version_fields.add value
142 continue
143 end
144
145 value = arg.as_string
146 if value != null then
147 version_fields.add value
148 end
149
150 value = arg.as_id
151 if value == "git_revision" then
152 # Get Git short revision
153 var proc = new IProcess("git", "rev-parse", "--short", "HEAD")
154 proc.wait
155 assert proc.status == 0
156 var lines = proc.read_all
157 var revision = lines.split("\n").first
158
159 # Is it dirty?
160 # If not, the return of `git diff --shortstat` is an empty line
161 proc = new IProcess("git", "diff-index", "--quiet", "HEAD")
162 proc.wait
163 var dirty = proc.status != 0
164 if dirty then revision += ".d"
165
166 version_fields.add revision
167 continue
168 end
169
170 modelbuilder.error(self, format_error)
171 return ""
172 end
173 end
174
175 return version_fields.join(".")
176 end
177 end