nitc: move up the default values of a project from the android platform
[nit.git] / src / platform / app_annotations.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Annotations to gather metadata on `app.nit` projects
16 module app_annotations
17
18 private import parser_util
19 import modelize
20 import literal
21 import semantize
22 private import annotation
23
24 # Metadata associated to an `app.nit` project
25 class AppProject
26 # Pretty name of the resulting application
27 var name: String = mainmodule.first_real_mmodule.name is lazy
28
29 # Short project name used in `namespace` and configuration files
30 var short_name: String = mainmodule.name.replace("-", "_") is lazy
31
32 # Namespace/package used to identify the application
33 var namespace = "org.nitlanguage.{short_name}" is lazy
34
35 # Version of the application
36 var version = "0.1"
37
38 # Numerical version code of the application
39 var version_code: Int is lazy do
40
41 # Get the date and time (down to the minute) as string
42 var local_time = new Tm.localtime
43 var local_time_s = local_time.strftime("%y%m%d%H%M")
44 return local_time_s.to_i
45 end
46
47 private var modelbuilder: ModelBuilder
48 private var mainmodule: MModule
49
50 init
51 do
52 var annot = modelbuilder.lookup_annotation_on_modules("app_name", mainmodule)
53 if annot != null then
54 var val = annot.arg_as_string(modelbuilder)
55 if val != null then name = val
56 end
57
58 annot = modelbuilder.lookup_annotation_on_modules("app_version", mainmodule)
59 if annot != null then version = annot.as_version(modelbuilder)
60
61 annot = modelbuilder.lookup_annotation_on_modules("app_namespace", mainmodule)
62 if annot != null then
63 var val = annot.arg_as_string(modelbuilder)
64 if val != null then namespace = val
65 end
66
67 modelbuilder.toolcontext.check_errors
68 end
69
70 redef fun to_s do return """
71 name: {{{name or else "null"}}}
72 namespace: {{{namespace or else "null"}}}
73 version: {{{version or else "null"}}}"""
74 end
75
76 redef class AAnnotation
77 # Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`.
78 #
79 # The user can enter as many fields as needed. The call to `git_revision` will be replaced by the short
80 # revision number. If the working tree is dirty, it will append another field with "d" for dirty.
81 private fun as_version(modelbuilder: ModelBuilder): String
82 do
83 var version_fields = new Array[Object]
84
85 var args = n_args
86 if args.length < 1 then
87 modelbuilder.error(self, "Annotation error: \"{name}\" expects at least a single argument.")
88 return ""
89 else
90 for arg in args do
91 var format_error = "Annotation error: \"{name}\" expects its arguments to be of type Int or a call to `git_revision`"
92
93 var value
94 value = arg.as_int
95 if value != null then
96 version_fields.add value
97 continue
98 end
99
100 value = arg.as_string
101 if value != null then
102 version_fields.add value
103 end
104
105 value = arg.as_id
106 if value == "git_revision" then
107 # Get Git short revision
108 var proc = new ProcessReader("git", "rev-parse", "--short", "HEAD")
109 proc.wait
110 assert proc.status == 0
111 var lines = proc.read_all
112 var revision = lines.split("\n").first
113
114 # Is it dirty?
115 # If not, the return of `git diff --shortstat` is an empty line
116 proc = new ProcessReader("git", "diff-index", "--quiet", "HEAD")
117 proc.wait
118 var dirty = proc.status != 0
119 if dirty then revision += ".d"
120
121 version_fields.add revision
122 continue
123 end
124
125 modelbuilder.error(self, format_error)
126 return ""
127 end
128 end
129
130 return version_fields.join(".")
131 end
132 end