public void JavaTypesAreMigratedAfterJetifierWithIntermediate() { var migratedAar = Utils.GetTempFilename(); var jetifier = new Jetifier(); jetifier.UseIntermediateFile = true; var result = jetifier.Jetify(SupportAar, migratedAar); Assert.True(result); var jar = ReadAarEntry(migratedAar, "classes.jar"); var classPath = new ClassPath(); classPath.Load(jar); var packages = classPath.GetPackages(); Assert.True(packages.Count > 0); Assert.Equal("com.xamarin.aarxercise", packages.Keys.FirstOrDefault()); var classes = packages["com.xamarin.aarxercise"]; var simpleFragment = classes.FirstOrDefault(c => c.ThisClass.Name.Value == "com/xamarin/aarxercise/SimpleFragment"); Assert.Equal("androidx/fragment/app/Fragment", simpleFragment.SuperClass.Name.Value); }
public override bool Execute() { Log.LogMessage("GenerateParameterNames Task"); Log.LogMessage(" ApiOutputFile: {0}", ApiOutputFile); Log.LogMessage(" GeneratedFile: {0}", GeneratedFile); Log.LogMessage(" SourceJars: {0}", string.Join(";", SourceJars.Select(x => x.ItemSpec))); Log.LogMessage(" TransformFiles: {0}", string.Join(";", TransformFiles.Select(x => x.ItemSpec))); var generatorParameters = new GeneratorParameters { ReservedPrefix = ReservedPrefix ?? string.Empty, ReservedSuffix = ReservedSuffix ?? string.Empty, ParameterCasing = (TextCasing)Enum.Parse(typeof(TextCasing), ParameterCasing, true), ForceMeaningfulParameterNames = ForceMeaningfulParameterNames }; // create the folder var dir = Path.GetDirectoryName(GeneratedFile.ItemSpec); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } // read the jar files var classPath = new ClassPath() { ApiSource = "class-parse" }; foreach (var jarFile in SourceJars) { if (ClassPath.IsJarFile(jarFile.ItemSpec)) { classPath.Load(jarFile.ItemSpec); } } // build up the object tree var metadataElement = classPath.ToXElement(); // remove any nodes that the user wants metadataElement = TransformXml(metadataElement); var packages = JavaPackage.Parse(metadataElement); var xParameters = packages.SelectMany(p => p.ToXElement(generatorParameters)); // create the new xml document var xDoc = new XDocument( new XElement("metadata", xParameters.ToArray())); // make sure we don't have anything not in the api.xml RemoveIgnoredApiXmlMembers(xDoc); // save xDoc.Save(GeneratedFile.ItemSpec); return(true); }
//[Fact] public async Task JetifierWrapperMigratesFacebookAar() { var facebookFilename = "facebook-android-sdk"; var facebookVersion = "4.40.0"; var facebookFullName = $"{facebookFilename}-{facebookVersion}"; var facebookTestUrl = $"https://origincache.facebook.com/developers/resources/?id={facebookFullName}.zip"; var workspace = Utils.GetTempFilename(); Directory.CreateDirectory(workspace); // download facebook var facebookZip = Path.Combine(workspace, "facebook.zip"); await Utils.DownloadFileAsync(facebookTestUrl, facebookZip); ZipFile.ExtractToDirectory(facebookZip, workspace); // run jetifier var aarFiles = Directory.GetFiles(workspace, "*.aar", SearchOption.AllDirectories); var pairs = aarFiles.Select(f => new MigrationPair(f, Path.ChangeExtension(f, "jetified.aar"))).ToArray(); var jetifier = new Jetifier(); var result = jetifier.Jetify(pairs); Assert.True(result); // read the classes var commonAar = pairs.FirstOrDefault(pair => Path.GetFileNameWithoutExtension(pair.Source) == "facebook-common"); var jar = ReadAarEntry(commonAar.Destination, "classes.jar"); var classPath = new ClassPath(); classPath.Load(jar); var packages = classPath.GetPackages(); // read the type var classes = packages["com.facebook"]; var activity = classes.FirstOrDefault(c => c.ThisClass.Name.Value == "com/facebook/FacebookActivity"); // read the layout var migratedLayout = ReadAarEntry(commonAar.Destination, "res/layout/com_facebook_device_auth_dialog_fragment.xml"); // check Assert.Equal("androidx/fragment/app/FragmentActivity", activity.SuperClass.Name.Value); Assert.Equal( "androidx.cardview.widget.CardView", XDocument.Load(migratedLayout).Root.Name.LocalName); }
static void DumpFileToXml(ClassPath jar, string file) { using (var s = File.OpenRead(file)) { if (ClassFile.IsClassFile(s)) { s.Position = 0; var c = new ClassFile(s); jar.Add(c); return; } } if (ClassPath.IsJarFile(file)) { jar.Load(file); return; } Console.Error.WriteLine("class-parse: Unable to read file '{0}': Unknown file format."); Environment.ExitCode = 1; }
public void CanReadJarFileAfterMigration() { var migratedAar = Utils.GetTempFilename(); var jetifier = new Jetifier(); var result = jetifier.Jetify(SupportAar, migratedAar); Assert.True(result); var jar = ReadAarEntry(migratedAar, "classes.jar"); var classPath = new ClassPath(); classPath.Load(jar); var packages = classPath.GetPackages(); Assert.True(packages.Count > 0); Assert.Equal("com.xamarin.aarxercise", packages.Keys.FirstOrDefault()); var classes = packages["com.xamarin.aarxercise"]; Assert.True(classes.Count > 0); }
protected override bool OnInvoke(IEnumerable <string> extras) { if (Program.Verbose) { Console.WriteLine($"Looking for the following classes:"); foreach (var javaClass in JavaClasses) { Console.WriteLine($" - {javaClass}"); } Console.WriteLine($"In the following directories:"); foreach (var directory in Directories) { Console.WriteLine($" - {directory}"); } } foreach (var directory in Directories) { var aarFiles = Directory.EnumerateFiles(directory, "*.aar", SearchOption.AllDirectories); var jarFiles = Directory.EnumerateFiles(directory, "*.jar", SearchOption.AllDirectories); var artifacts = jarFiles.Union(aarFiles); foreach (var artifact in artifacts) { if (Program.Verbose) { Console.WriteLine($"Processing {artifact}..."); } foreach (var javaClass in JavaClasses) { if (string.IsNullOrWhiteSpace(javaClass)) { continue; } Stream jarStream = null; try { if (Path.GetExtension(artifact).Equals(".aar", StringComparison.OrdinalIgnoreCase)) { jarStream = ReadZipEntry(artifact, "classes.jar"); } else if (Path.GetExtension(artifact).Equals(".jar", StringComparison.OrdinalIgnoreCase)) { jarStream = File.OpenRead(artifact); } if (jarStream != null) { var classPath = new ClassPath(); classPath.Load(jarStream); DoSearch(artifact, classPath, javaClass); } } finally { jarStream?.Dispose(); } } } } return(true); }