Skip to content

Commit

Permalink
Merge branch 'master' of github.com:esafirm/gadb
Browse files Browse the repository at this point in the history
  • Loading branch information
esafirm committed Oct 26, 2020
2 parents a1bb501 + a607229 commit 7f7a9b1
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 19 deletions.
4 changes: 4 additions & 0 deletions adb/adbcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func Install(apkPath string) CommandReturn {
return runWithPrint("adb", "install", apkPath)
}

func ReInstall(apkPath string) CommandReturn {
return runWithPrint("adb", "install", "-r", apkPath)
}

// InstallTo install APK to the specific device
func InstallTo(deviceID string, apkPath string) CommandReturn {
return runWithPrint("adb", "-s", deviceID, "install", apkPath)
Expand Down
44 changes: 44 additions & 0 deletions apkanalyzer/apkanalyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package adb

import (
"fmt"
"os"
"os/exec"
"regexp"
)

const ANALYZER = "apkanalyzer"

type CommandReturn struct {
Output []byte
Error error
}

func checkInPath() {
_, err := exec.LookPath(ANALYZER)
if err != nil {
fmt.Println(ANALYZER + " is not in the path")
os.Exit(1)
}
}

func Manifest(apkPath string) CommandReturn {
checkInPath()

output, err := exec.Command(ANALYZER, "manifest", "print", apkPath).CombinedOutput()
return CommandReturn{output, err}
}

func PackageName(apkPath string) (string, error) {
checkInPath()

output, err := exec.Command(ANALYZER, "manifest", "print", apkPath).CombinedOutput()
if err != nil {
return "", err
}

r, _ := regexp.Compile("package=\"(.*)\"")
result := r.FindStringSubmatch(string(output))

return result[1], nil
}
76 changes: 69 additions & 7 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
package cmd

import (
"errors"
"fmt"
"os"
"strings"

adb "github.com/esafirm/gadb/adb"
analyzer "github.com/esafirm/gadb/apkanalyzer"
pui "github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

var isYes bool

// installCmd represents the install command
var installCmd = &cobra.Command{
Use: "install [apk_path]",
Expand All @@ -50,7 +54,11 @@ func runCommand(apkPath string) {

if comamndReturn.Error != nil {
output := string(comamndReturn.Output)
if canRecoverAlreadyExist(output) {
if canRecoverAlreadyExist(apkPath, output) {
runCommand(apkPath)
return
}
if canRecoverVersionDowngrade(apkPath, output) {
runCommand(apkPath)
return
}
Expand Down Expand Up @@ -119,27 +127,81 @@ func shouldShowDevicePicker(output string) bool {
return strings.Contains(output, "more than one device/emulator")
}

func canRecoverAlreadyExist(text string) bool {
func canRecoverVersionDowngrade(apkPath string, text string) bool {
packageName, err := analyzer.PackageName(apkPath)
if err != nil {
panic(err)
}

isVersionDowngradeProblem := strings.Contains(text, "INSTALL_FAILED_VERSION_DOWNGRADE")

var isConfirmed bool = isYes
if isVersionDowngradeProblem && !isConfirmed {
message := fmt.Sprintf("%s already exist, do you want to uninstall first?", packageName)
isConfirmed = confirmUninstall(message)
}

if isConfirmed {
uninstall(packageName)
}
return false
}

func canRecoverAlreadyExist(apkPath string, text string) bool {
isAlreadyExistProblem := strings.Contains(text, "ALREADY_EXISTS")

if isAlreadyExistProblem {
var index = strings.Index(text, "re-install") + len("re-install")
var withoutIndex = strings.Index(text, "without")
var packageName = strings.TrimSpace(text[index:withoutIndex])
commandReturn := adb.ReInstall(apkPath)
if commandReturn.Error != nil {

uninstall(packageName)
var isConfirmed bool = isYes
if !isConfirmed {
isConfirmed = confirmUninstall("Do you want to uninstall first?")
}

return true
if isConfirmed {
var index = strings.Index(text, "re-install") + len("re-install")
var withoutIndex = strings.Index(text, "without")
var packageName = strings.TrimSpace(text[index:withoutIndex])

uninstall(packageName)
return true
}

return false
}
}

return false
}

func confirmUninstall(message string) bool {
validation := func(input string) error {
if input == "y" || input == "Y" || input == "n" || input == "N" {
return nil
}
return errors.New("Answer not valid")
}

prompt := pui.Prompt{
Label: message + " [Y,n]",
Validate: validation,
}

result, _ := prompt.Run()

if result == "Y" || result == "y" {
return true
}
return false
}

func uninstall(packageName string) {
fmt.Printf("Uninstalling %s ~\n", packageName)
adb.Uninstall(packageName)
}

func init() {
rootCmd.AddCommand(installCmd)
mockCmd.Flags().BoolVarP(&isYes, "yes", "y", false, "Set auto confirm")
}
18 changes: 6 additions & 12 deletions cmd/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ package cmd

import (
"fmt"
"os/exec"

analyzer "github.com/esafirm/gadb/apkanalyzer"

"github.com/spf13/cobra"
)

const ANALYZER = "apkanalyzer"

var manifestCmd = &cobra.Command{
Use: "manifest [apk]",
Short: "Print the AndroidManifest.xml from the APK",
Expand All @@ -32,16 +31,11 @@ var manifestCmd = &cobra.Command{
return
}

_, err := exec.LookPath(ANALYZER)
if err != nil {
fmt.Println(ANALYZER + " is not in the path")
}

output, err := exec.Command(ANALYZER, "manifest", "print", args[0]).CombinedOutput()
if err != nil {
fmt.Println(err)
commandReturn := analyzer.Manifest(args[0])
if commandReturn.Error != nil {
fmt.Println(commandReturn.Error)
} else {
fmt.Println(string(output))
fmt.Println(string(commandReturn.Output))
}
},
}
Expand Down
45 changes: 45 additions & 0 deletions cmd/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright © 2019 Esa Firman esafirm21@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

analyzer "github.com/esafirm/gadb/apkanalyzer"

"github.com/spf13/cobra"
)

var pakcageCmd = &cobra.Command{
Use: "package [apk]",
Short: "Print package name from the APK",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("You must provide the APK")
return
}

pakckageName, err := analyzer.PackageName(args[0])
if err != nil {
fmt.Printf("Package name: %s", pakckageName)
} else {
panic(err)
}
},
}

func init() {
rootCmd.AddCommand(pakcageCmd)
}

0 comments on commit 7f7a9b1

Please sign in to comment.