Restructure json a bit
authorCameron Ball <cameron@getapproved.com.au>
Fri, 12 Jun 2015 09:00:22 +0000 (17:00 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 12 Jun 2015 09:00:22 +0000 (17:00 +0800)
lel [new file with mode: 0644]
menu.json
menu.sh
short_to_full.sh [new file with mode: 0755]

diff --git a/lel b/lel
new file mode 100644 (file)
index 0000000..d2d6131
--- /dev/null
+++ b/lel
@@ -0,0 +1 @@
+asdf arge
\ No newline at end of file
index a75776f..b9e4138 100644 (file)
--- a/menu.json
+++ b/menu.json
@@ -2,64 +2,66 @@
   "MainMenu": {
     "type": "menu",
     "description": "PreCom Options",
-    "items": {
-      "Services": {
+    "items": [
+    {
+        "name": "Services",
         "type": "menu",
         "description": "Start/Stop services",
-        "items": {
-          "ITG": {
+        "items": [
+        {
+            "name": "ITG",
             "type": "service",
             "description": "Start/Stop the ITG service",
             "command": "/home/cameron/Games/OpenITG/openitg-beta-2",
             "user": "itg"
-          },
-          "gEdit": {
+        },
+        {
+          "name":"gEdit",
             "type": "service",
             "description": "Start/Stop the gEdit service",
             "command": "gedit",
             "user": "itg"
-          }
-        }
-      },
-      "Tasks": {
+        }]
+    },
+    { 
+        "name": "Tasks",
         "type": "menu",
         "description": "Run tasks",
-        "items": {
-          "UpdateSongs": {
+        "items": [
+        {
+            "name": "Update songs",
             "type": "task",
             "description": "Update the ITG song list",
             "commands": [
-               {
-                       "command": "./rsync-adapter.sh -ia --delete --exclude 'A is for Cool Shit' /home/cameron/PreComTestRepo/Songs /home/cameron/Games/OpenITG",
-                       "title": "Syncing songs..."
-               },
-               {
-                       "command": "./cache-update-adapter.sh -d /itg/Cache/Songs/",
-                       "title": "Updating cache..."
-               },
-               {
-                       "command": "./cache-update-confirmation-adapter.sh",
-                       "title": "Songs updated"
-               }
-           ],
+            {
+                "command": "./rsync-adapter.sh -ia --delete --exclude 'A is for Cool Shit' /home/cameron/PreComTestRepo/Songs /home/cameron/Games/OpenITG",
+                "title": "Syncing songs..."
+            },
+            {
+                "command": "./cache-update-adapter.sh -d /itg/Cache/Songs/",
+                "title": "Updating cache..."
+            },
+            {
+                "command": "./cache-update-confirmation-adapter.sh",
+                "title": "Songs updated"
+            }],
             "user": "itg"
-          }
-        }
-      },
-      "Presets": {
-        "type": "menu",
-        "description": "Run presets",
-        "items": {
-            "SyncAndLaunch": {
-               "type": "preset",
-               "description": "Sync songs and launch ITG",
-               "itemsToRun": [
-                       "MainMenu.Tasks.UpdateSongs",
-                       "MainMenu.Services.ITG"
-               ]
-            }
-         }
-      }
-    }
+        }]
+    },
+    {
+        "name": "Presets",
+        "type": "menu",
+        "description": "Run presets",
+        "items": [
+        {
+            "name": "Sync and Launch",
+            "type": "preset",
+            "description": "Sync songs and launch ITG",
+            "itemsToRun": [
+              "MainMenu.Tasks.UpdateSongs",
+              "MainMenu.Services.ITG"
+            ]
+        }]
+    }]
   }
 }
diff --git a/menu.sh b/menu.sh
index b945d3b..711cd8f 100755 (executable)
--- a/menu.sh
+++ b/menu.sh
@@ -3,7 +3,7 @@
 INPUT=/tmp/menu.sh.$$
 OUTPUT=/tmp/output.sh.$$
 
-trap "rm $OUTPUT' rm $INPUT; exit" SIGHUP SIGINT SIGTERM
+trap "rm $OUTPUT; rm $INPUT; exit" SIGHUP SIGINT SIGTERM
 
 menu_json="$(./JSON.sh -l < menu.json)"
 current_item="MainMenu"
@@ -36,14 +36,56 @@ function get_value_from_key()
        done <<< "$menu_json"
 }
 
-#simply puts item between each element
-#in the path. So Menu.Services.ITG
-#become Menu.items.Services.items.ITG
+#Helper for short_path_to_full_path
+#Given a menu item name, and a path to
+#its parent (a real JSON path like MainMenu.items.0)
+#this will return the path to the item from the given path
+#For example if it is passed MainMenu.items.0 and "ITG" it might
+#return: items.0
+#name, path
+function get_relative_child_path()
+{
+       if [[ -z "$2" ]]; then
+               echo "$1"
+       else
+               re="${2//./\\.}\.items\.([0-9]+?)\.name"
+               debug "$re"
+               while read -r line; do
+                       if [[ "$line" =~ $re ]]; then
+                               sub_path=${BASH_REMATCH[1]}
+                               key=$(get_key_from_line "$line")
+                               value=$(get_value_from_key "$key")
+                               [[ "$value" == "$1" ]] && echo "items.$sub_path"
+                       fi
+               done <<< "$menu_json"
+       fi
+}
+
+#When talking about menus it's useful to
+#be able to say something like:
+#MainMenu.Service.ITG instead of
+#MainMenu.items.0.items.0
+#This function converts the nice path
+#to the real JSON path.
 function short_path_to_full_path()
 {
-       echo "${1//./.items.}"
+       path_so_far=""
+       while IFS='.' read -ra parts; do
+               for i in "${parts[@]}"; do
+                       relative_path=$(get_relative_child_path "$i" "$path_so_far")
+
+                       if [[ -z "$path_so_far" ]]; then
+                               path_so_far="${relative_path}"
+                       else
+                               path_so_far="${path_so_far}.${relative_path}"
+                       fi
+               done
+       done <<< "$1"
+
+       echo "$path_so_far"
 }
 
+
 #Functions below here are for convenience.
 #They allow extraction of information about
 #menus without having to constantly specifiy
@@ -125,9 +167,15 @@ function render_menu()
                #Yes I do, it's because $line has a tab followed by text in it.
                #Without the quotes it thinks what follows the tab is the second arg
                if is_item_of_menu "$line" "$1"; then
-                       name_re="items.([a-zA-Z]+).description"
-                       desc_re=".description[[:space:]]\"(.+)\""
-                       [[ $line =~ $name_re ]] && name=${BASH_REMATCH[1]} && [[ $line =~ $desc_re ]] && desc=${BASH_REMATCH[1]} && options+=("$name" "$desc")
+                       name_re=".name[[:space:]].+?\"(.+)\""
+                       desc_re=".description[[:space:]].+?\"(.+)\""
+                       
+                       #todo: the issue here is that the name and description are on different line things now (before it was the same)
+                       #probably have to scrap this loop and extract the name and desc using one of my helper function things
+                       #see run_task where I use magic to go back a level (so like from thing.thing2.thing3 -> thing.thing2 and tack on
+                       #the extra bit I wanna extract
+                       [[ $line =~ $name_re ]] && debug "hello" && name=${BASH_REMATCH[1]} && [[ $line =~ $desc_re ]] && desc=${BASH_REMATCH[1]} && debug "${name} ${desc}" && options+=("$name" "$desc")
+sleep 1
                fi
        done <<< "$menu_json"
 
@@ -239,5 +287,5 @@ done
 ###########
 clear
 
-[ -f $OUTPUT ] && rm $OUTPUT
-[ -f $INPUT ] && rm $INPUT
+[[ -f $OUTPUT ]] && rm $OUTPUT
+[[ -f $INPUT ]] && rm $INPUT
diff --git a/short_to_full.sh b/short_to_full.sh
new file mode 100755 (executable)
index 0000000..1fc6e2f
--- /dev/null
@@ -0,0 +1,79 @@
+#!/bin/bash
+
+menu_json="$(./JSON.sh -l < menu.json)"
+
+function debug()
+{
+       >&2 echo "$1"
+}
+
+function get_key_from_line()
+{
+       key_re="(.+?)[[:space:]]+\""
+       while read -r line; do
+               [[ "$1" =~ $key_re ]] && echo ${BASH_REMATCH[1]} && break
+       done <<< "$menu_json"
+}
+
+#Pass something like MainMenu.type and it'll
+#give the value
+#nice bonus: If passed a full line from menu_json
+#this will return the value for it
+function get_value_from_key()
+{
+       value_re="[[:space:]]+\"(.+)\"$"
+       while read -r line; do
+               [[ $line == ${1}* ]] && [[ $line =~ $value_re ]] &&  echo ${BASH_REMATCH[1]} && break
+       done <<< "$menu_json"
+}
+
+#Helper for short_path_to_full_path
+#Given a menu item name, and a path to
+#its parent (a real JSON path like MainMenu.items.0)
+#this will return the path to the item from the given path
+#For example if it is passed MainMenu.items.0 and "ITG" it might
+#return: items.0
+#name, path
+function get_relative_child_path()
+{
+       if [[ -z "$2" ]]; then
+               echo "$1"
+       else
+               re="${2//./\\.}\.items\.([0-9]+?)\.name"
+               debug "$re"
+               while read -r line; do
+                       if [[ "$line" =~ $re ]]; then
+                               sub_path=${BASH_REMATCH[1]}
+                               key=$(get_key_from_line "$line")
+                               value=$(get_value_from_key "$key")
+                               [[ "$value" == "$1" ]] && echo "items.$sub_path"
+                       fi
+               done <<< "$menu_json"
+       fi
+}
+
+#When talking about menus it's useful to
+#be able to say something like:
+#MainMenu.Service.ITG instead of
+#MainMenu.items.0.items.0
+#This function converts the nice path
+#to the real JSON path.
+function short_path_to_full_path()
+{
+       path_so_far=""
+       while IFS='.' read -ra parts; do
+               for i in "${parts[@]}"; do
+                       relative_path=$(get_relative_child_path "$i" "$path_so_far")
+
+                       if [[ -z "$path_so_far" ]]; then
+                               path_so_far="${relative_path}"
+                       else
+                               path_so_far="${path_so_far}.${relative_path}"
+                       fi
+               done
+       done <<< "$1"
+
+       echo "$path_so_far"
+}
+
+short_path_to_full_path "MainMenu.Services.gEdit"