Builder/main.go

284 lines
9 KiB
Go
Raw Permalink Normal View History

2024-09-08 23:25:07 +00:00
package main
import (
2024-09-10 21:02:25 +00:00
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime" // for detecting system architecture and platform
"spitfire/spitfire"
"time"
//"errors"
2024-09-08 23:25:07 +00:00
)
var (
2024-09-10 21:02:25 +00:00
// Define all flags as package-level variables
all bool
buildFlag bool
clean bool
update bool
patches bool
run bool
compress bool
buildPath string
target string
version string
component string
arch string
release string
platform string
upload bool
uploadPath string
sourceRepo = "https://hg.mozilla.org/mozilla-central"
patchesRepo = "https://weforgecode.xyz/Spitfire/Browser.git"
url = "https://spitfirebrowser.com/"
licence = "AGPL-3.0"
name = "Spitfire"
maintainer = "Internet Addict"
initialDir string
2024-09-08 23:25:07 +00:00
)
func init() {
2024-09-10 21:02:25 +00:00
flag.StringVar(&buildPath, "p", "", "Path to the build directory")
flag.StringVar(&target, "t", "", "Target location format: component-arch-release-platform")
flag.BoolVar(&compress, "c", false, "Compress the build directory into a tar.gz file before uploading")
flag.StringVar(&version, "v", "", "Specify version for the package. For nightly, use current date if not specified.")
flag.StringVar(&component, "component", "browser", "Component name (default: browser)")
flag.StringVar(&arch, "arch", runtime.GOARCH, "Architecture (default: system architecture)")
flag.StringVar(&release, "release", "nightly", "Release type (default: nightly)")
flag.StringVar(&platform, "platform", runtime.GOOS, "Platform (default: system platform)")
flag.BoolVar(&all, "a", false, "Perform all steps (build, clean, update)")
flag.BoolVar(&buildFlag, "b", false, "Build Spitfire")
flag.BoolVar(&clean, "clean", false, "Clean build")
flag.BoolVar(&update, "u", false, "Update Mozilla repository")
flag.BoolVar(&patches, "patches", false, "Update patches")
flag.BoolVar(&run, "r", false, "Run the project after build")
flag.BoolVar(&upload, "upload", false, "Upload the compressed build file to SourceForge")
flag.StringVar(&uploadPath, "upload-path", "", "Path to the file to upload if no build present")
flag.Bool("h", false, "Display help message")
2024-09-08 23:25:07 +00:00
}
func printHelp() {
2024-09-10 21:09:24 +00:00
fmt.Println("Usage: go run . -p=<path-to-build> -t=<target> [-c|--compress] [-v|--version=<version>] [-component=<component>] [-arch=<architecture>] [-release=<release>] [-platform=<platform>]")
2024-09-10 21:02:25 +00:00
flag.PrintDefaults()
fmt.Println("Example: go run . --upload -c --upload-path=./mozilla-central/obj-x86_64-pc-linux-gnu/dist/bin -a")
os.Exit(0)
2024-09-08 23:25:07 +00:00
}
func main() {
2024-09-13 10:47:30 +00:00
// // Check system dependencies
// err := spitfire.CheckSystemDependencies()
// if err != nil {
// log.Fatalf("System check failed: %v", err)
// }
2024-09-10 21:30:34 +00:00
2024-09-10 21:02:25 +00:00
flag.Parse()
if flag.Lookup("h").Value.(flag.Getter).Get().(bool) {
printHelp()
}
// Set version to current date if it's empty and release is nightly
if version == "" && release == "nightly" {
version = time.Now().Format("2006.01.02") // Set version to current date if nightly
}
// Save the initial directory
2024-09-13 10:47:30 +00:00
var err2 error
initialDir, err2 = os.Getwd()
if err2 != nil {
log.Fatalf("Failed to get current working directory: %v", err2)
2024-09-10 21:02:25 +00:00
}
fmt.Printf("Initial working directory: %s\n", initialDir)
// Convert buildPath and uploadPath to absolute paths
if buildPath != "" {
2024-09-13 10:47:30 +00:00
buildPath, err2 = spitfire.ResolvePath(buildPath)
if err2 != nil {
log.Fatalf("Failed to convert buildPath to absolute path: %v", err2)
2024-09-10 21:02:25 +00:00
}
fmt.Printf("Resolved buildPath: %s\n", buildPath)
}
if uploadPath != "" {
2024-09-13 10:47:30 +00:00
uploadPath, err2 = spitfire.ResolvePath(uploadPath)
if err2 != nil {
log.Fatalf("Failed to convert uploadPath to absolute path: %v", err2)
2024-09-10 21:02:25 +00:00
}
fmt.Printf("Resolved uploadPath: %s\n", uploadPath)
}
if all || buildFlag {
BuildProcess()
}
if compress || upload {
PackageAndUploadProcess()
}
spitfire.PrintErrors()
2024-09-08 23:25:07 +00:00
}
// BuildProcess handles the build process: downloading, cleaning, configuring, and building the project.
func BuildProcess() {
2024-09-10 21:02:25 +00:00
sourcePath, err := spitfire.ResolvePath("./mozilla-central")
if err != nil {
log.Fatalf("Error resolving source path: %v", err)
}
patchesDir, err := spitfire.ResolvePath(filepath.Join(sourcePath, "Spitfire"))
if err != nil {
log.Fatalf("Error resolving patches directory: %v", err)
}
if all {
spitfire.DownloadSource(sourcePath, sourceRepo)
spitfire.DiscardChanges(sourcePath)
spitfire.CleanBuild(sourcePath)
spitfire.UpdateRepo(sourcePath)
spitfire.UpdatePatches(patchesDir, patchesRepo, sourcePath)
spitfire.Configure(sourcePath)
spitfire.Build(sourcePath)
if run {
spitfire.RunProject(sourcePath)
}
fmt.Println("Spitfire build completed successfully.")
} else if clean {
spitfire.CleanBuild(sourcePath)
fmt.Println("Cleaned Firefox build.")
} else if update {
spitfire.DownloadSource(sourcePath, sourceRepo)
spitfire.UpdateRepo(sourcePath)
fmt.Println("Mozilla repository updated.")
} else if patches {
spitfire.DownloadSource(sourcePath, sourceRepo)
spitfire.UpdatePatches(patchesDir, patchesRepo, sourcePath)
fmt.Println("Patches updated.")
} else if buildFlag {
spitfire.Configure(sourcePath)
spitfire.Build(sourcePath)
if run {
spitfire.RunProject(sourcePath)
}
fmt.Println("Spitfire build completed successfully.")
}
2024-09-08 23:25:07 +00:00
}
// PackageAndUploadProcess handles compressing, packaging, and uploading the build to SourceForge.
func PackageAndUploadProcess() {
2024-09-10 21:02:25 +00:00
// Restore working directory before performing SourceForge operations
restoreWorkingDirectory()
pathToUse := buildPath
if upload && uploadPath != "" {
pathToUse = uploadPath
}
if pathToUse == "" {
log.Fatalf("Error: no valid build or upload path provided.")
}
// // This is stupid, it wait for the path to exist (up to a maximum wait time)
// err := waitForPath(pathToUse, 60, 5) // Max 60 seconds, checking every 5 seconds
// if err != nil {
// log.Fatalf("Error: Build path or upload path not found: %v", err)
// }
uncompressedSize, err := spitfire.GetDirectorySize(pathToUse)
if err != nil {
log.Fatalf("Failed to calculate uncompressed size: %v", err)
}
fmt.Printf("Uncompressed directory size: %d bytes\n", uncompressedSize)
outputCompressedFile := filepath.Join(".", fmt.Sprintf("%s-%s-%s-%s.tar.gz", component, arch, release, platform))
if compress {
err := spitfire.CompressDirectory(pathToUse, outputCompressedFile)
if err != nil {
log.Fatalf("Failed to compress build directory: %v", err)
}
fmt.Printf("Build directory compressed to: %s\n", outputCompressedFile)
}
compressedSize, err := spitfire.GetFileSize(outputCompressedFile)
if err != nil {
log.Fatalf("Failed to get compressed file size: %v", err)
}
fmt.Printf("Compressed file size: %d bytes\n", compressedSize)
if upload {
config, err := spitfire.LoadConfig()
if err != nil {
log.Fatalf("Failed to load SourceForge config: %v", err)
}
if _, err := os.Stat(outputCompressedFile); err == nil {
err = spitfire.Upload(config, outputCompressedFile, "/home/frs/project/spitfire-browser/"+component+"/"+arch+"/"+release+"/"+version+"/")
if err != nil {
log.Fatalf("Failed to upload compressed file: %v", err)
}
fmt.Println("Compressed file uploaded successfully.")
} else {
log.Fatalf("No compressed file found to upload.")
}
err = spitfire.DownloadAPPINDEX(config, "/home/frs/project/spitfire-browser/")
if err != nil {
fmt.Println("Failed to download APPINDEX. A new APPINDEX will be created and uploaded.")
}
err = spitfire.PackageAPPINDEX(
name, release, version, arch,
fmt.Sprintf("%d", compressedSize),
fmt.Sprintf("%d", uncompressedSize),
"Spitfire build", url, licence, component, maintainer, "", platform,
)
if err != nil {
log.Fatalf("Failed to update APPINDEX: %v", err)
}
fmt.Println("APPINDEX updated successfully.")
if err := spitfire.CleanAppIndex(); err != nil {
log.Fatalf("Failed to clean APPINDEX: %v", err)
}
err = spitfire.UploadAPPINDEX(config, "/home/frs/project/spitfire-browser/")
if err != nil {
log.Fatalf("Failed to upload updated APPINDEX: %v", err)
}
fmt.Println("APPINDEX uploaded successfully.")
}
2024-09-08 23:25:07 +00:00
}
// // waitForPath checks if a path exists, waiting for up to maxWait seconds and retrying every interval seconds.
// func waitForPath(path string, maxWait int, interval int) error {
// waited := 0
// for {
// if PathExists(path) {
// return nil // Path exists
// }
// if waited >= maxWait {
// return errors.New("path does not exist after waiting")
// }
// fmt.Printf("Waiting for path %s to exist...\n", path)
// time.Sleep(time.Duration(interval) * time.Second)
// waited += interval
// }
// }
// // PathExists checks if the path exists
// func PathExists(path string) bool {
// _, err := os.Stat(path)
// return !os.IsNotExist(err)
// }
// restoreWorkingDirectory restores the initial working directory after any operation that might change it.
func restoreWorkingDirectory() {
2024-09-10 21:02:25 +00:00
err := os.Chdir(initialDir)
if err != nil {
log.Fatalf("Failed to restore the working directory: %v", err)
}
fmt.Printf("Restored working directory to: %s\n", initialDir)
}