Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jp2masa committed Dec 6, 2018
1 parent a563edb commit de52f31
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<LangVersion>Latest</LangVersion>
<NoWarn>CA1051;CA1707;CA1711;CA1801;$(NoWarn)</NoWarn>
<NoWarn>CA1051;CA1501;CA1707;CA1711;CA1801;$(NoWarn)</NoWarn>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.Build.Builder/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected override void OnStartup(StartupEventArgs e)
base.OnStartup(e);
}

private void ShowErrorMessageBox(string message) =>
private static void ShowErrorMessageBox(string message) =>
MessageBox.Show(message, "Cosmos Kit Builder", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
10 changes: 6 additions & 4 deletions source/Cosmos.Build.Builder/BuildTasks/BuildTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class BuildTask : MSBuildTargetBuildTaskBase

protected override IReadOnlyDictionary<string, string> Properties => _properties;

private Dictionary<string, string> _properties;
private readonly Dictionary<string, string> _properties;

public BuildTask(
IMSBuildService msBuildService,
Expand All @@ -28,9 +28,11 @@ public BuildTask(
{
ProjectFilePath = projectFilePath;

_properties = new Dictionary<string, string>();
_properties.Add("OutputPath", outputPath);
_properties.Add("VsixOutputPath", vsixOutputPath);
_properties = new Dictionary<string, string>()
{
["OutputPath"] = outputPath,
["VsixOutputPath"] = vsixOutputPath
};
}
}
}
16 changes: 8 additions & 8 deletions source/Cosmos.Build.Builder/BuildTasks/CreateSetupTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ internal class CreateSetupTask : ProcessBuildTaskBase
{
public override string Name => "Create Setup";

private IInnoSetupService _innoSetupService;
private readonly IInnoSetupService _innoSetupService;

private string _scriptFilePath;

private Dictionary<string, string> _defines;
private readonly string _scriptFilePath;
private readonly Dictionary<string, string> _defines;

public CreateSetupTask(
IInnoSetupService innoSetupService,
Expand All @@ -27,10 +26,11 @@ public CreateSetupTask(

_scriptFilePath = scriptFilePath;

_defines = new Dictionary<string, string>();

_defines.Add("BuildConfiguration", configuration);
_defines.Add("ChangeSetVersion", releaseVersion);
_defines = new Dictionary<string, string>()
{
["BuildConfiguration"] = configuration,
["ChangeSetVersion"] = releaseVersion
};
}

protected override string GetExePath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal abstract class MSBuildTargetBuildTaskBase : ProcessBuildTaskBase

protected abstract IReadOnlyDictionary<string, string> Properties { get; }

private IMSBuildService _msBuildService;
private readonly IMSBuildService _msBuildService;

protected MSBuildTargetBuildTaskBase(IMSBuildService msBuildService)
: base(true, false)
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.Build.Builder/BuildTasks/PackTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class PackTask : MSBuildTargetBuildTaskBase

protected override IReadOnlyDictionary<string, string> Properties => _properties;

private Dictionary<string, string> _properties;
private readonly Dictionary<string, string> _properties;

public PackTask(
IMSBuildService msBuildService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ internal abstract class ProcessBuildTaskBase : IBuildTask
{
public abstract string Name { get; }

private bool _waitForExit;
private bool _createWindow;
private readonly bool _waitForExit;
private readonly bool _createWindow;

protected ProcessBuildTaskBase(bool waitForExit, bool createWindow)
{
Expand Down Expand Up @@ -56,7 +56,7 @@ public Task RunAsync(ILogger logger)
protected abstract string GetExePath();
protected abstract string GetArguments();

private void WaitForExit(Process process)
private static void WaitForExit(Process process)
{
process.WaitForExit();

Expand All @@ -66,7 +66,7 @@ private void WaitForExit(Process process)
}
}

private async Task ReadOutputAsync(StreamReader reader, ILogger logger)
private static async Task ReadOutputAsync(StreamReader reader, ILogger logger)
{
while (true)
{
Expand Down
8 changes: 5 additions & 3 deletions source/Cosmos.Build.Builder/BuildTasks/PublishTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class PublishTask : MSBuildTargetBuildTaskBase

protected override IReadOnlyDictionary<string, string> Properties => _properties;

private Dictionary<string, string> _properties;
private readonly Dictionary<string, string> _properties;

public PublishTask(
IMSBuildService msBuildService,
Expand All @@ -27,8 +27,10 @@ public PublishTask(
{
ProjectFilePath = projectFilePath;

_properties = new Dictionary<string, string>();
_properties.Add("PublishDir", publishOutputPath);
_properties = new Dictionary<string, string>()
{
["PublishDir"] = publishOutputPath
};
}
}
}
6 changes: 3 additions & 3 deletions source/Cosmos.Build.Builder/BuildTasks/StartProcessTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ internal class StartProcessTask : ProcessBuildTaskBase
{
public override string Name => $"Run Process - {_processName ?? Path.GetFileNameWithoutExtension(_exePath)}";

private string _exePath;
private string _args;
private readonly string _exePath;
private readonly string _args;

private string _processName;
private readonly string _processName;

public StartProcessTask(
string exePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class CommandLineBuilderConfiguration : IBuilderConfiguration
public bool UserKit => GetSwitch();
public string VsPath => GetOption();

private Dictionary<string, string> _args;
private readonly Dictionary<string, string> _args;

public CommandLineBuilderConfiguration(string[] args)
{
Expand Down
6 changes: 3 additions & 3 deletions source/Cosmos.Build.Builder/CosmosBuildDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Cosmos.Build.Builder
{
internal class CosmosBuildDefinition : IBuildDefinition
{
private IInnoSetupService _innoSetupService;
private IMSBuildService _msBuildService;
private ISetupInstance2 _visualStudioInstance;
private readonly IInnoSetupService _innoSetupService;
private readonly IMSBuildService _msBuildService;
private readonly ISetupInstance2 _visualStudioInstance;

private readonly string _cosmosDir;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ namespace Cosmos.Build.Builder.Dependencies
internal class InnoSetupDependency : IDependency
{
private const string InnoSetupInstallerUrl = "http://www.jrsoftware.org/download.php/is.exe";

public string Name => "Inno Setup";

private IInnoSetupService _innoSetupService;
private readonly IInnoSetupService _innoSetupService;

public InnoSetupDependency(IInnoSetupService innoSetupService)
{
Expand Down Expand Up @@ -46,7 +47,7 @@ public async Task InstallAsync(CancellationToken cancellationToken)
}

var process = Process.Start(setupFilePath);
await Task.Run((Action)process.WaitForExit, cancellationToken).ConfigureAwait(false);
await Task.Run(process.WaitForExit, cancellationToken).ConfigureAwait(false);

if (process.ExitCode != 0)
{
Expand Down
4 changes: 2 additions & 2 deletions source/Cosmos.Build.Builder/Dependencies/ReposDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public Task InstallAsync(CancellationToken cancellationToken)
.Select(r => DownloadRepoAsync(r, usesGit, cancellationToken)));
}

private async Task DownloadRepoAsync(Repo repo, bool useGit, CancellationToken cancellationToken)
private static async Task DownloadRepoAsync(Repo repo, bool useGit, CancellationToken cancellationToken)
{
if (useGit)
{
var process = Process.Start("git", $"clone \"{repo.Url}.git\" \"{repo.LocalPath}\"");
await Task.Run((Action)process.WaitForExit).ConfigureAwait(false);
await Task.Run(process.WaitForExit).ConfigureAwait(false);

if (process.ExitCode != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class VisualStudioDependency : IDependency

public string Name => $"Visual Studio {MinimumVsVersion.Major}.{MinimumVsVersion.Minor}+";

private ISetupInstance2 _visualStudioInstance;
private readonly ISetupInstance2 _visualStudioInstance;

public VisualStudioDependency(ISetupInstance2 visualStudioInstance)
{
Expand Down Expand Up @@ -41,7 +41,7 @@ public async Task InstallAsync(CancellationToken cancellationToken)
var args = $"update --passive --norestart --installPath \"{vsInstancePath}\"";

var process = Process.Start(vsInstallerPath, args);
await Task.Run((Action)process.WaitForExit, cancellationToken).ConfigureAwait(false);
await Task.Run(process.WaitForExit, cancellationToken).ConfigureAwait(false);

if (process.ExitCode != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class VisualStudioWorkloadsDependency : IDependency

public string Name => "Visual Studio Workloads";

private ISetupInstance2 _visualStudioInstance;
private readonly ISetupInstance2 _visualStudioInstance;

public VisualStudioWorkloadsDependency(ISetupInstance2 visualStudioInstance)
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public async Task InstallAsync(CancellationToken cancellationToken)
}

var process = Process.Start(vsInstallerPath, args);
await Task.Run((Action)process.WaitForExit, cancellationToken).ConfigureAwait(false);
await Task.Run(process.WaitForExit, cancellationToken).ConfigureAwait(false);

if (process.ExitCode != 0)
{
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.Build.Builder/Models/Section.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class Section : INotifyPropertyChanged
public string Log => _logBuilder.ToString();
public bool HasLoggedErrors { get; private set; }

private StringBuilder _logBuilder;
private readonly StringBuilder _logBuilder;

public Section(string name)
{
Expand Down
4 changes: 2 additions & 2 deletions source/Cosmos.Build.Builder/Services/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Cosmos.Build.Builder.Services
{
internal class DialogService<TView, TViewModel> : IDialogService<TViewModel> where TView : Window
{
private Func<TView> _dialogFactory;
private Window _owner;
private readonly Func<TView> _dialogFactory;
private readonly Window _owner;

public DialogService(Func<TView> dialogFactory, Window owner = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Cosmos.Build.Builder.Services
{
internal class VisualStudioService : IVisualStudioService
{
private Lazy<IReadOnlyList<ISetupInstance2>> _instances;
private readonly Lazy<IReadOnlyList<ISetupInstance2>> _instances;

public VisualStudioService()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Cosmos.Build.Builder.ViewModels
{
internal class DependencyInstallationDialogViewModel : ViewModelBase
internal sealed class DependencyInstallationDialogViewModel : ViewModelBase, IDisposable
{
private const string InstallationSucceededText = "{0} installed successfully!";
private const string InstallationFailedText = "{0} failed to install!";
Expand Down Expand Up @@ -68,10 +68,16 @@ public DependencyInstallationDialogViewModel(IDependency dependency)
OkCommand = new RelayCommand(p => Close(p as Window, true));
}

public void Dispose()
{
_installTask.Dispose();
_installTaskCancellationTokenSource.Dispose();
}

private void Install(object parameter) => _installTask = InstallAsync();
private void CancelInstallation(object parameter) => _installTaskCancellationTokenSource.Cancel();

private void Close(Window window, bool? dialogResult)
private static void Close(Window window, bool? dialogResult)
{
#if DEBUG
if (window == null)
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.Build.Builder/ViewModels/MainWindowLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class MainWindowLogger : ILogger
{
private static readonly string[] NewLineStringArray = new string[] { Environment.NewLine };

private MainWindowViewModel _viewModel;
private readonly MainWindowViewModel _viewModel;

public MainWindowLogger(MainWindowViewModel viewModel)
{
Expand Down
22 changes: 12 additions & 10 deletions source/Cosmos.Build.Builder/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Cosmos.Build.Builder.ViewModels
{
internal class MainWindowViewModel : ViewModelBase
internal sealed class MainWindowViewModel : ViewModelBase
{
private const int TailItemCount = 10;

Expand All @@ -35,12 +35,12 @@ public WindowState WindowState
set => SetAndRaiseIfChanged(ref _windowState, value);
}

private ILogger _logger;
private readonly ILogger _logger;

private IDialogService<DependencyInstallationDialogViewModel> _dependencyInstallationDialogService;
private readonly IDialogService<DependencyInstallationDialogViewModel> _dependencyInstallationDialogService;

private IBuildDefinition _buildDefinition;
private Task _buildTask;
private readonly IBuildDefinition _buildDefinition;
private readonly Task _buildTask;

private bool _closeWhenCompleted;

Expand Down Expand Up @@ -109,12 +109,14 @@ private async Task BuildAsync()
{
_logger.LogMessage($"{dependency.Name} not found.");

var viewModel = new DependencyInstallationDialogViewModel(dependency);
_dependencyInstallationDialogService.ShowDialog(viewModel);

if (!viewModel.InstallationSucceeded)
using (var viewModel = new DependencyInstallationDialogViewModel(dependency))
{
throw new Exception($"Dependency installation failed! Dependency name: {dependency.Name}");
_dependencyInstallationDialogService.ShowDialog(viewModel);

if (!viewModel.InstallationSucceeded)
{
throw new Exception($"Dependency installation failed! Dependency name: {dependency.Name}");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Cosmos.Build.Builder.ViewModels
{
internal class VisualStudioInstanceDialogViewModel : ViewModelBase
internal sealed class VisualStudioInstanceDialogViewModel : ViewModelBase
{
public IEnumerable<VisualStudioInstance> VisualStudioInstances { get; }

Expand All @@ -22,7 +22,7 @@ public VisualStudioInstance SelectedVisualStudioInstance
public ICommand OkCommand { get; }
public ICommand CancelCommand { get; }

private IVisualStudioService _visualStudioService;
private readonly IVisualStudioService _visualStudioService;

private VisualStudioInstance _selectedVisualStudioInstance;

Expand All @@ -37,7 +37,7 @@ public VisualStudioInstanceDialogViewModel(IVisualStudioService visualStudioServ
CancelCommand = new RelayCommand(p => Close(p as Window, false));
}

private void Close(Window window, bool? dialogResult)
private static void Close(Window window, bool? dialogResult)
{
#if DEBUG
if (window == null)
Expand Down

0 comments on commit de52f31

Please sign in to comment.