-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSolutionGenerator.cs
More file actions
143 lines (131 loc) · 6.79 KB
/
SolutionGenerator.cs
File metadata and controls
143 lines (131 loc) · 6.79 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
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
/////////////////////////////////////////////////////////////////////////////////
// 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 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);
// Two space indent
StringBuilder slnxFile = new StringBuilder()
.AppendLine("<Solution>")
.AppendLine($" <Project Path=\"{projectName}/{projectName}.csproj\" />")
.AppendLine("</Solution>");
// 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 + ".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");
}
}
}