コード例 #1
0
        protected override async Task <bool> Execute(Context context)
        {
            if (Directory.Exists(Configurables.Paths.OldOpenJDKInstallDir))
            {
                Log.DebugLine($"Found old OpenJDK directory at {Configurables.Paths.OldOpenJDKInstallDir}, removing");
                Utilities.DeleteDirectorySilent(Configurables.Paths.OldOpenJDKInstallDir);
            }

            string jdkInstallDir = JdkInstallDir;

            if (OpenJDKExistsAndIsValid(jdkInstallDir, out string installedVersion))
            {
                Log.Status($"{ProductName} version ");
                Log.Status(installedVersion, ConsoleColor.Yellow);
                Log.StatusLine(" already installed in: ", jdkInstallDir, tailColor: ConsoleColor.Cyan);
                return(true);
            }

            Log.StatusLine($"JetBrains JDK {JdkVersion} r{JdkRelease} will be installed");
            Uri jdkURL = JdkUrl;

            if (jdkURL == null)
            {
                throw new InvalidOperationException($"{ProductName} URL must not be null");
            }

            string[] queryParams = jdkURL.Query.TrimStart('?').Split(QuerySeparator, StringSplitOptions.RemoveEmptyEntries);
            if (queryParams.Length == 0)
            {
                Log.ErrorLine($"Unable to extract file name from {ProductName} URL as it contains no query component");
                return(false);
            }

            string?packageName = null;

            foreach (string p in queryParams)
            {
                if (!p.StartsWith(URLQueryFilePathField, StringComparison.Ordinal))
                {
                    continue;
                }

                int idx = p.IndexOf('=');
                if (idx < 0)
                {
                    Log.DebugLine($"{ProductName} URL query field '{URLQueryFilePathField}' has no value, unable to detect file name");
                    break;
                }

                packageName = p.Substring(idx + 1).Trim();
            }

            if (String.IsNullOrEmpty(packageName))
            {
                Log.ErrorLine($"Unable to extract file name from {ProductName} URL");
                return(false);
            }

            string localPackagePath = Path.Combine(JdkCacheDir, packageName);

            if (!await DownloadOpenJDK(context, localPackagePath, jdkURL))
            {
                return(false);
            }

            string tempDir = $"{jdkInstallDir}.temp";

            try {
                if (!await Unpack(localPackagePath, tempDir, cleanDestinationBeforeUnpacking: true))
                {
                    Log.ErrorLine($"Failed to install {ProductName}");
                    return(false);
                }

                string rootDir = Path.Combine(tempDir, RootDirName);
                if (!Directory.Exists(rootDir))
                {
                    Log.ErrorLine($"JetBrains root directory not found after unpacking: {RootDirName}");
                    return(false);
                }

                MoveContents(rootDir, jdkInstallDir);
                File.WriteAllText(Path.Combine(jdkInstallDir, XAVersionInfoFile), $"{JdkRelease}{Environment.NewLine}");
            } finally {
                Utilities.DeleteDirectorySilent(tempDir);
                // Clean up zip after extraction if running on a hosted azure pipelines agent.
                if (context.IsRunningOnHostedAzureAgent)
                {
                    Utilities.DeleteFileSilent(localPackagePath);
                }
            }

            return(true);
        }