<commit_before># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Copyright 2014 <NAME> <<EMAIL>>
#
# 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 Android projects. Get the metadata
# by calling `ModelBuilder::android_project_for`.
module android_annotations

import parser_util
import modelbuilder
import modelize_property
import literal
import typing
private import annotation

# 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

	# Custom lines to add to the AndroidManifest.xml in the <manifest> node
	var manifest_lines = new Array[String]

	# Custom lines to add to the AndroidManifest.xml in the <application> node
	var manifest_application_lines = new Array[String]

	# Minimum API level required for the application to run
	var min_api: nullable Int = null

	# Build target API level
	var target_api: nullable Int = null

	# Maximum API level on which the application will be allowed to run
	var max_api: nullable Int = null

	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
	do
		var project = new AndroidProject

		var annot = priority_annotation_on_modules("app_name", mmodule)
		if annot != null then project.name = annot.arg_as_string(self)

		annot =  priority_annotation_on_modules("app_version", mmodule)
		if annot != null then project.version =  annot.as_version(self)

		annot = priority_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)
		for an in annots do project.min_api = an.arg_as_int(self)

		annots = collect_annotations_on_modules("max_api_version", mmodule)
		for an in annots do project.max_api = an.arg_as_int(self)

		annots = collect_annotations_on_modules("target_api_version", mmodule)
		for an in annots do project.target_api = an.arg_as_int(self)

		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 = 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 ""

		# 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

	# Get an annotation by name from `mmodule` and its super modules. Will recursively search
	# in imported module to find the "latest" declaration and detects priority conflicts.
	private fun priority_annotation_on_modules(name: String, mmodule: MModule): nullable AAnnotation
	do
		if mmodule2nmodule.keys.has(mmodule) then
			var amod = mmodule2nmodule[mmodule]
			var module_decl = amod.n_moduledecl
			if module_decl != null then
				var annotations = module_decl.get_annotations(name)
				if annotations.length == 1 then
					return annotations.first
				else if annotations.length > 1 then
					toolcontext.error(mmodule.location,
						"Multiple declaration of annotation {name}, it must be defined only once.")
				end
			end
		end

		var annotations = new HashSet[AAnnotation]
		for mmod in mmodule.in_importation.direct_greaters do
			var res = priority_annotation_on_modules(name, mmod)
			if res != null then annotations.add res
		end
		if annotations.length > 1 then
			var locs = new Array[Location]
			for annot in annotations do locs.add(annot.location)

			toolcontext.error(mmodule.location,
				"Priority conflict on annotation {name}, it has been defined in: {locs.join(", ")}")
			return null
		else if annotations.length == 1 then
			return annotations.first
		else return null<commit_msg>android: remove priority_annotation_on_modules and use lookup_annotation_on_modules instead<commit_after># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Copyright 2014 <NAME> <<EMAIL>>
#
# 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 Android projects. Get the metadata
# by calling `ModelBuilder::android_project_for`.
module android_annotations

import parser_util
import modelbuilder
import modelize_property
import literal
import typing
private import annotation

# 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

	# Custom lines to add to the AndroidManifest.xml in the <manifest> node
	var manifest_lines = new Array[String]

	# Custom lines to add to the AndroidManifest.xml in the <application> node
	var manifest_application_lines = new Array[String]

	# Minimum API level required for the application to run
	var min_api: nullable Int = null

	# Build target API level
	var target_api: nullable Int = null

	# Maximum API level on which the application will be allowed to run
	var max_api: nullable Int = null

	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
	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)
		for an in annots do project.min_api = an.arg_as_int(self)

		annots = collect_annotations_on_modules("max_api_version", mmodule)
		for an in annots do project.max_api = an.arg_as_int(self)

		annots = collect_annotations_on_modules("target_api_version", mmodule)
		for an in annots do project.target_api = an.arg_as_int(self)

		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 = 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 ""

		# 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
{'fraction_non_alphanumeric': 0.0720922027519699, 'fraction_numerical': 0.0028225332235681526, 'mean_word_length': 4.808743169398907, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2856230', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2432, 'n_words': 923}