mirror of
				https://github.com/GayPizzaSpecifications/darwin-apk.git
				synced 2025-11-04 07:59:38 +00:00 
			
		
		
		
	Read from repository configs instead of hardcoding repos/arch for update
This commit is contained in:
		@ -6,27 +6,15 @@
 | 
				
			|||||||
import Foundation
 | 
					import Foundation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public struct ApkIndexUpdater {
 | 
					public struct ApkIndexUpdater {
 | 
				
			||||||
  var repositories: [String]
 | 
					  public var repositories: [ApkIndexRepository]
 | 
				
			||||||
  var architectures: [String]
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public init() {
 | 
					  public init() {
 | 
				
			||||||
    self.repositories = [
 | 
					    self.repositories = []
 | 
				
			||||||
      "https://dl-cdn.alpinelinux.org/alpine/v3.20/main",
 | 
					 | 
				
			||||||
      "https://dl-cdn.alpinelinux.org/alpine/v3.20/community"
 | 
					 | 
				
			||||||
    ]
 | 
					 | 
				
			||||||
    // other archs: "armhf", "armv7", "loongarch64", "ppc64le", "riscv64", "s390x", "x86"
 | 
					 | 
				
			||||||
    self.architectures = [ "aarch64" /*, "x86_64" */ ]
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public func update() {
 | 
					  public func update() {
 | 
				
			||||||
    let repositories = self.repositories.flatMap { repo in
 | 
					 | 
				
			||||||
      self.architectures.map { arch in
 | 
					 | 
				
			||||||
        ApkIndexRepository(name: repo, arch: arch)
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let downloader = ApkIndexDownloader()
 | 
					    let downloader = ApkIndexDownloader()
 | 
				
			||||||
    for repo in repositories {
 | 
					    for repo in self.repositories {
 | 
				
			||||||
      let localIndex = URL(filePath: repo.localName)
 | 
					      let localIndex = URL(filePath: repo.localName)
 | 
				
			||||||
#if false
 | 
					#if false
 | 
				
			||||||
      let shouldDownload = true
 | 
					      let shouldDownload = true
 | 
				
			||||||
@ -41,7 +29,7 @@ public struct ApkIndexUpdater {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    let graph: ApkPackageGraph
 | 
					    let graph: ApkPackageGraph
 | 
				
			||||||
    do {
 | 
					    do {
 | 
				
			||||||
      let tables = try repositories.map { try readIndex(URL(filePath: $0.localName)) }
 | 
					      let tables = try self.repositories.map { try readIndex(URL(filePath: $0.localName)) }
 | 
				
			||||||
      graph = ApkPackageGraph(index: ApkIndex.merge(tables))
 | 
					      graph = ApkPackageGraph(index: ApkIndex.merge(tables))
 | 
				
			||||||
      graph.buildGraphNode()
 | 
					      graph.buildGraphNode()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										47
									
								
								Sources/dpk-cli/RepositoriesConfig.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								Sources/dpk-cli/RepositoriesConfig.swift
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * darwin-apk © 2024 Gay Pizza Specifications
 | 
				
			||||||
 | 
					 * SPDX-License-Identifier: Apache-2.0
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import Foundation
 | 
				
			||||||
 | 
					import ArgumentParser
 | 
				
			||||||
 | 
					import darwin_apk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct RepositoriesConfig {
 | 
				
			||||||
 | 
					  let repositories: [ApkIndexRepository]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  init() async throws(ExitCode) {
 | 
				
			||||||
 | 
					    let repositories: [String], architectures: [String]
 | 
				
			||||||
 | 
					    do {
 | 
				
			||||||
 | 
					      repositories = try await Self.read(name: "repositories")
 | 
				
			||||||
 | 
					    } catch {
 | 
				
			||||||
 | 
					      print("Failed to read repositories: \(error.localizedDescription)")
 | 
				
			||||||
 | 
					      throw .failure
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    do {
 | 
				
			||||||
 | 
					      architectures = try await Self.read(name: "arch")
 | 
				
			||||||
 | 
					    } catch {
 | 
				
			||||||
 | 
					      print("Failed to read arch: \(error.localizedDescription)")
 | 
				
			||||||
 | 
					      throw .failure
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    self.repositories = repositories.flatMap { repo in
 | 
				
			||||||
 | 
					      architectures.map { arch in
 | 
				
			||||||
 | 
					        ApkIndexRepository(name: repo, arch: arch)
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  var localRepositories: [URL] {
 | 
				
			||||||
 | 
					    self.repositories.map { repo in
 | 
				
			||||||
 | 
					      URL(filePath: repo.localName, directoryHint: .notDirectory)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private static func read(name: String) async throws -> [String] {
 | 
				
			||||||
 | 
					    try await URL(filePath: name, directoryHint: .notDirectory).lines
 | 
				
			||||||
 | 
					      .map { $0.trimmingCharacters(in: .whitespaces) }
 | 
				
			||||||
 | 
					      .filter { !$0.isEmpty && $0.first != "#" }  // Ignore empty & commented lines
 | 
				
			||||||
 | 
					      .reduce(into: [String]()) { $0.append($1) }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -41,25 +41,7 @@ struct DpkSearchCommand: AsyncParsableCommand {
 | 
				
			|||||||
    let match: any PatternMatcher
 | 
					    let match: any PatternMatcher
 | 
				
			||||||
    match = try matcher.init(patterns: patterns, ignoreCase: !self.caseSensitive)
 | 
					    match = try matcher.init(patterns: patterns, ignoreCase: !self.caseSensitive)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let repositories: [String], architectures: [String]
 | 
					    let localRepositories = try await RepositoriesConfig().localRepositories
 | 
				
			||||||
    do {
 | 
					 | 
				
			||||||
      repositories = try await PropertyFile.read(name: "repositories")
 | 
					 | 
				
			||||||
    } catch {
 | 
					 | 
				
			||||||
      print("Failed to read repositories: \(error.localizedDescription)")
 | 
					 | 
				
			||||||
      throw .failure
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    do {
 | 
					 | 
				
			||||||
      architectures = try await PropertyFile.read(name: "arch")
 | 
					 | 
				
			||||||
    } catch {
 | 
					 | 
				
			||||||
      print("Failed to read arch: \(error.localizedDescription)")
 | 
					 | 
				
			||||||
      throw .failure
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let localRepositories = repositories.flatMap { repo in
 | 
					 | 
				
			||||||
      architectures.map { arch in
 | 
					 | 
				
			||||||
        URL(filePath: ApkIndexRepository(name: repo, arch: arch).localName, directoryHint: .notDirectory)
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    let index: ApkIndex
 | 
					    let index: ApkIndex
 | 
				
			||||||
    do {
 | 
					    do {
 | 
				
			||||||
      index = ApkIndex.merge(try localRepositories.map(ApkIndex.init))
 | 
					      index = ApkIndex.merge(try localRepositories.map(ApkIndex.init))
 | 
				
			||||||
@ -75,12 +57,3 @@ struct DpkSearchCommand: AsyncParsableCommand {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
struct PropertyFile {
 | 
					 | 
				
			||||||
  static func read(name: String) async throws -> [String] {
 | 
					 | 
				
			||||||
    try await URL(filePath: name, directoryHint: .notDirectory).lines
 | 
					 | 
				
			||||||
      .map { $0.trimmingCharacters(in: .whitespaces) }
 | 
					 | 
				
			||||||
      .filter { !$0.isEmpty && $0.first != "#" }  // Ignore empty & commented lines
 | 
					 | 
				
			||||||
      .reduce(into: [String]()) { $0.append($1) }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -7,15 +7,17 @@ import Foundation
 | 
				
			|||||||
import ArgumentParser
 | 
					import ArgumentParser
 | 
				
			||||||
import darwin_apk
 | 
					import darwin_apk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct DpkUpdateCommand: ParsableCommand {
 | 
					struct DpkUpdateCommand: AsyncParsableCommand {
 | 
				
			||||||
  static let configuration = CommandConfiguration(
 | 
					  static let configuration = CommandConfiguration(
 | 
				
			||||||
    commandName: "update",
 | 
					    commandName: "update",
 | 
				
			||||||
    abstract: "Update the system package repositories.",
 | 
					    abstract: "Update the system package repositories.",
 | 
				
			||||||
    aliases: [ "u" ])
 | 
					    aliases: [ "u" ])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func run() throws {
 | 
					  func run() async throws {
 | 
				
			||||||
    print("Updating package repositories")
 | 
					    print("Updating package repositories")
 | 
				
			||||||
 | 
					    let repositories = try await RepositoriesConfig().repositories
 | 
				
			||||||
    var updater = ApkIndexUpdater()
 | 
					    var updater = ApkIndexUpdater()
 | 
				
			||||||
 | 
					    updater.repositories.append(contentsOf: repositories)
 | 
				
			||||||
    updater.update()
 | 
					    updater.update()
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user