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