diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2020-02-18 16:52:31 +0100 |
|---|---|---|
| committer | j-p <jp_bruyere@hotmail.com> | 2020-05-10 00:50:02 +0200 |
| commit | 81a8401cfd8942ae06afc876ddd46e632922fd53 (patch) | |
| tree | b449882e9240314d6a678385f81a8abf413ae69b /Samples | |
| parent | 78ff96edf53ba273aa157a5bda2aa5b7a10f1c5f (diff) | |
basic moving and selecting with SourceText as backend buffer, basic highlighting with syntaxTree, save commit before using msbuild workspace
Diffstat (limited to 'Samples')
| -rw-r--r-- | Samples/BasicTests/BasicTests.cs | 4 | ||||
| -rw-r--r-- | Samples/HelloWorld/HelloWorld.csproj | 28 | ||||
| -rw-r--r-- | Samples/HelloWorld/ILView.cs | 26 | ||||
| -rw-r--r-- | Samples/HelloWorld/main.cs | 444 | ||||
| -rw-r--r-- | Samples/HelloWorld/ui/helloworld.crow | 3 |
5 files changed, 440 insertions, 65 deletions
diff --git a/Samples/BasicTests/BasicTests.cs b/Samples/BasicTests/BasicTests.cs index 54bb8215..cc2285d6 100644 --- a/Samples/BasicTests/BasicTests.cs +++ b/Samples/BasicTests/BasicTests.cs @@ -30,9 +30,9 @@ namespace tests //testFiles = new string [] { @"Interfaces/Experimental/testDock.crow" }; //testFiles = new string [] { @"Interfaces/Divers/welcome.crow" }; - testFiles = new string [] { @"Interfaces/TemplatedGroup/3.crow" }; + //testFiles = new string [] { @"Interfaces/TemplatedGroup/3.crow" }; //testFiles = new string [] { @"Interfaces/Divers/perfMeasures.crow" }; - //testFiles = new string [] { @"Interfaces/Divers/colorPicker.crow" }; + testFiles = new string [] { @"Interfaces/Divers/colorPicker2.crow" }; testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/GraphicObject", "*.crow")).ToArray (); testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Container", "*.crow")).ToArray (); testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Group", "*.crow")).ToArray (); diff --git a/Samples/HelloWorld/HelloWorld.csproj b/Samples/HelloWorld/HelloWorld.csproj index 908af2fc..7d3574d0 100644 --- a/Samples/HelloWorld/HelloWorld.csproj +++ b/Samples/HelloWorld/HelloWorld.csproj @@ -23,7 +23,31 @@ <LogicalName>HelloWorld.%(Filename)%(Extension)</LogicalName> </EmbeddedResource> </ItemGroup> - <ItemGroup> - <PackageReference Include="Mono.Cecil" Version="0.11.1" /> + <ItemGroup> + <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.4.0" /> +<!-- <PackageReference Include="Microsoft.CodeAnalysis.Features" Version="3.4.0" />--> + <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.4.0" /> + <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" /> + <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" Version="3.4.0" /> </ItemGroup> + + <ItemGroup> + <PackageReference Include="SQLitePCLRaw.core" Version="2.0.2" /> + <PackageReference Include="System.Memory" Version="4.5.3" /> + </ItemGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.Build" Version="16.4.0"> + <IncludeAssets>compile</IncludeAssets> + </PackageReference> + <PackageReference Include="Microsoft.Build.Framework" Version="16.4.0"> + <IncludeAssets>compile</IncludeAssets> + </PackageReference> + <PackageReference Include="Microsoft.Build.Tasks.Core" Version="16.4.0"> + <IncludeAssets>compile</IncludeAssets> + </PackageReference> + <PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.4.0"> + <IncludeAssets>compile</IncludeAssets> + </PackageReference> + </ItemGroup> </Project> diff --git a/Samples/HelloWorld/ILView.cs b/Samples/HelloWorld/ILView.cs index 9bac2299..cb974801 100644 --- a/Samples/HelloWorld/ILView.cs +++ b/Samples/HelloWorld/ILView.cs @@ -1,15 +1,35 @@ -// Copyright (c) 2020 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> -// -// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +/* Copyright (c) 2020 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> + * + * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + */ + using System; using Crow; +namespace test { + public class TestClass { + int a = 0; + } +} + namespace HelloWorld { public class ILView : Widget { + readonly int a; + #region CTOR public ILView () { + a = 10; } + #endregion + void test () + { +#if DEBUG +Console.WriteLine ("debug" + a.ToString()); + +#endif + } + } } diff --git a/Samples/HelloWorld/main.cs b/Samples/HelloWorld/main.cs index c407b04e..75a2683b 100644 --- a/Samples/HelloWorld/main.cs +++ b/Samples/HelloWorld/main.cs @@ -1,66 +1,296 @@ using System; +using System.CodeDom.Compiler; +using System.Collections.Generic; +using System.Composition; +using System.IO; using System.Linq; using System.Reflection; using System.Reflection.Emit; +using System.Threading.Tasks; using Crow; using Crow.IML; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Completion; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Emit; +using Microsoft.CodeAnalysis.Host.Mef; +using Microsoft.CodeAnalysis.MSBuild; +using Microsoft.CodeAnalysis.Text; -namespace HelloWorld +/*namespace RoslynVarRewrite { - class Program : Interface { - Command CMDQuit; - static void Main (string[] args) { - using (Program vke = new Program ()) { - vke.Run (); - } + public class VarRewriter : CSharpSyntaxRewriter + { + private readonly SemanticModel model; + + public VarRewriter (SemanticModel model) + { + this.model = model; } - - protected override void Startup () + + public override SyntaxNode VisitLocalDeclarationStatement (LocalDeclarationStatementSyntax node) { - CMDQuit = new Command (new Action (() => running = false)) { Caption = "Quit", Icon = new SvgPicture ("#Crow.Icons.exit-symbol.svg") }; + var symbolInfo = model.GetSymbolInfo (node.Declaration.Type); + var typeSymbol = symbolInfo.Symbol; + var type = typeSymbol.ToDisplayString ( + SymbolDisplayFormat.MinimallyQualifiedFormat); - Widget w = Load ("#HelloWorld.helloworld.crow"); - w.KeyPress += W_KeyPress; - w.DataSource = this; + var declaration = SyntaxFactory + .LocalDeclarationStatement ( + SyntaxFactory + .VariableDeclaration (SyntaxFactory.IdentifierName ( + SyntaxFactory.Identifier (type))) + .WithVariables (node.Declaration.Variables) + .NormalizeWhitespace () + ) + .WithTriviaFrom (node); + return declaration; } + } - public SolidColor testColor = Color.Red; - public SolidColor TestColor { - get => testColor; - set { - if (testColor == value) - return; - testColor = value; - NotifyValueChanged ("TestColor", testColor); - } + class Program + { + static void Main (string [] args) + { + var tree = CSharpSyntaxTree.ParseText (@" +using System; +using System.Collections.Generic; +class Program { + static void Main(string[] args) { + var x = 5; + var s = ""Test string""; + var l = new List<string>(); + var scores = new byte[8][]; // Test comment + var names = new string[3] {""Diego"", ""Dani"", ""Seba""}; + } +}"); + + // Get the assembly file, the compilation and the semantic model + var Mscorlib = MetadataReference.CreateFromFile (typeof (object).Assembly.Location); + var compilation = CSharpCompilation.Create ("RoslynVarRewrite", + syntaxTrees: new [] { tree }, + references: new [] { Mscorlib }); + var model = compilation.GetSemanticModel (tree); + + var varRewriter = new VarRewriter (model); + var result = varRewriter.Visit (tree.GetRoot ()); + Console.WriteLine (result.ToFullString ()); } + } +}*/ +namespace HelloWorld +{ + class Program : Interface + { + //static void Main () + //{ + // using (Stream s = new FileStream("test.txt", FileMode.Create)) { + // using (StreamWriter sw = new StreamWriter (s)) { + // foreach (SyntaxKind v in Enum.GetValues(typeof(SyntaxKind))) { + // //sw.WriteLine ($"{v,-50} = {(int)v:X8}"); + // //sw.WriteLine ($"{v,-50} = {Convert.ToString ((int)v, 2).PadLeft (16, '0')}"); + // sw.WriteLine ($"case SyntaxKind.{v}:"); + // sw.WriteLine ($"\ttf = editor.formatting [\"default\"];"); + // sw.WriteLine ($"\tbreak;"); + // } + // } + // } + //} + // public class CustomWalker : CSharpSyntaxWalker + // { + // static int Tabs = 0; + // bool cancel; + // int visibleLines, firstLine, currentLine, printedLines; + + // public CustomWalker (int firstLine = 0, int visibleLines = 1) : base (SyntaxWalkerDepth.StructuredTrivia) + // { + // this.visibleLines = visibleLines; + // this.firstLine = firstLine; + // currentLine = 0; + // printedLines = (firstLine == 0) ? 0 : - 1;//<0 until firstLine is reached + + // } + // public override void DefaultVisit (SyntaxNode node) + // { + // if (!cancel) + // base.DefaultVisit (node); + // } + // public override void Visit (SyntaxNode node) + // { + // if (cancel) + // return; - Color [] colors = { Color.Blue, Color.DarkGoldenRod, Color.Red, Color.Azure, Color.Brown, Color.Black, Color.White, Color.Pink }; - int ptr = 0; + // FileLinePositionSpan ls = node.SyntaxTree.GetLineSpan(node.FullSpan); + // currentLine = ls.StartLinePosition.Line; + // if (ls.EndLinePosition.Line >= firstLine) + // base.Visit (node); + // } + // public override void VisitToken (SyntaxToken token) + // { + // if (cancel) + // return; - void W_KeyPress (object sender, KeyPressEventArgs e) + // if ((int)Depth >= 2) { + // Console.ForegroundColor = ConsoleColor.Blue; + // VisitLeadingTrivia (token); + // if (cancel) + // return; + // if (printedLines >= 0) { + // Console.ForegroundColor = ConsoleColor.White; + // Console.Write ($"{(token.RawKind)}{token.ToString ()}"); + // } + // Console.ForegroundColor = ConsoleColor.Green; + // VisitTrailingTrivia (token); + // } + // } + + // bool print => printedLines >= 0 && printedLines < visibleLines; + + // //public override void VisitLeadingTrivia (SyntaxToken token) + // //{ + // // leadingTrivia = true; + // // base.VisitLeadingTrivia (token); + // //} + // //public override void VisitTrailingTrivia (SyntaxToken token) + // //{ + // // if (cancel) + // // return; + // // leadingTrivia = false; + // // base.VisitTrailingTrivia (token); + // //} + // //bool leadingTrivia; + // public override void VisitTrivia (SyntaxTrivia trivia) + // { + + // if (cancel) + // return; + + // base.VisitTrivia (trivia); + + // if (print) { + // if (trivia.IsKind (SyntaxKind.EndOfLineTrivia)) { + // Console.Write ($"(eof)"); + // printedLines++; + // cancel = printedLines == visibleLines; + // } + // Console.Write ($"{trivia.ToString ()}"); + // } + + // if (trivia.IsKind (SyntaxKind.EndOfLineTrivia)) { + // currentLine++; + // if (printedLines < 0) { + // if (currentLine == firstLine) + // printedLines = 0; + // } + // } + + // } + // } + + // static void Main (string [] args) + // { + // var tree = CSharpSyntaxTree.ParseText (@"// Copyright (c) 2013-2019 Bruyère Jean-Philippe <jp_bruyere@hotmail.com> + //// + //// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + + //using System; + //using System.IO; + + //public class MyClass + //{ + // //this is a leading trivia + // public void MyMethod() + // { + // int a = 10; + //#if DEBUG + // int b = a + 10; + //#endif + // } + ///* this is a block comment + //*/ + // public void MyMethod(int n) + // { + // } + //}"); + + // var walker = new CustomWalker (4,5); + // walker.Visit (tree.GetRoot ()); + //} + + //Command CMDQuit; + static void Main (string [] args) { - switch (e.KeyChar) { - case 'w': - LoadIMLFragment ($"<DockWindow Name='win{ptr}' Left='450' Top='450' Width='150' Height='150' Background='{colors [ptr]}'/>"); - break; - case 'x': - LoadIMLFragment ($"<Window Name='win{ptr}' Left='450' Top='450' Width='150' Height='150' Background='{colors [ptr]}'/>"); - break; + Environment.SetEnvironmentVariable ("MSBUILD_EXE_PATH", Path.Combine (msbuildRoot, "MSBuild.dll")); + Environment.SetEnvironmentVariable ("MSBUILD_NUGET_PATH", "/home/jp/.nuget/packages"); + Environment.SetEnvironmentVariable ("COREHOST_TRACE", "1"); + + AppDomain currentDomain = AppDomain.CurrentDomain; + currentDomain.AssemblyResolve += msbuildAssembliesResolve; + + using (Program vke = new Program ()) { + vke.Run (); } + } + + //const string msbuildRoot = "/usr/lib/mono/msbuild/Current/bin/"; + const string msbuildRoot = "/usr/share/dotnet/sdk/3.1.101/"; + const string toolsVersion = "Current"; - ptr++; - if (ptr == colors.Length) - ptr = 0; + static Assembly msbuildAssembliesResolve (object sender, ResolveEventArgs args) + { + string assemblyPath = Path.Combine (msbuildRoot, new AssemblyName (args.Name).Name + ".dll"); + /*Console.BackgroundColor = ConsoleColor.DarkBlue; + Console.ForegroundColor = ConsoleColor.White; + Console.WriteLine ($"Probing: {assemblyPath}"); + Console.ResetColor ();*/ + if (!File.Exists (assemblyPath)) return null; + Assembly assembly = Assembly.LoadFrom (assemblyPath); + return assembly; } + + //protected override void Startup () + //{ + // CMDQuit = new Command (new Action (() => running = false)) { Caption = "Quit", Icon = new SvgPicture ("#Crow.Icons.exit-symbol.svg") }; + + // Widget w = Load ("#HelloWorld.helloworld.crow"); + // w.DataSource = this; + //} + + //public string source = @" + // using System; + + // namespace RoslynCompileSample + // { + // public class Writer + // { + // public void Write(string message) + // { + // Console.WriteLine(message); + // } + // } + // }"; + //public string Source { + // get => source; + // set { + // if (source == value) + // return; + // source = value; + // NotifyValueChanged ("Source", source); + // } + //} + + + public override bool OnKeyDown (Key key) { switch (key) { - case Key.d: - dump (); - break; - case Key.space: - TestColor = Color.Green; + //case Key.F2: + //compile (); + //break; + case Key.F3: + Task t = new Task (testWS); + t.Start (); break; default: return base.OnKeyDown (key); @@ -68,33 +298,135 @@ namespace HelloWorld return true; } - void dump () + //void printChild (IndentedTextWriter stream, CSharpSyntaxNode n) + //{ + // stream.WriteLine ($"{n.Kind()} {n.ToString()}"); + // stream.Indent++; + // foreach (CSharpSyntaxNode item in n.ChildNodes()) { + // printChild (stream, item); + // } + // stream.Indent--; + //} + string slnPath = "/mnt/devel/glfw-sharp/glfw-sharp.sln"; + async void testWS () { - Instantiator inst = Instantiators ["#HelloWorld.helloworld.crow"]; + if (!File.Exists (slnPath)) { + Console.WriteLine ($"File not found: {slnPath}"); + return; + } + Dictionary<String, String> globalProperties = new Dictionary<String, String> (); - foreach (Delegate cd in inst.CachedDelegates) { - FieldInfo mb = typeof(Delegate).GetField("original_method_info", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); + Console.WriteLine ("starting Compilation."); - DynamicMethod dynMethod = mb.GetValue (cd) as DynamicMethod; - /*Console.WriteLine (typeof (ILGenerator).GetFields (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField)); + //globalProperties ["NuGetPackageRoot"] = "/home/jp/.nuget/packages"; + //globalProperties ["RoslynTargetsPath"] = Path.Combine (msbuildRoot, "Roslyn/"); + //globalProperties ["MSBuildSDKsPath"] = Path.Combine (msbuildRoot, "Sdks/"); + //globalProperties ["AdditionalLibPaths"] = msbuildRoot; + //globalProperties ["AssemblySearchPaths"] = msbuildRoot; + foreach (var item in MSBuildMefHostServices.DefaultAssemblies) { + Console.WriteLine ($"{item.FullName}"); + } + + var host = MefHostServices.Create (MSBuildMefHostServices.DefaultAssemblies); - var ilgen = dynMethod.GetILGenerator (); - + using (var workspace = MSBuildWorkspace.Create (globalProperties)) { + //workspace. Properties["BuildingInsideVisualStudio"] = "false"; + workspace.WorkspaceFailed += (sender, e) => Console.WriteLine ($"Workspace error: {e.Diagnostic}"); + Console.WriteLine ($"Opening Solution {slnPath}"); + var solution = await workspace.OpenSolutionAsync (slnPath, new ProgressLog ()); + Console.WriteLine ($"Proj Count:{solution.Projects.Count ()}"); + foreach (Project project in solution.Projects) { + Console.WriteLine ($"Compiling project:{project.FilePath}"); + Compilation compilation = await project.GetCompilationAsync (); + Document doc = project.Documents.First (); -*/ + CompletionService.GetService (doc); + using (var ms = new MemoryStream ()) { + EmitResult result = compilation.Emit (ms); - MethodBody body = dynMethod.GetMethodBody (); + if (!result.Success) { + IEnumerable<Diagnostic> failures = result.Diagnostics.Where (diagnostic => + diagnostic.IsWarningAsError || + diagnostic.Severity == DiagnosticSeverity.Error); + + foreach (Diagnostic diagnostic in failures) { + Console.Error.WriteLine ("{0}: {1}", diagnostic.Id, diagnostic.GetMessage ()); + } + } else { + Console.WriteLine ("Compilation ok."); + } + } - byte [] il = body.GetILAsByteArray (); - foreach (byte b in il) { - Console.Write ($"{b:x2} "); - //Console.WriteLine (b); } + //workspace.TryApplyChanges() } - + Console.WriteLine ($"end."); + } + } + class ProgressLog : IProgress<ProjectLoadProgress> + { + public void Report (ProjectLoadProgress value) + { + Console.WriteLine ($"{value.ElapsedTime} {value.Operation} {value.TargetFramework}"); } - } + } +//void compile () +//{ +// string assemblyName = Path.GetRandomFileName (); +// MetadataReference [] references = new MetadataReference [] +// { +// MetadataReference.CreateFromFile(typeof(object).Assembly.Location), +// MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location) +// }; +// SourceText txt = SourceText.From (source); + +// SyntaxTree syntaxTree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText (txt); +// //CSharpCompilationOptions options = new CSharpCompilationOptions (); +// CSharpCompilation compilation = CSharpCompilation.Create( +// assemblyName, +// syntaxTrees: new [] { syntaxTree }, +// references: references, +// options: new CSharpCompilationOptions (OutputKind.DynamicallyLinkedLibrary)); + + +// CSharpSyntaxNode n = (CSharpSyntaxNode)syntaxTree.GetRoot (); + +// using (StringWriter sw = new StringWriter ()) { +// using (IndentedTextWriter itw = new IndentedTextWriter (sw)) +// printChild (itw, n); + +// Console.Write (sw); +// } + +// using (var ms = new MemoryStream ()) { +// EmitResult result = compilation.Emit (ms); + +// if (!result.Success) { +// IEnumerable<Diagnostic> failures = result.Diagnostics.Where (diagnostic => +// diagnostic.IsWarningAsError || +// diagnostic.Severity == DiagnosticSeverity.Error); + +// foreach (Diagnostic diagnostic in failures) { +// Console.Error.WriteLine ("{0}: {1}", diagnostic.Id, diagnostic.GetMessage ()); +// } +// } else { +// ms.Seek (0, SeekOrigin.Begin); +// Assembly assembly = Assembly.Load (ms.ToArray ()); +// Type type = assembly.GetType ("RoslynCompileSample.Writer"); +// object obj = Activator.CreateInstance (type); +// type.InvokeMember ("Write", BindingFlags.Default | BindingFlags.InvokeMethod, +// null, +// obj, +// new object [] { "Hello World" }); +// } +// } + + + +//} +// } +//} diff --git a/Samples/HelloWorld/ui/helloworld.crow b/Samples/HelloWorld/ui/helloworld.crow index 1bc9601f..f18e4bd8 100644 --- a/Samples/HelloWorld/ui/helloworld.crow +++ b/Samples/HelloWorld/ui/helloworld.crow @@ -1,6 +1,5 @@ <?xml version="1.0"?> <VerticalStack> - <TextBox Text="{TestColor}"/> - <ColorPicker CurrentColor="{²TestColor}" /> + <TextBox Text="{²Source}" Multiline="true" Height="50%" Width="Stretched" TextAlignment="TopLeft" /> </VerticalStack> <!--<ScrollBar Orientation="Vertical" Height="Stretched" Width="10" />-->
\ No newline at end of file |