Skip to content

Commit

Permalink
Protects from errors when project cannot be loaded for some reasons. …
Browse files Browse the repository at this point in the history
…Issue #56
  • Loading branch information
3F committed Nov 17, 2017
1 parent f39a1e9 commit 20eb9db
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 24 deletions.
62 changes: 62 additions & 0 deletions Wizard/DxpIsolatedEnv.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2017 Denis Kuzmin < [email protected] > :: github.com/3F
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using net.r_eg.MvsSln.Core;
using net.r_eg.MvsSln.Log;

namespace net.r_eg.DllExport.Wizard
{
/// <summary>
/// https://github.com/3F/DllExport/issues/56
/// TODO: MvsSln core
/// </summary>
public class DxpIsolatedEnv: IsolatedEnv, IEnvironment
{
public const string ERR_MSG = "DXPInternalErrorMsg";

public DxpIsolatedEnv(ISlnResult data)
: base(data)
{

}

protected override Microsoft.Build.Evaluation.Project Load(string path, IDictionary<string, string> properties)
{
try
{
return base.Load(path, properties);
}
catch(Exception ex)
{
LSender._.send(this, $"MBE. Found problem when new Project('{path}'): '{ex.Message}'", Message.Level.Debug);

var prj = new Microsoft.Build.Evaluation.Project();
prj.SetProperty(ERR_MSG, ex.Message);
return prj;
}
}
}
}
19 changes: 13 additions & 6 deletions Wizard/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace net.r_eg.DllExport.Wizard
{
public class Executor: IExecutor, IConfigInitializer, IDisposable
{
protected Dictionary<string, Sln> solutions = new Dictionary<string, Sln>();
protected Dictionary<string, IEnvironment> solutions = new Dictionary<string, IEnvironment>();

/// <summary>
/// Cache for loaded projects.
Expand Down Expand Up @@ -163,7 +163,7 @@ public bool UpdateCfgStorageType(string sln)
return false;
}

if(UniqueProjectsBy(sln).Any(p => p.HasExternalStorage)) {
if(UniqueProjectsBy(sln)?.Any(p => p.HasExternalStorage) == true) {
Config.CfgStorage = CfgStorageType.TargetsFile;
return true;
}
Expand Down Expand Up @@ -206,15 +206,22 @@ protected IEnvironment GetEnv(string file)
return null;
}

if(!solutions.ContainsKey(file)) {
solutions[file] = new Sln(file, SlnItems.EnvWithMinimalProjects);
if(!solutions.ContainsKey(file))
{
var sln = new Sln(file, SlnItems.Projects
| SlnItems.SolutionConfPlatforms
| SlnItems.ProjectConfPlatforms);

solutions[file] = new DxpIsolatedEnv(sln.Result);
solutions[file].LoadMinimalProjects();
}
return solutions[file].Result.Env;

return solutions[file];
}

private void Free()
{
solutions?.ForEach(sln => sln.Value.Result?.Env?.Dispose());
solutions?.ForEach(sln => sln.Value?.Dispose());

if(_targetsFile != null) {
_targetsFile.Dispose();
Expand Down
6 changes: 6 additions & 0 deletions Wizard/IProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public interface IProject
/// </summary>
bool Installed { get; }

/// <summary>
/// Message if an internal error occurred, otherwise null value.
/// TODO: because of DxpIsolatedEnv. See details there.
/// </summary>
string InternalError { get; }

/// <summary>
/// Special identifier. Like `ProjectGuid` that is not available in SDK-based projects.
/// https://github.com/3F/DllExport/issues/36#issuecomment-320794498
Expand Down
15 changes: 13 additions & 2 deletions Wizard/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,16 @@ public IXProject XProject
/// </summary>
public bool Installed
{
get => !String.IsNullOrWhiteSpace(GetProperty(MSBuildProperties.DXP_ID));
get => InternalError == null && !String.IsNullOrWhiteSpace(GetProperty(MSBuildProperties.DXP_ID));
}

/// <summary>
/// Message if an internal error occurred, otherwise null value.
/// TODO: because of DxpIsolatedEnv. See details there.
/// </summary>
public string InternalError
{
get => XProject?.GetProperty(DxpIsolatedEnv.ERR_MSG, true).evaluatedValue;
}

/// <summary>
Expand Down Expand Up @@ -306,7 +315,9 @@ protected IUserConfig GetUserConfig(IXProject project, IConfigInitializer cfg)
/// </summary>
protected void Save()
{
XProject?.Save();
if(XProject?.ProjectFullPath != null && InternalError == null) {
XProject.Save();
}
}

protected void CfgDDNS()
Expand Down
9 changes: 2 additions & 7 deletions Wizard/UI/ConfiguratorForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,8 @@ private void RenderProjects(string sln)

toolTipMain.SetToolTip(comboBoxSln, sln);

var projects = exec.UniqueProjectsBy(sln);
if(projects != null)
{
foreach(var project in projects) {
projectItems.Add(project);
}
}
exec.UniqueProjectsBy(sln)?
.ForEach(prj => projectItems.Add(prj));
}

private void DoSilentAction(Action act, ComboBox box, EventHandler handler)
Expand Down
26 changes: 22 additions & 4 deletions Wizard/UI/Controls/ProjectItemControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using RGiesecke.DllExport;

Expand All @@ -32,7 +33,12 @@ namespace net.r_eg.DllExport.Wizard.UI.Controls
internal sealed partial class ProjectItemControl: UserControl
{
private readonly int EX_HEIGHT;
private IProject project;

public IProject Project
{
get;
private set;
}

public bool Installed
{
Expand Down Expand Up @@ -159,9 +165,21 @@ public void SetNamespace(string name, bool addIfNotExists = true)
Namespaces.Text = name;
}

public bool LockIfError(string msg)
{
if(msg == null) {
return false;
}

chkInstalled.Enabled = false;
textBoxIdent.Text = "Project cannot be loaded:";
textBoxProjectPath.Text = msg;
return true;
}

public ProjectItemControl(IProject project)
{
this.project = project;
Project = project ?? throw new ArgumentNullException(nameof(project));

InitializeComponent();
EX_HEIGHT = Height;
Expand Down Expand Up @@ -244,9 +262,9 @@ private void InstalledStatus(bool status)

private void btnBrowse_Click(object sender, EventArgs e)
{
var path = project?.XProject?.ProjectPath;
var path = Project.XProject?.ProjectItem.project.fullPath;
if(!String.IsNullOrWhiteSpace(path)) {
Browse?.Invoke(path);
Browse?.Invoke(Path.GetDirectoryName(path));
}
}

Expand Down
14 changes: 9 additions & 5 deletions Wizard/UI/Controls/ProjectItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,21 @@ private void ConfigureControl(ProjectItemControl control, IProject project)
control.Platform = project.Config.Platform;
control.Compiler = project.Config.Compiler;

control.Browse = Browse;
control.NamespaceValidate = NamespaceValidate;
control.OpenUrl = OpenUrl;
control.Browse = Browse;
control.OpenUrl = OpenUrl;

if(control.LockIfError(project.InternalError)) {
return;
}

control.NamespaceValidate = NamespaceValidate;

control.Namespaces.Items.Clear();
control.Namespaces.Items.AddRange(project.Config.Namespaces.ToArray());
control.Namespaces.SelectedIndex = 0;

control.Namespaces.MaxLength = project.Config.NSBuffer;

if(project.Installed) {
if(control.Installed) {
control.SetNamespace(project.Config.Namespace, true);
}
}
Expand Down
1 change: 1 addition & 0 deletions Wizard/Wizard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="IExecutor.cs" />
<Compile Include="CompilerCfg.cs" />
<Compile Include="MSBuildProperties.cs" />
<Compile Include="DxpIsolatedEnv.cs" />
<Compile Include="Platform.cs" />
<Compile Include="IProject.cs" />
<Compile Include="ITargetsFile.cs" />
Expand Down

0 comments on commit 20eb9db

Please sign in to comment.