-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·358 lines (295 loc) · 8.9 KB
/
build.sh
File metadata and controls
executable file
·358 lines (295 loc) · 8.9 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
# PowerNight Build Script
# Generates version information and builds the application
set -e
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Helper functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if required tools are installed
check_requirements() {
log_info "Checking build requirements..."
if ! command -v python3 &> /dev/null; then
log_error "python3 is not installed"
exit 1
fi
if ! command -v node &> /dev/null; then
log_error "node is not installed"
exit 1
fi
if ! command -v npm &> /dev/null; then
log_error "npm is not installed"
exit 1
fi
if ! command -v jq &> /dev/null; then
log_warning "jq is not installed. Will use basic parsing (install jq for better results)"
fi
log_success "All requirements satisfied"
}
# Validate configuration
validate_config() {
log_info "Validating configuration..."
# Check if config file exists
if [ ! -f "config/config.yaml" ]; then
log_warning "config/config.yaml not found - skipping validation"
return 0
fi
# Run validation using Python
if python3 -m powernight.main --config config/config.yaml validate-config > /tmp/config_validation.log 2>&1; then
log_success "Configuration validation passed"
return 0
else
log_error "Configuration validation failed!"
log_error "See details below:"
echo ""
cat /tmp/config_validation.log
echo ""
log_error "Build aborted due to configuration errors"
log_info "Fix the configuration errors and try again, or use --skip-validation to skip this check"
exit 1
fi
}
# Generate timestamp in required format: yyyy-MM-dd HH:mm:ss zzz ZZZZ
generate_timestamp() {
log_info "Generating build timestamp..." >&2
# Format: 2025-10-18 14:30:45 UTC +0000
TIMESTAMP=$(date -u +'%Y-%m-%d %H:%M:%S %Z %z')
log_success "Timestamp: $TIMESTAMP" >&2
echo "$TIMESTAMP"
}
# Extract version from pyproject.toml
get_powernight_version() {
log_info "Reading PowerNight version from pyproject.toml..." >&2
if [ -f "pyproject.toml" ]; then
VERSION=$(grep "^version = " pyproject.toml | head -n 1 | cut -d'"' -f2)
log_success "PowerNight version: $VERSION" >&2
echo "$VERSION"
else
log_error "pyproject.toml not found" >&2
echo "unknown"
fi
}
# Get Python version
get_python_version() {
log_info "Detecting Python version..." >&2
PYTHON_VERSION=$(python3 --version 2>&1 | cut -d' ' -f2)
log_success "Python version: $PYTHON_VERSION" >&2
echo "$PYTHON_VERSION"
}
# Get Node.js version
get_node_version() {
log_info "Detecting Node.js version..." >&2
NODE_VERSION=$(node --version | sed 's/v//')
log_success "Node.js version: $NODE_VERSION" >&2
echo "$NODE_VERSION"
}
# Get npm version
get_npm_version() {
log_info "Detecting npm version..." >&2
NPM_VERSION=$(npm --version)
log_success "npm version: $NPM_VERSION" >&2
echo "$NPM_VERSION"
}
# Extract backend dependencies from pyproject.toml
get_backend_dependencies() {
log_info "Extracting backend dependencies..." >&2
if [ ! -f "pyproject.toml" ]; then
echo "{}"
return
fi
# Extract key dependencies with versions
# This is a simplified parser - ideally use a TOML parser
DEPS=$(cat <<'EOF'
{
"flask": ">=2.3.0",
"pypowerwall": ">=0.10.5",
"schedule": ">=1.2.0",
"pyyaml": ">=6.0.1",
"sqlalchemy": "Implicit (via Flask-SQLAlchemy)",
"requests": ">=2.31.0",
"structlog": ">=23.1.0",
"tenacity": ">=8.2.0"
}
EOF
)
echo "$DEPS"
}
# Extract frontend dependencies from package.json
get_frontend_dependencies() {
log_info "Extracting frontend dependencies..." >&2
if [ ! -f "package.json" ]; then
echo "{}"
return
fi
if command -v jq &> /dev/null; then
# Use jq for proper JSON parsing
DEPS=$(jq -r '{
react: .dependencies.react,
"react-dom": .dependencies["react-dom"],
"react-router-dom": .dependencies["react-router-dom"],
axios: .dependencies.axios,
"date-fns": .dependencies["date-fns"],
vite: .devDependencies.vite,
typescript: .devDependencies.typescript,
tailwindcss: .devDependencies.tailwindcss,
"@vitejs/plugin-react": .devDependencies["@vitejs/plugin-react"]
}' package.json)
else
# Fallback: Basic parsing without jq
DEPS=$(cat <<'EOF'
{
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.20.1",
"axios": "^1.6.2",
"date-fns": "^2.30.0",
"vite": "^7.1.9",
"typescript": "^5.2.2",
"tailwindcss": "^4.1.14",
"@vitejs/plugin-react": "^4.2.1"
}
EOF
)
fi
echo "$DEPS"
}
# Generate version-info.json
generate_version_info() {
local TIMESTAMP=$1
local POWERNIGHT_VERSION=$2
local PYTHON_VERSION=$3
local NODE_VERSION=$4
local NPM_VERSION=$5
local BACKEND_DEPS=$6
local FRONTEND_DEPS=$7
log_info "Generating version-info.json..."
# Create JSON file with proper formatting
# Note: We need to ensure dependencies are properly formatted without trailing commas
cat > version-info.json <<EOF
{
"application": "PowerNight",
"version": "$POWERNIGHT_VERSION",
"build_timestamp": "$TIMESTAMP",
"python_version": "$PYTHON_VERSION",
"node_version": "$NODE_VERSION",
"npm_version": "$NPM_VERSION",
"backend_dependencies": $(echo "$BACKEND_DEPS" | tr -d '\n'),
"frontend_dependencies": $(echo "$FRONTEND_DEPS" | tr -d '\n')
}
EOF
log_success "version-info.json created"
# Display the generated file
log_info "Generated version info:"
cat version-info.json | head -20
}
# Build frontend
build_frontend() {
log_info "Building frontend with Vite..."
npm run build
log_success "Frontend build completed"
}
# Copy version info to dist
copy_version_to_dist() {
log_info "Copying version-info.json to dist/..."
if [ ! -d "dist" ]; then
log_error "dist/ directory not found. Frontend build may have failed."
exit 1
fi
cp version-info.json dist/
log_success "version-info.json copied to dist/"
}
# Build Docker image
build_docker() {
local TIMESTAMP=$1
log_info "Building Docker image..."
docker build \
--label "build_timestamp=$TIMESTAMP" \
-t powernight:latest \
.
log_success "Docker image built successfully"
}
# Main build process
main() {
echo ""
log_info "========================================="
log_info "PowerNight Build Script"
log_info "========================================="
echo ""
# Check requirements
check_requirements
echo ""
# Validate configuration (skip if --skip-validation flag is set)
if [[ "$*" != *"--skip-validation"* ]]; then
validate_config
echo ""
else
log_warning "Skipping configuration validation (--skip-validation flag set)"
echo ""
fi
# Generate build metadata
TIMESTAMP=$(generate_timestamp)
POWERNIGHT_VERSION=$(get_powernight_version)
PYTHON_VERSION=$(get_python_version)
NODE_VERSION=$(get_node_version)
NPM_VERSION=$(get_npm_version)
echo ""
# Get dependencies
BACKEND_DEPS=$(get_backend_dependencies)
FRONTEND_DEPS=$(get_frontend_dependencies)
echo ""
# Generate version info
generate_version_info "$TIMESTAMP" "$POWERNIGHT_VERSION" \
"$PYTHON_VERSION" "$NODE_VERSION" "$NPM_VERSION" \
"$BACKEND_DEPS" "$FRONTEND_DEPS"
echo ""
# Build frontend
build_frontend
echo ""
# Copy version info
copy_version_to_dist
echo ""
# Build Docker image (optional - can be skipped with --no-docker flag)
if [[ "$*" != *"--no-docker"* ]]; then
build_docker "$TIMESTAMP"
echo ""
else
log_info "Skipping Docker build (--no-docker flag set)"
echo ""
fi
# Summary
echo ""
log_success "========================================="
log_success "Build completed successfully!"
log_success "========================================="
log_success "Version: $POWERNIGHT_VERSION"
log_success "Timestamp: $TIMESTAMP"
echo ""
if [[ "$*" == *"--no-docker"* ]]; then
log_info "To build Docker image manually, run:"
echo " docker build --label \"build_timestamp=$TIMESTAMP\" -t powernight:latest ."
echo ""
fi
if [[ "$*" == *"--skip-validation"* ]]; then
log_warning "Note: Configuration validation was skipped"
log_info "To validate your config manually, run:"
echo " python3 -m powernight.main validate-config"
echo ""
fi
}
# Run main function
main "$@"