-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathProgram.cs
More file actions
109 lines (100 loc) · 3.77 KB
/
Copy pathProgram.cs
File metadata and controls
109 lines (100 loc) · 3.77 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ProjectInitializer
{
/// <summary>
/// This progam is a tool for the FFWD template that helps you to rename the existing template to fit your project.
/// </summary>
class Program
{
public string projectName;
public string templateName = "FFWD.Template";
public string startingDir = "XNA";
public string[] ignoreDirs = new string[] { "bin", "obj" };
public string[] ignoreFileTypes = new string[] { ".ncrunchsolution", ".user", ".cachefile", ".docstates", ".suo" };
public string[] fixFileContentsOfFileTypes = new string[] { ".csproj", ".sln" };
static void Main(string[] args)
{
Program p = new Program();
if (args.Length != 1)
{
Console.WriteLine("What is the name of your new project:");
p.projectName = Console.ReadLine();
}
else
{
p.projectName = args[0];
}
#if DEBUG
p.startingDir = @"..\..\..\..\..\..\XNA";
#endif
p.Execute();
Console.WriteLine("Press a key to close the window.");
Console.ReadKey();
}
private void Execute()
{
if (String.IsNullOrEmpty(projectName))
{
Console.WriteLine("You did not provide a name for the project.");
return;
}
if (!Directory.Exists(startingDir))
{
Console.WriteLine("This program needs to be run in the root of the FFWD template project.");
return;
}
CopyContents(startingDir, startingDir);
Console.WriteLine("The project was copied successfully.");
}
private void RenameSubdirs(string path)
{
foreach (var item in Directory.EnumerateDirectories(path))
{
if (ignoreDirs.Contains(Path.GetFileName(item)))
{
continue;
}
if (item.Contains(templateName))
{
string newDir = item.Replace(templateName, projectName);
Directory.CreateDirectory(newDir);
CopyContents(item, newDir);
}
}
}
private void CopyContents(string dir, string newDir)
{
foreach (var item in Directory.EnumerateFiles(dir))
{
string newFileName = Path.GetFileName(item);
if (newFileName.Contains(templateName))
{
newFileName = newFileName.Replace(templateName, projectName);
}
if (!ignoreFileTypes.Contains(Path.GetExtension(newFileName)))
{
string completeNewName = Path.Combine(newDir, newFileName);
if (!item.Equals(completeNewName, StringComparison.InvariantCultureIgnoreCase))
{
File.Copy(item, completeNewName);
}
if (fixFileContentsOfFileTypes.Contains(Path.GetExtension(newFileName)))
{
FixFileContents(completeNewName);
}
}
}
RenameSubdirs(dir);
}
private void FixFileContents(string newFileName)
{
string text = File.ReadAllText(newFileName, Encoding.UTF8);
text = text.Replace(templateName, projectName);
File.WriteAllText(newFileName, text, Encoding.UTF8);
}
}
}