-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepublishOnly.sh
More file actions
197 lines (161 loc) · 4.9 KB
/
prepublishOnly.sh
File metadata and controls
197 lines (161 loc) · 4.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
#!/usr/bin/env bash
#===============================================================================
# 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:
# bash 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
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Log functions
log_info() {
echo -e "${GREEN}[Rest]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[Rest]${NC} $1"
}
log_error() {
echo -e "${RED}[Rest]${NC} $1"
}
# Ensure Rust toolchain is configured
if ! command -v rustup &>/dev/null; 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; 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 "${BASH_SOURCE[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; 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() {
local platform arch target
platform=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$platform" in
darwin)
if [[ "$arch" == "arm64" ]]; then
target="aarch64-apple-darwin"
else
target="x86_64-apple-darwin"
fi
;;
linux)
if [[ "$arch" == "aarch64" ]]; then
target="aarch64-unknown-linux-gnu"
else
target="x86_64-unknown-linux-gnu"
fi
;;
msys* | mingw* | cygwin*)
target="x86_64-pc-windows-msvc"
;;
*)
log_error "Unsupported platform: $platform"
exit 1
;;
esac
echo "$target"
}
# 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
# Note: Cargo.toml specifies target-dir = "Target", so we use "Target" instead of "target"
if [[ "$TARGET" == *"windows"* ]]; then
BINARY_PATH="Target/$TARGET/release/Rest.exe"
else
BINARY_PATH="Target/$TARGET/release/Rest"
fi
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 or symlink the binary to bin directory
BINARY_NAME=$(basename "$BINARY_PATH")
cp "$BINARY_PATH" "bin/$BINARY_NAME"
# Make the binary executable (Unix-like systems)
if [[ "$TARGET" != *"windows"* ]]; then
chmod +x "bin/$BINARY_NAME"
fi
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