forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualStudioSolutionWriter.cs
More file actions
98 lines (88 loc) · 3.92 KB
/
VisualStudioSolutionWriter.cs
File metadata and controls
98 lines (88 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ScriptCs.Contracts;
namespace ScriptCs.Hosting
{
public class VisualStudioSolutionWriter : IVisualStudioSolutionWriter
{
internal DirectoryInfo Root { get; set; }
private Guid _nullGuid = new Guid();
public string WriteSolution(IFileSystem fs, string script, IVisualStudioSolution solution, IList<ProjectItem> nestedItems = null)
{
if (nestedItems == null)
{
nestedItems = new List<ProjectItem>();
}
var launcher = Path.Combine(fs.TempPath, "launcher-" + Guid.NewGuid().ToString() + ".sln");
if (fs.FileExists(launcher))
{
fs.FileDelete(launcher);
}
var currentDir = fs.CurrentDirectory;
var scriptcsPath = Path.Combine(fs.HostBin, "scriptcs.exe");
var scriptcsArgs = string.Format("{0} -debug -loglevel info", script);
Root = new DirectoryInfo { Name = "Solution Items", FullPath = currentDir};
var projectGuid = Guid.NewGuid();
solution.AddScriptcsProject(scriptcsPath, currentDir, scriptcsArgs, false, projectGuid);
GetDirectoryInfo(fs, currentDir, Root);
AddDirectoryProject(solution, fs, Root, _nullGuid, nestedItems);
solution.AddGlobal(projectGuid, nestedItems);
fs.WriteToFile(launcher, solution.ToString());
return launcher;
}
private void AddDirectoryProject(IVisualStudioSolution solution, IFileSystem fs, DirectoryInfo currentDirectory, Guid parent, IList<ProjectItem> nestedItems)
{
solution.AddProject(currentDirectory.FullPath, currentDirectory.Name, currentDirectory.Guid, currentDirectory.Files.ToArray());
foreach (DirectoryInfo dir in currentDirectory.Directories.Values)
{
AddDirectoryProject(solution, fs, dir, currentDirectory.Guid, nestedItems);
}
if (parent != _nullGuid)
{
nestedItems.Add(new ProjectItem(currentDirectory.Guid, parent));
}
}
private void GetDirectoryInfo(IFileSystem fs, string currentDir, DirectoryInfo root)
{
IEnumerable<string> files;
var packagesFolder = Path.Combine(currentDir, fs.PackagesFolder);
files = fs.EnumerateFilesAndDirectories(currentDir, "*.csx").Where(
f => f.StartsWith(packagesFolder).Equals(false));
var skip = currentDir.Length;
foreach (var file in files)
{
var pruned = file.Substring(skip + 1);
var segments = pruned.Split(Path.DirectorySeparatorChar);
if (segments.Length == 1)
{
root.Files.Add(segments[0]);
}
else
{
var currentDirectory = root;
var path = "";
for (int i = 0; i < segments.Length - 1; i++)
{
var segment = segments[i];
path = path + segment + @"\";
if (!currentDirectory.Directories.ContainsKey(segment))
{
var newDirectory = new DirectoryInfo { Name = segment, Path=path, FullPath = Path.GetDirectoryName(file)};
currentDirectory.Directories[segment] = newDirectory;
currentDirectory = newDirectory;
}
else
{
currentDirectory = currentDirectory.Directories[segment];
}
}
currentDirectory.Files.Add(segments[segments.Length - 1]);
}
}
}
}
}