OILS / devtools / xshar.sh View on Github | oilshell.org

138 lines, 54 significant
1#!/usr/bin/env bash
2#
3# xshar - Executable shell archive
4#
5# Usage:
6# devtools/xshar.sh <function name>
7#
8# devtools/
9# xshar.sh # this file
10# test-oils.sh # can run benchmarks
11#
12# Results in:
13#
14# _release/test-oils.xshar
15#
16# Runtime deps:
17#
18# /bin/sh
19# tar, gzip -d
20# base64
21
22set -o nounset
23set -o pipefail
24set -o errexit
25
26source devtools/run-task.sh # run-task
27
28print-shell() {
29 local tar=$1
30 local main=$2
31
32 # Same as soil/worker.sh
33 local git_commit
34 git_commit=$(git log -n 1 --pretty='format:%H')
35
36 sed "s/__GIT_COMMIT__/$git_commit/" <<'EOF'
37#!/bin/sh
38
39export XSHAR_REPO=oilshell/oil
40export XSHAR_GIT_COMMIT=__GIT_COMMIT__
41
42name=$(basename $0) # e.g. hello-xshar.xshar
43default_dir=/tmp/$name.$$
44
45# User can override this, and then _build/oils.sh can use SKIP_REBUILD to make
46# it faster. Multiple runs without compiling.
47
48export XSHAR_DIR=${XSHAR_DIR:-$default_dir}
49
50change_dir() {
51 mkdir -p "$XSHAR_DIR"
52 cd "$XSHAR_DIR"
53}
54
55extract_data() {
56 base64 -d <<'XSHAR_DATA' | tar -x -z
57EOF
58
59 # Print code that extracts here doc
60 base64 < $tar
61
62 cat <<EOF
63XSHAR_DATA
64}
65
66change_dir
67extract_data
68EOF
69 echo "$main" '"$@"'
70
71 # We don't bother to clean up after, it's in /tmp
72}
73
74create() {
75 local main=${1:-devtools/test-oils.sh}
76 # Include the main file
77 #shift
78
79 local name
80 name=$(basename $main .sh)
81
82 local tar=_tmp/$name.tar.gz
83
84 tar --create --gzip --file $tar "$main" "$@"
85 ls -l $tar
86
87 local out=_release/$name.xshar
88
89 print-shell $tar $main > $out
90 chmod +x $out
91 ls -l $out
92 echo
93}
94
95create-hello() {
96 find yaks/ -name '*.py' | xargs -- $0 create devtools/hello-xshar.sh
97 ls -l -h _release
98}
99
100test-oils-manifest() {
101 echo '_release/oils-for-unix.tar'
102
103 echo 'oil-version.txt'
104
105 # TODO: need osh --tool shell-deps for these
106
107 echo 'devtools/release-native.sh'
108 echo 'benchmarks/time_.py'
109 echo 'benchmarks/time-helper.c'
110
111 # extracted tarball
112 #find _deps/osh-runtime/util-linux-2.40
113
114 # we could include Python-2.7.13 too
115}
116
117create-test-oils() {
118 devtools/release-native.sh make-tar
119
120 test-oils-manifest | xargs -- $0 create devtools/test-oils.sh
121 ls -l -h _release
122}
123
124soil-run-hello() {
125 create-hello
126 _release/hello-xshar.xshar main a b c
127}
128
129soil-run-test-oils() {
130 create-test-oils
131
132 # Run it twice to test that SKIP_REBUILD works
133 for x in 1 2; do
134 XSHAR_DIR=/tmp/test-oils.xshar.REUSED _release/test-oils.xshar demo a b c
135 done
136}
137
138run-task "$@"