[seed] Moving all JS files to a GResource to improve uninstalled setup.



commit 6bd605370f3f3e1f19368c191a25aea336d987bc
Author: Danilo Cesar Lemes de Paula <danilo cesar collabora co uk>
Date:   Fri Jun 17 14:11:29 2016 -0300

    Moving all JS files to a GResource to improve uninstalled setup.
    
    At the current moment, uninstalled usage is really hard with seed.
    If you try to run with: ./src/seed it runs, but it tries to read the
    javascript files and native modules from the INSTALL PATH.
    
    This commit solve that issue for the javascript files, however native
    modules still have the same issue.

 .gitignore                          |   14 ++++--
 Makefile.am                         |    6 +--
 configure.ac                        |    1 -
 jsextensions/Makefile.am            |   29 +++++++++---
 jsextensions/Seed.js.in             |    5 +-
 jsextensions/gjs/Makefile.am        |    4 --
 jsextensions/libseed.gresources.xml |   20 ++++++++
 libseed/Makefile.am                 |    5 ++-
 libseed/seed-api.c                  |    2 +-
 libseed/seed-builtins.c             |    9 ++--
 libseed/seed-engine.c               |    2 +-
 libseed/seed-importer.c             |   83 ++++++++++++++++++++++++++++++-----
 libseed/seed-importer.h             |    5 ++
 13 files changed, 143 insertions(+), 42 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 2053201..8c85890 100644
--- a/.gitignore
+++ b/.gitignore
@@ -113,8 +113,8 @@ examples/turtle/Makefile.in
 examples/turtle/seed_turtle
 examples/twitter/Makefile
 examples/twitter/Makefile.in
-extensions/Makefile
-extensions/Makefile.in
+jsextensions/Makefile
+jsextensions/Makefile.in
 gtk-doc.make
 install-sh
 intltool-extract.in
@@ -193,9 +193,9 @@ doc/reference/xml/seed-context.xml
 doc/reference/xml/seed-main.xml
 doc/reference/xml/seed-object.xml
 examples/same-seed/
-extensions/._Seed.js
-extensions/._Seed.js.in
-extensions/Seed.js
+jsextensions/._Seed.js
+jsextensions/._Seed.js.in
+jsextensions/Seed.js
 libseed/._seed-builtins.c
 libseed/._seed-engine.c
 libseed/._seed-exceptions.c
@@ -209,3 +209,7 @@ po/Makefile.in.in
 version.xml
 /compile_commands.json
 /.ycm_extra_conf.pyc
+
+jsextensions/libseed-resources.c
+libgjs-private/GjsPrivate-1.0.gir
+libgjs-private/GjsPrivate-1.0.typelib
diff --git a/Makefile.am b/Makefile.am
index 9bb37c3..af1eb02 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,13 +1,13 @@
 ## Process this file with automake to produce Makefile.in
 
 ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
-DISTCHECK_CONFIGURE_FLAGS = --enable-introspection
+DISTCHECK_CONFIGURE_FLAGS = --enable-introspection --enable-gtk-doc
 
 SUBDIRS = po \
+       jsextensions \
        libseed \
        libgjs-private \
        src \
-       jsextensions \
        modules \
        tests \
        doc
@@ -26,8 +26,6 @@ man_MANS = doc/seed.1
 
 EXTRA_DIST = $(pkgconfig_DATA)
 
-DISTCHECK_CONFIGURE_FLAGS=--enable-gtk-doc
-
 profile-reset:
        find -name '*.gcda' -delete
 
diff --git a/configure.ac b/configure.ac
index cb59bcd..b468285 100644
--- a/configure.ac
+++ b/configure.ac
@@ -512,7 +512,6 @@ modules/DynamicObject/Makefile
 
 modules/gjs/system/Makefile
 modules/gjs/_gi/Makefile
-jsextensions/gjs/Makefile
 modules/gjs/Makefile
 
 libseed/seed-path.h
diff --git a/jsextensions/Makefile.am b/jsextensions/Makefile.am
index dbb9b63..64099e8 100644
--- a/jsextensions/Makefile.am
+++ b/jsextensions/Makefile.am
@@ -1,12 +1,25 @@
-SUBDIRS = gjs
+GLIB_COMPILE_RESOURCES=glib-compile-resources
+resources=Clutter.js \
+          gjs/Gtk.js \
+          gjs/GObject.js \
+          gjs/Gio.js \
+          gjs/GLib.js \
+          GObject.js \
+          Gio.js \
+          Seed.js \
+          Gst.js \
+          GLib.js \
+          Gtk.js \
+          repl.js
 
-EXTRA_DIST= GLib.js Gio.js Seed.js.in Gtk.js GObject.js Clutter.js Gst.js repl.js
+Seed.js: Seed.js.in
+       $(AM_V_GEN) $(SED) -e "s|%pkglibdir%|$(pkglibdir)@SEED_GTK_VERSION@|" -e 
"s|%pkgdatadir%|$(pkgdatadir)@SEED_GTK_VERSION@|" -e "s|%gnomejsdir%|$(GNOME_JS_DIR)|" $< > $@
 
-extensiondir=$(datadir)/seed SEED_GTK_VERSION@/extensions
-extension_DATA = GLib.js Gio.js Seed.js Gtk.js GObject.js Clutter.js Gst.js
+libseed-resources.c: libseed.gresources.xml Seed.js.in $(resources)
+       $(GLIB_COMPILE_RESOURCES) libseed.gresources.xml --target=$@ --sourcedir=. --generate-source
 
-repldir=$(datadir)/seed SEED_GTK_VERSION@
-repl_DATA = repl.js
+all: libseed-resources.c
+
+clean:
+       rm libseed-resources.c
 
-Seed.js: Seed.js.in
-       $(AM_V_GEN) $(SED) -e "s|%pkglibdir%|$(pkglibdir)@SEED_GTK_VERSION@|" -e 
"s|%pkgdatadir%|$(pkgdatadir)@SEED_GTK_VERSION@|" -e "s|%gnomejsdir%|$(GNOME_JS_DIR)|" $< > $@
diff --git a/jsextensions/Seed.js.in b/jsextensions/Seed.js.in
index 3d576d0..0fe9deb 100644
--- a/jsextensions/Seed.js.in
+++ b/jsextensions/Seed.js.in
@@ -1,6 +1,7 @@
 if(!imports.searchPath || (imports.searchPath.length == 0))
 {
-       imports.searchPath = [ "%gnomejsdir%",
+       imports.searchPath = [ "/org/seed",
+                              "%gnomejsdir%",
                               "%pkglibdir%",
                               "%pkgdatadir%",
                               "/usr/local/lib/seed",
@@ -8,7 +9,7 @@ if(!imports.searchPath || (imports.searchPath.length == 0))
                               "/usr/lib/seed",
                               "/usr/share/seed" ];
 
-        const GLib = imports.gi.GLib;
+       const GLib = imports.gi.GLib;
        if(Seed.argv.length > 1)
        {
                __script_path__ = GLib.path_get_dirname(Seed.argv[1]);
diff --git a/jsextensions/libseed.gresources.xml b/jsextensions/libseed.gresources.xml
new file mode 100644
index 0000000..d6c2d64
--- /dev/null
+++ b/jsextensions/libseed.gresources.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/seed/extensions">
+       <file>Gio.js</file>
+       <file>GLib.js</file>
+       <file>Gtk.js</file>
+       <file>GObject.js</file>
+       <file>Gst.js</file>
+       <file>gjs/Gio.js</file>
+       <file>gjs/GLib.js</file>
+       <file>gjs/Gtk.js</file>
+       <file>gjs/GObject.js</file>
+       <file>Seed.js</file>
+       <file>Clutter.js</file>
+  </gresource>
+
+  <gresource prefix="/org/seed">
+       <file>repl.js</file>
+  </gresource>
+</gresources>
diff --git a/libseed/Makefile.am b/libseed/Makefile.am
index 4afeffe..a9c9ed2 100644
--- a/libseed/Makefile.am
+++ b/libseed/Makefile.am
@@ -23,7 +23,10 @@ libseed SEED_GTK_VERSION@_la_SOURCES = \
        seed-types.h \
        seed-importer.c \
        seed-importer.h \
-       seed-path.h
+       seed-path.h \
+       ../jsextensions/libseed-resources.c
+
+
 
 libseed SEED_GTK_VERSION@_la_CFLAGS = \
        -Wall \
diff --git a/libseed/seed-api.c b/libseed/seed-api.c
index ed0aaa2..c35c050 100644
--- a/libseed/seed-api.c
+++ b/libseed/seed-api.c
@@ -252,7 +252,7 @@ seed_script_new_from_file(JSContextRef ctx, gchar* file)
     GError* e = NULL;
     gchar* contents = NULL;
 
-    g_file_get_contents(file, &contents, NULL, &e);
+    seed_importer_get_file_contents(file, &contents, NULL, &e);
     script = seed_make_script(ctx, contents, file, 0);
     if (e) {
         seed_make_exception_from_gerror(ctx, &script->exception, e);
diff --git a/libseed/seed-builtins.c b/libseed/seed-builtins.c
index 08caf08..709d070 100644
--- a/libseed/seed-builtins.c
+++ b/libseed/seed-builtins.c
@@ -22,6 +22,7 @@
 #include <sys/mman.h>
 #include <stdio.h>
 #include <signal.h>
+#include <gio/gio.h>
 
 JSValueRef seed_print_ref;
 JSValueRef seed_printerr_ref;
@@ -54,7 +55,7 @@ seed_include(JSContextRef ctx,
 
     /* just try current dir if no path set, or use the absolute path */
     if (!eng->search_path || g_path_is_absolute(import_file))
-        g_file_get_contents(import_file, &buffer, 0, NULL);
+        seed_importer_get_file_contents(import_file, &buffer, 0, NULL);
     else /* A search path is set and path given is not absolute.  */
     {
         len = g_strv_length(eng->search_path);
@@ -66,7 +67,7 @@ seed_include(JSContextRef ctx,
 
             abs_path = g_build_filename(eng->search_path[i], import_file, NULL);
 
-            if (g_file_get_contents(abs_path, &buffer, 0, NULL)) {
+            if (seed_importer_get_file_contents(abs_path, &buffer, 0, NULL)) {
                 g_free(abs_path);
                 g_dir_close(dir);
                 break;
@@ -138,7 +139,7 @@ seed_scoped_include(JSContextRef ctx,
 
     /* just try current dir if no path set, or use the absolute path */
     if (!eng->search_path || g_path_is_absolute(import_file))
-        g_file_get_contents(import_file, &buffer, 0, NULL);
+        seed_importer_get_file_contents(import_file, &buffer, 0, NULL);
     else /* A search path is set and path given is not absolute.  */
     {
         for (i = 0; i < g_strv_length(eng->search_path); ++i) {
@@ -149,7 +150,7 @@ seed_scoped_include(JSContextRef ctx,
 
             abs_path = g_build_filename(eng->search_path[i], import_file, NULL);
 
-            if (g_file_get_contents(abs_path, &buffer, 0, NULL)) {
+            if (seed_importer_get_file_contents(abs_path, &buffer, 0, NULL)) {
                 g_free(abs_path);
                 break;
             }
diff --git a/libseed/seed-engine.c b/libseed/seed-engine.c
index 9ccc1a9..28d5807 100644
--- a/libseed/seed-engine.c
+++ b/libseed/seed-engine.c
@@ -1897,7 +1897,7 @@ seed_init_with_context_and_group(gint* argc,
     seed_gtype_init(eng);
 
     defaults_script = JSStringCreateWithUTF8CString(
-      "Seed.include(\"" SEED_PREFIX_PATH "extensions/Seed.js\");");
+      "Seed.include(\"/org/seed/extensions/Seed.js\");");
 
     JSEvaluateScript(eng->context, defaults_script, NULL, NULL, 0, NULL);
 
diff --git a/libseed/seed-importer.c b/libseed/seed-importer.c
index e2bd6cb..b40d614 100644
--- a/libseed/seed-importer.c
+++ b/libseed/seed-importer.c
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <gio/gio.h>
 
 #include "seed-private.h"
 
@@ -72,9 +73,47 @@ GHashTable* file_imports;
  *        for that context.
  */
 
+gboolean
+seed_file_test(const gchar* path, GFileTest test)
+{
+    // XXX: This is a nasty hack. what we should do is probably
+    // to check for GResource children in a given path and
+    // then return TRUE if any children exists for that path
+    gboolean exists = FALSE;
+    gboolean isdir = FALSE;
+
+    if (g_str_has_prefix(path, "/org/seed")) {
+        if (g_strcmp0(path, "/org/seed") == 0
+            || g_strcmp0(path, "/org/seed/extensions") == 0
+            || g_strcmp0(path, "/org/seed/extensions/gjs") == 0) {
+            isdir = TRUE;
+            exists = TRUE;
+        } else if (g_resources_get_info(path, G_RESOURCE_LOOKUP_FLAGS_NONE,
+                                        NULL, NULL, NULL)) {
+            isdir = FALSE;
+            exists = TRUE;
+        }
+
+        // Return only if file is in GResource.
+        // Otherwise fallback to g_file_test
+        if (exists) {
+            if (test & G_FILE_TEST_EXISTS)
+                return TRUE;
+            if (test & G_FILE_TEST_IS_REGULAR)
+                if (!isdir)
+                    return !isdir;
+            if (test & G_FILE_TEST_IS_DIR)
+                if (isdir)
+                    return isdir;
+        }
+    }
+
+    return g_file_test(path, test);
+}
+
 /*
 * Handle definition of toplevel functions in a namespace.
-* i.e. Gtk.main
+* i.e. Gtk.main.
 */
 
 static gboolean
@@ -555,6 +594,23 @@ seed_gi_importer_get_property(JSContextRef ctx,
     return ret;
 }
 
+gboolean
+seed_importer_get_file_contents(const gchar* filename,
+                                gchar** contents,
+                                gsize* length,
+                                GError** error)
+{
+    GBytes* bytes
+      = g_resources_lookup_data(filename, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
+    if (bytes) {
+        *contents = g_strdup(g_bytes_get_data(bytes, length));
+        g_bytes_unref(bytes);
+        return true;
+    }
+
+    return g_file_get_contents(filename, contents, length, error);
+}
+
 static JSObjectRef
 seed_make_importer_dir(JSContextRef ctx, gchar* path)
 {
@@ -564,7 +620,7 @@ seed_make_importer_dir(JSContextRef ctx, gchar* path)
     dir = JSObjectMake(ctx, importer_dir_class, path);
 
     init = g_build_filename(path, "__init__.js", NULL);
-    if (g_file_test(init, G_FILE_TEST_IS_REGULAR)) {
+    if (seed_file_test(init, G_FILE_TEST_IS_REGULAR)) {
         SeedScript* s;
         SEED_NOTE(IMPORTER, "Found __init__.js (%s)", path);
 
@@ -617,10 +673,11 @@ seed_importer_get_search_path(JSContextRef ctx, JSValueRef* exception)
                                        exception);
         entry = seed_value_to_string(ctx, entry_ref, exception);
 
-        if (g_file_test(entry, G_FILE_TEST_EXISTS) == TRUE)
+        if (seed_file_test(entry, G_FILE_TEST_EXISTS) == TRUE)
             path = g_slist_append(path, entry);
-        else
+        else {
             g_free(entry);
+        }
     }
 
     return path;
@@ -719,15 +776,15 @@ seed_importer_handle_file(JSContextRef ctx,
         return global;
     }
 
-    if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
-        if (g_file_test(file_path, G_FILE_TEST_IS_DIR)) {
+    if (!seed_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
+        if (seed_file_test(file_path, G_FILE_TEST_IS_DIR)) {
             SEED_NOTE(IMPORTER, "File is directory");
             return seed_make_importer_dir(ctx, file_path);
         }
         return NULL;
     }
 
-    g_file_get_contents(file_path, &contents, 0, NULL);
+    seed_importer_get_file_contents(file_path, &contents, 0, NULL);
     walk = contents;
     if (*walk == '#') {
         while (*walk != '\n')
@@ -809,7 +866,8 @@ seed_importer_try_load(JSContextRef ctx,
 
     // check if prop is a file or dir (imports['foo.js'] or imports.mydir)
     file_path = g_build_filename(test_path, prop, NULL);
-    if (g_file_test(file_path, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_DIR)) {
+    if (seed_file_test(file_path,
+                       G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_DIR)) {
         ret = seed_importer_handle_file(ctx, test_path, prop, prop, exception);
         g_free(file_path);
         return ret;
@@ -818,7 +876,7 @@ seed_importer_try_load(JSContextRef ctx,
 
     // check if prop is file ending with '.js'
     file_path = g_build_filename(test_path, prop_as_js, NULL);
-    if (g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
+    if (seed_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
         ret = seed_importer_handle_file(ctx, test_path, prop_as_js, prop,
                                         exception);
         g_free(file_path);
@@ -828,7 +886,7 @@ seed_importer_try_load(JSContextRef ctx,
 
     // check if file is native module
     file_path = g_build_filename(test_path, prop_as_lib, NULL);
-    if (g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
+    if (seed_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
         ret
           = seed_importer_handle_native_module(ctx, test_path, prop, exception);
         g_free(file_path);
@@ -898,6 +956,7 @@ seed_importer_search_dirs(JSContextRef ctx,
 static JSObjectRef
 seed_importer_search(JSContextRef ctx, gchar* prop, JSValueRef* exception)
 {
+
     JSObjectRef ret = NULL;
     GSList* path = seed_importer_get_search_path(ctx, exception);
     ret = seed_importer_search_dirs(ctx, path, prop, exception);
@@ -1021,7 +1080,7 @@ seed_importer_construct_dir(JSContextRef ctx,
     }
     path = seed_value_to_string(ctx, arguments[0], exception);
 
-    if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
+    if (!seed_file_test(path, G_FILE_TEST_IS_DIR)) {
         seed_make_exception(ctx, exception, "ArgumentError",
                             "Path (%s) is not a directory", path);
         g_free(path);
@@ -1134,6 +1193,8 @@ JSClassDefinition importer_dir_class_def = {
     NULL                                    /* Convert To Type */
 };
 
+extern GResource* libseed_get_resource(void);
+
 void
 seed_initialize_importer(JSContextRef ctx, JSObjectRef global)
 {
diff --git a/libseed/seed-importer.h b/libseed/seed-importer.h
index 11c59b5..9aa2309 100644
--- a/libseed/seed-importer.h
+++ b/libseed/seed-importer.h
@@ -29,4 +29,9 @@ JSObjectRef seed_gi_importer_do_namespace(JSContextRef ctx,
                                           gchar* namespace,
                                           JSValueRef* exception);
 
+gboolean seed_importer_get_file_contents(const gchar* filename,
+                                         gchar** contents,
+                                         gsize* length,
+                                         GError** error);
+
 #endif


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