refactor and more error messages

This commit is contained in:
Timon Ringwald 2022-08-17 19:18:57 +02:00
parent 003af32f46
commit 29c357bd24
4 changed files with 95 additions and 43 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
output

View File

@ -3,15 +3,23 @@ package main
import (
"fmt"
"os/exec"
"path/filepath"
"sync"
)
func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
filename := fmt.Sprintf("%s_%s_%s%s", ProjectName, cfg.OS, cfg.Arch, cfg.FileExt)
filePath := filepath.Join(*OutputDir, fmt.Sprintf("%s_%s_%s%s", ProjectName, cfg.OS, cfg.Arch, cfg.FileExt))
filePath, err := filepath.Abs(filePath)
if err != nil {
ch <- &CompileReport{Config: cfg, State: StateCompileError}
wg.Done()
return
}
ch <- &CompileReport{Config: cfg, State: StateCompiling}
compileCmd := exec.Command("go", "build", "-o", filename)
compileCmd := exec.Command("go", "build", "-o", filePath)
compileCmd.Dir = ModulePath
if err := compileCmd.Start(); err != nil {
@ -24,24 +32,23 @@ func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
wg.Done()
return
}
Compress(filename, cfg, ch, wg)
Compress(filePath, cfg, ch, wg)
// uncomment for independent compile and compress tasks
// (slightly slower in tests, most likely because of more context switches)
/*
ch <- &CompileReport{Config: cfg, State: StateWaiting}
go Runner.Run(func() { Compress(filename, cfg, ch, wg) })
go Runner.Run(func() { Compress(filePath, cfg, ch, wg) })
*/
}
func Compress(filename string, cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
func Compress(filePath string, cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
defer wg.Done()
ch <- &CompileReport{Config: cfg, State: StateCompressing}
if !*NoCompress && cfg.Compress {
compressCmd := exec.Command("upx", "--best", filename)
compressCmd := exec.Command("upx", "--best", filePath)
compressCmd.Dir = ModulePath
if err := compressCmd.Start(); err != nil {

65
init.go Normal file
View File

@ -0,0 +1,65 @@
package main
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
)
func Init() {
flag.Parse()
var err error
ModulePath, err = filepath.Abs(flag.Arg(0))
if err != nil {
ColorError.Fprintln(os.Stderr, "determining module path failed")
os.Exit(1)
}
*OutputDir, err = filepath.Abs(*OutputDir)
if err != nil {
ColorError.Fprintln(os.Stderr, "determining output path failed")
os.Exit(1)
}
if _, err := exec.LookPath("go"); err != nil {
ColorError.Fprintln(os.Stderr, "go not found in PATH. compilation not possible")
os.Exit(1)
}
if err := FillCompileConfigs(); err != nil {
ColorError.Fprintln(os.Stderr, fmt.Errorf("target architectures could not be determined: %w", err))
os.Exit(1)
}
if _, err := os.Stat(filepath.Join(ModulePath, "go.mod")); errors.Is(err, os.ErrNotExist) {
ColorError.Fprintf(os.Stderr, "no Go module found at '%s'\n", ModulePath)
os.Exit(1)
}
if err := DetermineProjectName(); err != nil {
ColorError.Fprintln(os.Stderr, fmt.Errorf("project name could not be determined: %w", err))
os.Exit(1)
}
if err := os.MkdirAll(*OutputDir, 0744); err != nil {
ColorError.Fprintln(os.Stderr, fmt.Errorf("output folder '%s' could not be made: %w", *OutputDir, err))
os.Exit(1)
}
if err := CheckDirWritable(*OutputDir); err != nil {
ColorError.Fprintf(os.Stderr, "output folder '%s' has insufficient permissions\n", *OutputDir)
os.Exit(1)
}
if !*NoCompress {
if _, err := exec.LookPath("upx"); err != nil {
ColorWarn.Fprintln(os.Stderr, "upx not found in PATH. file compression not possible")
*NoCompress = true
}
}
}

51
main.go
View File

@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
@ -28,46 +27,14 @@ var (
OS = flag.String("os", "", "comma-separated list of operating systems to compile for (empty for all)")
Arch = flag.String("arch", "", "comma-separated list of architectures to compile for (empty for all)")
OutputDir = flag.String("o", "output", "output directory")
Silent = flag.Bool("s", false, "silent mode (no output)")
NoCompress = flag.Bool("c", false, "dont compress any executables")
NumThreads = flag.Int("t", runtime.NumCPU(), "amount of threads (0 = infinite)")
)
func main() {
var err error
flag.Parse()
ModulePath, err = filepath.Abs(flag.Arg(0))
if err != nil {
panic(err)
}
if _, err := exec.LookPath("go"); err != nil {
ColorError.Fprintln(os.Stderr, "go not found in PATH. compilation not possible")
return
}
if !*NoCompress {
if _, err := exec.LookPath("upx"); err != nil {
ColorWarn.Fprintln(os.Stderr, "upx not found in PATH. file compression not possible")
*NoCompress = true
}
}
if err := FillCompileConfigs(); err != nil {
ColorError.Fprintln(os.Stderr, fmt.Errorf("target architectures could not be determined: %w", err))
return
}
if _, err := os.Stat(filepath.Join(ModulePath, "go.mod")); errors.Is(err, os.ErrNotExist) {
ColorError.Fprintf(os.Stderr, "no Go module found at '%s'\n", ModulePath)
return
}
if err := DetermineProjectName(); err != nil {
ColorError.Fprintln(os.Stderr, fmt.Errorf("project name could not be determined: %w", err))
return
}
Init()
ch := make(chan *CompileReport, len(CompileConfigs))
wg := new(sync.WaitGroup)
@ -90,7 +57,7 @@ func main() {
end := time.Now()
<-doneCh // wait for Watch
<-doneCh // wait for Watch routine
if !*Silent {
fmt.Printf("compilation took %s (using %s threads)\n", end.Sub(start), GetThreadCountString())
@ -129,3 +96,15 @@ func GetThreadCountString() string {
}
return strconv.Itoa(*NumThreads)
}
func CheckDirWritable(dir string) error {
path := filepath.Join(*OutputDir, ".gocc")
file, err := os.Create(path)
if err != nil {
return err
}
file.Close()
os.Remove(path)
return nil
}