Skip to content

Commit

Permalink
Detect platform target (x86, x64) automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed Apr 30, 2020
1 parent 8390ff4 commit 3d2b99c
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class VsProjectScope : IProjectScope
public string ProjectFolder { get; }
public string OutputAssemblyPath => VsUtils.GetOutputAssemblyPath(_project);
public string TargetFrameworkMoniker => VsUtils.GetTargetFrameworkMoniker(_project);
public string PlatformTargetName => VsUtils.GetPlatformTargetName(_project) ?? VsUtils.GetPlatformName(_project);
public string ProjectName { get; }
public string DefaultNamespace => GetDefaultNamespace();

Expand Down
28 changes: 28 additions & 0 deletions Deveroom.VisualStudio.Package/VsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,34 @@ public static string GetProjectFolder(Project project)
}
}

public static string GetPlatformName(Project project)
{
try
{
return project.ConfigurationManager.ActiveConfiguration.PlatformName;
}
catch (Exception ex)
{
Debug.WriteLine(ex, $"{nameof(VsUtils)}.{nameof(GetPlatformName)}");
return null;
}
}

public static string GetPlatformTargetName(Project project)
{
try
{
if (project.ConfigurationManager.ActiveConfiguration.Properties == null)
return null;
return project.ConfigurationManager.ActiveConfiguration.Properties.Item("PlatformTarget").Value.ToString();
}
catch (Exception ex)
{
Debug.WriteLine(ex, $"{nameof(VsUtils)}.{nameof(GetPlatformTargetName)}");
return null;
}
}

public static string GetOutputFileName(Project project)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DeveroomConfiguration
public TraceabilityConfiguration Traceability { get; set; } = new TraceabilityConfiguration();

// old settings to be reviewed
public ProcessorArchitectureSetting ProcessorArchitecture { get; set; } = ProcessorArchitectureSetting.UseSystem;
public ProcessorArchitectureSetting ProcessorArchitecture { get; set; } = ProcessorArchitectureSetting.AutoDetect;
public bool DebugConnector { get; set; } = false;
public string DefaultFeatureLanguage { get; set; } = "en-US";
public string ConfiguredBindingCulture { get; set; } = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public enum ProcessorArchitectureSetting
{
AutoDetect,
UseSystem,
X86,
X64
Expand Down
8 changes: 5 additions & 3 deletions Deveroom.VisualStudio/Connectors/OutProcSpecFlowConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public class OutProcSpecFlowConnector
private readonly IDeveroomLogger _logger;
private readonly TargetFrameworkMoniker _targetFrameworkMoniker;
private readonly string _extensionFolder;
private readonly ProcessorArchitectureSetting _processorArchitecture;

public OutProcSpecFlowConnector(DeveroomConfiguration configuration, IDeveroomLogger logger, TargetFrameworkMoniker targetFrameworkMoniker, string extensionFolder)
public OutProcSpecFlowConnector(DeveroomConfiguration configuration, IDeveroomLogger logger, TargetFrameworkMoniker targetFrameworkMoniker, string extensionFolder, ProcessorArchitectureSetting processorArchitecture)
{
_configuration = configuration;
_logger = logger;
_targetFrameworkMoniker = targetFrameworkMoniker;
_extensionFolder = extensionFolder;
_processorArchitecture = processorArchitecture;
}

private bool DebugConnector => _configuration.DebugConnector || Environment.GetEnvironmentVariable("DEVEROOM_DEBUGCONNECTOR") == "1";
Expand Down Expand Up @@ -147,7 +149,7 @@ private string GetConnectorPath(List<string> arguments)

//V1
string connectorName = ConnectorV1AnyCpu;
if (_configuration.ProcessorArchitecture == ProcessorArchitectureSetting.X86)
if (_processorArchitecture == ProcessorArchitectureSetting.X86)
connectorName = ConnectorV1X86;

return Path.Combine(connectorsFolder, connectorName);
Expand All @@ -156,7 +158,7 @@ private string GetConnectorPath(List<string> arguments)
private string GetDotNetInstallLocation()
{
var programFiles = Environment.GetEnvironmentVariable("ProgramW6432");
if (_configuration.ProcessorArchitecture == ProcessorArchitectureSetting.X86)
if (_processorArchitecture == ProcessorArchitectureSetting.X86)
programFiles = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
if (string.IsNullOrEmpty(programFiles))
programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Linq;
using Deveroom.VisualStudio.Configuration;
using Deveroom.VisualStudio.ProjectSystem;
using Deveroom.VisualStudio.ProjectSystem.Configuration;
using Deveroom.VisualStudio.ProjectSystem.Settings;

namespace Deveroom.VisualStudio.Connectors
{
public static class OutProcSpecFlowConnectorFactory
{
public static OutProcSpecFlowConnector Create(IProjectScope projectScope)
{
var ideScope = projectScope.IdeScope;
var projectSettings = projectScope.GetProjectSettings();
var deveroomConfiguration = projectScope.GetDeveroomConfiguration();
var processorArchitecture = GetProcessorArchitecture(deveroomConfiguration, projectSettings);
return new OutProcSpecFlowConnector(
deveroomConfiguration,
ideScope.Logger,
projectSettings.TargetFrameworkMoniker,
projectScope.IdeScope.GetExtensionFolder(),
processorArchitecture);
}

private static ProcessorArchitectureSetting GetProcessorArchitecture(DeveroomConfiguration deveroomConfiguration, ProjectSettings projectSettings)
{
if (deveroomConfiguration.ProcessorArchitecture != ProcessorArchitectureSetting.AutoDetect)
return deveroomConfiguration.ProcessorArchitecture;
if (projectSettings.PlatformTarget == ProjectPlatformTarget.x86)
return ProcessorArchitectureSetting.X86;
if (projectSettings.PlatformTarget == ProjectPlatformTarget.x64)
return ProcessorArchitectureSetting.X64;
return ProcessorArchitectureSetting.UseSystem;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public DiscoveryResult RunDiscovery(string testAssemblyPath, string configFilePa

private OutProcSpecFlowConnector GetConnector(ProjectSettings projectSettings)
{
return new OutProcSpecFlowConnector(_projectScope.GetDeveroomConfiguration(), Logger, projectSettings.TargetFrameworkMoniker, _projectScope.IdeScope.GetExtensionFolder());
return OutProcSpecFlowConnectorFactory.Create(_projectScope);
}
}
}
2 changes: 1 addition & 1 deletion Deveroom.VisualStudio/Generation/GenerationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public GenerationResult GenerateFeatureFile(string featureFilePath, string targe

try
{
var connector = new OutProcSpecFlowConnector(_projectScope.GetDeveroomConfiguration(), _logger, projectSettings.TargetFrameworkMoniker, _projectScope.IdeScope.GetExtensionFolder());
var connector = OutProcSpecFlowConnectorFactory.Create(_projectScope);

var result = connector.RunGenerator(featureFilePath, projectSettings.SpecFlowConfigFilePath,
targetExtension, targetNamespace, _projectScope.ProjectFolder, specFlowToolsFolder);
Expand Down
1 change: 1 addition & 0 deletions Deveroom.VisualStudio/ProjectSystem/IProjectScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IProjectScope : IPropertyOwner, IDisposable
IEnumerable<NuGetPackageReference> PackageReferences { get; }
string OutputAssemblyPath { get; }
string TargetFrameworkMoniker { get; }
string PlatformTargetName { get; }
string DefaultNamespace { get; }

void AddFile(string targetFilePath, string template);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Deveroom.VisualStudio.ProjectSystem.Settings
{
public enum ProjectPlatformTarget
{
Unknown,
AnyCpu,
x86,
x64
}
}
11 changes: 8 additions & 3 deletions Deveroom.VisualStudio/ProjectSystem/Settings/ProjectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ public class ProjectSettings
{
public DeveroomProjectKind Kind { get; }
public TargetFrameworkMoniker TargetFrameworkMoniker { get; }
public ProjectPlatformTarget PlatformTarget { get; }
public string OutputAssemblyPath { get; }
public string DefaultNamespace { get; }
public NuGetVersion SpecFlowVersion { get; }
public string SpecFlowGeneratorFolder { get; }
public string SpecFlowConfigFilePath { get; }
public SpecFlowProjectTraits SpecFlowProjectTraits { get; }

public ProjectSettings(DeveroomProjectKind kind, string outputAssemblyPath, TargetFrameworkMoniker targetFrameworkMoniker, string defaultNamespace,
public ProjectSettings(DeveroomProjectKind kind, string outputAssemblyPath, TargetFrameworkMoniker targetFrameworkMoniker, ProjectPlatformTarget platformTarget, string defaultNamespace,
NuGetVersion specFlowVersion, string specFlowGeneratorFolder, string specFlowConfigFilePath, SpecFlowProjectTraits specFlowProjectTraits)
{
Kind = kind;
TargetFrameworkMoniker = targetFrameworkMoniker;
PlatformTarget = platformTarget;
OutputAssemblyPath = outputAssemblyPath;
DefaultNamespace = defaultNamespace;
SpecFlowVersion = specFlowVersion;
Expand All @@ -41,6 +43,8 @@ public string GetSpecFlowVersionLabel()
public string GetShortLabel()
{
var result = $"{TargetFrameworkMoniker},SpecFlow:{GetSpecFlowVersionLabel()}";
if (PlatformTarget != ProjectPlatformTarget.Unknown && PlatformTarget != ProjectPlatformTarget.AnyCpu)
result += "," + PlatformTarget;
if (DesignTimeFeatureFileGenerationEnabled)
result += ",Gen";
return result;
Expand All @@ -50,14 +54,14 @@ public string GetShortLabel()

protected bool Equals(ProjectSettings other)
{
return Kind == other.Kind && TargetFrameworkMoniker == other.TargetFrameworkMoniker && string.Equals(OutputAssemblyPath, other.OutputAssemblyPath) && string.Equals(DefaultNamespace, other.DefaultNamespace) && Equals(SpecFlowVersion, other.SpecFlowVersion) && string.Equals(SpecFlowGeneratorFolder, other.SpecFlowGeneratorFolder) && string.Equals(SpecFlowConfigFilePath, other.SpecFlowConfigFilePath) && SpecFlowProjectTraits == other.SpecFlowProjectTraits;
return Kind == other.Kind && Equals(TargetFrameworkMoniker, other.TargetFrameworkMoniker) && PlatformTarget == other.PlatformTarget && OutputAssemblyPath == other.OutputAssemblyPath && DefaultNamespace == other.DefaultNamespace && Equals(SpecFlowVersion, other.SpecFlowVersion) && SpecFlowGeneratorFolder == other.SpecFlowGeneratorFolder && SpecFlowConfigFilePath == other.SpecFlowConfigFilePath && SpecFlowProjectTraits == other.SpecFlowProjectTraits;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
if (obj.GetType() != this.GetType()) return false;
return Equals((ProjectSettings) obj);
}

Expand All @@ -67,6 +71,7 @@ public override int GetHashCode()
{
var hashCode = (int) Kind;
hashCode = (hashCode * 397) ^ (TargetFrameworkMoniker != null ? TargetFrameworkMoniker.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (int) PlatformTarget;
hashCode = (hashCode * 397) ^ (OutputAssemblyPath != null ? OutputAssemblyPath.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (DefaultNamespace != null ? DefaultNamespace.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (SpecFlowVersion != null ? SpecFlowVersion.GetHashCode() : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,13 @@ private ProjectSettings LoadProjectSettings(out int? featureFileCount)
var specFlowSettings = _specFlowProjectSettingsProvider.GetSpecFlowSettings(packageReferences);
var hasFeatureFiles = (featureFileCount ?? 0) > 0;
var kind = GetKind(isInvalid, specFlowSettings != null, hasFeatureFiles);
var platformTarget = GetPlatformTarget(_projectScope.PlatformTargetName);

var settings = new ProjectSettings(
kind,
_projectScope.OutputAssemblyPath,
TargetFrameworkMoniker.Create(_projectScope.TargetFrameworkMoniker),
platformTarget,
_projectScope.DefaultNamespace,
specFlowSettings?.Version,
specFlowSettings?.GeneratorFolder,
Expand All @@ -155,6 +157,14 @@ private ProjectSettings LoadProjectSettings(out int? featureFileCount)
return settings;
}

private ProjectPlatformTarget GetPlatformTarget(string platformName)
{
if (platformName != null && Enum.TryParse<ProjectPlatformTarget>(platformName.Replace(" ", ""), true, out var platform))
return platform;

return ProjectPlatformTarget.Unknown;
}

private DeveroomProjectKind GetKind(bool isInvalid, bool isSpecFlowProject, bool hasFeatureFiles)
{
if (isInvalid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class InMemoryStubProjectScope : IProjectScope
public string ProjectFolder { get; } = Path.GetTempPath();
public string OutputAssemblyPath => Path.Combine(ProjectFolder, "out.dll");
public string TargetFrameworkMoniker { get; } = ".NETFramework,Version=v4.5.2";
public string PlatformTargetName { get; } = "Any CPU";
public string ProjectName { get; } = "Test Project";
public string DefaultNamespace => ProjectName.Replace(" ", "");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class StubProjectScope : IProjectScope
public string ProjectFolder { get; }
public string OutputAssemblyPath { get; }
public string TargetFrameworkMoniker { get; } = null;
public string PlatformTargetName { get; } = "Any CPU";
public string ProjectName { get; set; } = "Test Project";
public string DefaultNamespace => ProjectName.Replace(" ", "");

Expand Down

0 comments on commit 3d2b99c

Please sign in to comment.