summaryrefslogtreecommitdiff
path: root/profgen/profgen-cli/src/main/kotlin/com/android/tools/profgen/cli/ExpandWildcardsCommand.kt
blob: 9557dafd2b4967e3f25e22a6d4cdb016d6b5468c (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tools.profgen.cli

import com.android.tools.profgen.ArchiveClassFileResourceProvider
import com.android.tools.profgen.CLASS_EXTENSION
import com.android.tools.profgen.ClassFileResource
import com.android.tools.profgen.getClassDescriptorFromBinaryName
import com.android.tools.profgen.JAR_EXTENSION
import kotlin.io.path.Path
import kotlinx.cli.ArgType
import kotlinx.cli.ExperimentalCli
import kotlinx.cli.Subcommand
import kotlinx.cli.required
import kotlinx.cli.vararg
import kotlin.io.path.Path

@ExperimentalCli
class ExpandWildcardsCommand: Subcommand("expandWildcards", "Dump a binary profile to a HRF") {
    val hrpPath by option(ArgType.String, "profile", "p", "File path to the human readable profile")
        .required()
    val outPath by option(
        ArgType.String, "output", "o",
        "File path for the resulting human readable profile without wildcards"
    )
        .required()
    val programPaths by argument(
        ArgType.String,
        "program",
        "File paths to program sources (.class or .jar). "
                + "Class files must be on the form <src dir>:<path to file>.class, e.g., "
                + "src:pkg/Main.class.")
        .vararg()
    override fun execute() {
        val hrpFile = Path(hrpPath).toFile()
        require(hrpFile.exists()) { "File not found: $hrpPath" }

        val outFile = Path(outPath).toFile()
        require(outFile.parentFile.exists()) { "Directory does not exist: ${outFile.parent}" }

        require(programPaths.isNotEmpty()) { "Must pass at least one program source" }

        val hrp = readHumanReadableProfileOrExit(hrpFile)
        val archiveClassFileResourceProviders = mutableListOf<ArchiveClassFileResourceProvider>()
        val classFileResources = mutableListOf<ClassFileResource>()
        for (programPath in programPaths) {
            if (programPath.endsWith(CLASS_EXTENSION)) {
                val separatorIndex = programPath.lastIndexOf(':')
                require(separatorIndex >= 0) {
                    "Missing ':' separator for class file: $programPath"
                }
                val classBinaryName =
                    programPath.substring(separatorIndex + 1).dropLast(CLASS_EXTENSION.length)
                val classDescriptor = getClassDescriptorFromBinaryName(classBinaryName)
                val programFile =
                    Path(programPath.substring(0, separatorIndex), "$classBinaryName.class")
                        .toFile()
                require(programFile.exists()) { "File not found: $programFile" }
                classFileResources += ClassFileResource(classDescriptor, programFile.toPath())
            } else if (programPath.endsWith(JAR_EXTENSION)) {
                val programFile = Path(programPath).toFile()
                require(programFile.exists()) { "File not found: $programPath" }
                val archiveClassFileResourceProvider =
                    ArchiveClassFileResourceProvider(programFile.toPath())
                archiveClassFileResourceProviders += archiveClassFileResourceProvider
                classFileResources += archiveClassFileResourceProvider.getClassFileResources()
            } else {
                throw IllegalArgumentException("Unexpected program file: $programPath")
            }
        }
        val result = hrp.expandWildcards(classFileResources)
        outFile.printWriter().use {
            result.printExact(it)
        }
        for (archiveClassFileResourceProvider in archiveClassFileResourceProviders) {
            try {
                archiveClassFileResourceProvider.close()
            } catch (throwable: Throwable) {
            }
        }
    }
}