multi-lang-build 0.2.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,468 @@
1
+ """Go compiler with mirror acceleration support."""
2
+
3
+ from pathlib import Path
4
+ from typing import Final, Literal
5
+ import shutil
6
+ import subprocess
7
+ import os
8
+ import tempfile
9
+
10
+ from multi_lang_build.compiler.base import CompilerBase, BuildResult, CompilerInfo
11
+ from multi_lang_build.mirror.config import (
12
+ get_mirror_config,
13
+ apply_mirror_environment,
14
+ GO_PROXY_GOPROXY_CN,
15
+ GO_PROXY_GOPROXY_IO,
16
+ GO_PROXY_GOVIP_CN,
17
+ DEFAULT_GO_MIRROR,
18
+ )
19
+
20
+
21
+ class GoCompiler(CompilerBase):
22
+ """Compiler for Go projects with module support."""
23
+
24
+ NAME: Final[str] = "go"
25
+ DEFAULT_MIRROR: Final[str] = DEFAULT_GO_MIRROR
26
+
27
+ def __init__(
28
+ self,
29
+ go_path: str | None = None,
30
+ mirror: str | None = None,
31
+ ) -> None:
32
+ """Initialize the Go compiler.
33
+
34
+ Args:
35
+ go_path: Optional path to go executable. If None, uses system PATH.
36
+ mirror: Mirror configuration name (e.g., "go", "go_qiniu", "go_vip").
37
+ Defaults to DEFAULT_GO_MIRROR (goproxy.cn).
38
+ """
39
+ self._go_path = go_path
40
+ self._mirror = mirror
41
+ self._version_cache: str | None = None
42
+
43
+ @property
44
+ def name(self) -> str:
45
+ """Get the compiler name."""
46
+ return self.NAME
47
+
48
+ @property
49
+ def version(self) -> str:
50
+ """Get the Go version."""
51
+ if self._version_cache:
52
+ return self._version_cache
53
+
54
+ go_executable = self._get_executable_path()
55
+
56
+ try:
57
+ result = subprocess.run(
58
+ [go_executable, "version"],
59
+ capture_output=True,
60
+ text=True,
61
+ timeout=10,
62
+ )
63
+ output = result.stdout.strip()
64
+ if output:
65
+ parts = output.split()
66
+ if len(parts) >= 3:
67
+ self._version_cache = parts[2]
68
+ else:
69
+ self._version_cache = output
70
+ else:
71
+ self._version_cache = "unknown"
72
+ except Exception:
73
+ self._version_cache = "unknown"
74
+
75
+ return self._version_cache
76
+
77
+ @property
78
+ def supported_mirrors(self) -> list[str]:
79
+ """Get list of supported mirror configurations."""
80
+ return ["go", "go_qiniu", "go_vip"]
81
+
82
+ @property
83
+ def current_mirror(self) -> str:
84
+ """Get the current mirror configuration name."""
85
+ return self._mirror or "go"
86
+
87
+ def get_info(self) -> CompilerInfo:
88
+ """Get compiler information."""
89
+ return {
90
+ "name": self.name,
91
+ "version": self.version,
92
+ "supported_mirrors": self.supported_mirrors,
93
+ "default_mirror": self.DEFAULT_MIRROR,
94
+ "executable": self._get_executable_path(),
95
+ }
96
+
97
+ def set_mirror(self, mirror: str) -> None:
98
+ """Set the mirror configuration.
99
+
100
+ Args:
101
+ mirror: Mirror configuration name (e.g., "go", "go_qiniu", "go_vip")
102
+ """
103
+ self._mirror = mirror
104
+
105
+ def build(
106
+ self,
107
+ source_dir: Path,
108
+ output_dir: Path,
109
+ *,
110
+ environment: dict[str, str] | None = None,
111
+ mirror_enabled: bool = True,
112
+ extra_args: list[str] | None = None,
113
+ stream_output: bool = True,
114
+ ) -> BuildResult:
115
+ """Execute the Go build process.
116
+
117
+ Args:
118
+ source_dir: Source code directory
119
+ output_dir: Build output directory (for binary)
120
+ environment: Additional environment variables
121
+ mirror_enabled: Whether to use Go proxy mirror
122
+ extra_args: Additional arguments to pass to go build
123
+ stream_output: Whether to stream output in real-time (default: True)
124
+
125
+ Returns:
126
+ BuildResult containing success status and output information.
127
+ """
128
+ go_executable = self._get_executable_path()
129
+
130
+ # Validate directories
131
+ source_dir = self._validate_directory(source_dir, create_if_not_exists=False)
132
+ output_dir = self._validate_directory(output_dir, create_if_not_exists=True)
133
+
134
+ # Prepare environment
135
+ env = environment.copy() if environment else {}
136
+
137
+ if mirror_enabled:
138
+ mirror_key = self._mirror or "go"
139
+ env = apply_mirror_environment(mirror_key, env)
140
+
141
+ # Check for go.mod
142
+ go_mod = source_dir / "go.mod"
143
+ has_go_mod = go_mod.exists()
144
+
145
+ # Download dependencies if go.mod exists
146
+ if has_go_mod:
147
+ deps_result = self._run_build(
148
+ [go_executable, "mod", "download"],
149
+ source_dir,
150
+ output_dir,
151
+ environment=env,
152
+ stream_output=stream_output,
153
+ )
154
+
155
+ if not deps_result["success"]:
156
+ return deps_result
157
+
158
+ # Build the project
159
+ build_args = [go_executable, "build", "-o", str(output_dir)]
160
+
161
+ if extra_args:
162
+ build_args.extend(extra_args)
163
+
164
+ return self._run_build(
165
+ build_args,
166
+ source_dir,
167
+ output_dir,
168
+ environment=env,
169
+ stream_output=stream_output,
170
+ )
171
+
172
+ def build_binary(
173
+ self,
174
+ source_dir: Path,
175
+ output_path: Path,
176
+ *,
177
+ target: str | Path | None = None,
178
+ environment: dict[str, str] | None = None,
179
+ mirror_enabled: bool = True,
180
+ ldflags: str | None = None,
181
+ tags: list[str] | None = None,
182
+ stream_output: bool = True,
183
+ ) -> BuildResult:
184
+ """Build a specific Go binary.
185
+
186
+ Args:
187
+ source_dir: Source code directory (working directory for build)
188
+ output_path: Output path for the binary
189
+ target: Build target - can be a .go file (e.g., "server.go", "cmd/server/main.go")
190
+ or a directory (e.g., "./cmd/server"). If None, builds current directory.
191
+ environment: Additional environment variables
192
+ mirror_enabled: Whether to use Go proxy mirror
193
+ ldflags: Linker flags to pass to the compiler
194
+ tags: Build tags to pass to the compiler
195
+ stream_output: Whether to stream output in real-time (default: True)
196
+
197
+ Returns:
198
+ BuildResult containing success status and output information.
199
+ """
200
+ go_executable = self._get_executable_path()
201
+
202
+ source_dir = self._validate_directory(source_dir, create_if_not_exists=False)
203
+
204
+ # Ensure output directory exists
205
+ output_path.parent.mkdir(parents=True, exist_ok=True)
206
+
207
+ env = environment.copy() if environment else {}
208
+
209
+ if mirror_enabled:
210
+ mirror_key = self._mirror or "go"
211
+ env = apply_mirror_environment(mirror_key, env)
212
+
213
+ build_args = [go_executable, "build", "-o", str(output_path)]
214
+
215
+ if ldflags:
216
+ build_args.extend(["-ldflags", ldflags])
217
+
218
+ if tags:
219
+ build_args.extend(["-tags", ",".join(tags)])
220
+
221
+ # Add target if specified (target is relative to source_dir)
222
+ if target:
223
+ build_args.append(str(target))
224
+
225
+ return self._run_build(
226
+ build_args,
227
+ source_dir,
228
+ output_path.parent,
229
+ environment=env,
230
+ stream_output=stream_output,
231
+ )
232
+
233
+ def build_all(
234
+ self,
235
+ source_dir: Path,
236
+ output_dir: Path,
237
+ *,
238
+ environment: dict[str, str] | None = None,
239
+ mirror_enabled: bool = True,
240
+ platform: str | None = None,
241
+ stream_output: bool = True,
242
+ ) -> BuildResult:
243
+ """Build all binaries defined in a Makefile or build script.
244
+
245
+ Args:
246
+ source_dir: Source code directory
247
+ output_dir: Output directory for all binaries
248
+ environment: Additional environment variables
249
+ mirror_enabled: Whether to use Go proxy mirror
250
+ platform: Target platform (e.g., "linux/amd64", "windows/amd64")
251
+ stream_output: Whether to stream output in real-time (default: True)
252
+
253
+ Returns:
254
+ BuildResult containing success status and output information.
255
+ """
256
+ go_executable = self._get_executable_path()
257
+
258
+ source_dir = self._validate_directory(source_dir, create_if_not_exists=False)
259
+ output_dir = self._validate_directory(output_dir, create_if_not_exists=True)
260
+
261
+ env = environment.copy() if environment else {}
262
+
263
+ if mirror_enabled:
264
+ mirror_key = self._mirror or "go"
265
+ env = apply_mirror_environment(mirror_key, env)
266
+
267
+ build_args = [go_executable, "build", "-o", str(output_dir), "./..."]
268
+
269
+ if platform:
270
+ env["GOOS"], env["GOARCH"] = platform.split("/")
271
+
272
+ return self._run_build(
273
+ build_args,
274
+ source_dir,
275
+ output_dir,
276
+ environment=env,
277
+ stream_output=stream_output,
278
+ )
279
+
280
+ def run_tests(
281
+ self,
282
+ source_dir: Path,
283
+ *,
284
+ environment: dict[str, str] | None = None,
285
+ mirror_enabled: bool = True,
286
+ verbose: bool = True,
287
+ race: bool = False,
288
+ stream_output: bool = True,
289
+ ) -> BuildResult:
290
+ """Run Go tests.
291
+
292
+ Args:
293
+ source_dir: Source code directory
294
+ environment: Additional environment variables
295
+ mirror_enabled: Whether to use Go proxy mirror
296
+ verbose: Enable verbose test output
297
+ race: Enable race detector
298
+ stream_output: Whether to stream output in real-time (default: True)
299
+
300
+ Returns:
301
+ BuildResult containing success status and output information.
302
+ """
303
+ go_executable = self._get_executable_path()
304
+
305
+ source_dir = self._validate_directory(source_dir, create_if_not_exists=False)
306
+
307
+ env = environment.copy() if environment else {}
308
+
309
+ if mirror_enabled:
310
+ mirror_key = self._mirror or "go"
311
+ env = apply_mirror_environment(mirror_key, env)
312
+
313
+ test_args = [go_executable, "test"]
314
+
315
+ if verbose:
316
+ test_args.append("-v")
317
+
318
+ if race:
319
+ test_args.append("-race")
320
+
321
+ return self._run_build(
322
+ test_args,
323
+ source_dir,
324
+ source_dir,
325
+ environment=env,
326
+ stream_output=stream_output,
327
+ )
328
+
329
+ def tidy_modules(
330
+ self,
331
+ source_dir: Path,
332
+ *,
333
+ environment: dict[str, str] | None = None,
334
+ mirror_enabled: bool = True,
335
+ stream_output: bool = True,
336
+ ) -> BuildResult:
337
+ """Run go mod tidy to clean up dependencies.
338
+
339
+ Args:
340
+ source_dir: Source code directory
341
+ environment: Additional environment variables
342
+ mirror_enabled: Whether to use Go proxy mirror
343
+ stream_output: Whether to stream output in real-time (default: True)
344
+
345
+ Returns:
346
+ BuildResult containing success status and output information.
347
+ """
348
+ go_executable = self._get_executable_path()
349
+
350
+ source_dir = self._validate_directory(source_dir, create_if_not_exists=False)
351
+
352
+ env = environment.copy() if environment else {}
353
+
354
+ if mirror_enabled:
355
+ mirror_key = self._mirror or "go"
356
+ env = apply_mirror_environment(mirror_key, env)
357
+
358
+ return self._run_build(
359
+ [go_executable, "mod", "tidy"],
360
+ source_dir,
361
+ source_dir,
362
+ environment=env,
363
+ stream_output=stream_output,
364
+ )
365
+
366
+ def clean(self, directory: Path) -> bool:
367
+ """Clean Go build artifacts in the specified directory.
368
+
369
+ Args:
370
+ directory: Directory to clean
371
+
372
+ Returns:
373
+ True if successful, False otherwise.
374
+ """
375
+ import shutil
376
+
377
+ try:
378
+ directory = self._validate_directory(directory, create_if_not_exists=False)
379
+
380
+ # Remove go.sum
381
+ go_sum = directory / "go.sum"
382
+ if go_sum.exists():
383
+ go_sum.unlink()
384
+
385
+ # Remove vendor directory
386
+ vendor = directory / "vendor"
387
+ if vendor.exists():
388
+ shutil.rmtree(vendor)
389
+
390
+ # Remove bin directory
391
+ bin_dir = directory / "bin"
392
+ if bin_dir.exists():
393
+ shutil.rmtree(bin_dir)
394
+
395
+ return True
396
+
397
+ except Exception:
398
+ return False
399
+
400
+ def _get_executable_path(self) -> str:
401
+ """Get the go executable path."""
402
+ if self._go_path:
403
+ return self._go_path
404
+
405
+ go_path = shutil.which("go")
406
+ if go_path is None:
407
+ raise RuntimeError("go not found in PATH. Please install Go or provide go_path.")
408
+
409
+ return go_path
410
+
411
+ @staticmethod
412
+ def create(source_dir: Path, *, mirror_enabled: bool = True) -> "GoCompiler":
413
+ """Factory method to create a GoCompiler instance.
414
+
415
+ Args:
416
+ source_dir: Source directory for the project
417
+ mirror_enabled: Whether to enable mirror acceleration by default
418
+
419
+ Returns:
420
+ Configured GoCompiler instance
421
+ """
422
+ compiler = GoCompiler()
423
+ return compiler
424
+
425
+
426
+ def main() -> None:
427
+ """Go compiler CLI entry point."""
428
+ import argparse
429
+ import sys
430
+
431
+ parser = argparse.ArgumentParser(description="Go Build Compiler")
432
+ parser.add_argument("source_dir", type=Path, help="Source directory (working directory)")
433
+ parser.add_argument("-o", "--output", type=Path, required=True, help="Output binary path")
434
+ parser.add_argument("-t", "--target", type=str, help="Build target: .go file (e.g., server.go, cmd/main.go) or directory (e.g., ./cmd/server)")
435
+ parser.add_argument("--mirror", action="store_true", default=True, help="Enable Go proxy mirror")
436
+ parser.add_argument("--no-mirror", dest="mirror", action="store_false", help="Disable Go proxy mirror")
437
+ parser.add_argument("--ldflags", type=str, help="Linker flags")
438
+ parser.add_argument("--tags", type=str, help="Build tags (comma-separated)")
439
+ parser.add_argument("--test", action="store_true", help="Run tests instead of building")
440
+ parser.add_argument("--tidy", action="store_true", help="Run go mod tidy")
441
+ parser.add_argument("--all", action="store_true", help="Build all packages")
442
+
443
+ args = parser.parse_args()
444
+
445
+ compiler = GoCompiler()
446
+
447
+ if args.test:
448
+ result = compiler.run_tests(args.source_dir, mirror_enabled=args.mirror)
449
+ elif args.tidy:
450
+ result = compiler.tidy_modules(args.source_dir, mirror_enabled=args.mirror)
451
+ elif args.all:
452
+ result = compiler.build_all(
453
+ args.source_dir,
454
+ args.output,
455
+ mirror_enabled=args.mirror,
456
+ )
457
+ else:
458
+ tags = args.tags.split(",") if args.tags else None
459
+ result = compiler.build_binary(
460
+ args.source_dir,
461
+ args.output,
462
+ target=args.target,
463
+ mirror_enabled=args.mirror,
464
+ ldflags=args.ldflags,
465
+ tags=tags,
466
+ )
467
+
468
+ sys.exit(0 if result["success"] else 1)