[valadoc] valadoc: Improve plugin selection



commit a337c4b5b2cb7d947c4af5127dc13265e7cdc2b8
Author: Florian Brosch <flo brosch gmail com>
Date:   Mon Jul 25 08:57:04 2011 +0200

    valadoc: Improve plugin selection

 configure.in             |   22 ++++---
 src/valadoc/Makefile.am  |   17 +++--
 src/valadoc/valadoc.vala |  141 +++++++++++++++++++++++++++++++++++++++++-----
 src/vapi/config.vapi     |    3 +
 4 files changed, 152 insertions(+), 31 deletions(-)
---
diff --git a/configure.in b/configure.in
index 68516ca..b407c38 100755
--- a/configure.in
+++ b/configure.in
@@ -13,23 +13,27 @@ AC_PROG_CC
 AC_PROG_LIBTOOL
 
 
-AC_PATH_PROG(VALAC, valac, valac)
-AC_SUBST(VALAC)
-
-
-AC_SUBST(VALAFLAGS)
 
+##
+## Settings:
+##
 
-VALA_PACKAGE=libvala-0.14
-LIBVALA_REQUIRED=0.13.0
+VALA_REQUIRED=0.13.1.56
 LIBGEE_REQUIRED=0.5
 LIBGVC_REQUIRED=2.16
 GLIB_REQUIRED=2.12.0
-GTK_REQUIRED=2.10.0
 LIBGDKPIXBUF_REQUIRED=2.0
 
 
 
+##
+## Dependencies:
+##
+
+AC_PATH_PROG(VALAC, valac, valac)
+AC_SUBST(VALAC)
+AC_SUBST(VALAFLAGS)
+m4_ifdef([AM_PROG_VALAC], [AM_PROG_VALAC([$VALA_REQUIRED])])
 
 PKG_CHECK_MODULES(LIBGVC, libgvc >= $LIBGVC_REQUIRED)
 AC_SUBST(LIBGVC_CFLAGS)
@@ -89,7 +93,6 @@ AC_SUBST(LIBVALA_0_10_X_LIBS)
 
 
 
-
 AC_CONFIG_FILES([Makefile
                  src/libvaladoc/valadoc-1.0.pc
                  src/libvaladoc/valadoc-1.0.deps
@@ -111,4 +114,5 @@ AC_CONFIG_FILES([Makefile
                  tests/Makefile
 				 ])
 
+
 AC_OUTPUT
diff --git a/src/valadoc/Makefile.am b/src/valadoc/Makefile.am
index 9da97a2..156f883 100755
--- a/src/valadoc/Makefile.am
+++ b/src/valadoc/Makefile.am
@@ -1,14 +1,17 @@
 NULL =
 
+DEFAULT_DRIVER = "`$(VALAC) --version`"
 
-AM_CFLAGS =                             \
+
+AM_CFLAGS =                                 \
 	-DPACKAGE_DATADIR=\"$(libdir)/valadoc\" \
-	-DPACKAGE_VERSION=\"$(VERSION)\"    \
-	-I ../libvaladoc/                   \
-	$(GLIB_CFLAGS)                      \
-	$(LIBGEE_CFLAGS)                    \
-	$(GMODULE_CFLAGS)                   \
-	$(LIBGVC_CFLAGS)                    \
+	-DPACKAGE_VERSION=\"$(VERSION)\"        \
+	-DDEFAULT_DRIVER=\"$(DEFAULT_DRIVER)\"         \
+	-I ../libvaladoc/                       \
+	$(GLIB_CFLAGS)                          \
+	$(LIBGEE_CFLAGS)                        \
+	$(GMODULE_CFLAGS)                       \
+	$(LIBGVC_CFLAGS)                        \
 	$(NULL)
 
 
diff --git a/src/valadoc/valadoc.vala b/src/valadoc/valadoc.vala
index 67cffe0..4969deb 100755
--- a/src/valadoc/valadoc.vala
+++ b/src/valadoc/valadoc.vala
@@ -1,6 +1,7 @@
 /* valadoc.vala
  *
  * Copyright (C) 2008-2009 Florian Brosch
+ * Copyright (C) 2011      Florian Brosch
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -65,33 +66,39 @@ public class ValaDoc : Object {
 	private static string[] packages;
 
 	private const GLib.OptionEntry[] options = {
+		{ "directory", 'o', 0, OptionArg.FILENAME, ref directory, "Output directory", "DIRECTORY" },
+
 		{ "basedir", 'b', 0, OptionArg.FILENAME, ref basedir, "Base source directory", "DIRECTORY" },
 		{ "define", 'D', 0, OptionArg.STRING_ARRAY, ref defines, "Define SYMBOL", "SYMBOL..." },
+		{ "profile", 0, 0, OptionArg.STRING, ref profile, "Use the given profile instead of the default", "PROFILE" },
+
 		{ "enable-experimental", 0, 0, OptionArg.NONE, ref experimental, "Enable experimental features", null },
 		{ "enable-experimental-non-null", 0, 0, OptionArg.NONE, ref experimental_non_null, "Enable experimental enhancements for non-null types", null },
-		{ "vapidir", 0, 0, OptionArg.FILENAME_ARRAY, ref vapi_directories, "Look for package bindings in DIRECTORY", "DIRECTORY..." },
-		{ "importdir", 0, 0, OptionArg.FILENAME_ARRAY, ref import_directories, "Look for external documentation in DIRECTORY", "DIRECTORY..." },
-		{ "profile", 0, 0, OptionArg.STRING, ref profile, "Use the given profile instead of the default", "PROFILE" },
-		{ "driver", 0, 0, OptionArg.NONE, ref driverpath, "Display version number", null },
 
+		{ "vapidir", 0, 0, OptionArg.FILENAME_ARRAY, ref vapi_directories, "Look for package bindings in DIRECTORY", "DIRECTORY..." },
 		{ "pkg", 0, 0, OptionArg.STRING_ARRAY, ref packages, "Include binding for PACKAGE", "PACKAGE..." },
+
+		{ "driver", 0, 0, OptionArg.STRING, ref driverpath, "Name of an driver or path to a custom driver", null },
+
+		{ "importdir", 0, 0, OptionArg.FILENAME_ARRAY, ref import_directories, "Look for external documentation in DIRECTORY", "DIRECTORY..." },
 		{ "import", 0, 0, OptionArg.STRING_ARRAY, ref import_packages, "Include binding for PACKAGE", "PACKAGE..." },
-		{ "directory", 'o', 0, OptionArg.FILENAME, ref directory, "Output directory", "DIRECTORY" },
 
 		{ "wiki", 0, 0, OptionArg.FILENAME, ref wikidirectory, "Wiki directory", "DIRECTORY" },
+
 		{ "deps", 0, 0, OptionArg.NONE, ref with_deps, "Adds packages to the documentation", null },
 
-		{ "doclet-arg", 'X', 0, OptionArg.STRING_ARRAY, ref pluginargs, "Pass arguments to the doclet", "ARG" },
 		{ "doclet", 0, 0, OptionArg.STRING, ref docletpath, "Name of an included doclet or path to custom doclet", "PLUGIN"},
+		{ "doclet-arg", 'X', 0, OptionArg.STRING_ARRAY, ref pluginargs, "Pass arguments to the doclet", "ARG" },
 
 		{ "no-protected", 0, OptionFlags.REVERSE, OptionArg.NONE, ref _protected, "Removes protected elements from documentation", null },
 		{ "internal", 0, 0, OptionArg.NONE, ref _internal, "Adds internal elements to documentation", null },
 		{ "private", 0, 0, OptionArg.NONE, ref _private, "Adds private elements to documentation", null },
-//		{ "inherit", 0, 0, OptionArg.NONE, ref add_inherited, "Adds inherited elements to a class", null },
 
 		{ "package-name", 0, 0, OptionArg.STRING, ref pkg_name, "package name", "NAME" },
 		{ "package-version", 0, 0, OptionArg.STRING, ref pkg_version, "package version", "VERSION" },
 
+		{ "version", 0, 0, OptionArg.NONE, ref version, "Display version number", null },
+
 		{ "force", 0, 0, OptionArg.NONE, ref force, "force", null },
 		{ "verbose", 0, 0, OptionArg.NONE, ref verbose, "Show all warnings", null },
 		{ "", 0, 0, OptionArg.FILENAME_ARRAY, ref tsources, null, "FILE..." },
@@ -99,6 +106,30 @@ public class ValaDoc : Object {
 		{ null }
 	};
 
+	private struct LibvalaVersion {
+		public int segment_a;
+		public int segment_b;
+		public int segment_c;
+
+		public LibvalaVersion (int seg_a, int seg_b, int seg_c) {
+			segment_a = seg_a;
+			segment_b = seg_b;
+			segment_c = seg_c;
+		}
+	}
+
+	private struct DriverMetaData {
+		public LibvalaVersion min;
+		public LibvalaVersion max;
+		public string driver;
+
+		public DriverMetaData (LibvalaVersion min, LibvalaVersion max, string driver) {
+			this.driver = driver;
+			this.min = min;
+			this.max = max;
+		}
+	}
+
 	private static int quit (ErrorReporter reporter) {
 		if (reporter.errors == 0) {
 			stdout.printf ("Succeeded - %d warning(s)\n", reporter.warnings);
@@ -138,11 +169,7 @@ public class ValaDoc : Object {
 		return this.pkg_name;
 	}
 
-	private string get_plugin_dir (string? pluginpath, string subdir, string default_pkg) {
-		if (pluginpath == null) {
-			return build_filename (Config.plugin_dir, subdir, default_pkg);
-		}
-
+	private string get_plugin_path (string pluginpath, string pluginsubdir) {
 		if (is_absolute (pluginpath) == false) {
 			// Test to see if the plugin exists in the expanded path and then fallback
 			// to using the configured plugin directory
@@ -150,19 +177,99 @@ public class ValaDoc : Object {
 			if (FileUtils.test(local_path, FileTest.EXISTS)) {
 				return local_path;
 			} else {
-				return build_filename (Config.plugin_dir, subdir, pluginpath);
+				return build_filename (Config.plugin_dir, pluginsubdir, pluginpath);
 			}
 		}
 
 		return pluginpath;
 	}
 
+	private string get_doclet_path (ErrorReporter reporter) {
+		if (docletpath == null) {
+			return build_filename (Config.plugin_dir, "doclets", "html");
+		}
+
+		return get_plugin_path (docletpath, "doclet");
+	}
+
+	private bool is_driver (string path) {
+		string library_path = Path.build_filename (path, "libdriver." + Module.SUFFIX);
+		return FileUtils.test (path, FileTest.EXISTS) && FileUtils.test (library_path, FileTest.EXISTS);
+	}
+
+	private string? get_driver_path (ErrorReporter reporter) {
+		// no driver selected
+		if (driverpath == null) {
+			driverpath = Config.default_driver;
+		}
+
+
+		// selected string is a plugin directory
+		string extended_driver_path = get_plugin_path (driverpath, "drivers");
+		if (is_driver (extended_driver_path)) {
+			return extended_driver_path;
+		}
+
+
+		// selected string is a version number:
+		if (driverpath.has_prefix ("Vala ")) {
+			driverpath = driverpath.substring (5);
+		}
+
+		string[] segments = driverpath.split (".");
+		if (segments.length != 3 && segments.length != 4) {
+			reporter.simple_error ("Invalid driver version format.");
+			return null;
+		}
+
+
+		//TODO: add try_parse to int
+		int64 segment_a;
+		int64 segment_b;
+		int64 segment_c;
+		bool tmp;
+
+		tmp  = int64.try_parse (segments[0], out segment_a);
+		tmp &= int64.try_parse (segments[1], out segment_b);
+		tmp &= int64.try_parse (segments[2], out segment_c);
+
+		if (!tmp) {
+			reporter.simple_error ("Invalid driver version format.");
+			return null;
+		}
+
+		DriverMetaData[] lut = {
+				DriverMetaData (LibvalaVersion (0, 10, 0), LibvalaVersion (0, 10, -4), "0.10.x"),
+				DriverMetaData (LibvalaVersion (0, 11, 0), LibvalaVersion (0, 11,  0), "0.11.0"),
+				DriverMetaData (LibvalaVersion (0, 11, 1), LibvalaVersion (0, 11, -1), "0.11.x"),
+				DriverMetaData (LibvalaVersion (0, 12, 0), LibvalaVersion (0, 12, -1), "0.12.x"),
+				DriverMetaData (LibvalaVersion (0, 13, 0), LibvalaVersion (0, 13, -1), "0.13.x")
+			};
+
+		for (int i = 0; i < lut.length ; i++) {
+			if (lut[i].min.segment_a <= segment_a && lut[i].max.segment_a >= segment_a
+				&& lut[i].min.segment_b <= segment_b && lut[i].max.segment_b >= segment_b
+				&& lut[i].min.segment_c <= segment_c && (lut[i].max.segment_c >= segment_c || lut[i].max.segment_c < 0)) {
+				return Path.build_filename (Config.plugin_dir, "drivers", lut[i].driver);
+			}
+		}
+
+		// return invalid driver path
+		reporter.simple_error ("No suitable driver found.");
+		return null;
+	}
+
 	private ModuleLoader? create_module_loader (ErrorReporter reporter) {
 		ModuleLoader modules = new ModuleLoader ();
 		Taglets.init (modules);
 
+
 		// doclet:
-		string pluginpath = get_plugin_dir (docletpath, "doclets", "html");
+		string? pluginpath = get_doclet_path (reporter);
+		if (pluginpath == null) {
+			return null;
+		}
+
 		bool tmp = modules.load_doclet (pluginpath);
 		if (tmp == false) {
 			reporter.simple_error ("failed to load doclet");
@@ -171,7 +278,11 @@ public class ValaDoc : Object {
 
 
 		// driver:
-		pluginpath = get_plugin_dir (driverpath, "drivers", "0.13.x");
+		pluginpath = get_driver_path (reporter);
+		if (pluginpath == null) {
+			return null;
+		}
+
 		tmp = modules.load_driver (pluginpath);
 		if (tmp == false) {
 			reporter.simple_error ("failed to load driver");
diff --git a/src/vapi/config.vapi b/src/vapi/config.vapi
index dea37b2..55fed86 100755
--- a/src/vapi/config.vapi
+++ b/src/vapi/config.vapi
@@ -14,5 +14,8 @@ namespace Config {
 
 	[CCode (cname = "PACKAGE_VAPIDIR")]
 	public const string vapi_dir;
+
+	[CCode (cname = "DEFAULT_DRIVER")]
+	public const string default_driver;
 }
 



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