Esempio n. 1
0
        public override bool Execute()
        {
            var maxVersion = GetVersion(MaximumJdkVersion);

            XATInfo jdk = XATInfo.GetKnownSystemJdkInfos(CreateLogger())
                          .Where(j => maxVersion != null ? j.Version <= maxVersion : true)
                          .Where(j => j.IncludePath.Any())
                          .FirstOrDefault();

            if (jdk == null)
            {
                Log.LogError("Could not determine JAVA_HOME location. Please set JdksRoot or export the JAVA_HOME environment variable.");
                return(false);
            }

            var rtJarPaths = new[] {
                Path.Combine(Path.GetDirectoryName(jdk.JavacPath), "..", "jre", "lib", "rt.jar"),
            };
            var rtJarPath = rtJarPaths.FirstOrDefault(p => File.Exists(p));

            JavaHomePath = jdk.HomePath;

            Directory.CreateDirectory(Path.GetDirectoryName(PropertyFile.ItemSpec));
            Directory.CreateDirectory(Path.GetDirectoryName(MakeFragmentFile.ItemSpec));

            WritePropertyFile(jdk.JavaPath, jdk.JarPath, jdk.JavacPath, jdk.JdkJvmPath, rtJarPath, jdk.IncludePath);
            WriteMakeFragmentFile(jdk.JavaPath, jdk.JarPath, jdk.JavacPath, jdk.JdkJvmPath, rtJarPath, jdk.IncludePath);

            return(!Log.HasLoggedErrors);
        }
Esempio n. 2
0
        internal static IEnumerable <JdkInfo> GetJdkInfos(Action <TraceLevel, string> logger)
        {
            JdkInfo TryGetJdkInfo(string path, string locator)
            {
                JdkInfo jdk = null;

                try {
                    jdk = new JdkInfo(path, locator);
                }
                catch (Exception e) {
                    logger(TraceLevel.Warning, $"Not a valid JDK directory: `{path}`; via category: {locator}");
                    logger(TraceLevel.Verbose, e.ToString());
                }
                return(jdk);
            }

            IEnumerable <JdkInfo> ToJdkInfos(IEnumerable <string> paths, string locator)
            {
                return(paths.Select(p => TryGetJdkInfo(p, locator))
                       .Where(jdk => jdk != null)
                       .OrderByDescending(jdk => jdk, JdkInfoVersionComparer.Default));
            }

            return(ToJdkInfos(GetPreferredJdkPaths(), "Preferred Registry")
                   .Concat(ToJdkInfos(GetOpenJdkPaths(), "OpenJDK"))
                   .Concat(ToJdkInfos(GetOracleJdkPaths(), "Oracle JDK")));
        }
        internal static IEnumerable <JdkInfo> GetJdkInfos(Action <TraceLevel, string> logger)
        {
            JdkInfo?TryGetJdkInfo(string path, string locator)
            {
                JdkInfo?jdk = null;

                try {
                    jdk = new JdkInfo(path, locator);
                }
                catch (Exception e) {
                    logger(TraceLevel.Warning, string.Format(Resources.InvalidJdkDirectory_path_locator_message, path, locator, e.Message));
                    logger(TraceLevel.Verbose, e.ToString());
                }
                return(jdk);
            }

            IEnumerable <JdkInfo> ToJdkInfos(IEnumerable <string> paths, string locator)
            {
                return(paths.Select(p => TryGetJdkInfo(p, locator))
                       .Where(jdk => jdk != null)
                       .Select(jdk => jdk !)
                       .OrderByDescending(jdk => jdk, JdkInfoVersionComparer.Default));
            }

            return(ToJdkInfos(GetPreferredJdkPaths(), "Preferred Registry")
                   .Concat(ToJdkInfos(GetOpenJdkPaths(), "OpenJDK"))
                   .Concat(ToJdkInfos(GetKnownOpenJdkPaths(), "Well-known OpenJDK paths"))
                   .Concat(ToJdkInfos(GetOracleJdkPaths(), "Oracle JDK"))
                   );
        }
        internal static IEnumerable <JdkInfo> GetVSAndroidJdks(Action <TraceLevel, string> logger)
        {
            if (!OS.IsWindows)
            {
                yield break;
            }
            var root      = RegistryEx.LocalMachine;
            var wows      = new[] { RegistryEx.Wow64.Key32, RegistryEx.Wow64.Key64 };
            var subKey    = @"SOFTWARE\Microsoft\VisualStudio\Android";
            var valueName = "JavaHome";

            foreach (var wow in wows)
            {
                if (!RegistryEx.CheckRegistryKeyForExecutable(root, subKey, valueName, wow, "bin", "java.exe"))
                {
                    continue;
                }
                var path = RegistryEx.GetValueString(root, subKey, valueName, wow) ?? "";
                if (string.IsNullOrEmpty(path))
                {
                    continue;
                }
                var jdk = JdkInfo.TryGetJdkInfo(path, logger, subKey);
                if (jdk == null)
                {
                    continue;
                }
                yield return(jdk);
            }
        }
Esempio n. 5
0
 protected static IEnumerable <JdkInfo> FromPaths(IEnumerable <string> paths, Action <TraceLevel, string> logger, string locator)
 {
     return(paths
            .Select(p => JdkInfo.TryGetJdkInfo(p, logger, locator))
            .Where(jdk => jdk != null)
            .Select(jdk => jdk !));
 }
 static void Main(string[] args)
 {
     foreach (var jdk in JdkInfo.GetKnownSystemJdkInfos())
     {
         Console.WriteLine($"Found JDK: {jdk.HomePath}");
         Console.WriteLine($"  Locator: {jdk.Locator}");
     }
 }
 static IEnumerable <JdkInfo> GetUnixPreferredJdks(Action <TraceLevel, string> logger)
 {
     return(GetUnixConfiguredJdkPaths(logger)
            .Select(p => JdkInfo.TryGetJdkInfo(p, logger, "monodroid-config.xml"))
            .Where(jdk => jdk != null)
            .Select(jdk => jdk !)
            .OrderByDescending(jdk => jdk, JdkInfoVersionComparer.Default));
 }
Esempio n. 8
0
        static JdkInfo TryGetJdkInfo(string path, Action <TraceLevel, string> logger, string locator)
        {
            JdkInfo jdk = null;

            try {
                jdk = new JdkInfo(path, locator);
            }
            catch (Exception e) {
                logger(TraceLevel.Warning, $"Not a valid JDK directory: `{path}`; via locator: {locator}");
                logger(TraceLevel.Verbose, e.ToString());
            }
            return(jdk);
        }
 internal static IEnumerable <JdkInfo> GetOracleJdks(Action <TraceLevel, string> logger)
 {
     if (!OS.IsWindows)
     {
         yield break;
     }
     foreach (var path in GetOracleJdkPaths())
     {
         var jdk = JdkInfo.TryGetJdkInfo(path, logger, "Oracle Registry");
         if (jdk == null)
         {
             continue;
         }
         yield return(jdk);
     }
 }
Esempio n. 10
0
 internal static IEnumerable <JdkInfo> GetPreferredJdks(Action <TraceLevel, string> logger)
 {
     if (OS.IsWindows)
     {
         var path = GetWindowsPreferredJdkPath();
         var jdk  = path == null ? null : JdkInfo.TryGetJdkInfo(path, logger, "Windows Registry");
         if (jdk != null)
         {
             yield return(jdk);
         }
     }
     foreach (var jdk in GetUnixPreferredJdks(logger))
     {
         yield return(jdk);
     }
 }
Esempio n. 11
0
        XATInfo[] GetJdkRoots()
        {
            XATInfo jdk = null;

            try {
                if (!string.IsNullOrEmpty(JdksRoot))
                {
                    jdk = new XATInfo(JdksRoot);
                }
            } catch (Exception e) {
                Log.LogWarning($"Could not get information about JdksRoot path `{JdksRoot}`: {e.Message}");
                Log.LogMessage(MessageImportance.Low, e.ToString());
            }
            return(jdk == null
                                ? Array.Empty <XATInfo>()
                                : new[] { jdk });
        }
        protected static IEnumerable <JdkInfo> GetWindowsRegistryJdks(
            Action <TraceLevel, string> logger,
            string parentKey,
            string childKeyGlob,
            string?grandChildKey,
            string valueName,
            string?locator = null)
        {
            if (!OS.IsWindows)
            {
                yield break;
            }

            var regex = ToRegex(childKeyGlob);
            var paths = new List <(Version version, string path)> ();
            var roots = new[] { RegistryEx.CurrentUser, RegistryEx.LocalMachine };
            var wows  = new[] { RegistryEx.Wow64.Key32, RegistryEx.Wow64.Key64 };

            foreach (var root in roots)
            {
                foreach (var wow in wows)
                {
                    foreach (var subkeyName in RegistryEx.EnumerateSubkeys(root, parentKey, wow))
                    {
                        if (!regex.Match(subkeyName).Success)
                        {
                            continue;
                        }
                        var key = $@"{parentKey}\{subkeyName}" +
                                  (grandChildKey == null ? "" : "\\" + grandChildKey);
                        var path = RegistryEx.GetValueString(root, key, valueName, wow);
                        if (path == null)
                        {
                            continue;
                        }
                        var jdk = JdkInfo.TryGetJdkInfo(path, logger, locator ?? $"Windows Registry @ {parentKey}");
                        if (jdk == null)
                        {
                            continue;
                        }
                        yield return(jdk);
                    }
                }
            }
        }
        protected static IEnumerable <JdkInfo> GetWindowsFileSystemJdks(string pattern, Action <TraceLevel, string> logger, string?locator = null)
        {
            if (!OS.IsWindows)
            {
                yield break;
            }

            var roots = new List <string>()
            {
                // "Compatibility" with existing codebases; should probably be avoided…
                Environment.ExpandEnvironmentVariables("%ProgramW6432%"),
                Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
                Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
            };

            foreach (var root in roots)
            {
                if (!Directory.Exists(root))
                {
                    continue;
                }
                IEnumerable <string> homes;
                try {
                    homes = Directory.EnumerateDirectories(root, pattern);
                }
                catch (IOException) {
                    continue;
                }
                foreach (var home in homes)
                {
                    if (!File.Exists(Path.Combine(home, "bin", "java.exe")))
                    {
                        continue;
                    }
                    var loc = locator ?? $"{pattern} via {root}";
                    var jdk = JdkInfo.TryGetJdkInfo(home, logger, loc);
                    if (jdk == null)
                    {
                        continue;
                    }
                    yield return(jdk);
                }
            }
        }
 protected override string?GetJavaSdkPath()
 {
     return(JdkInfo.GetKnownSystemJdkInfos(Logger).FirstOrDefault()?.HomePath);
 }
 IEnumerable <string> GetJavaSdkPaths()
 {
     return(JdkInfo.GetKnownSystemJdkInfos(Logger)
            .Select(jdk => jdk.HomePath));
 }