nitc: extract common app.nit annotations from Android
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 31 Mar 2015 19:23:13 +0000 (15:23 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 2 Apr 2015 15:50:27 +0000 (11:50 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/android/aware.nit
lib/app/app_base.nit
src/platform/android.nit
src/platform/android_annotations.nit
src/platform/app_annotations.nit [new file with mode: 0644]

index bb512d2..fec387d 100644 (file)
@@ -20,7 +20,6 @@
 # used to tag `ldflags` annotations.
 module aware is
        new_annotation android
-       new_annotation java_package
        new_annotation min_api_version
        new_annotation max_api_version
        new_annotation target_api_version
index a528d57..f892033 100644 (file)
@@ -16,6 +16,7 @@
 
 module app_base is
        new_annotation app_name
+       new_annotation app_namespace
        new_annotation app_version
 end
 
index 2c9e706..e87a6e4 100644 (file)
@@ -62,7 +62,7 @@ class AndroidToolchain
        redef fun write_files(compile_dir, cfiles)
        do
                var android_project_root = android_project_root.as(not null)
-               var project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
+               var project = new AndroidProject(toolcontext.modelbuilder, compiler.mainmodule)
                var short_project_name = compiler.mainmodule.name.replace("-", "_")
                var release = toolcontext.opt_release.value
 
@@ -70,7 +70,7 @@ class AndroidToolchain
                if app_name == null then app_name = compiler.mainmodule.name
                if not release then app_name += " Debug"
 
-               var app_package = project.java_package
+               var app_package = project.namespace
                if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
                if not release then app_package += "_debug"
 
index 717c6ae..347c4dc 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Annotations to gather metadata on Android projects. Get the metadata
-# by calling `ModelBuilder::android_project_for`.
+# Additionnal annotations to gather metadata on Android projects
 module android_annotations
 
-private import parser_util
-import modelize
-import literal
-import semantize
-private import annotation
+intrude import app_annotations
 
 # Metadata associated to an Android project
 class AndroidProject
-       # Name of the resulting application
-       var name: nullable String = null
-
-       # Java package used to identify the APK
-       var java_package: nullable String = null
-
-       # Version of the Android application and APK
-       var version: nullable String = null
-
-       # Numerical version code of the Android application and APK
-       var version_code: Int = 0
+       super AppProject
 
        # Custom lines to add to the AndroidManifest.xml in the <manifest> node
        var manifest_lines = new Array[String]
@@ -59,134 +44,48 @@ class AndroidProject
        # Activities to declare in the manifest
        var activities = new Array[String]
 
-       redef fun to_s do return """
-name: {{{name or else "null"}}}
-namespace: {{{java_package or else "null"}}}
-version: {{{version or else "null"}}}"""
-end
-
-redef class ModelBuilder
-       # Get the `AndroidProject` gathered from `mmodule` and its importations
-       fun android_project_for(mmodule: MModule): AndroidProject
+       init
        do
-               var project = new AndroidProject
-
-               var annot = lookup_annotation_on_modules("app_name", mmodule)
-               if annot != null then project.name = annot.arg_as_string(self)
-
-               annot =  lookup_annotation_on_modules("app_version", mmodule)
-               if annot != null then project.version =  annot.as_version(self)
-
-               annot = lookup_annotation_on_modules("java_package", mmodule)
-               if annot != null then project.java_package = annot.arg_as_string(self)
-
-               var annots = collect_annotations_on_modules("min_api_version", mmodule)
+               var annots = modelbuilder.collect_annotations_on_modules("min_api_version", mainmodule)
                if not annots.is_empty then
-                       var i = annots.pop.arg_as_int(self)
+                       var i = annots.pop.arg_as_int(modelbuilder)
                        if i == null then i = 0
-                       project.min_api = i
+                       min_api = i
                        for an in annots do
-                               i = an.arg_as_int(self)
+                               i = an.arg_as_int(modelbuilder)
                                if i == null then continue
-                               project.min_api = project.min_api.max(i)
+                               min_api = min_api.max(i)
                        end
                end
 
-               annots = collect_annotations_on_modules("max_api_version", mmodule)
+               annots = modelbuilder.collect_annotations_on_modules("max_api_version", mainmodule)
                if not annots.is_empty then
-                       var i = annots.pop.arg_as_int(self)
+                       var i = annots.pop.arg_as_int(modelbuilder)
                        if i == null then i = 0
-                       project.max_api = i
+                       max_api = i
                        for an in annots do
-                               i = an.arg_as_int(self)
+                               i = an.arg_as_int(modelbuilder)
                                if i == null then continue
-                               project.max_api = project.max_api.min(i)
+                               max_api = max_api.min(i)
                        end
                end
 
-               annot = lookup_annotation_on_modules("target_api_version", mmodule)
-               if annot != null then project.target_api = annot.arg_as_int(self) or else 0
+               var annot = modelbuilder.lookup_annotation_on_modules("target_api_version", mainmodule)
+               if annot != null then target_api = annot.arg_as_int(modelbuilder) or else 0
 
-               annots = collect_annotations_on_modules("android_manifest", mmodule)
-               for an in annots do project.manifest_lines.add an.arg_as_string(self) or else ""
+               annots = modelbuilder.collect_annotations_on_modules("android_manifest", mainmodule)
+               for an in annots do manifest_lines.add an.arg_as_string(modelbuilder) or else ""
 
-               annots = collect_annotations_on_modules("android_manifest_application", mmodule)
-               for an in annots do project.manifest_application_lines.add an.arg_as_string(self) or else ""
+               annots = modelbuilder.collect_annotations_on_modules("android_manifest_application", mainmodule)
+               for an in annots do manifest_application_lines.add an.arg_as_string(modelbuilder) or else ""
 
-               annots = collect_annotations_on_modules("android_manifest_activity", mmodule)
-               for an in annots do project.manifest_activity_attributes.add an.arg_as_string(self) or else ""
+               annots = modelbuilder.collect_annotations_on_modules("android_manifest_activity", mainmodule)
+               for an in annots do manifest_activity_attributes.add an.arg_as_string(modelbuilder) or else ""
 
-               annots = collect_annotations_on_modules("android_activity", mmodule)
+               annots = modelbuilder.collect_annotations_on_modules("android_activity", mainmodule)
                for an in annots do
-                       var activity = an.arg_as_string(self)
-                       if activity != null then project.activities.add activity
+                       var activity = an.arg_as_string(modelbuilder)
+                       if activity != null then activities.add activity
                end
-
-               # Get the date and time (down to the minute) as string
-               var local_time = new Tm.localtime
-               var local_time_s = local_time.strftime("%y%m%d%H%M")
-               project.version_code = local_time_s.to_i
-
-               toolcontext.check_errors
-
-               return project
-       end
-end
-
-redef class AAnnotation
-       # Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`.
-       #
-       # The user can enter as many fields as needed. The call to `git_revision` will be replaced by the short
-       # revision number. If the working tree is dirty, it will append another field with "d" for dirty.
-       private fun as_version(modelbuilder: ModelBuilder): String
-       do
-               var version_fields = new Array[Object]
-
-               var args = n_args
-               if args.length < 1 then
-                       modelbuilder.error(self, "Annotation error: \"{name}\" expects at least a single argument.")
-                       return ""
-               else
-                       for arg in args do
-                               var format_error = "Annotation error: \"{name}\" expects its arguments to be of type Int or a call to `git_revision`"
-
-                               var value
-                               value = arg.as_int
-                               if value != null then
-                                       version_fields.add value
-                                       continue
-                               end
-
-                               value = arg.as_string
-                               if value != null then
-                                       version_fields.add value
-                               end
-
-                               value = arg.as_id
-                               if value == "git_revision" then
-                                       # Get Git short revision
-                                       var proc = new ProcessReader("git", "rev-parse", "--short", "HEAD")
-                                       proc.wait
-                                       assert proc.status == 0
-                                       var lines = proc.read_all
-                                       var revision = lines.split("\n").first
-
-                                       # Is it dirty?
-                                       # If not, the return of `git diff --shortstat` is an empty line
-                                       proc = new ProcessReader("git", "diff-index", "--quiet", "HEAD")
-                                       proc.wait
-                                       var dirty = proc.status != 0
-                                       if dirty then revision += ".d"
-
-                                       version_fields.add revision
-                                       continue
-                               end
-
-                               modelbuilder.error(self, format_error)
-                               return ""
-                       end
-               end
-
-               return version_fields.join(".")
        end
 end
diff --git a/src/platform/app_annotations.nit b/src/platform/app_annotations.nit
new file mode 100644 (file)
index 0000000..4862c9b
--- /dev/null
@@ -0,0 +1,122 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Annotations to gather metadata on `app.nit` projects
+module app_annotations
+
+private import parser_util
+import modelize
+import literal
+import semantize
+private import annotation
+
+# Metadata associated to an Android project
+class AppProject
+       # Name of the resulting application
+       var name: nullable String = null
+
+       # Namespace/package used to identify the application
+       var namespace: nullable String = null
+
+       # Version of the application
+       var version: nullable String = null
+
+       # Numerical version code of the application
+       var version_code: Int = 0
+
+       private var modelbuilder: ModelBuilder
+       private var mainmodule: MModule
+
+       init
+       do
+               var annot = modelbuilder.lookup_annotation_on_modules("app_name", mainmodule)
+               if annot != null then name = annot.arg_as_string(modelbuilder)
+
+               annot = modelbuilder.lookup_annotation_on_modules("app_version", mainmodule)
+               if annot != null then version = annot.as_version(modelbuilder)
+
+               annot = modelbuilder.lookup_annotation_on_modules("app_namespace", mainmodule)
+               if annot != null then namespace = annot.arg_as_string(modelbuilder)
+
+               # Get the date and time (down to the minute) as string
+               var local_time = new Tm.localtime
+               var local_time_s = local_time.strftime("%y%m%d%H%M")
+               version_code = local_time_s.to_i
+
+               modelbuilder.toolcontext.check_errors
+       end
+
+       redef fun to_s do return """
+name: {{{name or else "null"}}}
+namespace: {{{namespace or else "null"}}}
+version: {{{version or else "null"}}}"""
+end
+
+redef class AAnnotation
+       # Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`.
+       #
+       # The user can enter as many fields as needed. The call to `git_revision` will be replaced by the short
+       # revision number. If the working tree is dirty, it will append another field with "d" for dirty.
+       private fun as_version(modelbuilder: ModelBuilder): String
+       do
+               var version_fields = new Array[Object]
+
+               var args = n_args
+               if args.length < 1 then
+                       modelbuilder.error(self, "Annotation error: \"{name}\" expects at least a single argument.")
+                       return ""
+               else
+                       for arg in args do
+                               var format_error = "Annotation error: \"{name}\" expects its arguments to be of type Int or a call to `git_revision`"
+
+                               var value
+                               value = arg.as_int
+                               if value != null then
+                                       version_fields.add value
+                                       continue
+                               end
+
+                               value = arg.as_string
+                               if value != null then
+                                       version_fields.add value
+                               end
+
+                               value = arg.as_id
+                               if value == "git_revision" then
+                                       # Get Git short revision
+                                       var proc = new ProcessReader("git", "rev-parse", "--short", "HEAD")
+                                       proc.wait
+                                       assert proc.status == 0
+                                       var lines = proc.read_all
+                                       var revision = lines.split("\n").first
+
+                                       # Is it dirty?
+                                       # If not, the return of `git diff --shortstat` is an empty line
+                                       proc = new ProcessReader("git", "diff-index", "--quiet", "HEAD")
+                                       proc.wait
+                                       var dirty = proc.status != 0
+                                       if dirty then revision += ".d"
+
+                                       version_fields.add revision
+                                       continue
+                               end
+
+                               modelbuilder.error(self, format_error)
+                               return ""
+                       end
+               end
+
+               return version_fields.join(".")
+       end
+end