Run 'cleanup' subcommand when 'compile' subcommand encounters an error

This commit is contained in:
2025-10-06 20:10:20 +03:00
parent 1d858076fd
commit abc6674b97
+43 -27
View File
@@ -1045,9 +1045,39 @@ func compilePackage() {
} }
} }
// Setup cleanup function
cleanupFunc := func() {
if installSrcPkgDepends && len(unmetDepends) > 0 {
// Get path to current executable
executable, err := os.Executable()
if err != nil {
log.Printf("Warning: could not get path to executable: %s\n", err)
}
// Run 'bpm cleanup' using the set privilege escalator command
cmd := exec.Command(bpmlib.CompilationBPMConfig.PrivilegeEscalatorCmd, executable, "cleanup")
if yesAll {
cmd.Args = slices.Insert(cmd.Args, 3, "-y")
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if verbose {
fmt.Println("Running command: " + cmd.String())
}
err = cmd.Run()
if err != nil {
log.Printf("Warning: dependency cleanup command failed: %s\n", err)
}
}
}
// Get current working directory // Get current working directory
workdir, err := os.Getwd() workdir, err := os.Getwd()
if err != nil { if err != nil {
// Remove unused packages
cleanupFunc()
log.Printf("Error: could not get working directory: %s", err) log.Printf("Error: could not get working directory: %s", err)
exitCode = 1 exitCode = 1
return return
@@ -1056,6 +1086,9 @@ func compilePackage() {
// Get user home directory // Get user home directory
homedir, err := os.UserHomeDir() homedir, err := os.UserHomeDir()
if err != nil { if err != nil {
// Remove unused packages
cleanupFunc()
log.Printf("Error: could not get user home directory: %s", err) log.Printf("Error: could not get user home directory: %s", err)
exitCode = 1 exitCode = 1
return return
@@ -1088,11 +1121,17 @@ func compilePackage() {
// Ensure output directory exists and is a directory // Ensure output directory exists and is a directory
stat, err := os.Stat(outputDirectory) stat, err := os.Stat(outputDirectory)
if err != nil { if err != nil {
// Remove unused packages
cleanupFunc()
log.Printf("Error: could not stat output directory (%s): %s", outputDirectory, err) log.Printf("Error: could not stat output directory (%s): %s", outputDirectory, err)
exitCode = 1 exitCode = 1
return return
} }
if !stat.IsDir() { if !stat.IsDir() {
// Remove unused packages
cleanupFunc()
log.Printf("Error: output directory (%s) is not a directory", outputDirectory) log.Printf("Error: output directory (%s) is not a directory", outputDirectory)
exitCode = 1 exitCode = 1
return return
@@ -1100,6 +1139,9 @@ func compilePackage() {
outputBpmPackages, err := bpmlib.CompileSourcePackage(sourcePackage, outputDirectory, skipChecks) outputBpmPackages, err := bpmlib.CompileSourcePackage(sourcePackage, outputDirectory, skipChecks)
if err != nil { if err != nil {
// Remove unused packages
cleanupFunc()
log.Printf("Error: could not compile source package (%s): %s", sourcePackage, err) log.Printf("Error: could not compile source package (%s): %s", sourcePackage, err)
exitCode = 1 exitCode = 1
return return
@@ -1120,33 +1162,7 @@ func compilePackage() {
} }
// Remove unused packages // Remove unused packages
if installSrcPkgDepends && len(unmetDepends) > 0 { cleanupFunc()
// Get path to current executable
executable, err := os.Executable()
if err != nil {
log.Printf("Error: could not get path to executable: %s\n", err)
exitCode = 1
return
}
// Run 'bpm cleanup' using the set privilege escalator command
cmd := exec.Command(bpmlib.CompilationBPMConfig.PrivilegeEscalatorCmd, executable, "cleanup")
if yesAll {
cmd.Args = slices.Insert(cmd.Args, 3, "-y")
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if verbose {
fmt.Println("Running command: " + cmd.String())
}
err = cmd.Run()
if err != nil {
log.Printf("Error: dependency cleanup command failed: %s\n", err)
exitCode = 1
return
}
}
} }
} }