/****************************************************************************** __ ____ __ / / ___ ____ _/ __/_____________ _/ /__ / / / _ \/ __ `/ /_/ ___/ ___/ __ `/ / _ \ / /___/ __/ /_/ / __(__ ) /__/ /_/ / / __/ /_____/\___/\__,_/_/ /____/\___/\__,_/_/\___/ (C)opyright 2025, Leafscale, LLC - https://www.leafscale.com Project: Zygaena Filename: sync.reef Authors: Chris Tusa License: Description: Sync command - update ports collection from repository ******************************************************************************/ module commands.sync import sys.args import sys.process import io.dir import io.path import io.file import core.str import core.config import core.port import core.portindex import util.color import exitcodes as ec export fn execute(): int end export fn execute(): int // Load configuration let cfg = config.load() let ports_dir = config.get_ports_dir(cfg) color.print_action("Syncing ports collection...") println("") // Check if ports directory exists if not dir.dir_exists(ports_dir) color.print_error("Ports directory does not exist: " + ports_dir) color.print_info("You may need to clone the ports repository first:") println(" git clone " + ports_dir) return ec.EXIT_CONFIG_ERROR() end if // Check if it's a git repository let git_dir = path.join_path(ports_dir, ".git") if not dir.dir_exists(git_dir) color.print_warning("Ports directory is not a git repository") color.print_info("Cannot sync - no version control detected") return ec.EXIT_CONFIG_ERROR() end if // Run git pull color.print_info("Updating from remote repository...") let cmd = "cd \"" + ports_dir + "\" && git pull" let pid = process.process_spawn_shell(cmd) if pid < 0 color.print_error("Failed to start git pull") return ec.EXIT_REPO_UNREACHABLE() end if let exit_code = process.process_wait(pid) if exit_code == 0 color.print_success("Ports collection updated successfully") // Rebuild ports index after successful sync portindex.rebuild_port_index() // Show summary of changes show_update_summary(ports_dir) return ec.EXIT_SUCCESS() else color.print_error("Git pull failed with exit code " + int_to_str(exit_code)) color.print_info("Check your network connection and repository access") return ec.EXIT_REPO_UNREACHABLE() end if end execute proc show_update_summary(ports_dir: string) // Count ports in each category (dynamically discovered) mut cats: [string] = new [string](32) let cat_count = port.get_categories(cats, 32) mut total = 0 mut i = 0 while i < cat_count let cat = cats[i] let cat_path = path.join_path(ports_dir, cat) if dir.dir_exists(cat_path) mut entries: [string] = new [string](256) let count = dir.list_dir(cat_path, entries, 256) // Count valid ports (directories with package.toml) mut port_count = 0 mut j = 0 while j < count let entry = entries[j] if str.length(entry) > 0 and entry[0] != '.' let toml_path = path.join_path(path.join_path(cat_path, entry), "package.toml") if file.fileExists(toml_path) port_count = port_count + 1 end if end if j = j + 1 end while if port_count > 0 println(" " + cat + ": " + int_to_str(port_count) + " ports") total = total + port_count end if end if i = i + 1 end while println("") color.print_info("Total: " + int_to_str(total) + " ports available") end show_update_summary // Helper: convert int to string fn int_to_str(n: int): string if n == 0 return "0" end if mut negative = false mut value = n if n < 0 negative = true value = 0 - n end if mut result = "" while value > 0 let digit = value % 10 result = str.concat(str.substring("0123456789", digit, 1), result) value = value / 10 end while if negative result = str.concat("-", result) end if return result end int_to_str end module