struct.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package sdk
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "sort"
  7. "notabug.org/Umnik/GoAndroidSDK/v2/components/misc"
  8. )
  9. // SDK represents Android SDK
  10. type SDK struct {
  11. dirPath string
  12. }
  13. // RootDir returns path to SDK dir
  14. func (s SDK) RootDir() string {
  15. return s.dirPath
  16. }
  17. func testDir(dir string) (string, error) {
  18. if err := misc.TestDir(dir); err != nil {
  19. return "", err
  20. }
  21. return dir, nil
  22. }
  23. // PlatformTools returns path to "platform-tools" dir
  24. func (s SDK) PlatformTools() (string, error) {
  25. return testDir(path.Join(s.RootDir(), "platform-tools"))
  26. }
  27. // BuildTools returns path to "build-tools" dir
  28. func (s SDK) BuildTools() (string, error) {
  29. return testDir(path.Join(s.RootDir(), "build-tools"))
  30. }
  31. // Platforms returns path to platforms directory
  32. func (s SDK) Platforms() (string, error) {
  33. return testDir(path.Join(s.RootDir(), "platforms"))
  34. }
  35. // SystemImages returns path to systemImages catalog
  36. func (s SDK) SystemImages() (string, error) {
  37. return testDir(path.Join(s.RootDir(), "system-images"))
  38. }
  39. // LastBuildToolsVersion returns path to last available version in "build-tools" dir
  40. func (s SDK) LastBuildToolsVersion() (string, error) {
  41. if p, err := s.BuildTools(); err != nil {
  42. return "", err
  43. } else {
  44. if dirEntries, err := os.ReadDir(p); err != nil {
  45. return "", fmt.Errorf("cannot find last build-tools version due error: %v", err)
  46. } else {
  47. versions := make([]string, len(dirEntries))
  48. for i := range dirEntries {
  49. versions[i] = dirEntries[i].Name()
  50. }
  51. sort.Strings(versions)
  52. return s.SpecificBuildToolsVersion(versions[len(versions)-1])
  53. }
  54. }
  55. }
  56. // SpecificBuildToolsVersion returns path to specified version in "build-tools" dir
  57. func (s SDK) SpecificBuildToolsVersion(version string) (string, error) {
  58. return testDir(path.Join(s.RootDir(), "build-tools", version))
  59. }
  60. // SpecificPlatformsVersion returns path to specific version in "platforms" dir
  61. func (s SDK) SpecificPlatformsVersion(version string) (string, error) {
  62. if p, err := s.Platforms(); err != nil {
  63. return "", err
  64. } else {
  65. return testDir(path.Join(p, version))
  66. }
  67. }