forked from diezep/heroku-buildpack-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile
136 lines (105 loc) · 3.75 KB
/
compile
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bash
# bin/compile <build-dir> <cache-dir> <env-dir>
set -e
unset GIT_DIR
function print() {
echo "-----> $1"
}
# Taken from https://devcenter.heroku.com/articles/buildpack-api#bin-compile-summary
function export_env_dir() {
env_dir=$1
acceptlist_regex=${2:-''}
denylist_regex=${3:-'^(PATH|GIT_DIR|CPATH|CPPATH|LD_PRELOAD|LIBRARY_PATH)$'}
if [ -d "$env_dir" ]; then
for e in $(ls $env_dir); do
echo "$e" | grep -E "$acceptlist_regex" | grep -qvE "$denylist_regex" &&
export "$e=$(cat $env_dir/$e)"
:
done
fi
}
# Taken from https://devcenter.heroku.com/articles/buildpack-api#style
function indent() {
sed -u 's/^/ /'
}
BUILD_DIR=${1:-}
CACHE_DIR=${2:-}
export_env_dir "$3"
# Create the cache directory if not exist.
mkdir -p $CACHE_DIR
cd $BUILD_DIR
if [ -d "$CACHE_DIR/flutter" ]; then
print "Restoring Flutter SDK from CACHE"
cp -R $CACHE_DIR/flutter $BUILD_DIR
else
print "Installing SDK from Github repository."
git clone https://github.com/flutter/flutter.git --quiet
fi
# Check if FLUTTER_VERSION variables is set.
if [ -n "$FLUTTER_VERSION" ]; then
# Load bash variables from flutter --machine --version and read it like $FLUTTER_VERSION variable.
flutter/bin/flutter --machine --version >flutter.json
FLUTTER_INSTALLED_VERSION=$(grep -o '"frameworkVersion": *"[^"]*' flutter.json | grep -o '[^"]*$')
# Check if the FLUTTER_VERSION is the same with the installed one.
if [ "$FLUTTER_VERSION" != "$FLUTTER_INSTALLED_VERSION" ]; then
print "Installing Flutter SDK version : $FLUTTER_VERSION"
flutter/bin/flutter version $FLUTTER_VERSION --quiet | indent
fi
rm flutter.json
else
print "Running flutter upgrade command"
if [ "$channel" != "beta" ]; then
flutter/bin/flutter channel beta | indent
fi
flutter/bin/flutter upgrade --quiet | indent
fi
print "Enabling Web support"
flutter/bin/flutter config --enable-web --quiet | indent
print "Running flutter clean command."
flutter/bin/flutter clean --quiet | indent
print "Getting packages from Flutter project"
flutter/bin/flutter pub get --quiet | indent
PATH="$PATH":"$(pwd)/flutter/bin/"
if [ -z "$FLUTTER_BUILD" ]; then
FLUTTER_BUILD="flutter build web --release --quiet"
fi
print "Compiling the project with $FLUTTER_BUILD"
eval $FLUTTER_BUILD | indent
PUB_CACHE="$HOME/.pub-cache/"
mkdir -p $PUB_CACHE
# Saving SDK in Cache and in app.
if [ -x flutter/bin/flutter ]; then
print "Saving Flutter SDK in Cache"
rm -rf $CACHE_DIR/flutter
cp -R $BUILD_DIR/flutter $CACHE_DIR/flutter
mv $BUILD_DIR/flutter/bin/cache/dart-sdk/ $BUILD_DIR
rm -rf $BUILD_DIR/flutter
fi
# To read hidden directories and use move with exclusion.
shopt -s extglob
# Check FLUTTER_CLEANUP var to delete all files or keep it.
if [ "$FLUTTER_CLEANUP" != false ]; then
mkdir -p $BUILD_DIR/TO_DELETE
mv !("TO_DELETE") TO_DELETE
mv TO_DELETE/build/web/* $BUILD_DIR
mv TO_DELETE/dart-sdk $BUILD_DIR
rm -rf TO_DELETE
fi
# Activate dhttpd dart package to run the server.
# Is taken from my Github repo because some variables need to be changed every run
# like PORT from environment.
if [ -d "$CACHE_DIR/.pub-cache/" ]; then
print "dhttpd Found in cache. Restoring."
cp -R $CACHE_DIR/.pub-cache/* $PUB_CACHE
else
print "Getting dhttpd to run web service."
$BUILD_DIR/dart-sdk/bin/dart pub global activate -sgit https://github.com/diezep/dhttpd.git | indent
print "Getting dhtppd to run web service."
mkdir -p $CACHE_DIR/.pub-cache/
cp -R $PUB_CACHE/* $CACHE_DIR/.pub-cache/
fi
# Moving to the build, after running the compile. All will be deleted and removed to app directory.
# Copying the result, the files will be kept in storage.
cp -R $PUB_CACHE $BUILD_DIR
export PATH="$PATH":"/app/dart-sdk/bin/"
export PATH="$PATH":"/app/.pub-cache/bin"