File: embed.sh

package info (click to toggle)
ocaml-obuild 0.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,456 kB
  • sloc: ml: 14,491; sh: 211; ansic: 34; makefile: 11
file content (51 lines) | stat: -rwxr-xr-x 982 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/sh
# Simple embed script: generates OCaml module from text files
# Usage: embed.sh -o output.ml file1.txt file2.txt ...

OUTPUT=""
FILES=""

while [ $# -gt 0 ]; do
    case "$1" in
        -o)
            OUTPUT="$2"
            shift 2
            ;;
        *)
            FILES="$FILES $1"
            shift
            ;;
    esac
done

if [ -z "$OUTPUT" ]; then
    echo "Usage: embed.sh -o output.ml file1.txt ..." >&2
    exit 1
fi

# Generate OCaml module
cat > "$OUTPUT" << 'HEADER'
(* Auto-generated by embed.sh *)

let files = [
HEADER

for f in $FILES; do
    if [ -f "$f" ]; then
        basename=$(basename "$f")
        content=$(cat "$f" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g')
        echo "  (\"$basename\", \"$content\");" >> "$OUTPUT"
    fi
done

cat >> "$OUTPUT" << 'FOOTER'
]

let get name =
  try Some (List.assoc name files)
  with Not_found -> None

let list () = List.map fst files
FOOTER

echo "Generated $OUTPUT with $(echo $FILES | wc -w) files"