Пример #1
0
        bool OpenJDKExistsAndIsValid(string installDir, out string installedVersion)
        {
            installedVersion = null;
            if (!Directory.Exists(installDir))
            {
                Log.DebugLine($"{ProductName} directory {installDir} does not exist");
                return(false);
            }

            string corettoVersionFile = Path.Combine(installDir, "version.txt");

            if (File.Exists(corettoVersionFile))
            {
                Log.DebugLine($"Corretto version file {corettoVersionFile} found, will replace Corretto with {ProductName}");
                return(false);
            }

            string jetBrainsReleaseFile = Path.Combine(installDir, "release");

            if (!File.Exists(jetBrainsReleaseFile))
            {
                Log.DebugLine($"{ProductName} release file {jetBrainsReleaseFile} does not exist, cannot determine version");
                return(false);
            }

            string[] lines = File.ReadAllLines(jetBrainsReleaseFile);
            if (lines == null || lines.Length == 0)
            {
                Log.DebugLine($"{ProductName} release file {jetBrainsReleaseFile} is empty, cannot determine version");
                return(false);
            }

            string cv = null;

            foreach (string l in lines)
            {
                string line = l.Trim();
                if (!line.StartsWith("JAVA_VERSION=", StringComparison.Ordinal))
                {
                    continue;
                }

                cv = line.Substring(line.IndexOf('=') + 1).Trim('"');
                break;
            }

            if (String.IsNullOrEmpty(cv))
            {
                Log.DebugLine($"Unable to find version of {ProductName} in release file {jetBrainsReleaseFile}");
                return(false);
            }

            string xaVersionFile = Path.Combine(installDir, XAVersionInfoFile);

            if (!File.Exists(xaVersionFile))
            {
                installedVersion = cv;
                Log.DebugLine($"Unable to find Xamarin.Android version file {xaVersionFile}");
                return(false);
            }

            lines = File.ReadAllLines(xaVersionFile);
            if (lines == null || lines.Length == 0)
            {
                Log.DebugLine($"Xamarin.Android version file {xaVersionFile} is empty, cannot determine release version");
                return(false);
            }

            string rv = lines[0].Trim();

            if (String.IsNullOrEmpty(rv))
            {
                Log.DebugLine($"Xamarin.Android version file {xaVersionFile} does not contain release version information");
                return(false);
            }

            installedVersion = $"{cv} r{rv}";

            if (!Version.TryParse(cv, out Version cversion))
            {
                Log.DebugLine($"Unable to parse {ProductName} version from: {cv}");
                return(false);
            }

            if (cversion != JdkVersion)
            {
                Log.DebugLine($"Invalid {ProductName} version. Need {JdkVersion}, found {cversion}");
                return(false);
            }

            if (!Version.TryParse(rv, out cversion))
            {
                Log.DebugLine($"Unable to parse {ProductName} release version from: {rv}");
                return(false);
            }

            if (cversion != JdkRelease)
            {
                Log.DebugLine($"Invalid {ProductName} version. Need {JdkRelease}, found {cversion}");
                return(false);
            }

            foreach (string f in jdkFiles)
            {
                string file = Path.Combine(installDir, f);
                if (!File.Exists(file))
                {
                    bool foundExe = false;
                    foreach (string exe in Utilities.FindExecutable(f))
                    {
                        file = Path.Combine(installDir, exe);
                        if (File.Exists(file))
                        {
                            foundExe = true;
                            break;
                        }
                    }

                    if (!foundExe)
                    {
                        Log.DebugLine($"JDK file {file} missing from {ProductName}");
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #2
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);
        }
#pragma warning restore CS1998

        void GenerateThirdPartyNotices(string outputPath, ThirdPartyLicenseType licenseType, bool includeExternalDeps, bool includeBuildDeps)
        {
            List <Type> types = Utilities.GetTypesWithCustomAttribute <TPNAttribute> ();

            if (types.Count == 0)
            {
                Log.StatusLine("No Third Party Notice entries found", ConsoleColor.Gray);
                return;
            }

            var licenses = new SortedDictionary <string, ThirdPartyNotice> (StringComparer.OrdinalIgnoreCase);

            foreach (Type type in types)
            {
                EnsureValidTPNType(type);

                if (type.IsSubclassOf(typeof(ThirdPartyNoticeGroup)))
                {
                    ProcessTPN(licenses, Activator.CreateInstance(type) as ThirdPartyNoticeGroup, includeExternalDeps, includeBuildDeps);
                    continue;
                }

                if (type.IsSubclassOf(typeof(ThirdPartyNotice)))
                {
                    ProcessTPN(licenses, Activator.CreateInstance(type) as ThirdPartyNotice, includeExternalDeps, includeBuildDeps);
                    continue;
                }

                throw new NotSupportedException($"ThirdPartyNotice type {type.FullName} not supported");
            }

            if (licenses.Count == 0)
            {
                return;
            }

            string blurb;

            if (!tpnBlurbs.TryGetValue(licenseType, out blurb))
            {
                throw new InvalidOperationException($"Unknown license type {licenseType}");
            }

            using (StreamWriter sw = Utilities.OpenStreamWriter(outputPath)) {
                Log.StatusLine($" Generating: {Utilities.GetRelativePath (BuildPaths.XamarinAndroidSourceRoot, outputPath)}", ConsoleColor.Gray);
                Log.DebugLine($"Full path: {outputPath}");

                sw.WriteLine(blurb);
                sw.WriteLine();

                uint i   = 0;
                int  pad = licenses.Count >= 10 ? 4 : 3;
                foreach (var kvp in licenses)
                {
                    string           name = kvp.Key;
                    ThirdPartyNotice tpn  = kvp.Value;

                    sw.Write($"{++i}.".PadRight(pad));
                    sw.WriteLine($"{name} ({tpn.SourceUrl})");
                }
                sw.WriteLine();

                foreach (string key in licenses.Keys)
                {
                    ThirdPartyNotice tpn = licenses [key];

                    string heading   = $"%% {tpn.Name} NOTICES AND INFORMATION BEGIN HERE";
                    string underline = "=".PadRight(heading.Length, '=');
                    sw.WriteLine(heading);
                    sw.WriteLine(underline);
                    if (tpn.LicenseText.Length > 0)
                    {
                        sw.WriteLine(tpn.LicenseText.TrimStart());
                    }
                    else
                    {
                        sw.WriteLine(FetchTPNLicense(tpn.LicenseFile));
                    }
                    sw.WriteLine();
                    sw.WriteLine(underline);
                    sw.WriteLine($"END OF {tpn.Name} NOTICES AND INFORMATION");
                    sw.WriteLine();
                }

                sw.Flush();
            }
        }