2016-04-20 03:51:32 +00:00
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
2015-11-17 19:40:07 +00:00
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2016-05-27 19:37:17 +00:00
|
|
|
|
using System.IO.Compression;
|
2015-11-17 19:40:07 +00:00
|
|
|
|
|
2015-12-03 10:16:52 +00:00
|
|
|
|
namespace Microsoft.DotNet.Tools.New
|
2015-11-17 19:40:07 +00:00
|
|
|
|
{
|
2016-01-31 05:47:50 +00:00
|
|
|
|
public class NewCommand
|
2015-11-17 19:40:07 +00:00
|
|
|
|
{
|
|
|
|
|
private static string GetFileNameFromResourceName(string s)
|
|
|
|
|
{
|
|
|
|
|
// A.B.C.D.filename.extension
|
|
|
|
|
string[] parts = s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (parts.Length < 2)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// filename.extension
|
|
|
|
|
return parts[parts.Length - 2] + "." + parts[parts.Length - 1];
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-26 01:46:33 +00:00
|
|
|
|
public int CreateEmptyProject(string languageName, string templateDir)
|
2015-11-17 19:40:07 +00:00
|
|
|
|
{
|
2016-01-31 05:47:50 +00:00
|
|
|
|
var thisAssembly = typeof(NewCommand).GetTypeInfo().Assembly;
|
2015-11-17 19:40:07 +00:00
|
|
|
|
var resources = from resourceName in thisAssembly.GetManifestResourceNames()
|
2015-12-26 01:46:33 +00:00
|
|
|
|
where resourceName.Contains(templateDir)
|
2015-11-17 19:40:07 +00:00
|
|
|
|
select resourceName;
|
|
|
|
|
|
2015-11-17 18:22:26 +00:00
|
|
|
|
var resourceNameToFileName = new Dictionary<string, string>();
|
2015-11-17 19:40:07 +00:00
|
|
|
|
bool hasFilesToOverride = false;
|
|
|
|
|
foreach (string resourceName in resources)
|
|
|
|
|
{
|
|
|
|
|
string fileName = GetFileNameFromResourceName(resourceName);
|
|
|
|
|
|
2016-05-27 19:37:17 +00:00
|
|
|
|
using (var resource = thisAssembly.GetManifestResourceStream(resourceName))
|
2015-11-17 19:40:07 +00:00
|
|
|
|
{
|
2016-05-27 19:37:17 +00:00
|
|
|
|
var archive = new ZipArchive(resource);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
archive.ExtractToDirectory(Directory.GetCurrentDirectory());
|
|
|
|
|
|
|
|
|
|
File.Move(
|
|
|
|
|
Path.Combine(Directory.GetCurrentDirectory(), "project.json.template"),
|
|
|
|
|
Path.Combine(Directory.GetCurrentDirectory(), "project.json"));
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine(ex.Message);
|
|
|
|
|
hasFilesToOverride = true;
|
|
|
|
|
}
|
2015-11-17 19:40:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hasFilesToOverride)
|
|
|
|
|
{
|
2015-12-26 01:46:33 +00:00
|
|
|
|
Reporter.Error.WriteLine($"Creating new {languageName} project failed.");
|
2015-11-17 19:40:07 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-26 01:46:33 +00:00
|
|
|
|
Reporter.Output.WriteLine($"Created new {languageName} project in {Directory.GetCurrentDirectory()}.");
|
2015-11-17 19:40:07 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-31 05:47:50 +00:00
|
|
|
|
public static int Run(string[] args)
|
2015-11-17 19:40:07 +00:00
|
|
|
|
{
|
|
|
|
|
DebugHelper.HandleDebugSwitch(ref args);
|
|
|
|
|
|
|
|
|
|
var app = new CommandLineApplication();
|
2015-12-03 10:16:52 +00:00
|
|
|
|
app.Name = "dotnet new";
|
2015-11-17 19:40:07 +00:00
|
|
|
|
app.FullName = ".NET Initializer";
|
|
|
|
|
app.Description = "Initializes empty project for .NET Platform";
|
|
|
|
|
app.HelpOption("-h|--help");
|
|
|
|
|
|
2015-12-26 01:46:33 +00:00
|
|
|
|
var lang = app.Option("-l|--lang <LANGUAGE>", "Language of project [C#|F#]", CommandOptionType.SingleValue);
|
|
|
|
|
var type = app.Option("-t|--type <TYPE>", "Type of project", CommandOptionType.SingleValue);
|
|
|
|
|
|
2016-01-31 05:47:50 +00:00
|
|
|
|
var dotnetNew = new NewCommand();
|
2015-12-26 01:46:33 +00:00
|
|
|
|
app.OnExecute(() => {
|
|
|
|
|
|
2016-05-27 19:37:17 +00:00
|
|
|
|
var csharp = new { Name = "C#", Alias = new[] { "c#", "cs", "csharp" }, TemplatePrefix = "CSharp", Templates = new[] { "Console", "Web" } };
|
2015-12-26 01:46:33 +00:00
|
|
|
|
var fsharp = new { Name = "F#", Alias = new[] { "f#", "fs", "fsharp" }, TemplatePrefix = "FSharp", Templates = new[] { "Console" } };
|
|
|
|
|
|
|
|
|
|
string languageValue = lang.Value() ?? csharp.Name;
|
|
|
|
|
|
|
|
|
|
var language = new[] { csharp, fsharp }
|
|
|
|
|
.FirstOrDefault(l => l.Alias.Contains(languageValue, StringComparer.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (language == null)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine($"Unrecognized language: {languageValue}".Red());
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string typeValue = type.Value() ?? language.Templates.First();
|
|
|
|
|
|
|
|
|
|
string templateName = language.Templates.FirstOrDefault(t => StringComparer.OrdinalIgnoreCase.Equals(typeValue, t));
|
|
|
|
|
if (templateName == null)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine($"Unrecognized type: {typeValue}".Red());
|
|
|
|
|
Reporter.Error.WriteLine($"Avaiable types for {language.Name} :".Red());
|
|
|
|
|
foreach (var t in language.Templates)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine($"- {t}".Red());
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string templateDir = $"{language.TemplatePrefix}_{templateName}";
|
|
|
|
|
|
|
|
|
|
return dotnetNew.CreateEmptyProject(language.Name, templateDir);
|
|
|
|
|
});
|
2015-11-17 19:40:07 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return app.Execute(args);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
2015-11-17 20:06:07 +00:00
|
|
|
|
Reporter.Error.WriteLine(ex.ToString());
|
2015-11-17 19:40:07 +00:00
|
|
|
|
#else
|
2015-11-17 20:06:07 +00:00
|
|
|
|
Reporter.Error.WriteLine(ex.Message);
|
2015-11-17 19:40:07 +00:00
|
|
|
|
#endif
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|