-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepublishOnly.sh
More file actions
executable file
·191 lines (158 loc) · 4.62 KB
/
prepublishOnly.sh
File metadata and controls
executable file
·191 lines (158 loc) · 4.62 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
#!/usr/bin/env sh
#===============================================================================
# prepublishOnly.sh - Build script for @codeeditorland/Rest NPM publishing
#===============================================================================
#
# This script builds the Rest compiler binary for all supported platforms
# before publishing to NPM. It should be run as part of the prepublishOnly
# lifecycle script.
#
# Usage:
# sh prepublishOnly.sh
#
# Environment Variables:
# REST_BUILD_TARGET - Override the build target (default: auto-detect)
# REST_SKIP_BUILD - Set to "true" to skip the build
# Compiler - Set to "Rest" to enable the Rest compiler
#
#===============================================================================
set -e
log_info() {
printf "[Rest] %s\n" "$1"
}
log_warn() {
printf "[Rest] %s\n" "$1"
}
log_error() {
printf "[Rest] %s\n" "$1" >&2
}
# Ensure Rust toolchain is configured
if ! command -v rustup >/dev/null 2>&1; then
log_error "rustup is not installed. Please install Rust from https://rustup.rs/"
exit 1
fi
# Set default toolchain to stable if not already configured
if ! rustup default >/dev/null 2>&1; then
log_info "Setting Rust default toolchain to stable..."
rustup default stable
fi
# Check if build should be skipped
if [ "${REST_SKIP_BUILD}" = "true" ]; then
log_info "Build skipped via REST_SKIP_BUILD environment variable"
exit 0
fi
# Ensure we're in the Rest directory
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"
log_info "Starting Rest build for NPM publishing..."
log_info "Working directory: $SCRIPT_DIR"
# Check if Cargo is available
if ! command -v cargo >/dev/null 2>&1; then
log_error "Cargo is not installed. Please install Rust from https://rustup.rs/"
exit 1
fi
log_info "Cargo version: $(cargo --version)"
# Detect target platform
detect_target() {
platform=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$platform" in
darwin)
if [ "$arch" = "arm64" ]; then
echo "aarch64-apple-darwin"
else
echo "x86_64-apple-darwin"
fi
;;
linux)
if [ "$arch" = "aarch64" ]; then
echo "aarch64-unknown-linux-gnu"
else
echo "x86_64-unknown-linux-gnu"
fi
;;
msys* | mingw* | cygwin*)
echo "x86_64-pc-windows-msvc"
;;
*)
log_error "Unsupported platform: $platform"
exit 1
;;
esac
}
# Override target if environment variable is set
TARGET="${REST_BUILD_TARGET:-$(detect_target)}"
log_info "Build target: $TARGET"
# Build the release binary
log_info "Building Rest compiler in release mode..."
cargo build --release --target "$TARGET"
# Verify the binary was created
case "$TARGET" in
*windows*)
BINARY_PATH="Target/$TARGET/release/Rest.exe"
;;
*)
BINARY_PATH="Target/$TARGET/release/Rest"
;;
esac
if [ ! -f "$BINARY_PATH" ]; then
log_error "Binary not found at: $BINARY_PATH"
exit 1
fi
log_info "Build successful: $BINARY_PATH"
# Create bin directory for NPM package
mkdir -p bin
# Copy the binary to bin directory
BINARY_NAME=$(basename "$BINARY_PATH")
cp "$BINARY_PATH" "bin/$BINARY_NAME"
# Make the binary executable (Unix-like systems)
case "$TARGET" in
*windows*) ;;
*)
chmod +x "bin/$BINARY_NAME"
;;
esac
log_info "Binary copied to: bin/$BINARY_NAME"
# Create the bin wrapper script (Rest.js)
cat >bin/Rest.js <<'EOF'
#!/usr/bin/env node
/**
* Rest.js - CLI wrapper for Rest compiler binary
*/
import { execSync } from 'node:child_process';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { existsSync } from 'node:fs';
const __dirname = dirname(fileURLToPath(import.meta.url));
// Determine the binary path based on platform
const platform = process.platform;
const binaryName = platform === 'win32' ? 'Rest.exe' : 'Rest';
const binaryPath = join(__dirname, binaryName);
if (!existsSync(binaryPath)) {
console.error(`[Rest] Binary not found at: ${binaryPath}`);
console.error('[Rest] Please run: npm install or npm rebuild');
process.exit(1);
}
// Execute the binary with all arguments
try {
execSync(`"${binaryPath}" ${process.argv.slice(2).map(a => `"${a}"`).join(' ')}`, {
stdio: 'inherit',
});
} catch (error) {
process.exit(error.status || 1);
}
EOF
# Make the wrapper script executable
chmod +x bin/Rest.js
log_info "CLI wrapper created: bin/Rest.js"
# Build summary
log_info "=========================================="
log_info "Build Summary"
log_info "=========================================="
log_info "Target: $TARGET"
log_info "Binary: bin/$BINARY_NAME"
log_info "CLI: bin/Rest.js"
log_info "=========================================="
log_info "Ready for NPM publish!"
log_info "=========================================="
exit 0