-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSolutionGenerator.cs
177 lines (164 loc) · 9.04 KB
/
SolutionGenerator.cs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/////////////////////////////////////////////////////////////////////////////////
// CodeLab for Paint.NET
// Copyright 2018 Jason Wendt. All Rights Reserved.
// Portions Copyright ©Microsoft Corporation. All Rights Reserved.
//
// THE DEVELOPERS MAKE NO WARRANTY OF ANY KIND REGARDING THE CODE. THEY
// SPECIFICALLY DISCLAIM ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE OR
// ANY OTHER WARRANTY. THE CODELAB DEVELOPERS DISCLAIM ALL LIABILITY RELATING
// TO THE USE OF THIS CODE. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR
// OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED HEREIN.
//
// Latest distribution: https://www.BoltBait.com/pdn/codelab
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace PdnCodeLab
{
internal static class Solution
{
internal static string Generate(string slnPath, string name, string effectSource, string iconPath, string resourcePath)
{
string effectName = Regex.Replace(name, @"[^\w]", "");
string projectName = effectName + "Effect";
string slnGuid = "{" + Guid.NewGuid().ToString() + "}";
string projGroupGuid = "{" + Guid.NewGuid().ToString() + "}";
string projGuid = "{" + Guid.NewGuid().ToString() + "}";
string pdnPath = Application.StartupPath;
string projPath = Path.Combine(slnPath, projectName);
string propPath = Path.Combine(projPath, "Properties");
bool iconExists = File.Exists(iconPath);
string samplePath = Path.ChangeExtension(resourcePath, ".sample.png");
bool sampleExists = File.Exists(samplePath);
string rtfPath = Path.ChangeExtension(resourcePath, ".rtz");
bool rtfExists = File.Exists(rtfPath);
// Tab indent
StringBuilder slnFile = new StringBuilder();
slnFile.AppendLine();
slnFile.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00");
slnFile.AppendLine("# Visual Studio 17");
slnFile.AppendLine("VisualStudioVersion = 17.9.34728.123");
slnFile.AppendLine("MinimumVisualStudioVersion = 10.0.40219.1");
slnFile.AppendLine($"Project(\"{projGroupGuid}\") = \"{projectName}\", \"{projectName}\\{projectName}.csproj\", \"{projGuid}\"");
slnFile.AppendLine("EndProject");
slnFile.AppendLine("Global");
slnFile.AppendLine("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution");
slnFile.AppendLine("\t\tDebug|Any CPU = Debug|Any CPU");
slnFile.AppendLine("\t\tRelease|Any CPU = Release|Any CPU");
slnFile.AppendLine("\tEndGlobalSection");
slnFile.AppendLine("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution");
slnFile.AppendLine($"\t\t{projGuid}.Debug|Any CPU.ActiveCfg = Debug|Any CPU");
slnFile.AppendLine($"\t\t{projGuid}.Debug|Any CPU.Build.0 = Debug|Any CPU");
slnFile.AppendLine($"\t\t{projGuid}.Release|Any CPU.ActiveCfg = Release|Any CPU");
slnFile.AppendLine($"\t\t{projGuid}.Release|Any CPU.Build.0 = Release|Any CPU");
slnFile.AppendLine("\tEndGlobalSection");
slnFile.AppendLine("\tGlobalSection(SolutionProperties) = preSolution");
slnFile.AppendLine("\t\tHideSolutionNode = FALSE");
slnFile.AppendLine("\tEndGlobalSection");
slnFile.AppendLine("\tGlobalSection(ExtensibilityGlobals) = postSolution");
slnFile.AppendLine($"\t\tSolutionGuid = {slnGuid}");
slnFile.AppendLine("\tEndGlobalSection");
slnFile.AppendLine("EndGlobal");
/*
// Two space indent
StringBuilder slnxFile = new StringBuilder()
.AppendLine("<Solution>")
.AppendLine($" <Project Path=\"{projectName}\\{projectName}.csproj\" />")
.Append("</Solution>"); // no end-of-line at the end of this file
*/
// Two space indent
StringBuilder csprojFile = new StringBuilder();
csprojFile.AppendLine("<Project Sdk=\"Microsoft.NET.Sdk\">");
csprojFile.AppendLine();
csprojFile.AppendLine(" <PropertyGroup>");
csprojFile.AppendLine($" <TargetFramework>net{Environment.Version.ToString(2)}-windows</TargetFramework>");
csprojFile.AppendLine(" <GenerateAssemblyInfo>false</GenerateAssemblyInfo>");
csprojFile.AppendLine(" <UseWindowsForms>true</UseWindowsForms>");
csprojFile.AppendLine(" <AllowUnsafeBlocks>true</AllowUnsafeBlocks>");
csprojFile.AppendLine($" <RootNamespace>{projectName}</RootNamespace>");
csprojFile.AppendLine($" <AssemblyName>{effectName}</AssemblyName>");
csprojFile.AppendLine(" <Deterministic>false</Deterministic>");
csprojFile.AppendLine(" </PropertyGroup>");
csprojFile.AppendLine();
if (iconExists || sampleExists || rtfExists)
{
csprojFile.AppendLine(" <ItemGroup>");
if (iconExists)
{
csprojFile.AppendLine($" <EmbeddedResource Include=\"{effectName}.png\" />");
}
if (sampleExists)
{
csprojFile.AppendLine($" <EmbeddedResource Include=\"{effectName}.sample.png\" />");
}
if (rtfExists)
{
csprojFile.AppendLine($" <EmbeddedResource Include=\"{effectName}.rtz\" />");
}
csprojFile.AppendLine(" </ItemGroup>");
csprojFile.AppendLine();
}
csprojFile.AppendLine(" <ItemGroup>");
foreach (string assemblyName in Intelli.PdnAssemblyNames)
{
csprojFile.AppendLine($" <Reference Include=\"{assemblyName}\">");
csprojFile.AppendLine($" <HintPath>{Path.Combine(pdnPath, $"{assemblyName}.dll")}</HintPath>");
csprojFile.AppendLine(" </Reference>");
}
csprojFile.AppendLine(" </ItemGroup>");
csprojFile.AppendLine();
csprojFile.AppendLine(" <Target Name=\"PostBuild\" AfterTargets=\"PostBuildEvent\">");
csprojFile.AppendLine(" <Exec Command=\"cmd /c explorer "$(TargetDir)"
exit 0\" />");
csprojFile.AppendLine(" </Target>");
csprojFile.Append("</Project>"); // no end-of-line at the end of this file
// Two space indent
StringBuilder launchSettingsFile = new StringBuilder();
launchSettingsFile.AppendLine("{");
launchSettingsFile.AppendLine(" \"profiles\": {");
launchSettingsFile.AppendLine(" \"" + projectName + "\": {");
launchSettingsFile.AppendLine(" \"commandName\": \"Executable\",");
launchSettingsFile.AppendLine($" \"executablePath\": \"{Path.Combine(pdnPath, "PaintDotNet.exe").Replace(@"\", @"\\")}\",");
launchSettingsFile.AppendLine($" \"workingDirectory\": \"{pdnPath.Replace(@"\", @"\\")}\"");
launchSettingsFile.AppendLine(" }");
launchSettingsFile.AppendLine(" }");
launchSettingsFile.Append("}"); // no end-of-line at the end of this file
try
{
File.WriteAllText(Path.Combine(slnPath, projectName + ".sln"), slnFile.ToString());
//File.WriteAllText(Path.Combine(slnPath, projectName + ".slnx"), slnxFile.ToString());
if (!Directory.Exists(projPath))
{
Directory.CreateDirectory(projPath);
}
File.WriteAllText(Path.Combine(projPath, projectName + ".csproj"), csprojFile.ToString());
File.WriteAllText(Path.Combine(projPath, effectName + ".cs"), effectSource);
if (iconExists)
{
File.Copy(iconPath, Path.Combine(projPath, Path.GetFileName(iconPath)), true);
}
if (sampleExists)
{
File.Copy(samplePath, Path.Combine(projPath, Path.GetFileName(samplePath)), true);
}
if (rtfExists)
{
File.Copy(rtfPath, Path.Combine(projPath, Path.GetFileName(rtfPath)), true);
}
if (!Directory.Exists(propPath))
{
Directory.CreateDirectory(propPath);
}
File.WriteAllText(Path.Combine(propPath, "launchSettings.json"), launchSettingsFile.ToString());
}
catch (Exception ex)
{
FlexibleMessageBox.Show("Solution generated failed.\r\n\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
return Path.Combine(slnPath, projectName + ".sln");
}
}
}