Skip to content

Commit

Permalink
[csharp] Port csharp scripts in src/ to .NET projects. (#21498)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfbjarne authored Oct 29, 2024
1 parent e581020 commit f11253b
Show file tree
Hide file tree
Showing 22 changed files with 256 additions and 57 deletions.
45 changes: 45 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Scripts

This directory contains numerous short C# scripts, each in their own directory
with its own project file.

To create a new script, the easy way is to run the `new-script.sh` script.

The harder way is to copy an existing script directory, and:

* Rename the csproj file to match the directory name.
* Add your C# code, and document what it's supposed to do in a README.md file.
* Edit the arguments to the `TemplateScript` template in the `fragment.mk`
file according to how you named your script (directory). The first argument
will be used in other makefiles that use the script, the second is the name
of the script (directory). Say your script is `my-script`, then that would be:

```make
$(eval $(call TemplateScript,MY_SCRIPT,my-script))

To use the new script:

1. In the consuming `Makefile`, import the `fragment.mk` file from the script directory:

```make
include $(TOP)/scripts/my-script/fragment.mk
```

2. In the target where you want to execute the script, depend on the script executable, which is named `MY_SCRIPT` (from the call to the `TemplateScript` template):

```make
dostuff: $(MY_SCRIPT)
echo "Doing stuff"
```

This makes sure the script is actually built before you want to execute it.

3. The actual invocation to call the script, is the same variable, but with `_EXEC` appended:

```make
dostuff: $(MY_SCRIPT)
$(MY_SCRIPT_EXEC) --arguments to/my/script
```

Sidenote: if https://github.com/dotnet/designs/pull/296 were ever implemented,
we could dispense with the project file, making these C# files actual scripts.
17 changes: 17 additions & 0 deletions scripts/generate-defines/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# generate-defines

This script takes the list of frameworks that are supported for a given
platform, and generates a response file for the C# compiler with a
`HAS_<framework>` define for each framework.

Example output file for iOS:

```
-d:HAS_ACCELERATE
-d:HAS_ACCESSIBILITY
-d:HAS_ACCESSORYSETUPKIT
-d:HAS_ACCOUNTS
[...]
-d:HAS_WEBKIT
-d:HAS_XKIT
```
2 changes: 2 additions & 0 deletions scripts/generate-defines/fragment.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include $(TOP)/scripts/template.mk
$(eval $(call TemplateScript,GENERATE_DEFINES,generate-defines))
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/usr/bin/env /Library/Frameworks/Mono.framework/Commands/csharp -s

using System.IO;
using System.Text;

try {
var args = Args;
var expectedArgumentCount = 2;
if (args.Length != expectedArgumentCount) {
Console.WriteLine ($"Need {expectedArgumentCount} arguments, got {args.Length} arguments");
Environment.Exit (1);
return;
return 1;
}

var output = args [0];
Expand All @@ -20,8 +16,8 @@
sb.AppendLine ($"-d:HAS_{fw.ToUpperInvariant ()}");
File.WriteAllText (output, sb.ToString ());

Environment.Exit (0);
return 0;
} catch (Exception e) {
Console.WriteLine ("Failed: {0}", e);
Environment.Exit (1);
return 1;
}
5 changes: 5 additions & 0 deletions scripts/generate-defines/generate-defines.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)</TargetFramework>
</PropertyGroup>
</Project>
29 changes: 29 additions & 0 deletions scripts/generate-errors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# generate-errors

This script takes a resource resx as input, and generates a C# file with constants for each resource string (which won't be localizable, because they're constant strings).

Example input:

```xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<data name="BI0000" xml:space="preserve">
<value>Unexpected error - Please fill a bug report at https://github.com/xamarin/xamarin-macios/issues/new</value>
</data>
</root>
```

Example output:

```cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool: generate-errors.csharp
// </auto-generated>
//------------------------------------------------------------------------------
namespace Xamarin.Bundler {
internal class Errors {
internal const string MT0000 = "Unexpected error - Please fill a bug report at https://github.com/xamarin/xamarin-macios/issues/new";
}
}
```
2 changes: 2 additions & 0 deletions scripts/generate-errors/fragment.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include $(TOP)/scripts/template.mk
$(eval $(call TemplateScript,GENERATE_ERRORS,generate-errors))
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/env /Library/Frameworks/Mono.framework/Commands/csharp -s

using System.IO;
using System.Text;
using System.Xml;

var args = Environment.GetCommandLineArgs ();
var dir = Path.GetDirectoryName (args [1]);
var file = Path.Combine (dir, "..", "tools", "mtouch", "Errors.resx");
var file = args [0];
var output = args [1];
var doc = new XmlDocument ();
doc.Load (file);

Console.Write (
var sb = new StringBuilder ();
sb.Append (
@"//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool: generate-errors.csharp
Expand All @@ -20,18 +19,17 @@ namespace Xamarin.Bundler {
internal class Errors {
");

foreach (XmlNode node in doc.SelectNodes ("/root/data")) {
var name = node.Attributes ["name"].InnerText;
foreach (XmlNode node in doc.SelectNodes ("/root/data")!) {
var name = node.Attributes! ["name"]!.InnerText;
if (name == "default") {
name = "@" + name;
}
var value = node.ChildNodes [1].InnerText
var value = node.ChildNodes [1]!.InnerText
.Trim ()
.Replace ("\\", "\\\\")
.Replace ("\"", "\\\"");
Console.WriteLine ($"\t\tinternal const string {name} = \"{value}\";");
sb.AppendLine ($"\t\tinternal const string {name} = \"{value}\";");
}

Console.WriteLine ("\t}\n}");

Environment.Exit (0);
sb.AppendLine ("\t}\n}");
File.WriteAllText (output, sb.ToString ());
5 changes: 5 additions & 0 deletions scripts/generate-errors/generate-errors.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)</TargetFramework>
</PropertyGroup>
</Project>
29 changes: 29 additions & 0 deletions scripts/generate-frameworks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# generate-frameworks

This script takes the list of frameworks that are supported for all platforms,
and generates a C# file with a hashtable of all the frameworks for each
platform.

Example output file for iOS:

```cs
using System.Collections.Generic;

partial class Frameworks {
internal readonly HashSet<string> iosframeworks = new HashSet<string> {
"Accelerate",
"Accessibility",
"AccessorySetupKit",
/// ...
"WebKit",
"XKit",
};
internal readonly HashSet<string> macosframeworks = new HashSet<string> {
"Accelerate",
"Accessibility",
/// ...
"WebKit",
"XKit",
};
}
```
2 changes: 2 additions & 0 deletions scripts/generate-frameworks/fragment.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include $(TOP)/scripts/template.mk
$(eval $(call TemplateScript,GENERATE_FRAMEWORKS,generate-frameworks))
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/usr/bin/env /Library/Frameworks/Mono.framework/Commands/csharp -s

using System.IO;
using System.Text;

try {
var args = Args;
var actualArgumentCount = 6;
if (args.Length != actualArgumentCount) {
Console.WriteLine ($"Need {actualArgumentCount} arguments, got {args.Length} arguments");
Environment.Exit (1);
return;
return 1;
}

var csharpOutput = args [0];
Expand Down Expand Up @@ -67,8 +63,8 @@

File.WriteAllText (csharpOutput, sb.ToString ());

Environment.Exit (0);
return 0;
} catch (Exception e) {
Console.WriteLine ("Failed: {0}", e);
Environment.Exit (1);
return 1;
}
5 changes: 5 additions & 0 deletions scripts/generate-frameworks/generate-frameworks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)</TargetFramework>
</PropertyGroup>
</Project>
18 changes: 18 additions & 0 deletions scripts/generate-sourcelink-json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# generate-sourcelink-json

This script generates a SourceLink.json file that maps paths of local source
code into links to GitHub source code.

This SourceLink.json file is then passed to the C# compiler, which embeds it
in the pdb file, and which is then consumed by debuggers or IDEs to find the
source code online for any given source file the pdb refers to.

Example output file:

```json
{
"documents": {
"/local/path/to/xamarin-macios/src*": "https://raw.githubusercontent.com/xamarin/xamarin-macios/c2c617bf000c4ff864cbba9d65421f915941136b/src*"
}
}
```
2 changes: 2 additions & 0 deletions scripts/generate-sourcelink-json/fragment.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include $(TOP)/scripts/template.mk
$(eval $(call TemplateScript,GENERATE_SOURCELINK_JSON,generate-sourcelink-json))
15 changes: 15 additions & 0 deletions scripts/generate-sourcelink-json/generate-sourcelink-json.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.IO;
using System.Text;

var idx = 0;
var latestCommit = args [idx++];
var src = args [idx++];
var outputPath = args [idx++];

using (var writer = new StreamWriter (outputPath)) {
writer.WriteLine ("{");
writer.WriteLine (" \"documents\": {");
writer.WriteLine ($" \"{src}*\": \"https://raw.githubusercontent.com/xamarin/xamarin-macios/{latestCommit}/src*\"");
writer.WriteLine (" }");
writer.WriteLine ("}");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)</TargetFramework>
</PropertyGroup>
</Project>
34 changes: 34 additions & 0 deletions scripts/new-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash -e

echo 'Run this script to create a new script!'

echo ""
if test -z "$SCRIPT_NAME"; then
read -p "What's the name of your new script? " SCRIPT_NAME
if test -z "$SCRIPT_NAME"; then
echo "Exiting, no script name provided"
exit 0
fi
fi

echo "Name of the new script: $SCRIPT_NAME"

UPPER_NAME=$(echo "$SCRIPT_NAME" | tr 'a-z' "A-Z" | tr '-' '_')
echo "Variable name: $UPPER_NAME"

mkdir -p "$SCRIPT_NAME"

printf 'include $(TOP)/scripts/template.mk\n' > "$SCRIPT_NAME/fragment.mk"
printf "\$(eval \$(call TemplateScript,$UPPER_NAME,$SCRIPT_NAME))\n" >> "$SCRIPT_NAME/fragment.mk"

printf '<Project Sdk="Microsoft.NET.Sdk">\n' > "$SCRIPT_NAME/$SCRIPT_NAME.csproj"
printf ' <PropertyGroup>\n' >> "$SCRIPT_NAME/$SCRIPT_NAME.csproj"
printf ' <TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)</TargetFramework>\n' >> "$SCRIPT_NAME/$SCRIPT_NAME.csproj"
printf ' </PropertyGroup>\n' >> "$SCRIPT_NAME/$SCRIPT_NAME.csproj"
printf '</Project>\n' >> "$SCRIPT_NAME/$SCRIPT_NAME.csproj"

printf "Console.WriteLine (\"Hello $SCRIPT_NAME\");\n" > "$SCRIPT_NAME/$SCRIPT_NAME.cs"

printf "# $SCRIPT_NAME\n" > "$SCRIPT_NAME/README.md"

echo "Your new script is located in ./$SCRIPT_NAME. Read the README.md in this directory for how to use it."
9 changes: 9 additions & 0 deletions scripts/versions-check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# versions-check

This script verifies the OS versions listed in `xamarin-macios/builds/Versions-<platform>.list.in`:

* No versions below the minimum deployment target.
* Both minimum deployment target and current deployment target must be listed.
* That the `SupportedTargetPlatformVersions` list is coherent with the
`KnownVersions` list (all versions in `KnownVersions` must also be in
`SupportedTargetPlatformVersions`).
11 changes: 7 additions & 4 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ include ./generator-diff.mk
SHARED_RESX = $(TOP)/tools/mtouch/Errors.resx
SHARED_DESIGNER_CS = $(DOTNET_BUILD_DIR)/common/Errors.Designer.cs

$(SHARED_DESIGNER_CS): $(SHARED_RESX) | $(DOTNET_BUILD_DIR)/common
$(Q_GEN) ./generate-errors.csharp > $(SHARED_DESIGNER_CS)
include $(TOP)/scripts/generate-errors/fragment.mk
$(SHARED_DESIGNER_CS): $(SHARED_RESX) $(GENERATE_ERRORS) | $(DOTNET_BUILD_DIR)/common
$(Q_GEN) $(GENERATE_ERRORS_EXEC) $(SHARED_RESX) $(SHARED_DESIGNER_CS)

DOTNET_TARGETS_DIRS += $(DOTNET_BUILD_DIR)/common

Expand Down Expand Up @@ -340,6 +341,8 @@ $(MACCATALYST_DOTNET_BUILD_DIR)/AssemblyInfo.cs: $(TOP)/src/AssemblyInfo.cs.in $

### .NET ###

include $(TOP)/scripts/generate-sourcelink-json/fragment.mk

define BuildDotNetIntermediateAssembly
$($(2)_DOTNET_BUILD_DIR)/core-$(3).dll: $($(2)_DOTNET_CORE_SOURCES) frameworks.sources $(RSP_DIR)/dotnet/$(3)-defines-dotnet.rsp | $($(2)_DOTNET_BUILD_DIR)
$$(Q_DOTNET_GEN) \
Expand Down Expand Up @@ -388,8 +391,8 @@ dotnet-gen:: dotnet-gen-$(3)
$($(2)_DOTNET_BUILD_DIR)/ILLink.LinkAttributes.xml: $(TOP)/src/ILLink.LinkAttributes.xml.in | $($(2)_DOTNET_BUILD_DIR)
$$(call Q_PROF_GEN,$(3)) sed < $$< > $$@ 's|@PRODUCT_NAME@|Microsoft.$(1)|g;'

$($(2)_DOTNET_BUILD_DIR)/SourceLink.json: $($(2)_DOTNET_BUILD_DIR)
$$(Q) $(TOP)/src/generate-sourcelink-json.csharp "$(PACKAGE_HEAD_REV)" "$(abspath $(TOP)/src)" "$$@"
$($(2)_DOTNET_BUILD_DIR)/SourceLink.json: $($(2)_DOTNET_BUILD_DIR) $(GENERATE_SOURCELINK_JSON)
$$(Q) $(GENERATE_SOURCELINK_JSON_EXEC) "$(PACKAGE_HEAD_REV)" "$(abspath $(TOP)/src)" "$$@"

$($(2)_DOTNET_BUILD_DIR)/embed-files.rsp: $($(2)_DOTNET_BUILD_DIR)/$(3)-generated-sources $($(2)_DOTNET_SOURCES) $(TOP)/src/generate-embed-files.sh
$$(Q) $(TOP)/src/generate-embed-files.sh $($(2)_DOTNET_BUILD_DIR)/$(3)-generated-sources "$($(2)_DOTNET_SOURCES)" > $$@.tmp
Expand Down
Loading

6 comments on commit f11253b

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.