Skip to content

Commit

Permalink
addressed all PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
deepchoudhery committed Dec 11, 2024
1 parent dda57eb commit 9392248
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.DotNet.Scaffolding.Core.CommandLine;
using Microsoft.DotNet.Scaffolding.Core.ComponentModel;
using Microsoft.DotNet.Scaffolding.Core.Logging;
using Microsoft.DotNet.Scaffolding.Internal;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
Expand All @@ -23,7 +23,6 @@ internal class ScaffoldRunnerBuilder : IScaffoldRunnerBuilder
public ScaffoldRunnerBuilder()
{
_logging = new LoggingBuilder(Services);

AddDefaultServices();
}

Expand Down Expand Up @@ -74,43 +73,50 @@ private void AddDefaultServices()
}

private void AddCoreServices()
{
var loggerConfig = GetLoggingConfiguration();
Services.AddLogging(builder =>
{
builder.ClearProviders();
builder.AddSerilog(loggerConfig.CreateLogger(), dispose: true);
});

Logging.AddDebug();
}

private LoggerConfiguration GetLoggingConfiguration()
{
//get some logging properties from environment variables
var isVerboseEnabled = Environment.GetEnvironmentVariable(ScaffolderConstants.ENABLE_VERBOSE_LOGGING)?.Equals("true", StringComparison.OrdinalIgnoreCase) == true;
var isLogToFileEnabled = Environment.GetEnvironmentVariable(ScaffolderConstants.LOG_TO_FILE)?.Equals("true", StringComparison.OrdinalIgnoreCase) == true; ;
var isVerboseEnabled = EnvironmentHelpers.GetEnvironmentVariableAsBool(ScaffolderConstants.ENABLE_VERBOSE_LOGGING);
var isLogToFileEnabled = EnvironmentHelpers.GetEnvironmentVariableAsBool(ScaffolderConstants.LOG_TO_FILE);
// Configure Serilog Logger
var loggerConfig = new LoggerConfiguration().WriteTo.Sink(new AnsiConsoleSink());
loggerConfig.MinimumLevel.Information();
if(isVerboseEnabled)
if (isVerboseEnabled)
{
loggerConfig.MinimumLevel.Verbose();
}

if (isLogToFileEnabled)
{
var currentDirectory = Directory.GetCurrentDirectory();
if (!string.IsNullOrEmpty(currentDirectory))
var loggingDirectory = Path.Combine(EnvironmentHelpers.GetUserProfilePath(), _defaultLogFolder);
if (!Directory.Exists(loggingDirectory))
{
var filePath = $"dotnet-scaffold-{DateTime.UtcNow:yyyy-MM-dd_HH-mm}.log";
var logPath = StringUtil.GetUniqueFilePath(Path.Combine(currentDirectory, _defaultLogFolder, filePath));
loggerConfig.WriteTo.File(logPath, rollingInterval: RollingInterval.Infinite);
Directory.CreateDirectory(loggingDirectory);
}
}

Log.Logger = loggerConfig.CreateLogger();
Services.AddLogging(builder =>
{
builder.ClearProviders();
builder.AddSerilog(dispose: true);
});
var filePath = $"dotnet-scaffold-{DateTime.Now:yyyy-MM-dd_HH-mm}.log";
var logPath = StringUtil.GetUniqueFilePath(Path.Combine(loggingDirectory, filePath));
loggerConfig.WriteTo.File(logPath, rollingInterval: RollingInterval.Infinite);
}

Logging.AddDebug();
return loggerConfig;
}

private sealed class LoggingBuilder(IServiceCollection services) : ILoggingBuilder
{
public IServiceCollection Services { get; } = services;
}

private readonly string _defaultLogFolder = ".logs";
private readonly string _defaultLogFolder = Path.Combine(".dotnet-scaffold", ".logs");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.Extensions.Logging;

namespace Microsoft.DotNet.Scaffolding.Core.CommandLine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Scaffolding.Core.ComponentModel;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Serilog.Core;
using Serilog.Events;
using Spectre.Console;
Expand All @@ -12,7 +13,7 @@ public class AnsiConsoleSink : ILogEventSink
public void Emit(LogEvent logEvent)
{
//LAUNCHED_BY_DOTNET_SCAFFOLD should be "true", or some other value. "true" indicates being launched by dotnet-scaffold.
bool isLaunchedByDotnetScaffold = Environment.GetEnvironmentVariable(ScaffolderConstants.LAUNCHED_BY_DOTNET_SCAFFOLD)?.Equals("true", StringComparison.OrdinalIgnoreCase) == true;
bool isLaunchedByDotnetScaffold = EnvironmentHelpers.GetEnvironmentVariableAsBool(ScaffolderConstants.LAUNCHED_BY_DOTNET_SCAFFOLD);
var formattedMessage = FormatMessage(logEvent.Level, logEvent.RenderMessage());
if (isLaunchedByDotnetScaffold && (Console.IsOutputRedirected || Console.IsErrorRedirected))
{
Expand All @@ -32,7 +33,7 @@ private string FormatMessage(LogEventLevel level, string message)
{
LogEventLevel.Verbose => $"[gray]{message}[/]",
LogEventLevel.Debug => $"[gray]{message}[/]",
LogEventLevel.Information => $"[green]{message}[/]",
LogEventLevel.Information => $"[default]{message}[/]",
LogEventLevel.Warning => $"[yellow]{message}[/]",
LogEventLevel.Error => $"[red]{message}[/]",
LogEventLevel.Fatal => $"[bold red]{message}[/]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)src\dotnet-scaffolding\Microsoft.DotNet.Scaffolding.Internal\CliHelpers\*.cs">
<Link>CliHelpers\%(Filename)%(Extension)</Link>
<Compile Include="$(RepoRoot)src\dotnet-scaffolding\Microsoft.DotNet.Scaffolding.Internal\Shared\*.cs">
<Link>Shared\%(Filename)%(Extension)</Link>
</Compile>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Runtime.InteropServices;
using Microsoft.DotNet.Scaffolding.Internal.Shared;

namespace Microsoft.DotNet.Scaffolding.Internal.Services;
/// <summary>
Expand Down Expand Up @@ -33,16 +33,7 @@ public static string LocalNugetCachePath
}
}

public static string LocalUserProfilePath
{
get
{
return System.Environment.GetEnvironmentVariable(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "USERPROFILE"
: "HOME") ?? "USERPROFILE";
}
}
public static string LocalUserProfilePath => EnvironmentHelpers.GetUserProfilePath();

public const string DotnetProfileDirectoryName = ".dotnet";

Expand Down Expand Up @@ -123,25 +114,7 @@ public string GetMachineName()
/// <inheritdoc />
public bool GetEnvironmentVariableAsBool(string name, bool defaultValue = false)
{
var str = Environment.GetEnvironmentVariable(name);
if (string.IsNullOrEmpty(str))
{
return defaultValue;
}

switch (str.ToLowerInvariant())
{
case "true":
case "1":
case "yes":
return true;
case "false":
case "0":
case "no":
return false;
default:
return defaultValue;
}
return EnvironmentHelpers.GetEnvironmentVariableAsBool(name, defaultValue);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
using Microsoft.DotNet.Scaffolding.Internal.Shared;

namespace Microsoft.DotNet.Scaffolding.Internal.Services;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text;

namespace Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
namespace Microsoft.DotNet.Scaffolding.Internal.Shared;

internal static class ArgumentEscaper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;

namespace Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
namespace Microsoft.DotNet.Scaffolding.Internal.Shared;

/// <summary>
/// To run 'dotnet' or any processes and capture consequent stdout and stderr (using 'ExecuteAndCaptureOutput'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices;

namespace Microsoft.DotNet.Scaffolding.Internal.Shared;

internal static class EnvironmentHelpers
{
public static bool GetEnvironmentVariableAsBool(string name, bool defaultValue = false)
{
var str = Environment.GetEnvironmentVariable(name);
if (string.IsNullOrEmpty(str))
{
return defaultValue;
}

switch (str.ToLowerInvariant())
{
case "true":
case "1":
case "yes":
return true;
case "false":
case "0":
case "no":
return false;
default:
return defaultValue;
}
}

public static string GetUserProfilePath()
{
return Environment.GetEnvironmentVariable(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "USERPROFILE"
: "HOME") ?? "USERPROFILE";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Text;

namespace Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
namespace Microsoft.DotNet.Scaffolding.Internal.Shared;

internal sealed class ProcessOutputStreamReader : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.DotNet.Scaffolding.Internal;
namespace Microsoft.DotNet.Scaffolding.Internal.Shared;

internal static class StringUtil
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public MsBuildInitializer(ILogger logger)

public void Initialize()
{
_logger.LogTrace($"Initializing MSBuild in '{nameof(MsBuildInitializer)}.Initialize()...'");
RegisterMsbuild();
_logger.LogTrace("Done");
}

/// <summary>
Expand All @@ -29,6 +27,7 @@ private void RegisterMsbuild()
{
if (!MSBuildLocator.IsRegistered)
{
_logger.LogTrace($"Initializing MSBuild in '{nameof(MsBuildInitializer)}.Initialize()...'");
// Path to the directory containing the SDKs
string sdkBasePath = GetDefaultSdkPath();
if (!Directory.Exists(sdkBasePath))
Expand All @@ -47,14 +46,15 @@ private void RegisterMsbuild()

if (!sdkPaths.Any())
{
_logger.LogDebug($"Could not find a .NET SDK at the default locations.");
_logger.LogDebug($"No sdk locations found, using MSBuildLocator.RegisterDefaults()");
MSBuildLocator.RegisterDefaults();
return;
}

foreach (var sdkPath in sdkPaths)
{
if (File.Exists(sdkPath))
var msbuildPath = Path.Combine(sdkPath, "MSBuild.dll");
if (File.Exists(msbuildPath))
{
// Register the latest SDK
_logger.LogTrace($"Registering MSBuild at path '{sdkPath}'");
Expand All @@ -68,6 +68,8 @@ private void RegisterMsbuild()
_logger.LogDebug($"MSBuild.dll not found at default locations.");
MSBuildLocator.RegisterDefaults();
}

_logger.LogTrace("Done");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Scaffolding.Core.Scaffolders;
using Microsoft.DotNet.Scaffolding.Internal;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.DotNet.Scaffolding.TextTemplating.DbContext;
using Microsoft.Extensions.Logging;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Scaffolding.Internal;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.DotNet.Scaffolding.TextTemplating.DbContext;
using Microsoft.DotNet.Tools.Scaffold.AspNet.Common;
using Constants = Microsoft.DotNet.Scaffolding.Internal.Constants;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Reflection;
using Microsoft.DotNet.Scaffolding.Internal;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.DotNet.Scaffolding.TextTemplating;
using Microsoft.DotNet.Tools.Scaffold.AspNet.Models;
using Microsoft.DotNet.Tools.Scaffold.AspNet.Templates.Files;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Scaffolding.Internal;
using Microsoft.DotNet.Scaffolding.Internal.Shared;

namespace Microsoft.DotNet.Tools.Scaffold.AspNet.Helpers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Scaffolding.Internal;
using System.Reflection;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.DotNet.Scaffolding.TextTemplating;
using Microsoft.DotNet.Tools.Scaffold.AspNet.Models;
using Microsoft.DotNet.Tools.Scaffold.AspNet.Templates.Files;
using System.Reflection;

namespace Microsoft.DotNet.Tools.Scaffold.AspNet.Helpers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Globalization;
using Microsoft.DotNet.Scaffolding.Core.Scaffolders;
using Microsoft.DotNet.Scaffolding.Core.Steps;
using Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
using Microsoft.DotNet.Scaffolding.Internal.Services;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.DotNet.Scaffolding.Internal.Telemetry;
using Microsoft.DotNet.Tools.Scaffold.AspNet.Common;
using Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps.Settings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Globalization;
using Microsoft.DotNet.Scaffolding.Core.Scaffolders;
using Microsoft.DotNet.Scaffolding.Core.Steps;
using Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
using Microsoft.DotNet.Scaffolding.Internal.Services;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.DotNet.Scaffolding.Internal.Telemetry;
using Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps.Settings;
using Microsoft.DotNet.Tools.Scaffold.AspNet.Telemetry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.DotNet.Scaffolding.Core.Scaffolders;
using Microsoft.DotNet.Scaffolding.Core.Steps;
using Microsoft.DotNet.Scaffolding.Internal;
using Microsoft.DotNet.Scaffolding.Internal.Services;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.DotNet.Scaffolding.Internal.Telemetry;
using Microsoft.DotNet.Scaffolding.TextTemplating.DbContext;
using Microsoft.DotNet.Tools.Scaffold.AspNet.Common;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Scaffolding.Core.ComponentModel;
using Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
using Microsoft.DotNet.Scaffolding.Internal.Services;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.DotNet.Scaffolding.Internal.Telemetry;
using Microsoft.DotNet.Tools.Scaffold.Telemetry;
using Spectre.Console;
Expand Down
Loading

0 comments on commit 9392248

Please sign in to comment.