public static bool Execute(ProjectProperties properties, Log log) { Console.WriteLine("compiling"); var processSettings = new ProcessStartInfo(); processSettings.FileName = properties.CscPath; processSettings.Arguments = properties.FormatCscArguments(); log.WriteLine("Executing {0}", processSettings.FileName); log.WriteLine("Csc Arguments: {0}", processSettings.Arguments); processSettings.CreateNoWindow = true; processSettings.RedirectStandardOutput = true; processSettings.UseShellExecute = false; Process cscProcess = null; try { cscProcess = Process.Start(processSettings); } catch (Win32Exception) { Console.WriteLine("ERROR: csc.exe needs to be on the path."); return false; } var output = cscProcess.StandardOutput.ReadToEnd(); log.WriteLine(output); cscProcess.WaitForExit(); if (output.Contains("error CS")) return false; return true; }
static void LogProperties(this Log log, ProjectProperties project, string heading) { if (!log.IsEnabled) return; log.WriteLine(heading); log.WriteLine("ProjectDirectory {0}", project.ProjectDirectory); log.WriteLine("PackagesDirectory {0}", project.PackagesDirectory); log.WriteLine("OutputDirectory {0}", project.OutputDirectory); log.WriteLine("ToolsDirectory {0}", project.ToolsDirectory); log.WriteLine("ExecutableFilename {0}", project.AssemblyName); log.WriteLine("csc.exe Path {0}", project.CscPath); log.WriteLine("output path {0}", project.OutputAssemblyPath); log.WriteList(project.Sources, "SOURCES"); log.WriteList(project.Packages, "PACKAGES"); log.WriteList(project.References, "REFERENCES"); log.WriteList(project.Dependencies, "DEPENDENCIES"); log.WriteList(project.CscOptions, "CSCOPTIONS"); log.WriteLine("-------------------------------------------------"); }
static void FindCompiler(ProjectProperties properties) { properties.CscPath = Path.Combine(properties.ToolsDirectory, "csc.exe"); if (File.Exists(properties.CscPath)) { return; } properties.CscPath = @"D:\git\roslyn\Binaries\Debug\core-clr\csc.exe"; if (!File.Exists(properties.CscPath)) { properties.CscPath = "csc.exe"; } }
public static ProjectProperties InitializeProperties(string[] args, Log log) { // General Properites ProjectProperties properties = new ProjectProperties(); properties.ProjectDirectory = Path.Combine(Environment.CurrentDirectory); properties.PackagesDirectory = Path.Combine(properties.ProjectDirectory, "packages"); properties.OutputDirectory = Path.Combine(properties.ProjectDirectory, "bin"); properties.ToolsDirectory = Path.Combine(properties.ProjectDirectory, "tools"); properties.AssemblyName = Path.GetFileName(properties.ProjectDirectory); FindCompiler(properties); // Sources properties.Sources.AddRange(Directory.GetFiles(properties.ProjectDirectory, "*.cs")); if (properties.Sources.Count == 1) { properties.AssemblyName = Path.GetFileNameWithoutExtension(properties.Sources[0]); } // Packages properties.Packages.Add(@"""Microsoft.NETCore.Console"": ""1.0.0-beta-*"""); properties.Packages.Add(@"""Microsoft.NETCore.ConsoleHost-x86"": ""1.0.0-beta-23123"""); // References properties.References.Add(Path.Combine(properties.PackagesDirectory, @"System.Runtime\4.0.20\ref\dotnet\System.Runtime.dll")); properties.References.Add(Path.Combine(properties.PackagesDirectory, @"System.Console\4.0.0-beta-23123\ref\dotnet\System.Console.dll")); // Runtime Dependencies properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"Microsoft.NETCore.Runtime.CoreCLR-x86\1.0.0\runtimes\win7-x86\native")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"Microsoft.NETCore.Runtime.CoreCLR-x86\1.0.0\runtimes\win7-x86\lib\dotnet")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Runtime\4.0.20\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Console\4.0.0-beta-23123\lib\DNXCore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.IO\4.0.10\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Threading\4.0.10\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.IO.FileSystem.Primitives\4.0.0\lib\dotnet")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Text.Encoding\4.0.10\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Threading.Tasks\4.0.10\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Text.Encoding.Extensions\4.0.10\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Runtime.InteropServices\4.0.20\lib\netcore50")); // CSC OPTIONS properties.CscOptions.Add("/nostdlib"); properties.CscOptions.Add("/noconfig"); LogProperties(log, properties, "Initialized Properties Log:"); Adjust(Path.Combine(properties.ProjectDirectory, "dependencies.txt"), properties.Dependencies); Adjust(Path.Combine(properties.ProjectDirectory, "references.txt"), properties.References); Adjust(Path.Combine(properties.ProjectDirectory, "cscoptions.txt"), properties.CscOptions); Adjust(Path.Combine(properties.ProjectDirectory, "packages.txt"), properties.Packages); LogProperties(log, properties, "Adjusted Properties Log:"); return properties; }
static void OutputRuntimeDependenciesAction(ProjectProperties properties) { foreach (var dependencyFolder in properties.Dependencies) { Helpers.CopyAllFiles(dependencyFolder, properties.OutputDirectory); } }
static void ConvertToCoreConsoleAction(ProjectProperties properties) { var dllPath = Path.ChangeExtension(properties.OutputAssemblyPath, "dll"); if (File.Exists(dllPath)) { File.Delete(dllPath); } File.Move(properties.OutputAssemblyPath, dllPath); File.Copy(Path.Combine(properties.PackagesDirectory, @"Microsoft.NETCore.ConsoleHost-x86\1.0.0-beta-23123\runtimes\win7-x86\native\CoreConsole.exe"), properties.OutputAssemblyPath); }
static void CreateDefaultProjectJson(ProjectProperties properties) { using (var file = new StreamWriter(Path.Combine(properties.ToolsDirectory, "project.json"), false)) { file.WriteLine(@"{"); file.WriteLine(@" ""dependencies"": {"); for (int index = 0; index < properties.Packages.Count; index++) { var package = properties.Packages[index]; file.Write(@" "); file.Write(package); if (index < properties.Packages.Count - 1) { file.WriteLine(","); } else { file.WriteLine(); } } file.WriteLine(@" },"); file.WriteLine(@" ""frameworks"": {"); file.WriteLine(@" ""dnxcore50"": { }"); file.WriteLine(@" }"); file.WriteLine(@"}"); //"runtimes": { //"win7-x86": { }, //"win7-x64": { } //}, } }
//static void CreateNugetConfig(ProjectProperties properties) //{ // using (var file = new StreamWriter(Path.Combine(properties.ToolsDirectory, "nuget.config"), false)) // { // } //} public static void RestorePackagesAction(ProjectProperties properties, Log log) { Console.WriteLine("restoring packages"); var projectFile = Path.Combine(properties.ProjectDirectory, "project.json"); if (!File.Exists(projectFile)) { projectFile = Path.Combine(properties.ToolsDirectory, "project.json"); } var processSettings = new ProcessStartInfo(); processSettings.FileName = Path.Combine(properties.ToolsDirectory, "nuget.exe"); processSettings.Arguments = "restore " + projectFile + " -PackagesDirectory " + properties.PackagesDirectory; processSettings.CreateNoWindow = true; processSettings.UseShellExecute = false; processSettings.RedirectStandardOutput = true; log.WriteLine("Executing {0}", processSettings.FileName); log.WriteLine("Arguments: {0}", processSettings.Arguments); log.WriteLine("project.json:\n{0}", File.ReadAllText(projectFile)); var process = Process.Start(processSettings); var output = process.StandardOutput.ReadToEnd(); log.WriteLine(output); process.WaitForExit(); }
public static void DownloadNugetAction(ProjectProperties properties) { CreateDefaultProjectJson(properties); string destination = Path.Combine(properties.ToolsDirectory, "nuget.exe"); if (File.Exists(destination)) { return; } var client = new HttpClient(); using (var sourceStreamTask = client.GetStreamAsync(@"http://dist.nuget.org/win-x86-commandline/v3.1.0-beta/nuget.exe")) { sourceStreamTask.Wait(); using (var sourceStream = sourceStreamTask.Result) using (var destinationStream = new FileStream(destination, FileMode.Create, FileAccess.Write)) { byte[] buffer = new byte[1024]; while (true) { var read = sourceStream.Read(buffer, 0, buffer.Length); if (read < 1) break; destinationStream.Write(buffer, 0, read); } } } }
static List<string> ParseProjectFile(ProjectProperties properties, string projectFile) { var sourceFiles = new List<string>(); using (XmlReader xmlReader = XmlReader.Create(projectFile)) { xmlReader.MoveToContent(); while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "Compile") { var sourceFile = xmlReader.GetAttribute("Include"); if (sourceFile == "*.cs") { sourceFiles.AddRange(Directory.GetFiles(properties.ProjectDirectory, "*.cs")); } else if(sourceFile.EndsWith("\\*.cs")) { sourceFiles.AddRange(Directory.GetFiles(Path.Combine(properties.ProjectDirectory, sourceFile.Replace("\\*.cs", "")), "*.cs")); } else { sourceFiles.Add(Path.Combine(properties.ProjectDirectory, sourceFile)); } } } } return sourceFiles; }
public static ProjectProperties InitializeProperties(string[] args, Log log, bool buildDll = false) { // General Properites ProjectProperties properties = new ProjectProperties(); properties.ProjectDirectory = Path.Combine(Environment.CurrentDirectory); properties.PackagesDirectory = Path.Combine(properties.ProjectDirectory, "packages"); properties.OutputDirectory = Path.Combine(properties.ProjectDirectory, "bin"); properties.ToolsDirectory = Path.Combine(properties.ProjectDirectory, "tools"); properties.AssemblyName = Path.GetFileName(properties.ProjectDirectory); properties.OutputType = buildDll ? ".dll" : ".exe"; FindCompiler(properties); var projectFiles = Directory.GetFiles(properties.ProjectDirectory, "*.dotnetproj"); // Sources if (projectFiles.Length == 1) { properties.Sources.AddRange(ParseProjectFile(properties, projectFiles[0])); } var sourceFiles = Directory.GetFiles(properties.ProjectDirectory, "*.cs", (args.Length > 1 && args[1] == "*.cs") ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); foreach (var f in sourceFiles) { if (!f.StartsWith(properties.PackagesDirectory) && !f.StartsWith(properties.OutputDirectory) && !f.StartsWith(properties.ToolsDirectory)) { properties.Sources.Add(f); } } if (args.Length > 1 && args[1] != "*.cs") { var subdirectoryPath = Path.Combine(properties.ProjectDirectory, args[1]); properties.Sources.AddRange(Directory.GetFiles(subdirectoryPath, "*.cs")); } if (properties.Sources.Count == 1) { properties.AssemblyName = Path.GetFileNameWithoutExtension(properties.Sources[0]); } // Packages properties.Packages.Add(@"""Microsoft.NETCore"": ""5.0.0"""); properties.Packages.Add(@"""System.Console"": ""4.0.0-beta-23123"""); //properties.Packages.Add(@"""Microsoft.NETCore.Console"": ""1.0.0-beta-*"""); properties.Packages.Add(@"""Microsoft.NETCore.ConsoleHost-x86"": ""1.0.0-beta-23123"""); properties.Packages.Add(@"""Microsoft.NETCore.Runtime.CoreCLR-x86"": ""1.0.0"""); // References properties.References.Add(Path.Combine(properties.PackagesDirectory, @"System.Runtime\4.0.20\ref\dotnet\System.Runtime.dll")); properties.References.Add(Path.Combine(properties.PackagesDirectory, @"System.Console\4.0.0-beta-23123\ref\dotnet\System.Console.dll")); // Runtime Dependencies properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"Microsoft.NETCore.Runtime.CoreCLR-x86\1.0.0\runtimes\win7-x86\native")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"Microsoft.NETCore.Runtime.CoreCLR-x86\1.0.0\runtimes\win7-x86\lib\dotnet")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Runtime\4.0.20\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Console\4.0.0-beta-23123\lib\DNXCore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.IO\4.0.10\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Threading\4.0.10\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.IO.FileSystem.Primitives\4.0.0\lib\dotnet")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Text.Encoding\4.0.10\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Threading.Tasks\4.0.10\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Text.Encoding.Extensions\4.0.10\lib\netcore50")); properties.Dependencies.Add(Path.Combine(properties.PackagesDirectory, @"System.Runtime.InteropServices\4.0.20\lib\netcore50")); // CSC OPTIONS properties.CscOptions.Add("/nostdlib"); properties.CscOptions.Add("/noconfig"); LogProperties(log, properties, "Initialized Properties Log:", buildDll); Adjust(Path.Combine(properties.ProjectDirectory, "dependencies.txt"), properties.Dependencies); Adjust(Path.Combine(properties.ProjectDirectory, "references.txt"), properties.References); Adjust(Path.Combine(properties.ProjectDirectory, "cscoptions.txt"), properties.CscOptions); Adjust(Path.Combine(properties.ProjectDirectory, "packages.txt"), properties.Packages); LogProperties(log, properties, "Adjusted Properties Log:", buildDll); return properties; }
static void CreateNugetConfig(ProjectProperties properties) { using (var file = new StreamWriter(Path.Combine(properties.ToolsDirectory, "nuget.config"), false)) { file.WriteLine(@"<?xml version = ""1.0"" encoding=""utf-8""?>"); file.WriteLine(@"<configuration>"); file.WriteLine(@" <packageSources>"); file.WriteLine(@" <add key = ""netcore-prototype"" value=""https://www.myget.org/F/netcore-package-prototyping""/>"); file.WriteLine(@" <add key = ""nuget.org"" value = ""https://api.nuget.org/v3/index.json"" protocolVersion = ""3""/>"); file.WriteLine(@" <add key = ""nuget.org"" value = ""https://www.nuget.org/api/v2/""/>"); file.WriteLine(@" </packageSources>"); file.WriteLine(@"</configuration>"); } }