Skip to content

Commit a99fdfd

Browse files
authored
Merge pull request #11 from zachgrayio/feature/hot-reload
Feature/hot reload
2 parents 8896ef0 + 4adb419 commit a99fdfd

File tree

5 files changed

+114
-34
lines changed

5 files changed

+114
-34
lines changed

Dockerfile

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ RUN swift package update
1212
ADD ./ /usr/src
1313

1414
# user can pass in CONFIG=release to override
15-
ARG CONFIG=debug
15+
ENV CONFIG=debug
1616

17-
RUN swift build --configuration ${CONFIG}
17+
# user can pass in LIVE=true
18+
ENV LIVE=false
1819

19-
RUN cp ./.build/${CONFIG}/libSTSLibrary.so /usr/lib/libSTSLibrary.so
20-
RUN cp ./.build/${CONFIG}/STSLibrary.swiftmodule /usr/lib/STSLibrary.swiftmodule
21-
RUN cp ./.build/${CONFIG}/STSApplication /usr/bin/STSApplication
22-
23-
ENTRYPOINT /usr/bin/STSApplication
20+
ENTRYPOINT ./entrypoint $CONFIG $LIVE

Package.resolved

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Projects built with this template will have the following traits:
3434
2. Quick and easy [REPL](https://github.com/tensorflow/swift/blob/master/Usage.md#repl-read-eval-print-loop) access against the project's Swift for Tensorflow code and third-party libraries
3535
3. Easily unit testable
3636
4. Runs anywhere Docker is available with no additional setup necessary - zero conflicts with existing Swift or TensorFlow installations.
37+
5. Swift code is hot-reloaded on change; third-party libraries are downloaded automatically as well. See the `--live` flag.
3738

3839
This will enable both ease of use during the research phase and a rapid transition to a scalable training solution and beyond (production deployment).
3940

@@ -140,6 +141,7 @@ A control script is included for extra convenience for users on macOS/Linux, but
140141

141142
Some example commands:
142143

144+
* `./sts run app --live` - automatically rebuild and run the application on code change; packages are updated automatically as well!
143145
* `./sts build --release`, `./sts build -r`, `./sts build -p`, `./sts build --prod` - build the image with a release executable
144146
* `./sts run repl --build --name myrepl -v` - run a REPL in a container named myrepl, mounting the current directory as a volume, building the project first
145147
* `./sts run test`, `./sts run tests --name testcontainer` - run unit tests

entrypoint

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bash
2+
3+
CONFIG=$1
4+
LIVE=$2
5+
KILL=false
6+
7+
handle_interrupt() {
8+
KILL=true
9+
}
10+
11+
build() {
12+
swift build --configuration ${CONFIG}
13+
}
14+
15+
install_application() {
16+
cp ./.build/${CONFIG}/libSTSLibrary.so /usr/lib/libSTSLibrary.so
17+
cp ./.build/${CONFIG}/STSLibrary.swiftmodule /usr/lib/STSLibrary.swiftmodule
18+
cp ./.build/${CONFIG}/STSApplication /usr/bin/STSApplication
19+
}
20+
21+
run_application() {
22+
/usr/bin/STSApplication
23+
}
24+
25+
build_and_run() {
26+
build && install_application && run_application
27+
}
28+
29+
watch_files() {
30+
while [ ${KILL} != true ]
31+
do
32+
files=`find *.swift -type f -newer /usr/bin/STSApplication`
33+
if [[ ${files} != "" ]] ; then
34+
echo "Changes in files: $files, building..."
35+
if [[ ${files} = *"Package.swift" ]] ; then
36+
swift package update
37+
fi
38+
build_and_run
39+
echo "Waiting for changes to run application again."
40+
fi
41+
sleep 2
42+
done
43+
}
44+
45+
entrypoint() {
46+
build_and_run
47+
}
48+
49+
live_entrypoint() {
50+
build_and_run
51+
echo "Waiting for changes to run application again."
52+
watch_files &
53+
wait
54+
}
55+
56+
if [ ${LIVE} = true ] ; then
57+
live_entrypoint
58+
else
59+
entrypoint
60+
fi

sts

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,43 @@
33
POSITIONAL=()
44
STS_NAME=sts-application
55
STS_BUILD_ENABLED=false
6+
STS_LIVE_ENABLED=false
67

78
TXT_NC='\033[0m'
89
TXT_GREEN='\033[0;32m'
910
TXT_YELLOW='\033[0;33m'
1011
TXT_RED='\033[0;31m'
1112

12-
while [[ $# -gt 0 ]]
13+
while [ $# -gt 0 ]
1314
do
14-
key="$1"
15-
16-
case ${key} in
17-
-n|--name)
18-
STS_NAME="$2"
19-
shift
20-
shift
21-
;;
22-
-v|--volume)
23-
STS_VOLUME_ENABLED=true
24-
shift
25-
;;
26-
-b|--build)
27-
STS_BUILD_ENABLED=true
28-
shift
29-
;;
30-
-p|--prod|-r|--release)
31-
STS_RELEASE_CONFIG_ENABLED=true
32-
shift
33-
;;
34-
*) # unknown option
35-
POSITIONAL+=("$1")
36-
shift
37-
;;
38-
esac
15+
key="$1"
16+
case ${key} in
17+
-n|--name)
18+
STS_NAME="$2"
19+
shift
20+
shift
21+
;;
22+
-v|--volume)
23+
STS_VOLUME_ENABLED=true
24+
shift
25+
;;
26+
-b|--build)
27+
STS_BUILD_ENABLED=true
28+
shift
29+
;;
30+
-p|--prod|-r|--release)
31+
STS_RELEASE_CONFIG_ENABLED=true
32+
shift
33+
;;
34+
-l|--live)
35+
STS_LIVE_ENABLED=true
36+
shift
37+
;;
38+
*) # unknown option
39+
POSITIONAL+=("$1")
40+
shift
41+
;;
42+
esac
3943
done
4044
set -- "${POSITIONAL[@]}"
4145

@@ -56,6 +60,15 @@ else
5660
STS_BUILD_COMMAND="docker build -t ${STS_NAME} ."
5761
fi
5862

63+
# live flag
64+
if [ "$STS_LIVE_ENABLED" = true ] ; then
65+
STS_VOLUME_FLAG="-v $PWD:/usr/src"
66+
STS_LIVE_FLAG="-e LIVE=true"
67+
else
68+
STS_VOLUME_FLAG=""
69+
STS_LIVE_FLAG=""
70+
fi
71+
5972
case ${STS_COMMAND} in
6073
build)
6174
echo -e "[STS][build] Building with: ${TXT_GREEN}${STS_BUILD_COMMAND}${TXT_NC}"
@@ -64,7 +77,7 @@ case ${STS_COMMAND} in
6477
run)
6578
case ${STS_COMMAND_ARG} in
6679
app)
67-
RUN_COMMAND="docker run --rm ${STS_VOLUME_FLAG} ${STS_NAME}"
80+
RUN_COMMAND="docker run --rm -it ${STS_LIVE_FLAG} ${STS_VOLUME_FLAG} ${STS_NAME}"
6881
;;
6982
repl)
7083
RUN_COMMAND="docker run --rm ${STS_VOLUME_FLAG} --security-opt seccomp:unconfined -it \

0 commit comments

Comments
 (0)