PageRenderTime 25ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/KProjectConverter/KProject.cs

https://github.com/desdesdes/KProjectConverter
C# | 114 lines | 94 code | 16 blank | 4 comment | 9 complexity | d8eaabcb57103e26473d26aa63fef280 MD5 | raw file
  1using Newtonsoft.Json.Linq;
  2using System;
  3using System.Collections.Generic;
  4using System.IO;
  5using System.Linq;
  6using System.Xml.Linq;
  7
  8namespace KProjectConverter
  9{
 10  /// <summary>
 11  /// Summary description for KProject
 12  /// </summary>
 13  public class KProject
 14  {
 15    private static XNamespace _msbuildNs = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
 16    CSProject _project;
 17    ISet<string> _projectReferences;
 18
 19    public KProject(CSProject project, ISet<string> projectReferences)
 20    {
 21      _project = project;
 22      _projectReferences = projectReferences;
 23    }
 24
 25    private static void MakeBackup(string filePath)
 26    {
 27      if (File.Exists(filePath))
 28      {
 29        File.Move(filePath, string.Format("{0}.backup", filePath));
 30      }
 31    }
 32
 33    public void DeleteOldProjectFiles()
 34    {
 35      File.Delete(_project.ProjectFilePath);
 36      File.Delete(string.Format("{0}.vspscc", _project.ProjectFilePath));
 37    }
 38
 39    private static string GetProjectNameFromReference(string reference)
 40    {
 41      var pos = reference.IndexOf(',');
 42
 43      if (pos == -1)
 44      {
 45        return reference;
 46      }
 47
 48      return reference.Substring(0, pos);
 49    }
 50
 51    public void BuildKproj()
 52    {
 53      var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("embed/base.kproj");
 54      var kprojXml = XElement.Load(stream);
 55      var globalXml = kprojXml.Elements(_msbuildNs + "PropertyGroup").Single(e => e.Attribute("Label")?.Value?.Equals("Globals") == true);
 56      globalXml.Element(_msbuildNs + "ProjectGuid").Value = _project.Info.ProjectGuid.ToString("D");
 57      globalXml.Element(_msbuildNs + "RootNamespace").Value = _project.Info.RootNamespace;
 58
 59      var kprojectFilePath = Path.ChangeExtension(_project.ProjectFilePath, "kproj");
 60      kprojXml.Save(kprojectFilePath);
 61    }
 62
 63    public void BuildProjectJson(IEnumerable<KDependency> additionalDependencies)
 64    {
 65      var packageSet = new HashSet<string>();
 66      var generalDependencies = new List<JProperty>();
 67
 68      foreach (var additionalDependency in additionalDependencies)
 69      {
 70        generalDependencies.Add(new JProperty(additionalDependency.Package, additionalDependency.Version));
 71      }
 72
 73      foreach (var package in _project.Info.Packages)
 74      {
 75        generalDependencies.Add(new JProperty(package.Package, package.Version));
 76        packageSet.Add(package.Package);
 77      }
 78
 79      var netDependencies = new List<JProperty>();
 80      foreach (var reference in _project.Info.References)
 81      {
 82        if (FrameworkReferenceResolver.IsFrameworkReference(reference))
 83        {
 84          if (!FrameworkReferenceResolver.IsStandardKReference(reference))
 85          {
 86            netDependencies.Add(new JProperty(FrameworkReferenceResolver.GetFrameworkReferenceCorrectCase(reference), ""));
 87          }
 88        }
 89        else
 90        {
 91          // Build project name from reference
 92          var projectName = GetProjectNameFromReference(reference);
 93
 94          if (_projectReferences.Contains(projectName))
 95          {
 96            generalDependencies.Add(new JProperty(reference, "1.0.0-*"));
 97          }
 98          else if (!packageSet.Contains(projectName))
 99          {
100            _project.Warnings.Add(string.Format("Possible resolvable reference to '{0}', check if this is part of a package", projectName));
101          }
102        }
103      }
104
105      var projectJson = new JObject();
106      projectJson.Add(new JProperty("version", "1.0.0-*"));
107      projectJson.Add(new JProperty("dependencies", new JObject(generalDependencies)));
108      projectJson.Add(new JProperty("frameworks", new JObject(new JProperty("aspnet50", new JObject(new JProperty("frameworkAssemblies", new JObject(netDependencies)))))));
109
110      var projectPath = Path.Combine(Path.GetDirectoryName(_project.ProjectFilePath), "project.json");
111      File.WriteAllText(projectPath, projectJson.ToString());
112    }
113  }
114}