-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add "run" command to invoke a compiler
this commit adds a "run" command to zvm that allows you to invoke a zig compiler via its version name. for example: `zvm run 0.14.0 build run --help` which is useful if you switch around projects a lot and just want to run a single command without symlinking the version first.
- Loading branch information
Showing
4 changed files
with
136 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2022 Tristan Isham. All rights reserved. | ||
// Use of this source code is governed by the MIT | ||
// license that can be found in the LICENSE file. | ||
package meta | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// Execute the given Zig command with a specified compiler | ||
func Exec(bin string, cmd []string) error { | ||
// zvm run 0.14.0 build run --help | ||
if bin == "" { | ||
return fmt.Errorf("compiler binary cannot be empty") | ||
} | ||
|
||
zig := exec.Command(bin, cmd...) | ||
zig.Stdin, zig.Stdout, zig.Stderr = os.Stdin, os.Stdout, os.Stderr | ||
|
||
err := zig.Run() | ||
if err != nil { | ||
if err, ok := err.(*exec.ExitError); ok { | ||
os.Exit(err.ExitCode()) | ||
} else { | ||
return fmt.Errorf("Error executing command '%s': %s\n", cmd, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Execute the given Zig command with a specified compiler | ||
// This is a convenience function that will automatically split a | ||
// command and execute it | ||
func ExecString(cmd string) error { | ||
command := strings.Split(cmd, " ") | ||
if len(command) < 1 { | ||
return fmt.Errorf("No command given") | ||
} | ||
|
||
zig := exec.Command(command[0], command[1:]...) | ||
zig.Stdin, zig.Stdout, zig.Stderr = os.Stdin, os.Stdout, os.Stderr | ||
|
||
err := zig.Run() | ||
if err != nil { | ||
return fmt.Errorf("Error executing command '%s': %s\n", cmd, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2022 Tristan Isham. All rights reserved. | ||
// Use of this source code is governed by the MIT | ||
// license that can be found in the LICENSE file. | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/tristanisham/zvm/cli/meta" | ||
) | ||
|
||
// Run the given Zig compiler with the provided arguments | ||
func (z *ZVM) Run(ver string, cmd []string) error { | ||
if err := z.getVersion(ver); err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
|
||
fmt.Printf("It looks like %s isn't installed. Would you like to install it first? [y/n]\n", ver) | ||
|
||
if getConfirmation() { | ||
if err = z.Install(ver, false); err != nil { | ||
return err | ||
} | ||
} else { | ||
return fmt.Errorf("version %s is not installed", ver) | ||
} | ||
} | ||
} | ||
|
||
return z.runBin(ver, cmd) | ||
} | ||
|
||
func (z *ZVM) runBin(ver string, cmd []string) error { | ||
// $ZVM_PATH/$VERSION/zig cmd | ||
bin := filepath.Join(z.baseDir, ver, "zig") | ||
|
||
// Skip symlink checks, does this Zig binary exist? | ||
stat, err := os.Stat(bin) | ||
if err != nil { | ||
return fmt.Errorf("%w: %s", err, stat.Name()) | ||
} | ||
|
||
// the logging here really muddies up the output of the Zig compiler | ||
// and adds a lot of noise. For that reason this function exits with | ||
// the zig compilers exit code | ||
if err := meta.Exec(bin, cmd); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters