[gnome-weather/wip/new-package-js: 1/4] package: add the ability to guess prefix and libdir



commit f39e256c490310ed7a0afe1b601fae3843db0025
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sat Feb 8 19:24:26 2014 +0100

    package: add the ability to guess prefix and libdir
    
    If no prefix/libdir is passed in the parameters, guess it
    by looking at where the spawned JS file lives, and walking up
    the hierarchy.
    This assumes the application is configured following the FHS,
    and is installed in $(pkglibdir), togheter with the src GResource.
    It also assumes that $(pkglibdir) is something like $(prefix)/lib{32,64,}/$(name),
    that is, exactly one level of directory to get from $(pkglibdir)
    to $(libdir), and one level of directory from there to $(prefix).
    This means that it is unsuitable for debian multi-arch, but this
    is not really a problem: this is an application, it makes no
    sense to multi-arch it.

 src/package.js |   44 +++++++++++++++++++++++++++++++-------------
 1 files changed, 31 insertions(+), 13 deletions(-)
---
diff --git a/src/package.js b/src/package.js
index e8e50b0..472c6bc 100644
--- a/src/package.js
+++ b/src/package.js
@@ -57,16 +57,23 @@ function _makeNamePath(name) {
     return '/' + name.replace('.', '/', 'g');
 }
 
+function _guessPrefixAndLibdir() {
+    let application = Gio.File.new_for_path(System.programInvocationName);
+    let pkglibdir = application.get_parent();
+    libdir = pkglibdir.get_parent().get_path();
+    prefix = pkglibdir.get_parent().get_parent().get_path();
+}
+
 /**
  * init:
  * @params: package parameters
  *
  * Initialize directories and global variables. Must be called
  * before any of other API in Package is used.
- * @params must be an object with at least the following keys:
- *  - name: the package name ($(PACKAGE_NAME) in autotools)
+ * @params is an object with the following optional keys:
  *  - version: the package version
  *  - prefix: the installation prefix
+ *  - libdir: the installation libdir
  *
  * init() will take care to check if the program is running from
  * the source directory or not, by looking for a 'src' directory.
@@ -74,8 +81,10 @@ function _makeNamePath(name) {
  * At the end, the global variable 'pkg' will contain the
  * Package module (imports.package). Additionally, the following
  * module variables will be available:
- *  - name, version: same as in @params
- *  - prefix: the installation prefix (as passed in @params)
+ *  - name: the baseneme of the application script
+ *  - version: same as in @params (or empty if not provided)
+ *  - prefix: the installation prefix (as passed in @params or
+ *            guessed from the location of the application script)
  *  - datadir, libdir: the final datadir and libdir when installed;
  *                     usually, these would be prefix + '/share' and
  *                     and prefix + '/lib' (or '/lib64')
@@ -95,21 +104,30 @@ function _makeNamePath(name) {
  *               and './po' in the source tree
  *
  * All paths are absolute and will not end with '/'.
+ * Prefix, datadir and libdir are only meaningful when the application
+ * is run installed, and should not be used when running from the
+ * source (you can check this with pkg.moduledir.startsWith('resource:///')).
  *
  * As a side effect, init() calls GLib.set_prgname().
  */
 function init(params) {
+    params = params || {};
     window.pkg = imports.package;
-    name = params.name;
-    version = params.version;
+    name = GLib.path_get_basename(System.programInvocationName);
+    version = params.version || '';
 
     // Must call it first, because it can only be called
     // once, and other library calls might have it as a
     // side effect
     GLib.set_prgname(name);
 
-    prefix = params.prefix;
-    libdir = params.libdir;
+    if ('prefix' in params) {
+        prefix = params.prefix;
+        libdir = params.libdir;
+    } else {
+        _guessPrefixAndLibdir();
+    }
+
     datadir = GLib.build_filenamev([prefix, 'share']);
     let libpath, girpath;
 
@@ -132,11 +150,11 @@ function init(params) {
         localedir = GLib.build_filenamev([datadir, 'locale']);
 
         try {
-            let resource = Gio.Resource.load(GLib.build_filenamev([pkg.pkgdatadir,
-                                                                   pkg.name + '.src.gresource']));
+            let resource = Gio.Resource.load(GLib.build_filenamev([pkglibdir,
+                                                                   name + '.src.gresource']));
             resource._register();
 
-            moduledir = 'resource://' + _makeNamePath(pkg.name) + '/js';
+            moduledir = 'resource://' + _makeNamePath(name) + '/js';
         } catch(e) {
             moduledir = pkgdatadir;
         }
@@ -147,8 +165,8 @@ function init(params) {
     GIRepository.Repository.prepend_library_path(libpath);
 
     try {
-        let resource = Gio.Resource.load(GLib.build_filenamev([pkg.pkgdatadir,
-                                                               pkg.name + '.data.gresource']));
+        let resource = Gio.Resource.load(GLib.build_filenamev([pkgdatadir,
+                                                               name + '.data.gresource']));
         resource._register();
     } catch(e) { }
 }


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