-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🩹 [Fix] set new default ADB path + remove date from Logcat (#6)
* Create use case and errors for get adb version * Remove day month and year in logcat view (It's useless) * Can get default adb path * Use default adb path * Generate new version
- Loading branch information
1 parent
ba72b7c
commit 55fd124
Showing
14 changed files
with
341 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// ShellHelper.swift | ||
// AndroidTools | ||
// | ||
// Created by Thomas Bernard on 16/05/2024. | ||
// | ||
|
||
import Foundation | ||
import os | ||
|
||
class ShellHelper { | ||
|
||
private let logger = Logger( | ||
subsystem: Bundle.main.bundleIdentifier!, | ||
category: String(describing: AdbRepositoryImpl.self) | ||
) | ||
|
||
func runAdbCommand(_ command: String) -> String { | ||
logger.info("Running command:\n\(command)") | ||
let task = Process() | ||
let pipe = Pipe() | ||
|
||
task.standardOutput = pipe | ||
task.standardError = pipe | ||
task.arguments = ["-c", command] | ||
task.launchPath = "/bin/sh" | ||
task.launch() | ||
|
||
let data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" | ||
logger.info("Result:\n\(output)") | ||
return output | ||
} | ||
|
||
func runAdbCommand(_ command: String, outputHandler: @escaping (String) -> Void) { | ||
logger.info("Running command:\n\(command)") | ||
let task = Process() | ||
let pipe = Pipe() | ||
|
||
task.standardOutput = pipe | ||
task.standardError = pipe | ||
task.arguments = ["-c", command] | ||
task.launchPath = "/bin/sh" | ||
|
||
pipe.fileHandleForReading.readabilityHandler = { fileHandle in | ||
let data = fileHandle.availableData | ||
if let output = String(data: data, encoding: .utf8) { | ||
outputHandler(output) | ||
} | ||
} | ||
|
||
task.launch() | ||
task.waitUntilExit() | ||
pipe.fileHandleForReading.readabilityHandler = nil | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
AndroidTools/AndroidTools/Data/Repositories/AdbRepositoryImpl.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// AdbRepository.swift | ||
// AndroidTools | ||
// | ||
// Created by Thomas Bernard on 16/05/2024. | ||
// | ||
|
||
import Foundation | ||
import os | ||
|
||
class AdbRepositoryImpl : AdbRepository { | ||
|
||
private var shellHelper : ShellHelper = ShellHelper() | ||
|
||
private var adbPath: String { | ||
var preferencePath = UserDefaults.standard.string(forKey: "adbPath") | ||
|
||
if preferencePath == nil { | ||
if let path = try? getDefaultPath() { | ||
preferencePath = path | ||
} else { | ||
preferencePath = "" | ||
} | ||
} | ||
|
||
return preferencePath! | ||
} | ||
|
||
private let logger = Logger( | ||
subsystem: Bundle.main.bundleIdentifier!, | ||
category: String(describing: AdbRepositoryImpl.self) | ||
) | ||
|
||
func runAdbCommand(_ command: String) throws -> String { | ||
return shellHelper.runAdbCommand("\(adbPath) \(command)") | ||
} | ||
|
||
func runAdbCommand(_ command: String, outputHandler: @escaping (String) -> Void) { | ||
return shellHelper.runAdbCommand("\(adbPath) \(command)", outputHandler: outputHandler) | ||
} | ||
|
||
|
||
func getVersion() throws -> String { | ||
let result = try runAdbCommand("version") | ||
|
||
let lines = result.split(separator: "\n") | ||
|
||
// Find the line that starts with "Version" | ||
if let versionLine = lines.first(where: { $0.starts(with: "Version") }) { | ||
|
||
let version = versionLine | ||
.replacingOccurrences(of: "Version ", with: "") | ||
.split(separator: " ") | ||
.first ?? "" | ||
return String(version) | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func getPath() throws -> String { | ||
return adbPath | ||
} | ||
|
||
private func getDefaultPath() throws -> String { | ||
let result = shellHelper.runAdbCommand("which adb") | ||
if result.contains("not found"){ | ||
throw AdbError.notExists | ||
} | ||
else { | ||
return result | ||
} | ||
} | ||
|
||
func setPath(path : String?) -> String { | ||
|
||
if path == nil { | ||
let defaultPath = try? getDefaultPath() | ||
UserDefaults.standard.set(defaultPath, forKey: "adbPath") | ||
} | ||
else { | ||
UserDefaults.standard.set(path, forKey: "adbPath") | ||
} | ||
|
||
return adbPath | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
AndroidTools/AndroidTools/Domain/Models/Errors/DisplayableError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// DisplayableError.swift | ||
// AndroidTools | ||
// | ||
// Created by Thomas Bernard on 16/05/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
enum DisplayableError : Error { | ||
case error(message : String) | ||
} |
13 changes: 13 additions & 0 deletions
13
AndroidTools/AndroidTools/Domain/Models/Errors/GetAdbVersionError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// AdbError.swift | ||
// AndroidTools | ||
// | ||
// Created by Thomas Bernard on 16/05/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
enum AdbError : Error { | ||
case notFound(path : String) | ||
case notExists | ||
} |
Oops, something went wrong.