windows patch

This commit is contained in:
partisan 2024-09-10 23:02:25 +02:00
parent f2f704bccf
commit f9cc59e40a

399
main.go
View file

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