Path: blob/trunk/dotnet/private/GenerateResourcesTool.cs
10192 views
// <copyright file="GenerateResourcesTool.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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.
// </copyright>
// Generates C# ResourceUtilities partial class with embedded JS resources.
//
// Usage:
// generate_resources_tool --output path/to/ResourceUtilities.g.cs \
// --input Ident1=path/to/file1.js \
// --input Ident2=path/to/file2.js ...
//
// Each identifier becomes an internal const string in ResourceUtilities class.
// The content is emitted as a C# raw string literal using 5-quotes.
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
string? output = null;
var inputs = new List<(string Name, string Path)>();
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "--output" when i + 1 < args.Length:
output = args[++i];
break;
case "--input" when i + 1 < args.Length:
var spec = args[++i];
var eqIndex = spec.IndexOf('=');
if (eqIndex < 1 || eqIndex == spec.Length - 1)
{
Console.Error.WriteLine($"Invalid --input value, expected IDENT=path, got: {spec}");
return 1;
}
inputs.Add((spec[..eqIndex].Trim(), spec[(eqIndex + 1)..].Trim()));
break;
default:
Console.Error.WriteLine($"Unknown argument: {args[i]}");
return 1;
}
}
if (output is null)
{
Console.Error.WriteLine("Missing required --output argument.");
return 1;
}
var sb = new StringBuilder();
sb.AppendLine("// <auto-generated />");
sb.AppendLine("namespace OpenQA.Selenium.Internal;");
sb.AppendLine();
sb.AppendLine("internal static partial class ResourceUtilities");
sb.AppendLine("{");
foreach (var (name, path) in inputs)
{
var content = File.ReadAllText(path, Encoding.UTF8);
// Use a C# raw string literal with five quotes.
// The content must start on a new line and the closing quotes
// must be on their own line as well.
sb.AppendLine($" internal const string {name} = {new string('"', 5)}");
sb.AppendLine(content);
sb.AppendLine($"{new string('"', 5)};");
}
sb.AppendLine("}");
var dir = Path.GetDirectoryName(output);
if (!string.IsNullOrEmpty(dir))
{
Directory.CreateDirectory(dir);
}
File.WriteAllText(output, sb.ToString(), new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
return 0;