示例#1
0
        public async Task Test_Refresh_URL()
        {
            var repo = MavenRepository.FromUrl(ANDROID_THINGS_URL_REPO);
            await repo.Refresh();

            Assert.True(repo.Groups.Any());
        }
示例#2
0
        public static async Task BinderateAsync(BindingConfig config)
        {
            MavenRepository maven;

            if (config.MavenRepositoryType == MavenRepoType.Directory)
            {
                maven = MavenRepository.FromDirectory(config.MavenRepositoryLocation);
            }
            else if (config.MavenRepositoryType == MavenRepoType.Url)
            {
                maven = MavenRepository.FromUrl(config.MavenRepositoryLocation);
            }
            else if (config.MavenRepositoryType == MavenRepoType.MavenCentral)
            {
                maven = MavenRepository.FromMavenCentral();
            }
            else
            {
                maven = MavenRepository.FromGoogle();
            }

            await maven.Refresh(config.MavenArtifacts.Where(ma => !ma.DependencyOnly).Select(ma => ma.GroupId).Distinct().ToArray());

            if (config.DownloadExternals)
            {
                var artifactDir = Path.Combine(config.BasePath, config.ExternalsDir);
                if (!Directory.Exists(artifactDir))
                {
                    Directory.CreateDirectory(artifactDir);
                }
            }

            await ProcessConfig(maven, config);
        }
示例#3
0
        public async Task Test_Refresh_GOOGLE()
        {
            var repo = MavenRepository.FromGoogle();
            await repo.Refresh();

            Assert.True(repo.Groups.Any());
        }
示例#4
0
        public async Task Test_Refresh_FILE()
        {
            var repo = MavenRepository.FromDirectory(GPS_LOCAL_REPO);
            await repo.Refresh();

            Assert.IsTrue(repo.Groups.Any());
        }
示例#5
0
        static MavenRepository GetOrCreateRepository(MavenRepoType type, string location)
        {
            var key = $"{type}|{location}";

            if (repositories.TryGetValue(key, out MavenRepository repository))
            {
                return(repository);
            }

            MavenRepository maven;

            if (type == MavenRepoType.Directory)
            {
                maven = MavenRepository.FromDirectory(location);
            }
            else if (type == MavenRepoType.Url)
            {
                maven = MavenRepository.FromUrl(location);
            }
            else if (type == MavenRepoType.MavenCentral)
            {
                maven = MavenRepository.FromMavenCentral();
            }
            else
            {
                maven = MavenRepository.FromGoogle();
            }

            repositories.Add(key, maven);

            return(maven);
        }
示例#6
0
        public async Task Test_GroupIds_Project_URL()
        {
            var repo = MavenRepository.FromUrl(ANDROID_THINGS_URL_REPO);
            await repo.Refresh("com.google.android.things");

            var project = await repo.GetProjectAsync("com.google.android.things", "androidthings", "0.2-devpreview");

            Assert.True(project != null);
        }
示例#7
0
        public async Task Test_GroupIds_Project_URL()
        {
            var repo = MavenRepository.FromUrl(JCENTER_URL_REPO);
            await repo.Refresh("io.kommunicate.sdk");

            var project = await repo.GetProjectAsync("io.kommunicate.sdk", "kommunicateui", "2.0.5");

            Assert.True(project != null);
        }
示例#8
0
        public async Task Test_GroupIds_Project_URL_Maven()
        {
            var repo = MavenRepository.FromMavenCentral();
            await repo.Refresh("com.facebook.android");

            var project = await repo.GetProjectAsync("com.facebook.android", "facebook-android-sdk", "4.33.0");

            Assert.True(project != null);
            Assert.NotEmpty(project.Dependencies);
        }
示例#9
0
        public async Task Test_GroupIds_Project_MAVENCENTRAL()
        {
            var repo = MavenRepository.FromMavenCentral();
            await repo.Refresh("org.bouncycastle");

            var project = await repo.GetProjectAsync("org.bouncycastle", "bcpkix-jdk15on", "1.57");

            Assert.True(project != null);

            Assert.True(project.Dependencies?.Any());
        }
示例#10
0
        public async Task Test_GroupIds_Project_GOOGLE()
        {
            var repo = MavenRepository.FromGoogle();
            await repo.Refresh("com.google.android.gms");

            var project = await repo.GetProjectAsync("com.google.android.gms", "play-services-basement", "10.2.0");

            Assert.True(project != null);

            Assert.True(project.Dependencies?.Any());
        }
示例#11
0
        public async Task Test_Project_FILE()
        {
            var repo = MavenRepository.FromDirectory(GPS_LOCAL_REPO);
            await repo.Refresh();

            var project = await repo.GetProjectAsync("com.google.android.gms", "play-services-basement", "10.2.0");

            Assert.IsTrue(project != null);

            Assert.IsTrue(project.Dependencies?.Any());
        }
        public void Test_Maven_Google()
        {
            MavenRepository repo = MavenRepository.FromGoogle();

            repo.Refresh("androidx.car");

            global::MavenNet.Models.Project project;
            project = repo.GetProjectAsync("androidx.car", "car").Result;

            #if MSTEST
            Assert.IsNotNull(repo);
            #elif NUNIT
            Assert.NotNull(repo);
            #elif XUNIT
            Assert.NotNull(repo);
            #endif

            return;
        }
示例#13
0
        static async Task ProcessConfig(MavenRepository maven, BindingConfig config)
        {
            var mavenProjects = new Dictionary <string, Project>();
            var mavenGroups   = new List <MavenGroup>();

            foreach (var artifact in config.MavenArtifacts)
            {
                if (artifact.DependencyOnly)
                {
                    continue;
                }

                var mavenGroup = maven.Groups.FirstOrDefault(g => g.Id == artifact.GroupId);

                Project project = null;

                project = await maven.GetProjectAsync(artifact.GroupId, artifact.ArtifactId, artifact.Version);

                if (project != null)
                {
                    mavenProjects.Add($"{artifact.GroupId}/{artifact.ArtifactId}-{artifact.Version}", project);
                }
            }

            if (config.DownloadExternals)
            {
                await DownloadArtifacts(maven, config, mavenProjects);
            }

            var slnProjModels = new Dictionary <string, BindingProjectModel>();

            foreach (var template in config.Templates)
            {
                var models = BuildProjectModels(config, template, mavenProjects);

                var json = Newtonsoft.Json.JsonConvert.SerializeObject(models);

                if (config.Debug.DumpModels)
                {
                    File.WriteAllText(Path.Combine(config.BasePath, "models.json"), json);
                }

                var inputTemplateFile = Path.Combine(config.BasePath, template.TemplateFile);
                var templateSrc       = File.ReadAllText(inputTemplateFile);

                var engine = new RazorLightEngineBuilder()
                             .UseMemoryCachingProvider()
                             .Build();

                foreach (var model in models)
                {
                    var outputFile = new FileInfo(template.GetOutputFile(config, model));
                    if (!outputFile.Directory.Exists)
                    {
                        outputFile.Directory.Create();
                    }

                    string result = await engine.CompileRenderAsync(inputTemplateFile, templateSrc, model);

                    File.WriteAllText(outputFile.FullName, result);

                    // We want to collect all the models for the .csproj's so we can add them to a .sln file after
                    if (!slnProjModels.ContainsKey(outputFile.FullName) && template.OutputFileRule.EndsWith(".csproj"))
                    {
                        slnProjModels.Add(outputFile.FullName, model);
                    }
                }
            }

            if (!string.IsNullOrEmpty(config.SolutionFile))
            {
                var slnPath = Path.Combine(config.BasePath ?? AppDomain.CurrentDomain.BaseDirectory, config.SolutionFile);
                var sln     = SolutionFileBuilder.Build(config, slnProjModels);
                File.WriteAllText(slnPath, sln);
            }
        }
示例#14
0
        static async Task DownloadArtifacts(MavenRepository maven, BindingConfig config, Dictionary <string, Project> mavenProjects)
        {
            var httpClient = new HttpClient();

            foreach (var mavenArtifact in config.MavenArtifacts)
            {
                // Skip downloading dependencies
                if (mavenArtifact.DependencyOnly)
                {
                    continue;
                }

                var version = mavenArtifact.Version;

                if (!mavenProjects.TryGetValue($"{mavenArtifact.GroupId}/{mavenArtifact.ArtifactId}-{mavenArtifact.Version}", out var mavenProject))
                {
                    continue;
                }

                var artifactDir = Path.Combine(
                    config.BasePath,
                    config.ExternalsDir,
                    mavenArtifact.GroupId);
                var artifactFile = Path.Combine(artifactDir, config.DownloadExternalsWithFullName ? $"{mavenArtifact.GroupId}.{mavenArtifact.ArtifactId}.{mavenProject.Packaging}"
                                        : $"{mavenArtifact.ArtifactId}.{mavenProject.Packaging}");
                var md5File     = artifactFile + ".md5";
                var sha256File  = artifactFile + ".sha256";
                var sourcesFile = Path.Combine(artifactDir, config.DownloadExternalsWithFullName ? $"{mavenArtifact.GroupId}.{mavenArtifact.ArtifactId}-sources.jar"
                                        : $"{mavenArtifact.ArtifactId}-sources.jar");
                var artifactExtractDir = Path.Combine(artifactDir, mavenArtifact.ArtifactId);

                if (!Directory.Exists(artifactDir))
                {
                    Directory.CreateDirectory(artifactDir);
                }
                if (!Directory.Exists(artifactExtractDir))
                {
                    Directory.CreateDirectory(artifactExtractDir);
                }

                var mvnArt = maven.Groups.FirstOrDefault(g => g.Id == mavenArtifact.GroupId)?.Artifacts?.FirstOrDefault(a => a.Id == mavenArtifact.ArtifactId);

                // Download artifact
                using (var astrm = await mvnArt.OpenLibraryFile(mavenArtifact.Version, mavenProject.Packaging))
                    using (var sw = File.Create(artifactFile))
                        await astrm.CopyToAsync(sw);

                // Determine MD5
                try
                {
                    // First try download
                    using (var astrm = await mvnArt.OpenLibraryFile(mavenArtifact.Version, mavenProject.Packaging + ".md5"))
                        using (var sw = File.Create(md5File))
                            await astrm.CopyToAsync(sw);
                }
                catch
                {
                    // Then hash the downloaded artifact
                    using (var file = File.OpenRead(artifactFile))
                        File.WriteAllText(md5File, Util.HashMd5(file));
                }

                // Determine Sha256
                try
                {
                    // First try download, this almost certainly won't work
                    // but in case Maven ever starts supporting sha256 it should start
                    // they currently support .sha1 so there's no reason to believe the naming
                    // convention should be any different, and one day .sha256 may exist
                    using (var astrm = await mvnArt.OpenLibraryFile(mavenArtifact.Version, mavenProject.Packaging + ".sha256"))
                        using (var sw = File.Create(sha256File))
                            await astrm.CopyToAsync(sw);
                }
                catch
                {
                    // Create Sha256 hash if we couldn't download
                    using (var file = File.OpenRead(artifactFile))
                        File.WriteAllText(sha256File, Util.HashSha256(file));
                }

                if (config.DownloadJavaSourceJars)
                {
                    try
                    {
                        using (var astrm = await maven.OpenArtifactSourcesFile(mavenArtifact.GroupId, mavenArtifact.ArtifactId, version))
                            using (var sw = File.Create(sourcesFile))
                                await astrm.CopyToAsync(sw);
                    }
                    catch { }
                }

                if (Directory.Exists(artifactExtractDir))
                {
                    Directory.Delete(artifactExtractDir, true);
                }

                // Unzip artifact into externals
                if (mavenProject.Packaging.ToLowerInvariant() == "aar")
                {
                    ZipFile.ExtractToDirectory(artifactFile, artifactExtractDir);
                }
            }
        }
示例#15
0
        static async Task DownloadArtifacts(MavenRepository maven, BindingConfig config, Dictionary <string, Project> mavenProjects)
        {
            var httpClient = new HttpClient();

            foreach (var mavenArtifact in config.MavenArtifacts)
            {
                // Skip downloading dependencies
                if (mavenArtifact.DependencyOnly)
                {
                    continue;
                }

                var version = mavenArtifact.Version;

                if (!mavenProjects.TryGetValue($"{mavenArtifact.GroupId}/{mavenArtifact.ArtifactId}-{mavenArtifact.Version}", out var mavenProject))
                {
                    continue;
                }

                var artifactDir = Path.Combine(
                    config.BasePath,
                    config.ExternalsDir,
                    mavenArtifact.GroupId);
                var artifactFile       = Path.Combine(artifactDir, $"{mavenArtifact.ArtifactId}.{mavenProject.Packaging}");
                var md5File            = artifactFile + ".md5";
                var sourcesFile        = Path.Combine(artifactDir, $"{mavenArtifact.ArtifactId}-sources.jar");
                var artifactExtractDir = Path.Combine(artifactDir, mavenArtifact.ArtifactId);

                if (!Directory.Exists(artifactDir))
                {
                    Directory.CreateDirectory(artifactDir);
                }
                if (!Directory.Exists(artifactExtractDir))
                {
                    Directory.CreateDirectory(artifactExtractDir);
                }

                var mvnArt = maven.Groups.FirstOrDefault(g => g.Id == mavenArtifact.GroupId)?.Artifacts?.FirstOrDefault(a => a.Id == mavenArtifact.ArtifactId);

                // Download artifact
                using (var astrm = await mvnArt.OpenLibraryFile(mavenArtifact.Version, mavenProject.Packaging))
                    using (var sw = File.Create(artifactFile))
                        await astrm.CopyToAsync(sw);

                // Determine MD5
                try
                {
                    // First try download
                    using (var astrm = await mvnArt.OpenLibraryFile(mavenArtifact.Version, mavenProject.Packaging + ".md5"))
                        using (var sw = File.Create(md5File))
                            await astrm.CopyToAsync(sw);
                }
                catch
                {
                    // Then hash the downloaded artifact
                    using (var file = File.OpenRead(artifactFile))
                        File.WriteAllText(md5File, HashMd5(file));
                }

                if (config.DownloadJavaSourceJars)
                {
                    try
                    {
                        using (var astrm = await maven.OpenArtifactSourcesFile(mavenArtifact.GroupId, mavenArtifact.ArtifactId, version))
                            using (var sw = File.Create(sourcesFile))
                                await astrm.CopyToAsync(sw);
                    }
                    catch { }
                }

                if (Directory.Exists(artifactExtractDir))
                {
                    Directory.Delete(artifactExtractDir, true);
                }

                // Unzip artifact into externals
                if (mavenProject.Packaging.ToLowerInvariant() == "aar")
                {
                    ZipFile.ExtractToDirectory(artifactFile, artifactExtractDir);
                }
            }
        }
示例#16
0
        public static IReadOnlyList <string> Create(MavenGeneratorViewModel model)
        {
            MavenBuilder builder = new MavenBuilder
            {
                GroupId    = model.GroupId,
                ArtifactId = model.ArtifactId,
                Version    = model.Version,
                Packaging  = "jar"
            };

            builder.AddDefaults();

            MavenProperties properties = new MavenProperties();

            properties.AddProperty(new MavenProperty("java.version", model.JavaVersion));
            if (model.MainClass != null)
            {
                properties.AddProperty(new MavenProperty("main.class", model.MainClass));
            }

            if (model.DeployUrl != null)
            {
                MavenDistributionManagement distributionManagement = new MavenDistributionManagement();
                MavenRepository             repository             = new MavenRepository
                {
                    Id  = "deploy-id",
                    Url = model.DeployUrl
                };

                distributionManagement.AddRepository(repository);
                builder.PushElement(distributionManagement);
            }

            properties.AddProperty(new MavenProperty("project.build.sourceEncoding", "UTF-8"));

            MavenBuild        build        = new MavenBuild();
            MavenRepositories repositories = new MavenRepositories();
            MavenDependencies dependencies = new MavenDependencies();
            MavenPlugins      plugins      = new MavenPlugins();

            if (model.Language == Language.Kotlin)
            {
                build.AddElement(new MavenProperty("sourceDirectory", "src/main/kotlin"));

                properties.AddProperty(new MavenProperty("kotlin.version", "1.2.41"));
                // Enables experimental kotlin compiler
                properties.AddProperty(new MavenProperty("kotlin.compiler.incremental", "true"));

                MavenDependency kotlinDependency = new MavenDependency
                {
                    GroupId    = "org.jetbrains.kotlin",
                    ArtifactId = "kotlin-stdlib",
                    Version    = "${kotlin.version}",
                    Scope      = Maven.Data.Scope.Compile
                };
                dependencies.AddDependency(kotlinDependency);

                MavenPlugin kotlinPlugin = new MavenPlugin
                {
                    GroupId    = "org.jetbrains.kotlin",
                    ArtifactId = "kotlin-maven-plugin",
                    Version    = "${kotlin.version}"
                };
                CustomElement configuration = new CustomElement(3);
                configuration.AddLine("<executions>");
                configuration.AddLine(2, "<execution>");
                configuration.AddLine(3, "<id>compile</id>");
                configuration.AddLine(3, "<goals><goal>compile</goal></goals>");
                configuration.AddLine(3, "<configuration>");
                configuration.AddLine(4, "<sourceDirs>");
                configuration.AddLine(5, "<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>");
                configuration.AddLine(5, "<sourceDir>${project.basedir}/src/main/java</sourceDir>");
                configuration.AddLine(4, "</sourceDirs>");
                configuration.AddLine(3, "</configuration>");
                configuration.AddLine(2, "</execution>");
                configuration.AddLine("</executions>");
                kotlinPlugin.AddCustomElement(configuration);

                plugins.AddPlugin(kotlinPlugin);
            }

            if (model.IsBukkitPlugin)
            {
                properties.AddProperty(new MavenProperty("bukkit.version", "1.12.2-R0.1-SNAPSHOT"));

                MavenRepository bukkitRepository = new MavenRepository()
                {
                    Id  = "bukkit-repo",
                    Url = "https://hub.spigotmc.org/nexus/content/groups/public/"
                };
                repositories.AddRepository(bukkitRepository);

                MavenDependency bukkitDependency = new MavenDependency
                {
                    GroupId    = "org.bukkit",
                    ArtifactId = "bukkit",
                    Version    = "${bukkit.version}",
                    Scope      = Maven.Data.Scope.Provided
                };
                dependencies.AddDependency(bukkitDependency);
            }
            builder.PushElement(properties);
            builder.PushElement(repositories);
            if (model.IsBukkitPlugin)
            {
                builder.PushElement(dependencies);
            }

            foreach (Plugin plugin in model.Plugins)
            {
                switch (plugin)
                {
                case Plugin.Compiler:
                {
                    MavenPlugin compilerPlugin = new MavenPlugin
                    {
                        GroupId    = "org.apache.maven.plugins",
                        ArtifactId = "maven-compiler-plugin",
                        Version    = "3.7.0"
                    };
                    CustomElement configuration = new CustomElement(3);
                    configuration.AddLine("<configuration>");
                    configuration.AddLine(2, "<source>${java.version}</source>");
                    configuration.AddLine(2, "<target>${java.version}</target>");
                    configuration.AddLine("</configuration>");
                    compilerPlugin.AddCustomElement(configuration);

                    if (model.Language == Language.Kotlin)
                    {
                        CustomElement executions = new CustomElement(3);
                        executions.AddLine("<executions>");
                        executions.AddLine(2, "<execution>");
                        executions.AddLine(3, "<id>default-compile</id>");
                        executions.AddLine(3, "<phase>none</phase>");
                        executions.AddLine(2, "</execution>");
                        executions.AddLine("</executions>");

                        executions.AddLine("<executions>");
                        executions.AddLine(2, "<execution>");
                        executions.AddLine(3, "<id>java-compile</id>");
                        executions.AddLine(3, "<phase>compile</phase>");
                        executions.AddLine(3, "<goals>");
                        executions.AddLine(4, "<goal>compile</goal>");
                        executions.AddLine(3, "</goals>");
                        executions.AddLine(2, "</execution>");
                        executions.AddLine("</executions>");

                        compilerPlugin.AddCustomElement(executions);
                    }
                    plugins.AddPlugin(compilerPlugin);
                    break;
                }

                case Plugin.Shade: {
                    MavenPlugin shadePlugin = new MavenPlugin
                    {
                        GroupId    = "org.apache.maven.plugins",
                        ArtifactId = "maven-shade-plugin",
                        Version    = "3.1.1"
                    };

                    CustomElement executions = new CustomElement(3);
                    executions.AddLine("<executions>");
                    executions.AddLine(2, "<execution>");
                    executions.AddLine(3, "<phase>package</phase>");
                    executions.AddLine(3, "<goals>");
                    executions.AddLine(4, "<goal>shade</goal>");
                    executions.AddLine(3, "</goals>");
                    executions.AddLine(2, "</execution>");
                    executions.AddLine("</executions>");
                    shadePlugin.AddCustomElement(executions);
                    plugins.AddPlugin(shadePlugin);
                    break;
                }

                case Plugin.Jar:
                {
                    MavenPlugin jarPlugin = new MavenPlugin
                    {
                        GroupId    = "org.apache.maven.plugins",
                        ArtifactId = "maven-jar-plugin",
                        Version    = "3.1.0"
                    };

                    if (model.MainClass != null)
                    {
                        CustomElement configuration = new CustomElement(3);
                        configuration.AddLine("<configuration>");
                        configuration.AddLine(2, "<archive>");
                        configuration.AddLine(3, "<manifest>");
                        configuration.AddLine(4, "<addClasspath>true</addClasspath>");
                        configuration.AddLine(4, "<mainClass>${main.class}</mainClass>");
                        configuration.AddLine(3, "</manifest>");
                        configuration.AddLine(2, "</archive>");
                        configuration.AddLine("</configuration>");
                        jarPlugin.AddCustomElement(configuration);
                    }
                    plugins.AddPlugin(jarPlugin);
                    break;
                }

                case Plugin.Assembly:
                {
                    MavenPlugin jarPlugin = new MavenPlugin
                    {
                        GroupId    = "org.apache.maven.plugins",
                        ArtifactId = "maven-assembly-plugin",
                        Version    = "3.1.0"
                    };
                    CustomElement configuration = new CustomElement(3);
                    configuration.AddLine("<configuration>");
                    if (model.MainClass != null)
                    {
                        configuration.AddLine(2, "<archive>");
                        configuration.AddLine(3, "<manifest>");
                        configuration.AddLine(4, "<addClasspath>true</addClasspath>");
                        configuration.AddLine(4, "<mainClass>${main.class}</mainClass>");
                        configuration.AddLine(3, "</manifest>");
                        configuration.AddLine(2, "</archive>");
                    }
                    configuration.AddLine(2, "<descriptorRefs>");
                    configuration.AddLine(3, "<descriptorRef>jar-with-dependencies</descriptorRef>");
                    configuration.AddLine(2, "</descriptorRefs>");
                    configuration.AddLine("</configuration>");

                    jarPlugin.AddCustomElement(configuration);
                    plugins.AddPlugin(jarPlugin);
                    break;
                }
                }
            }

            build.AddElement(plugins);

            MavenResources resources = new MavenResources();
            MavenResource  resource  = new MavenResource(); // Default: src/main/resources

            resources.AddResource(resource);
            build.AddElement(resources);

            builder.PushElement(build);

            return(builder.GetGeneratedLines());
        }