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

CommandLineHelpers.cs « CommandLine « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 25f2c7b9851919a080f75d082dfae2e66938595f (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// 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.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

namespace Internal.CommandLine
{
    //
    // Helpers for command line processing
    //
    internal class Helpers
    {
        // Helper to create a collection of paths unique in their simple names.
        public static void AppendExpandedPaths(Dictionary<string, string> dictionary, string pattern, bool strict)
        {
            bool empty = true;

            string directoryName = Path.GetDirectoryName(pattern);
            string searchPattern = Path.GetFileName(pattern);

            if (directoryName == "")
                directoryName = ".";

            if (Directory.Exists(directoryName))
            {
                foreach (string fileName in Directory.EnumerateFiles(directoryName, searchPattern))
                {
                    string fullFileName = Path.GetFullPath(fileName);

                    string simpleName = Path.GetFileNameWithoutExtension(fileName);

                    if (dictionary.ContainsKey(simpleName))
                    {
                        if (strict)
                        {
                            throw new CommandLineException("Multiple input files matching same simple name " +
                                fullFileName + " " + dictionary[simpleName]);
                        }
                    }
                    else
                    {
                        dictionary.Add(simpleName, fullFileName);
                    }

                    empty = false;
                }
            }

            if (empty)
            {
                if (strict)
                {
                    throw new CommandLineException("No files matching " + pattern);
                }
                else
                {
                    Console.WriteLine("Warning: No files matching " + pattern);
                }
            }
        }

// ILVerify needs to switch command line processing to Internal.CommandLine and then it can take advantage of this too
#if !ILVERIFY
        public static void MakeReproPackage(string makeReproPath, string outputFilePath, string[] args, ArgumentSyntax argSyntax, IEnumerable<string> inputOptions)
        {
            Directory.CreateDirectory(makeReproPath);

            List<string> details = new List<string>();
            details.Add("Tool version");
            try
            {
                details.Add(Environment.GetCommandLineArgs()[0]);
            }
            catch { }
            try
            {
                details.Add(System.Diagnostics.FileVersionInfo.GetVersionInfo(Environment.GetCommandLineArgs()[0]).ToString());
            }
            catch { }

            details.Add("------------------------");
            details.Add("Actual Command Line Args");
            details.Add("------------------------");
            details.AddRange(args);
            foreach (string arg in args)
            {
                if (arg.StartsWith('@'))
                {
                    string rspFileName = arg.Substring(1);
                    details.Add("------------------------");
                    details.Add(rspFileName);
                    details.Add("------------------------");
                    try
                    {
                        details.AddRange(File.ReadAllLines(rspFileName));
                    }
                    catch { }
                }
            }

            HashCode hashCodeOfArgs = new HashCode();
            foreach (string s in details)
                hashCodeOfArgs.Add(s);

            string zipFileName = ((uint)hashCodeOfArgs.ToHashCode()).ToString();

            if (outputFilePath != null)
                zipFileName = zipFileName + "_" + Path.GetFileName(outputFilePath);

            zipFileName = Path.Combine(makeReproPath, Path.ChangeExtension(zipFileName, ".zip"));

            Console.WriteLine($"Creating {zipFileName}");
            using (var archive = ZipFile.Open(zipFileName, ZipArchiveMode.Create))
            {
                ZipArchiveEntry commandEntry = archive.CreateEntry("command.txt");
                using (StreamWriter writer = new StreamWriter(commandEntry.Open()))
                {
                    foreach (string s in details)
                        writer.WriteLine(s);
                }

                HashSet<string> inputOptionNames = new HashSet<string>(inputOptions);
                Dictionary<string, string> inputToReproPackageFileName = new Dictionary<string, string>();

                List<string> rspFile = new List<string>();
                foreach (var option in argSyntax.GetOptions())
                {
                    if (option.GetDisplayName() == "--make-repro-path")
                    {
                        continue;
                    }

                    if (option.Value != null && !option.Value.Equals(option.DefaultValue))
                    {
                        if (option.IsList)
                        {
                            if (inputOptionNames.Contains(option.GetDisplayName()))
                            {
                                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                                foreach (string optInList in (IEnumerable)option.Value)
                                {
                                    Helpers.AppendExpandedPaths(dictionary, optInList, false);
                                }
                                foreach (string inputFile in dictionary.Values)
                                {
                                    rspFile.Add($"{option.GetDisplayName()}:{ConvertFromInputPathToReproPackagePath(inputFile)}");
                                }
                            }
                            else
                            {
                                foreach (object optInList in (IEnumerable)option.Value)
                                {
                                    rspFile.Add($"{option.GetDisplayName()}:{optInList}");
                                }
                            }
                        }
                        else
                        {
                            rspFile.Add($"{option.GetDisplayName()}:{option.Value}");
                        }
                    }
                }

                foreach (var parameter in argSyntax.GetParameters())
                {
                    if (parameter.Value != null)
                    {
                        if (parameter.IsList)
                        {
                            foreach (object optInList in (IEnumerable)parameter.Value)
                            {
                                rspFile.Add($"{ConvertFromInputPathToReproPackagePath((string)optInList)}");
                            }
                        }
                        else
                        {
                            rspFile.Add($"{ConvertFromInputPathToReproPackagePath((string)parameter.Value.ToString())}");
                        }
                    }
                }

                ZipArchiveEntry rspEntry = archive.CreateEntry("repro.rsp");
                using (StreamWriter writer = new StreamWriter(rspEntry.Open()))
                {
                    foreach (string s in rspFile)
                        writer.WriteLine(s);
                }

                string ConvertFromInputPathToReproPackagePath(string inputPath)
                {
                    if (inputToReproPackageFileName.TryGetValue(inputPath, out string reproPackagePath))
                    {
                        return reproPackagePath;
                    }

                    try
                    {
                        string inputFileDir = inputToReproPackageFileName.Count.ToString();
                        reproPackagePath = Path.Combine(inputFileDir, Path.GetFileName(inputPath));
                        archive.CreateEntryFromFile(inputPath, reproPackagePath);
                        inputToReproPackageFileName.Add(inputPath, reproPackagePath);

                        return reproPackagePath;
                    }
                    catch
                    {
                        return inputPath;
                    }
                }
            }
        }
#endif // !ILVERIFY
    }
}