Ejemplo n.º 1
0
        static async Task Deploy(DeploymentParameters parameters)
        {
            Logger.instance.LogMessage($"Deployment started [Rebuild={parameters.Rebuild}, keep-terminal={parameters.KeepTerminal}, show-terminal={parameters.ShowTerminal}]");
            try
            {
                string apkPath = string.Empty;
                apkPath = await BuildEngine.Build(parameters);

                await BuildEngine.Sign(apkPath, parameters);

                await GooglePlayHelper.Instance.Initialize(parameters.CredentialsPath);

                await GooglePlayHelper.Instance.UploadAPK(apkPath, parameters);
            }
            catch (Exception e)
            {
                Logger.instance.LogError(e.Message);
            }
        }
        public static async Task Sign(string apkPath, DeploymentParameters parameters)
        {
            Logger.instance.LogMessage($"Signing apk ...");
            if (!File.Exists(apkPath))
            {
                throw new Exception($"Failed to locate the apk at [{apkPath}]");
            }
            //Sign APK
            string signScript = SCRIPT_SIGN;

            signScript = parameters.KeepTerminal ? signScript + TEMPLATE_PAUSE : signScript;
            string signContent = string.Format(signScript,
                                               apkPath,
                                               parameters.KeystoreFilePath,
                                               parameters.KeystoreType,
                                               parameters.KeystorePassword,
                                               parameters.KeyPassword,
                                               parameters.KeystoreAlias
                                               );
            string signScriptPath = Path.Combine(Path.GetTempPath(), $"sign_{Guid.NewGuid()}.bat");

            File.WriteAllText(signScriptPath, signContent);
            if (!File.Exists(signScriptPath))
            {
                throw new FileNotFoundException($"Script file not found '{signScriptPath}'");
            }
            Process proc = new Process();

            proc.StartInfo.FileName = signScriptPath;
            if (parameters.ShowTerminal == false)
            {
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            }
            await Task.Run(() =>
            {
                proc.Start();
                proc.WaitForExit();
            });

            File.Delete(parameters.KeystoreFilePath);
            File.Delete(signScriptPath);
            Logger.instance.LogMessage("APK signed successfully");
        }
        public static async Task <string> Build(DeploymentParameters parameters)
        {
            Logger.instance.LogMessage($"Generating apk ...");
            string buildScript         = string.Empty;
            string apkReleaseDirectory = string.Empty;
            string apkName             = string.Empty;

            //Determine project type
            string technology         = string.Empty;
            string flutter_check_path = Path.Combine(parameters.BaseDirectory, "pubspec.yaml");
            string ionic_check_path   = Path.Combine(parameters.BaseDirectory, "ionic.config.json");
            string native_check_path  = Path.Combine(parameters.BaseDirectory, "build.gradle");

            if (File.Exists(flutter_check_path))
            {
                apkReleaseDirectory = APK_RELEASE_DIRECTORY_FLUTTER;
                apkName             = APK_NAME_FLUTTER;
                buildScript         = SCRIPT_BUILD_FLUTTER;
            }
            if (File.Exists(ionic_check_path))
            {
                buildScript         = SCRIPT_BUILD_IONIC;
                apkName             = APK_NAME_IONIC;
                apkReleaseDirectory = APK_RELEASE_DIRECTORY_IONIC;
            }
            if (File.Exists(native_check_path))
            {
                buildScript         = SCRIPT_BUILD_NATIVE;
                apkName             = APK_NAME_NATIVE;
                apkReleaseDirectory = APK_RELEASE_DIRECTORY_NATIVE;
            }
            if (string.IsNullOrWhiteSpace(buildScript))
            {
                throw new Exception("Cannot determine project type");
            }

            //Creating Build Script
            string buildScriptPath = Path.Combine(Path.GetTempPath(), $"build_{Guid.NewGuid()}.bat");

            buildScript = parameters.KeepTerminal ? buildScript + TEMPLATE_PAUSE : buildScript;
            string buildFileContent = string.Format(buildScript, parameters.BaseDirectory);

            File.WriteAllText(buildScriptPath, buildFileContent);


            //Generating APK
            if (parameters.Rebuild)
            {
                Process proc = new Process();
                proc.StartInfo.FileName = buildScriptPath;
                if (parameters.ShowTerminal == false)
                {
                    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                }
                await Task.Run(() =>
                {
                    proc.Start();
                    proc.WaitForExit();
                });
            }
            else
            {
                Logger.instance.LogMessage($"Build step has been skipped");
            }
            string timestamp        = DateTime.Now.ToString("yyyy.MM.dd.hh.mm.ss");
            string apkTargetName    = $"{parameters.PackageName}.VC{parameters.versionCode}.V{parameters.versionName}.{timestamp}.apk";
            string outputFileName   = $"{parameters.PackageName}.VC{parameters.versionCode}.V{parameters.versionName}.{timestamp}.json";
            string apkTargetPath    = Path.Combine(parameters.ApkTargetDirectory, apkTargetName);
            string apkSourcePath    = parameters.BaseDirectory + apkReleaseDirectory + @"\" + apkName;
            string outputFilePath   = parameters.BaseDirectory + apkReleaseDirectory + @"\output.json";
            string outputTargetPath = Path.Combine(parameters.ApkTargetDirectory, outputFileName);

            if (File.Exists(apkSourcePath) && File.Exists(outputFilePath))
            {
                if (!Directory.Exists(parameters.ApkTargetDirectory))
                {
                    throw new Exception($"Directory [{parameters.ApkTargetDirectory}] doesn't exist");
                }
                File.Copy(apkSourcePath, apkTargetPath);
                File.Copy(outputFilePath, outputTargetPath);
            }
            else
            {
                throw new Exception($"Failed to locate the generated apk or the outputfile -> [{apkSourcePath}]");
            }
            Logger.instance.LogMessage($"Apk generated successfully at [{apkTargetPath}]");
            return(apkTargetPath);
        }