Exemple #1
0
        public override void Run(IEnumerable <string> args)
        {
            args = Options.SafeParse(args);
            if (!args.Any() || args.Any(Templates.HelpOptions.Contains))
            {
                Help(args);
                return;
            }
            if (!(temp == "csharp" || temp == "fsharp" || temp == "none"))
            {
                Tools.FailWith("error: invalid language template '{0}'.", temp);
            }
            if (!new [] { "library", "exe", "winexe", "module" }.Contains(type))
            {
                Tools.FailWith("error: invalid output type '{0}'.", type);
            }

            var fn = args.First();

            if (!Path.GetFileName(fn).Contains("."))
            {
                fn += ".proj";
            }
            var f    = Templates.GenPath(outputdir, fn);
            var name = Path.GetFileName(fn).Split('.').Rev().Skip(1).Rev().JoinToString();

            if (!string.IsNullOrEmpty(outputdir) && !Directory.Exists(outputdir))
            {
                Directory.CreateDirectory(outputdir);
            }
            if (!File.Exists(f))
            {
                File.Create(f).Close();
            }

            // prepare project
            var p = new Project();

            p.DefaultTargets      = "Build";
            p.DefaultToolsVersion = "4.0";

            // add assembly properties
            var assemblyProperty = p.AddNewPropertyGroup(false);
            var defcond          = assemblyProperty.AddNewProperty("Configuration", "Debug");

            defcond.Condition = " '$(Configuration)' == '' ";
            var defarch = assemblyProperty.AddNewProperty("Platform", arch);

            defarch.Condition = " '$(Platform)' == '' ";
            assemblyProperty.AddNewProperty("ProjectGuid", "{" + Guid.NewGuid().ToString().ToUpper() + "}");
            assemblyProperty.AddNewProperty("OutputType", type.Substring(0, 1).ToUpper() + type.Substring(1));
            assemblyProperty.AddNewProperty("RootNamespace", name);
            assemblyProperty.AddNewProperty("AssemblyName", name);

            // add properties for each configurations
            var gs              = ProjectTools.AddDefaultConfigurations(p, arch);
            var debugProperty   = gs.Item1;
            var releaseProperty = gs.Item2;

            // add sources
            var sourceItems = p.AddNewItemGroup();

            // add references
            var referenceItems = p.AddNewItemGroup();

            referenceItems.AddNewItem("Reference", "System");
            if (temp == "fsharp")
            {
                referenceItems.AddNewItem("Reference", "mscorlib");
                referenceItems.AddNewItem("Reference", "FSharp.Core");
                referenceItems.AddNewItem("Reference", "System.Core");
                referenceItems.AddNewItem("Reference", "System.Numerics");
            }

            // custom
            if (temp == "csharp")
            {
                var ai = Templates.GenPath(outputdir, Templates.AssemblyInfo);
                File.WriteAllText(ai, Templates.GenerateAssemblyInfo(name));
                sourceItems.AddNewItem("Compile", Templates.AssemblyInfo);
                if (type.Contains("exe"))
                {
                    var prg = Templates.GenPath(outputdir, "Program.cs");
                    File.WriteAllText(prg, Templates.GenerateExeClass(name));
                    sourceItems.AddNewItem("Compile", "Program.cs");
                    debugProperty.AddNewProperty("PlatformTarget", arch);
                    releaseProperty.AddNewProperty("PlatformTarget", arch);
                }
                else if (type == "library")
                {
                    var lib = Templates.GenPath(outputdir, "MyClass.cs");
                    File.WriteAllText(lib, Templates.GenerateLibraryClass(name));
                    sourceItems.AddNewItem("Compile", "MyClass.cs");
                }
            }
            else if (temp == "fsharp")
            {
                debugProperty.AddNewProperty("Tailcalls", "false");
                releaseProperty.AddNewProperty("Tailcalls", "true");

                var ai = Templates.GenPath(outputdir, Templates.AssemblyInfoFSharp);
                File.WriteAllText(ai, Templates.GenerateAssemblyInfoFSharp(name));
                sourceItems.AddNewItem("Compile", Templates.AssemblyInfoFSharp);
                if (type.Contains("exe"))
                {
                    var prg = Templates.GenPath(outputdir, "Program.fs");
                    File.WriteAllText(prg, Templates.GenerateExeFSharp());
                    sourceItems.AddNewItem("Compile", "Program.fs");
                    debugProperty.AddNewProperty("PlatformTarget", arch);
                    releaseProperty.AddNewProperty("PlatformTarget", arch);
                }
                else if (type == "library")
                {
                    var lib = Templates.GenPath(outputdir, "Component1.fs");
                    File.WriteAllText(lib, Templates.GenerateLibraryFSharp(name));
                    sourceItems.AddNewItem("Compile", "Component1.fs");
                }
            }

            // add imports
            if (temp == "csharp")
            {
                p.AddNewImport("$(MSBuildBinPath)\\Microsoft.CSharp.targets", "");
            }
            else if (temp == "fsharp")
            {
                p.AddNewImport("$(MSBuildExtensionsPath32)\\..\\Microsoft SDKs\\F#\\3.1\\Framework\\v4.0\\Microsoft.FSharp.Targets", "");
            }

            // save
            Using.SelectMany(
                File.OpenWrite(f).Use(),
                fs => new StreamWriter(fs).Use(),
                (_, sw) => p.Save(sw)
                );
        }