[vala/switch-to-gir: 1/49] Add --metadatadir option to specify path for GIR metadata



commit 7bd2a36b8079c77feebc7dea3461561deb5e5b77
Author: Luca Bruno <lucabru src gnome org>
Date:   Mon Nov 8 13:16:35 2010 +0100

    Add --metadatadir option to specify path for GIR metadata

 compiler/valacompiler.vala |    6 +++++-
 vala/valacodecontext.vala  |   39 ++++++++++++++++++++++++++++++++++-----
 vala/valagirparser.vala    |    6 +++---
 vapigen/valavapigen.vala   |    4 ++++
 4 files changed, 46 insertions(+), 9 deletions(-)
---
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index 95e6d19..61cb4f2 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -33,6 +33,8 @@ class Vala.Compiler {
 	static string[] vapi_directories;
 	[CCode (array_length = false, array_null_terminated = true)]
 	static string[] gir_directories;
+	[CCode (array_length = false, array_null_terminated = true)]
+	static string[] metadata_directories;
 	static string vapi_filename;
 	static string library;
 	static string gir;
@@ -85,8 +87,9 @@ class Vala.Compiler {
 	private CodeContext context;
 
 	const OptionEntry[] options = {
-		{ "girdir", 0, 0, OptionArg.FILENAME_ARRAY, ref gir_directories, "Look for .gir files in DIRECTORY", "DIRECTORY..." },
 		{ "vapidir", 0, 0, OptionArg.FILENAME_ARRAY, ref vapi_directories, "Look for package bindings in DIRECTORY", "DIRECTORY..." },
+		{ "girdir", 0, 0, OptionArg.FILENAME_ARRAY, ref gir_directories, "Look for .gir files in DIRECTORY", "DIRECTORY..." },
+		{ "metadatadir", 0, 0, OptionArg.FILENAME_ARRAY, ref metadata_directories, "Look for GIR .metadata files in DIRECTORY", "DIRECTORY..." },
 		{ "pkg", 0, 0, OptionArg.STRING_ARRAY, ref packages, "Include binding for PACKAGE", "PACKAGE..." },
 		{ "vapi", 0, 0, OptionArg.FILENAME, ref vapi_filename, "Output VAPI file name", "FILE" },
 		{ "library", 0, 0, OptionArg.STRING, ref library, "Library name", "NAME" },
@@ -199,6 +202,7 @@ class Vala.Compiler {
 		}
 		context.vapi_directories = vapi_directories;
 		context.gir_directories = gir_directories;
+		context.metadata_directories = metadata_directories;
 		context.debug = debug;
 		context.thread = thread;
 		context.mem_profiler = mem_profiler;
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index 30d05f3..3fcd6f3 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -114,6 +114,11 @@ public class Vala.CodeContext {
 	public string[] gir_directories;
 
 	/**
+	 * List of directories where to find .metadata files for .gir files.
+	 */
+	public string[] metadata_directories;
+
+	/**
 	 * Produce debug information.
 	 */
 	public bool debug { get; set; }
@@ -498,7 +503,29 @@ public class Vala.CodeContext {
 		return get_file_path (gir + ".gir", "gir-1.0", null, gir_directories);
 	}
 
-	string? get_file_path (string basename, string versioned_data_dir, string? data_dir, string[] directories) {
+	/*
+	 * Returns the .metadata file associated with the given .gir file.
+	 */
+	public string? get_metadata_path (string gir_filename) {
+		var basename = Path.get_basename (gir_filename);
+		var metadata_basename = "%s.metadata".printf (basename.substring (0, basename.length - ".gir".length));
+
+		// look into metadata directories
+		var metadata_filename = get_file_path (metadata_basename, null, null, metadata_directories);
+		if (metadata_filename != null) {
+			return metadata_filename;
+		}
+
+		// look into the same directory of .gir
+		metadata_filename = Path.build_filename (Path.get_dirname (gir_filename), metadata_basename);
+		if (FileUtils.test (metadata_filename, FileTest.EXISTS)) {
+			return metadata_filename;
+		}
+
+		return null;
+	}
+
+	string? get_file_path (string basename, string? versioned_data_dir, string? data_dir, string[] directories) {
 		string filename = null;
 
 		if (directories != null) {
@@ -510,10 +537,12 @@ public class Vala.CodeContext {
 			}
 		}
 
-		foreach (string dir in Environment.get_system_data_dirs ()) {
-			filename = Path.build_filename (dir, versioned_data_dir, basename);
-			if (FileUtils.test (filename, FileTest.EXISTS)) {
-				return filename;
+		if (versioned_data_dir != null) {
+			foreach (string dir in Environment.get_system_data_dirs ()) {
+				filename = Path.build_filename (dir, versioned_data_dir, basename);
+				if (FileUtils.test (filename, FileTest.EXISTS)) {
+					return filename;
+				}
 			}
 		}
 
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index 53fd2e4..b65170f 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -574,9 +574,9 @@ public class Vala.GirParser : CodeVisitor {
 		metadata = Metadata.empty;
 		girdata_stack = new ArrayList<HashMap<string,string>> ();
 
-		// load metadata
-		string metadata_filename = "%s.metadata".printf (source_file.filename.substring (0, source_file.filename.length - ".gir".length));
-		if (FileUtils.test (metadata_filename, FileTest.EXISTS)) {
+		// load metadata, first look into metadata directories then in the same directory of the .gir.
+		string? metadata_filename = context.get_metadata_path (source_file.filename);
+		if (metadata_filename != null && FileUtils.test (metadata_filename, FileTest.EXISTS)) {
 			var metadata_parser = new MetadataParser ();
 			var metadata_file = new SourceFile (context, source_file.file_type, metadata_filename);
 			context.add_source_file (metadata_file);
diff --git a/vapigen/valavapigen.vala b/vapigen/valavapigen.vala
index 5345b63..573a3a7 100644
--- a/vapigen/valavapigen.vala
+++ b/vapigen/valavapigen.vala
@@ -33,6 +33,8 @@ class Vala.VAPIGen : Object {
 	static string[] vapi_directories;
 	[CCode (array_length = false, array_null_terminated = true)]
 	static string[] gir_directories;
+	[CCode (array_length = false, array_null_terminated = true)]
+	static string[] metadata_directories;
 	static string library;
 	[CCode (array_length = false, array_null_terminated = true)]
 	static string[] packages;
@@ -41,6 +43,7 @@ class Vala.VAPIGen : Object {
 	const OptionEntry[] options = {
 		{ "vapidir", 0, 0, OptionArg.FILENAME_ARRAY, ref vapi_directories, "Look for package bindings in DIRECTORY", "DIRECTORY..." },
 		{ "girdir", 0, 0, OptionArg.FILENAME_ARRAY, ref gir_directories, "Look for GIR bindings in DIRECTORY", "DIRECTORY..." },
+		{ "metadatadir", 0, 0, OptionArg.FILENAME_ARRAY, ref metadata_directories, "Look for GIR .metadata files in DIRECTORY", "DIRECTORY..." },
 		{ "pkg", 0, 0, OptionArg.STRING_ARRAY, ref packages, "Include binding for PACKAGE", "PACKAGE..." },
 		{ "library", 0, 0, OptionArg.STRING, ref library, "Library name", "NAME" },
 		{ "directory", 'd', 0, OptionArg.FILENAME, ref directory, "Output directory", "DIRECTORY" },
@@ -70,6 +73,7 @@ class Vala.VAPIGen : Object {
 		context.profile = Profile.GOBJECT;
 		context.vapi_directories = vapi_directories;
 		context.gir_directories = gir_directories;
+		context.metadata_directories = metadata_directories;
 		context.report.enable_warnings = !disable_warnings;
 		context.report.set_verbose_errors (!quiet_mode);
 		CodeContext.push (context);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]