Skip to content

Commit e5d9d31

Browse files
Merge pull request #1 from pglet/v1
V1
2 parents 32ef5eb + 1e0549c commit e5d9d31

File tree

7 files changed

+360
-11
lines changed

7 files changed

+360
-11
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Feodor Fitsner
3+
Copyright (c) 2020 Appveyor Systems Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

pglet.sh

Lines changed: 264 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,284 @@
1-
echo "Pipe name: $1"
2-
page_pipe=$1
1+
# Constants
2+
PGLET_VER="0.1.12" # Pglet version required by this script
3+
4+
# Default session variables:
5+
PGLET_EXE="" # full path to Pglet executable
6+
PGLET_CONNECTION_ID="" # the last page connection ID.
7+
PGLET_PAGE_URL="" # the last page URL.
8+
PGLET_EVENT_TARGET="" # the last received event target (control ID).
9+
PGLET_EVENT_NAME="" # the last received event name.
10+
PGLET_EVENT_DATA="" # the last received event data.
11+
12+
# Parameters:
13+
# $1 - page name
14+
# Variables:
15+
# PGLET_WEB - makes the page available as public at pglet.io service or a self-hosted Pglet server
16+
# PGLET_PRIVATE - makes the page available as private at pglet.io service or a self-hosted Pglet server
17+
# PGLET_SERVER - connects to the page on a self-hosted Pglet server
18+
# PGLET_TOKEN - authentication token for pglet.io service or a self-hosted Pglet server
19+
20+
function pglet_page() {
21+
local pargs=(page)
22+
23+
if [[ "$1" != "" ]]; then
24+
pargs+=($1)
25+
fi
26+
27+
if [[ "$PGLET_WEB" == "true" ]]; then
28+
pargs+=(--web)
29+
fi
30+
31+
if [[ "$PGLET_SERVER" != "" ]]; then
32+
pargs+=(--server $PGLET_SERVER)
33+
fi
34+
35+
if [[ "$PGLET_TOKEN" != "" ]]; then
36+
pargs+=(--token $PGLET_TOKEN)
37+
fi
38+
39+
if [[ "$PGLET_NO_WINDOW" != "" ]]; then
40+
pargs+=(--no-window)
41+
fi
42+
43+
# execute pglet and get page connection ID
44+
local page_results=`$PGLET_EXE "${pargs[@]}"`
45+
IFS=' ' read -r PGLET_CONNECTION_ID PGLET_PAGE_URL <<< "$page_results"
46+
47+
echo "Page URL: $PGLET_PAGE_URL"
48+
}
49+
50+
function __pglet_start_session() {
51+
echo "Started session: $1"
52+
PGLET_CONNECTION_ID=$1
53+
local fn=$2
54+
55+
eval "$fn"
56+
}
57+
58+
function pglet_app() {
59+
local pargs=(app)
60+
61+
if [[ $# -eq 1 ]]; then
62+
# only hander function specified
63+
local fn=$1
64+
elif [[ $# -eq 2 ]]; then
65+
# page name and hander function specified
66+
pargs+=($1)
67+
local fn=$2
68+
else
69+
echo "Error: wrong number of arguments"
70+
exit 1
71+
fi
72+
73+
if [[ "$PGLET_WEB" == "true" ]]; then
74+
pargs+=(--web)
75+
fi
76+
77+
if [[ "$PGLET_SERVER" != "" ]]; then
78+
pargs+=(--server $PGLET_SERVER)
79+
fi
80+
81+
if [[ "$PGLET_TOKEN" != "" ]]; then
82+
pargs+=(--token $PGLET_TOKEN)
83+
fi
84+
85+
if [[ "$PGLET_NO_WINDOW" != "" ]]; then
86+
pargs+=(--no-window)
87+
fi
88+
89+
# reset vars
90+
PGLET_PAGE_URL=""
91+
92+
# execute pglet
93+
$PGLET_EXE "${pargs[@]}" |
94+
{
95+
while read -r session_id
96+
do
97+
if [[ "$PGLET_PAGE_URL" == "" ]]; then
98+
PGLET_PAGE_URL="$session_id"
99+
echo "Page URL: $PGLET_PAGE_URL"
100+
else
101+
__pglet_start_session $session_id $fn &
102+
fi
103+
done
104+
}
105+
}
106+
107+
function pglet_send() {
108+
if [[ $# -eq 1 ]]; then
109+
local conn_id=$PGLET_CONNECTION_ID
110+
local cmd=$1
111+
elif [[ $# -eq 2 ]]; then
112+
local conn_id=$1
113+
local cmd=$2
114+
else
115+
echo "Error: wrong number of arguments"
116+
exit 1
117+
fi
3118

4-
function pglet() {
5119
# send command
6-
echo "$1" > "$page_pipe"
120+
echo "$cmd" > "$conn_id"
121+
122+
# take result if command doesn't end with "f" (fire-and-forget)
123+
if [[ "$cmd" =~ ^[[:space:]]*[A-Za-z]+f ]]; then
124+
return
125+
fi
7126

8127
# read result
9-
IFS=' ' read result_status result_value < "$page_pipe"
10-
echo $result_value
128+
local firstLine="true"
129+
local result_value=""
130+
IFS=''
131+
while read -r line; do
132+
if [[ $firstLine == "true" ]]; then
133+
IFS=' ' read -r result_status result_value <<< "$line"
134+
firstLine="false"
135+
if [[ "$result_status" == "error" ]]; then
136+
echo "Error: $result_value"
137+
exit 2
138+
fi
139+
else
140+
result_value="$line"
141+
fi
142+
echo "$result_value"
143+
done <"$conn_id"
144+
}
145+
146+
function pglet_add() {
147+
pglet_send "add $1"
148+
}
149+
150+
function pglet_addf() {
151+
pglet_send "addf $1"
152+
}
153+
154+
function pglet_set() {
155+
pglet_send "set $1"
156+
}
157+
158+
function pglet_setf() {
159+
pglet_send "setf $1"
160+
}
161+
162+
function pglet_set_value() {
163+
pglet_send "set $1 value=\"${2//\"/\\\"}\""
11164
}
12165

13-
function pglet_event() {
166+
function pglet_set_valuef() {
167+
pglet_send "setf $1 value=\"${2//\"/\\\"}\""
168+
}
169+
170+
function pglet_get_value() {
171+
pglet_send "get $1 value"
172+
}
173+
174+
function pglet_show() {
175+
pglet_send "set $1 visible=true"
176+
}
177+
178+
function pglet_hide() {
179+
pglet_send "set $1 visible=false"
180+
}
181+
182+
function pglet_enable() {
183+
pglet_send "set $1 enabled=true"
184+
}
185+
186+
function pglet_disable() {
187+
pglet_send "set $1 enabled=false"
188+
}
189+
190+
function pglet_clean() {
191+
pglet_send "clean $1"
192+
}
193+
194+
function pglet_remove() {
195+
pglet_send "remove $1"
196+
}
197+
198+
function pglet_wait_event() {
199+
if [[ "$1" != "" ]]; then
200+
local conn_id=$1
201+
else
202+
local conn_id=$PGLET_CONNECTION_ID
203+
fi
204+
205+
IFS=' ' read PGLET_EVENT_TARGET PGLET_EVENT_NAME PGLET_EVENT_DATA < "$conn_id.events"
206+
}
207+
208+
function pglet_dispatch_events() {
14209
# https://askubuntu.com/questions/992439/bash-pass-both-array-and-non-array-parameter-to-function
210+
211+
#echo "count: $#"
212+
15213
arr=("$@")
16214
IFS=' '
17215
while true
18216
do
19-
read eventTarget eventName eventData < "$page_pipe.events"
217+
pglet_wait_event
20218
for evt in "${arr[@]}";
21219
do
22220
IFS=' ' read -r et en fn <<< "$evt"
23-
if [[ "$eventTarget" == "$et" && "$eventName" == "$en" ]]; then
221+
if [[ "$PGLET_EVENT_TARGET" == "$et" && "$PGLET_EVENT_NAME" == "$en" ]]; then
24222
eval "$fn"
25223
return
26224
fi
27225
#echo "$et - $en - $fn"
28226
done
29227
done
30-
}
228+
}
229+
230+
function __pglet_install() {
231+
if [ "$(uname -m)" != "x86_64" ]; then
232+
echo "Error: Unsupported architecture $(uname -m). Only x64 binaries are available." 1>&2
233+
exit 1
234+
fi
235+
236+
if [ "$OS" = "Windows_NT" ]; then
237+
echo "Error: Bash for Windows is not supported." 1>&2
238+
exit 1
239+
else
240+
case $(uname -s) in
241+
Darwin) target="darwin-amd64.tar.gz" ;;
242+
*) target="linux-amd64.tar.gz" ;;
243+
esac
244+
fi
245+
246+
# check if pglet.exe is in PATH already (development mode)
247+
if command -v pglet &> /dev/null
248+
then
249+
PGLET_EXE=`which pglet`
250+
return
251+
fi
252+
253+
# check if there is Pglet aready installed
254+
local pglet_dir="$HOME/.pglet"
255+
local pglet_bin="$pglet_dir/bin"
256+
PGLET_EXE="$pglet_bin/pglet"
257+
258+
local ver="$PGLET_VER"
259+
local installed_ver=""
260+
261+
if [ -f "$PGLET_EXE" ]; then
262+
installed_ver=$($PGLET_EXE --version)
263+
fi
264+
265+
#echo "Installed version: $installed_ver"
266+
267+
if [[ "$installed_ver" != "$ver" ]]; then
268+
printf "Installing Pglet v$ver..."
269+
270+
if [ ! -d "$pglet_bin" ]; then
271+
mkdir -p "$pglet_bin"
272+
fi
273+
274+
local pglet_url="https://github.com/pglet/pglet/releases/download/v${ver}/pglet-${target}"
275+
local tempTar="$HOME/.pglet/pglet.tar.gz"
276+
curl -fsSL $pglet_url -o $tempTar
277+
tar zxf $tempTar -C $pglet_bin
278+
rm $tempTar
279+
280+
#echo "OK"
281+
fi
282+
}
283+
284+
__pglet_install

tests/app-test.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
4+
set -e
5+
6+
# include Pglet library
7+
. $DIR/../pglet.sh
8+
9+
function hello() {
10+
pglet_send "clean page"
11+
pglet_send "add text value=\"That's all, folks!\""
12+
}
13+
14+
function main() {
15+
pglet_send "add text value='Hello, world!'"
16+
pglet_send "add button id=ok text=OK"
17+
pglet_dispatch_events "ok click hello"
18+
}
19+
20+
pglet_app "app1" "main"

tests/install-test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
4+
set -e
5+
6+
# pglet is getting installed while loading the script
7+
. $DIR/../pglet.sh

tests/page-test.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/sh
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
4+
set -e
5+
6+
# include Pglet library
7+
. $DIR/../pglet.sh
8+
9+
PGLET_NO_WINDOW=true pglet_page "index"
10+
11+
#echo "$PGLET_CONNECTION_ID"
12+
13+
pglet_clean
14+
pglet_add "text value='Hello world' size=xxLarge"
15+
txt1=`pglet_send "add textbox multiline label=Data"`
16+
pglet_addf "button id=ok text=OK"
17+
18+
function hello() {
19+
echo "PGLET_EVENT_TARGET: $PGLET_EVENT_TARGET"
20+
echo "PGLET_EVENT_NAME: $PGLET_EVENT_NAME"
21+
echo "PGLET_EVENT_DATA: $PGLET_EVENT_DATA"
22+
23+
r=`pglet_get_value $txt1`
24+
echo "value: $r"
25+
26+
pglet_remove "at=0"
27+
# echo "PGLET_EXE: $PGLET_EXE"
28+
# echo "PGLET_CONNECTION_ID: $PGLET_CONNECTION_ID"
29+
# echo "PGLET_PAGE_URL: $PGLET_PAGE_URL"
30+
}
31+
32+
#events=("ok click hello")
33+
#pglet_dispatch_events "${events[@]}"
34+
35+
36+
pglet_dispatch_events "ok click hello"
37+
38+
# while true
39+
# do
40+
# pglet_wait_event
41+
# if [[ "$PGLET_EVENT_TARGET" == "ok" && "$PGLET_EVENT_NAME" == "click" ]]; then
42+
# pglet_send "clean page"
43+
# pglet_send "add text value=\"That's all, folks!\""
44+
# exit 0
45+
# fi
46+
# done

tests/quickstart.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
4+
set -e
5+
6+
# include Pglet library
7+
. $DIR/../pglet.sh
8+
9+
PGLET_WEB=false pglet_page
10+
11+
pglet_clean
12+
pglet_add "text value='Hello, world!'"

0 commit comments

Comments
 (0)