Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PatchExclusionListInApks.cs « TestExclusionListTasks « tasks « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 254bef87ef6274036ec64d1473526f0b6d06f732 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace TestExclusionListTasks;

public class PatchExclusionListInApks : Task
{
    [Required]
    public ITaskItem[]? ApkPaths { get; set; }

    [Required]
    public ITaskItem[]? ExcludedTests { get; set; }

    public string? AndroidSdk { get; set; }

    public string? MinApiLevel { get; set; }

    public string? BuildToolsVersion { get; set; }

    public string? KeyStorePath { get; set; }

    public override bool Execute()
    {
        var apkBuilder = new ApkBuilder(Log);
        apkBuilder.AndroidSdk = AndroidSdk;
        apkBuilder.MinApiLevel = MinApiLevel;
        apkBuilder.BuildToolsVersion = BuildToolsVersion;
        apkBuilder.KeyStorePath = KeyStorePath;

        string testExclusionList = string.Join(
            '\n',
            (ExcludedTests ?? Enumerable.Empty<ITaskItem>()).Select(t => t.ItemSpec));
        foreach (ITaskItem apk in ApkPaths ?? Enumerable.Empty<ITaskItem>())
        {
            string apkPath = apk.GetMetadata("FullPath")!;
            apkBuilder.OutputDir = Path.GetDirectoryName(apkPath)!;
            using (ZipArchive apkArchive = ZipFile.Open(apkPath, ZipArchiveMode.Update))
            {
                ZipArchiveEntry assetsZipEntry = apkArchive.GetEntry("assets/assets.zip")!;
                using ZipArchive assetsArchive = new ZipArchive(assetsZipEntry.Open(), ZipArchiveMode.Update);
                ZipArchiveEntry testExclusionListEntry = assetsArchive.GetEntry("TestExclusionList.txt")!;
                using StreamWriter textExclusionListWriter = new StreamWriter(testExclusionListEntry.Open());
                textExclusionListWriter.WriteLine(testExclusionList);
            }
            apkBuilder.ZipAndSignApk(apkPath);
        }
        return true;
    }
}