From 540d35aa9bfde0e9b378719db86c9d4eac04313c Mon Sep 17 00:00:00 2001 From: ANKDDEV Date: Fri, 20 Dec 2024 06:51:21 +0300 Subject: [PATCH 1/3] chore: update spinners text --- scripts/build.go | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/scripts/build.go b/scripts/build.go index 65dcee0..c6f3bf1 100644 --- a/scripts/build.go +++ b/scripts/build.go @@ -19,11 +19,10 @@ func main() { if !isWindows() { os.Exit(1) } - - s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) - + + s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) s.Suffix = " [1/4] Building..." - s.FinalMSG = "[3/4] Building...\n" + s.FinalMSG = "[1/4] Build successful\n" s.HideCursor = true s.Start() ldflags := os.Getenv("GO_LDFLAGS") @@ -34,7 +33,7 @@ func main() { fmt.Println("Build failed:", err) os.Exit(1) } - s.Stop() + s.Stop() // Get current directory currentDir, err := os.Getwd() @@ -82,8 +81,9 @@ func main() { "bin": binDir, } + s = spinner.New(spinner.CharSets[14], 100*time.Millisecond) s.Suffix = " [2/4] Adding directories..." - s.FinalMSG = "[2/4] Adding directories...\n" + s.FinalMSG = "[2/4] Directories added\n" s.HideCursor = true s.Start() for zipPath, fsPath := range dirsToAdd { @@ -97,7 +97,7 @@ func main() { // Add individual files s.Suffix = " [3/4] Adding files..." - s.FinalMSG = "[3/4] Adding files...\n" + s.FinalMSG = "[3/4] Files added\n" s.HideCursor = true s.Start() filesToAdd := map[string]string{ @@ -208,15 +208,3 @@ func run(args ...string) error { cmd.Stderr = os.Stderr return cmd.Run() } - -func shellInspect(args []string) string { - fmtArgs := make([]string, len(args)) - for i, arg := range args { - if strings.ContainsAny(arg, " \t'\"") { - fmtArgs[i] = fmt.Sprintf("%q", arg) - } else { - fmtArgs[i] = arg - } - } - return strings.Join(fmtArgs, " ") -} From e9a9b520de56e478928d618b99e53fd457d34a01 Mon Sep 17 00:00:00 2001 From: ANKDDEV Date: Fri, 20 Dec 2024 06:54:41 +0300 Subject: [PATCH 2/3] refactor: make build script more readable --- scripts/build.go | 179 +++++++++++++++++++++++++++++------------------ 1 file changed, 110 insertions(+), 69 deletions(-) diff --git a/scripts/build.go b/scripts/build.go index c6f3bf1..6aea32d 100644 --- a/scripts/build.go +++ b/scripts/build.go @@ -15,114 +15,155 @@ import ( "github.com/cli/safeexec" ) +type buildConfig struct { + currentDir string + buildDir string + binDir string + listsDir string + preConfigsDir string + zipPath string +} + func main() { if !isWindows() { os.Exit(1) } - s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) - s.Suffix = " [1/4] Building..." - s.FinalMSG = "[1/4] Build successful\n" - s.HideCursor = true - s.Start() - ldflags := os.Getenv("GO_LDFLAGS") - ldflags = fmt.Sprintf("-X main.version=%s %s", version(), ldflags) - _ = os.Mkdir("build", os.ModePerm) - err := run("go", "build", "-ldflags", ldflags, "-o", "build", "./cmd/...") + config, err := initBuildConfig() if err != nil { - fmt.Println("Build failed:", err) + fmt.Printf("Error initializing build config: %v\n", err) os.Exit(1) } - s.Stop() - // Get current directory + if err := runBuildSteps(config); err != nil { + fmt.Printf("Build failed: %v\n", err) + os.Exit(1) + } + + fmt.Printf("\nRelease build ready! Check '%s'\n", config.zipPath) + fmt.Println("Press Enter to continue...") + fmt.Scanln() +} + +func initBuildConfig() (*buildConfig, error) { currentDir, err := os.Getwd() if err != nil { - fmt.Printf("Error getting current directory: %v\n", err) - os.Exit(1) + return nil, fmt.Errorf("error getting current directory: %v", err) } - // Define paths buildDir := filepath.Join(currentDir, "build") - binDir := filepath.Join(currentDir, "bin") - listsDir := filepath.Join(currentDir, "lists") - preConfigsDir := filepath.Join(currentDir, "pre-configs") + return &buildConfig{ + currentDir: currentDir, + buildDir: buildDir, + binDir: filepath.Join(currentDir, "bin"), + listsDir: filepath.Join(currentDir, "lists"), + preConfigsDir: filepath.Join(currentDir, "pre-configs"), + zipPath: filepath.Join(buildDir, "zapret-discord-youtube-ankddev.zip"), + }, nil +} + +func runBuildSteps(config *buildConfig) error { + s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) + s.HideCursor = true - // Check required paths exist - requiredPaths := []string{ - buildDir, - listsDir, - preConfigsDir, + // Step 1: Build + s.Suffix = " [1/4] Building..." + s.FinalMSG = "[1/4] Build successful\n" + s.Start() + if err := buildExecutables(); err != nil { + return fmt.Errorf("build failed: %v", err) } + s.Stop() - for _, path := range requiredPaths { - if _, err := os.Stat(path); os.IsNotExist(err) { - fmt.Printf("Required directory not found: %s\n", path) - os.Exit(1) - } + // Step 2: Check paths and create zip + if err := validatePaths(config); err != nil { + return err } - // Create zip file - zipPath := filepath.Join(buildDir, "zapret-discord-youtube-ankddev.zip") - zipFile, err := os.Create(zipPath) + zipFile, zipWriter, err := createZipFile(config.zipPath) if err != nil { - fmt.Printf("Error creating zip file: %v\n", err) - os.Exit(1) + return err } defer zipFile.Close() - - zipWriter := zip.NewWriter(zipFile) defer zipWriter.Close() - // Add directories to zip - dirsToAdd := map[string]string{ - "lists": listsDir, - "pre-configs": preConfigsDir, - "bin": binDir, - } - - s = spinner.New(spinner.CharSets[14], 100*time.Millisecond) + // Step 3: Add directories s.Suffix = " [2/4] Adding directories..." s.FinalMSG = "[2/4] Directories added\n" - s.HideCursor = true s.Start() - for zipPath, fsPath := range dirsToAdd { - err = addDirToZip(zipWriter, zipPath, fsPath) - if err != nil { - fmt.Printf("Error adding directory %s to zip: %v\n", fsPath, err) - os.Exit(1) - } + if err := addDirectories(zipWriter, config); err != nil { + return err } s.Stop() - // Add individual files + // Step 4: Add files s.Suffix = " [3/4] Adding files..." s.FinalMSG = "[3/4] Files added\n" - s.HideCursor = true s.Start() - filesToAdd := map[string]string{ - "blockcheck.cmd": filepath.Join(currentDir, "blockcheck.cmd"), - "Add to autorun.exe": filepath.Join(buildDir, "add_to_autorun.exe"), - "Automatically search pre-config.exe": filepath.Join(buildDir, "preconfig_tester.exe"), - "Run pre-config.exe": filepath.Join(buildDir, "run_preconfig.exe"), - "Set domain list.exe": filepath.Join(buildDir, "select_domains.exe"), - "Check for updates.exe": filepath.Join(buildDir, "check_for_updates.exe"), + if err := addFiles(zipWriter, config); err != nil { + return err } + s.Stop() - for zipPath, fsPath := range filesToAdd { - err = addFileToZip(zipWriter, zipPath, fsPath) - if err != nil { - fmt.Printf("Error adding file %s to zip: %v\n", fsPath, err) - os.Exit(1) + fmt.Println("[4/4] Release archive created successfully!") + return nil +} + +func buildExecutables() error { + _ = os.Mkdir("build", os.ModePerm) + ldflags := fmt.Sprintf("-X main.version=%s %s", version(), os.Getenv("GO_LDFLAGS")) + return run("go", "build", "-ldflags", ldflags, "-o", "build", "./cmd/...") +} + +func validatePaths(config *buildConfig) error { + paths := []string{config.buildDir, config.listsDir, config.preConfigsDir} + for _, path := range paths { + if _, err := os.Stat(path); os.IsNotExist(err) { + return fmt.Errorf("required directory not found: %s", path) } } + return nil +} - s.Stop() +func createZipFile(zipPath string) (*os.File, *zip.Writer, error) { + zipFile, err := os.Create(zipPath) + if err != nil { + return nil, nil, fmt.Errorf("error creating zip file: %v", err) + } + return zipFile, zip.NewWriter(zipFile), nil +} - fmt.Println("[4/4] Release archive created successfully!") - fmt.Printf("\nRelease build ready! Check '%s'\n", zipPath) - fmt.Println("Press Enter to continue...") - fmt.Scanln() +func addDirectories(zipWriter *zip.Writer, config *buildConfig) error { + dirsToAdd := map[string]string{ + "lists": config.listsDir, + "pre-configs": config.preConfigsDir, + "bin": config.binDir, + } + + for zipPath, fsPath := range dirsToAdd { + if err := addDirToZip(zipWriter, zipPath, fsPath); err != nil { + return fmt.Errorf("error adding directory %s: %v", fsPath, err) + } + } + return nil +} + +func addFiles(zipWriter *zip.Writer, config *buildConfig) error { + filesToAdd := map[string]string{ + "blockcheck.cmd": filepath.Join(config.currentDir, "blockcheck.cmd"), + "Add to autorun.exe": filepath.Join(config.buildDir, "add_to_autorun.exe"), + "Automatically search pre-config.exe": filepath.Join(config.buildDir, "preconfig_tester.exe"), + "Run pre-config.exe": filepath.Join(config.buildDir, "run_preconfig.exe"), + "Set domain list.exe": filepath.Join(config.buildDir, "select_domains.exe"), + "Check for updates.exe": filepath.Join(config.buildDir, "check_for_updates.exe"), + } + + for zipPath, fsPath := range filesToAdd { + if err := addFileToZip(zipWriter, zipPath, fsPath); err != nil { + return fmt.Errorf("error adding file %s: %v", fsPath, err) + } + } + return nil } func addDirToZip(zipWriter *zip.Writer, zipPath string, fsPath string) error { From f33b1bce43bf3516d62342baf799d3bca8fc44c3 Mon Sep 17 00:00:00 2001 From: ANKDDEV Date: Fri, 20 Dec 2024 06:58:10 +0300 Subject: [PATCH 3/3] docs: add comments to build script --- scripts/build.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/scripts/build.go b/scripts/build.go index 6aea32d..6e003c8 100644 --- a/scripts/build.go +++ b/scripts/build.go @@ -15,6 +15,7 @@ import ( "github.com/cli/safeexec" ) +// buildConfig is the configuration for the build process type buildConfig struct { currentDir string buildDir string @@ -45,6 +46,7 @@ func main() { fmt.Scanln() } +// initBuildConfig initializes the build configuration func initBuildConfig() (*buildConfig, error) { currentDir, err := os.Getwd() if err != nil { @@ -62,6 +64,7 @@ func initBuildConfig() (*buildConfig, error) { }, nil } +// runBuildSteps runs the build steps func runBuildSteps(config *buildConfig) error { s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) s.HideCursor = true @@ -109,12 +112,14 @@ func runBuildSteps(config *buildConfig) error { return nil } +// buildExecutables builds the executables func buildExecutables() error { _ = os.Mkdir("build", os.ModePerm) ldflags := fmt.Sprintf("-X main.version=%s %s", version(), os.Getenv("GO_LDFLAGS")) return run("go", "build", "-ldflags", ldflags, "-o", "build", "./cmd/...") } +// validatePaths validates the paths func validatePaths(config *buildConfig) error { paths := []string{config.buildDir, config.listsDir, config.preConfigsDir} for _, path := range paths { @@ -125,6 +130,7 @@ func validatePaths(config *buildConfig) error { return nil } +// createZipFile creates the zip file func createZipFile(zipPath string) (*os.File, *zip.Writer, error) { zipFile, err := os.Create(zipPath) if err != nil { @@ -133,6 +139,7 @@ func createZipFile(zipPath string) (*os.File, *zip.Writer, error) { return zipFile, zip.NewWriter(zipFile), nil } +// addDirectories adds the directories to the zip file func addDirectories(zipWriter *zip.Writer, config *buildConfig) error { dirsToAdd := map[string]string{ "lists": config.listsDir, @@ -148,6 +155,7 @@ func addDirectories(zipWriter *zip.Writer, config *buildConfig) error { return nil } +// addFiles adds the files to the zip file func addFiles(zipWriter *zip.Writer, config *buildConfig) error { filesToAdd := map[string]string{ "blockcheck.cmd": filepath.Join(config.currentDir, "blockcheck.cmd"), @@ -166,6 +174,7 @@ func addFiles(zipWriter *zip.Writer, config *buildConfig) error { return nil } +// addDirToZip adds the directory to the zip file func addDirToZip(zipWriter *zip.Writer, zipPath string, fsPath string) error { return filepath.Walk(fsPath, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -187,6 +196,7 @@ func addDirToZip(zipWriter *zip.Writer, zipPath string, fsPath string) error { }) } +// addFileToZip adds the file to the zip file func addFileToZip(zipWriter *zip.Writer, zipPath string, fsPath string) error { file, err := os.Open(fsPath) if err != nil { @@ -218,6 +228,7 @@ func version() string { return rev } +// cmdOutput runs a command and returns its output func cmdOutput(args ...string) (string, error) { exe, err := safeexec.LookPath(args[0]) if err != nil { @@ -239,6 +250,7 @@ func isWindows() bool { return false } +// run runs a command func run(args ...string) error { exe, err := safeexec.LookPath(args[0]) if err != nil {