Merge pull request #1027 from enricosada/teach_fsharp_to_dotnet_new_rel_1.0.0

teach fsharp to dotnet new
This commit is contained in:
Piotr Puszkiewicz 2016-01-26 19:18:12 -08:00
commit 511015c331
9 changed files with 98 additions and 12 deletions

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="dotnet-core" value="https://www.myget.org/F/dotnet-core/api/v3/index.json" />
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="fsharp-daily" value="https://www.myget.org/F/fsharp-daily/api/v3/index.json" />
</packageSources>
</configuration>

View file

@ -0,0 +1,9 @@
// Learn more about F# at http://fsharp.org
open System
[<EntryPoint>]
let main argv =
printfn "Hello World!"
printfn "%A" argv
0 // return an integer exit code

View file

@ -0,0 +1,20 @@
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"compilerName": "fsc",
"compileFiles": [
"Program.fs"
],
"dependencies": {
"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-151221",
"NETStandard.Library": "1.0.0-rc2-23704"
},
"frameworks": {
"dnxcore50": { }
}
}

View file

@ -24,13 +24,11 @@ namespace Microsoft.DotNet.Tools.New
return parts[parts.Length - 2] + "." + parts[parts.Length - 1];
}
public int CreateEmptyProject()
public int CreateEmptyProject(string languageName, string templateDir)
{
var thisAssembly = typeof(Program).GetTypeInfo().Assembly;
var resources = from resourceName in thisAssembly.GetManifestResourceNames()
where resourceName.ToLowerInvariant().EndsWith(".cs")
|| resourceName.ToLowerInvariant().EndsWith(".json")
|| resourceName.ToLowerInvariant().EndsWith(".config")
where resourceName.Contains(templateDir)
select resourceName;
var resourceNameToFileName = new Dictionary<string, string>();
@ -42,14 +40,14 @@ namespace Microsoft.DotNet.Tools.New
resourceNameToFileName.Add(resourceName, fileName);
if (File.Exists(fileName))
{
Reporter.Error.WriteLine($"Creating new project would override file {fileName}.");
Reporter.Error.WriteLine($"Creating new {languageName} project would override file {fileName}.");
hasFilesToOverride = true;
}
}
if (hasFilesToOverride)
{
Reporter.Error.WriteLine("Creating new project failed.");
Reporter.Error.WriteLine($"Creating new {languageName} project failed.");
return 1;
}
@ -64,7 +62,7 @@ namespace Microsoft.DotNet.Tools.New
}
}
Reporter.Output.WriteLine($"Created new project in {Directory.GetCurrentDirectory()}.");
Reporter.Output.WriteLine($"Created new {languageName} project in {Directory.GetCurrentDirectory()}.");
return 0;
}
@ -79,8 +77,44 @@ namespace Microsoft.DotNet.Tools.New
app.Description = "Initializes empty project for .NET Platform";
app.HelpOption("-h|--help");
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);
var dotnetNew = new Program();
app.OnExecute(() => dotnetNew.CreateEmptyProject());
app.OnExecute(() => {
var csharp = new { Name = "C#", Alias = new[] { "c#", "cs", "csharp" }, TemplatePrefix = "CSharp", Templates = new[] { "Console" } };
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);
});
try
{

View file

@ -13,16 +13,29 @@ The new command provides a convenient way to initalize a valid .NET Core project
This command is invoked in the context of a directory. When invoked, the command will result in two main artifacts being dropped to the directory:
1. A sample "Hello World" program that exists in `Program.cs` file.
1. A sample "Hello World" program that exists in `Program.cs` ( or `Program.fs` ) file.
2. A valid `project.json` file
After this, the project is ready to be compiled and/or edited further.
# Options
`-l`, `--lang [C#|F#]`
Language of project. Defaults to `C#`. Also `csharp` ( `fsharp` ) or `cs` ( `fs` ) works.
# EXAMPLES
`dotnet new`
Drops a sample in the current directory.
Drops a sample C# project in the current directory.
`dotnet new --lang f#`
Drops a sample F# project in the current directory.
`dotnet new --lang c#`
Drops a sample C# project in the current directory.
# SEE ALSO
dotnet-run(1)

View file

@ -14,8 +14,8 @@
"version": "1.0.0-*"
}
},
"compileExclude": [ "Template/**" ],
"resource": [ "Template/**" ],
"compileExclude": [ "CSharp_Console/**", "FSharp_Console/**" ],
"resource": [ "CSharp_Console/**", "FSharp_Console/**" ],
"frameworks": {
"dnxcore50": { }
},