Skip to content

Commit

Permalink
Fix generate script (#3220)
Browse files Browse the repository at this point in the history
Fixes #3209
  • Loading branch information
m-nash authored Apr 23, 2024
1 parent 07b340a commit e2adc07
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 103 deletions.
2 changes: 1 addition & 1 deletion packages/http-client-csharp/emitter/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"noEmit": false,
"rootDir": "./src",
"outDir": "./dist",
"outDir": "../dist/emitter",
"tsBuildInfoFile": "temp/tsconfig.tsbuildinfo"
},
"references": [],
Expand Down
3 changes: 0 additions & 3 deletions packages/http-client-csharp/eng/scripts/Build-Packages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ Push-Location "$packageRoot"
try {
Write-Host "Working in $PWD"

# TODO this is happening 4 times until ..... gets fixed
Invoke-LoggedCommand "dotnet build ./generator" -GroupOutput

Invoke-LoggedCommand "npm run build" -GroupOutput

if ($BuildNumber) {
Expand Down
11 changes: 6 additions & 5 deletions packages/http-client-csharp/eng/scripts/Generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force;

$repoRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..' 'generator')
$mgcArtifactRoot = Join-Path $repoRoot 'artifacts' 'bin' 'Microsoft.Generator.CSharp' 'Debug' 'net8.0'
$clientModelTestProjectsDirectory = Join-Path $repoRoot 'Microsoft.Generator.CSharp.ClientModel.TestProjects'
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..')

Invoke "dotnet build $repoRoot"
Invoke "npm run build"

$mgcPath = Join-Path $mgcArtifactRoot "Microsoft.Generator.CSharp"
$mgcArtifactRoot = Join-Path $repoRoot 'dist' 'generator'
$clientModelTestProjectsDirectory = Join-Path $repoRoot 'generator' 'Microsoft.Generator.CSharp.ClientModel.TestProjects'

$mgcPath = Join-Path $mgcArtifactRoot "Microsoft.Generator.CSharp.exe"
$unbrandedTypespecTestProject = Join-Path $clientModelTestProjectsDirectory "Unbranded-TypeSpec"
Invoke "$mgcPath $unbrandedTypespecTestProject"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>Microsoft.Generator.CSharp.ClientModel</PackageId>
Expand All @@ -16,4 +16,13 @@
<!--Add this as a project reference for now until the core generator library is packaged and published.-->
<ProjectReference Include="..\..\Microsoft.Generator.CSharp\src\Microsoft.Generator.CSharp.csproj" />
</ItemGroup>
</Project>

<!-- Copy output to package dist path for local execution and -->
<Target Name="CopyForNpmPackage" AfterTargets="Build">
<Message Text="Copying output to dist path" Importance="high" />
<ItemGroup>
<SourceDir Include="$(OutputPath)**\*.*" />
</ItemGroup>
<Copy SourceFiles="@(SourceDir)" DestinationFolder="$(MSBuildThisFileDirectory)..\..\..\dist\generator\%(RecursiveDir)" />
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.IO;
using System.Threading.Tasks;
using Microsoft.Generator.CSharp.ClientModel.Expressions;

namespace Microsoft.Generator.CSharp.ClientModel
{
internal static class Program
{
public static int Main(string[] args)
{
//var outputPath = args[0];
//var generatedPath = Path.Combine(outputPath, "src", "Generated");
////TODO need to load config / type factory
//Configuration? configuration = null;
//var generator = new CSharpGen();
//var plugin = new ClientModelPlugin(new GeneratorContext(configuration!));

//await generator.ExecuteAsync(plugin);
return 0;
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"profiles": {
"Unbranded-TypeSpec": {
"commandName": "Project",
"commandLineArgs": "$(SolutionDir)\\Microsoft.Generator.CSharp.ClientModel.TestProjects\\Unbranded-TypeSpec"
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe",
"commandLineArgs": "$(SolutionDir)/Microsoft.Generator.CSharp.ClientModel.TestProjects/Unbranded-TypeSpec"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Diagnostics.CodeAnalysis;
using System.IO;
using CommandLine;

namespace Microsoft.Generator.CSharp
Expand All @@ -13,10 +12,8 @@ namespace Microsoft.Generator.CSharp
internal class CommandLineOptions
{
private const string OutputDirectoryOptionName = "DIRECTORY";
private const string ModelPluginOptionName = "model-plugin";
private const string ShouldDebugOptionName = "debug";
private const string CmdLineOutputDirectoryOptionHelpText = "The path to the directory containing the input files to the generator including the code model file and the configuration file for the generator.";
private const string CmdLineModelPluginOptionHelpText = "The name of the custom client model plugin NuGet package to use to generate the code. If not provided, the default client model plugin package `Microsoft.Generator.CSharp.ClientModel` will be used.";
private const string CmdLineDebugOptionHelpText = "Attempt to attach the debugger on execute.";

/// <summary>
Expand All @@ -26,12 +23,6 @@ internal class CommandLineOptions
[NotNull]
public string? OutputDirectory { get; set; }

/// <summary>
/// The command line option to specify the name of the custom client model plugin Nuget package to use to generate the client.
/// </summary>
[Option(ModelPluginOptionName, Required = false, Default = null, HelpText = CmdLineModelPluginOptionHelpText)]
public string? ModelPlugin { get; set; }

[Option(longName: ShouldDebugOptionName, Required = false, Default = false, Hidden = true, HelpText = CmdLineDebugOptionHelpText)]
public bool ShouldDebug { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading.Tasks;
Expand All @@ -9,7 +9,7 @@ internal class GeneratorRunner
{
public async Task RunAsync(CommandLineOptions options)
{
PluginHandler pluginHandler = new(options.ModelPlugin);
PluginHandler pluginHandler = new();
pluginHandler.LoadPlugin(options.OutputDirectory);

var csharpGen = new CSharpGen();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;

namespace Microsoft.Generator.CSharp
{
internal class PluginHandler
{
private readonly string _defaultClientModelPluginId = "@typespec" + Path.DirectorySeparatorChar + "http-client-csharp-generator-clientModel";
private readonly string _pluginName;

/// <summary>
/// Constructs a new instance of <see cref="PluginHandler"/> with the specified plugin name and version.
/// The plugin is then loaded using the <see cref="LoadPlugin"/> method.
/// </summary>
/// <param name="name">The name of the plugin to load.</param>
public PluginHandler(string? name)
{
_pluginName = name ?? _defaultClientModelPluginId;
}

private string? _nodeModulesFolder;
private string NodeModulesFolder => _nodeModulesFolder ??= GetDirectoryCatalog();

private string GetDirectoryCatalog()
{
return FindNodeModules(new DirectoryInfo(AppContext.BaseDirectory));
}

public void LoadPlugin(string outputDirectory)
{
using DirectoryCatalog directoryCatalog = new(Path.Combine(NodeModulesFolder, _pluginName));
using DirectoryCatalog directoryCatalog = new(AppContext.BaseDirectory);
using (CompositionContainer container = new(directoryCatalog))
{
try
Expand All @@ -42,37 +20,14 @@ public void LoadPlugin(string outputDirectory)
var plugin = container.GetExportedValue<CodeModelPlugin>();
if (plugin == null)
{
throw new InvalidOperationException($"Cannot find exported value in composition container for {_pluginName}.");
throw new InvalidOperationException($"Cannot find exported value in current directory {AppContext.BaseDirectory}.");
}
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to load client model plugin {_pluginName}.", ex);
}
}
}

private string FindNodeModules(DirectoryInfo? path)
{
if (path is null)
{
throw new InvalidOperationException($"Unable to find node_modules in path {AppContext.BaseDirectory}");
}

if (path.Name == "node_modules")
{
return path.FullName;
}

foreach (var child in path.GetDirectories())
{
if (child.Name == "node_modules")
{
return child.FullName;
throw new InvalidOperationException($"Failed to load client model plugin from {AppContext.BaseDirectory}.", ex);
}
}

return FindNodeModules(path.Parent);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
Expand All @@ -20,7 +20,7 @@ internal class CommandLineOptionsTests
private static IEnumerable<TestCaseData> TestParseCommandLineOptionsArgsTestData()
{
// happy path scenarios
yield return new TestCaseData(new string[] { "../inputDir", "--model-plugin", "my-plugin" }, false);
yield return new TestCaseData(new string[] { "../inputDir" }, false);
}

// Validates parsing different command line options
Expand Down
13 changes: 7 additions & 6 deletions packages/http-client-csharp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
"typespec"
],
"type": "module",
"main": "emitter/dist/index.js",
"main": "dist/emitter/index.js",
"exports": {
".": "./emitter/dist/index.js"
".": "./dist/emitter/index.js"
},
"scripts": {
"clean": "rimraf ./emitter/dist ./emitter/temp",
"build": "tsc -p ./emitter/tsconfig.build.json",
"clean": "rimraf ./dist ./emitter/temp && dotnet clean ./generator",
"build:emitter": "tsc -p ./emitter/tsconfig.build.json",
"build:generator": "dotnet build ./generator",
"build": "npm run build:emitter && npm run build:generator",
"watch": "tsc -p ./emitter/tsconfig.build.json --watch",
"lint-typespec-library": "tsp compile . --warn-as-error --import @typespec/library-linter --no-emit",
"test": "vitest run",
Expand All @@ -35,8 +37,7 @@
"format": "pnpm -w format:dir packages/http-client-csharp"
},
"files": [
"emitter/dist/**",
"generator/artifacts/bin/release/8.0/**"
"dist/**"
],
"dependencies": {
"json-serialize-refs": "0.1.0-0",
Expand Down

0 comments on commit e2adc07

Please sign in to comment.