1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
//
// Project.cs
//
// Author:
// jp <>
//
// Copyright (c) 2013-2017 Jean-Philippe Bruyère
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using Crow;
using System.Text.RegularExpressions;
namespace Crow.Coding
{
public class Project: IValueChange
{
#region IValueChange implementation
public event EventHandler<ValueChangeEventArgs> ValueChanged;
public virtual void NotifyValueChanged(string MemberName, object _value)
{
ValueChanged.Raise(this, new ValueChangeEventArgs(MemberName, _value));
}
#endregion
bool isLoaded = false;
bool isExpanded;
XmlDocument xmlDoc;
XmlNode nodeProject;
XmlNode nodeProps;
XmlNodeList nodesItems;
SolutionProject solutionProject;
Crow.Command cmdSave, cmdOpen, cmdCompile, cmdSetAsStartProj, cmdNewFile;
#region CTOR
public Project (Solution sol, SolutionProject sp) {
solutionProject = sp;
solution = sol;
cmdSave = new Crow.Command (new Action (() => Save ()))
{ Caption = "Save", Icon = new SvgPicture ("#Crow.Coding.icons.save.svg"), CanExecute = true };
cmdOpen = new Crow.Command (new Action (() => Load ()))
{ Caption = "Open", Icon = new SvgPicture ("#Crow.Coding.icons.open.svg"), CanExecute = false };
cmdCompile = new Crow.Command (new Action (() => Compile ())) {
Caption = "Compile",
Icon = "#Crow.Coding.icons.compile.svg"
};
cmdSetAsStartProj = new Crow.Command (new Action (() => setAsStartupProject ())) {
Caption = "Set as Startup Project"
};
cmdNewFile = new Crow.Command (new Action (() => AddNewFile ())) {
Caption = "Add New File", Icon = new SvgPicture ("#Crow.Coding.icons.blank-file.svg"), CanExecute = true
};
Commands = new List<Crow.Command> (new Crow.Command[] {cmdOpen,cmdSave,cmdSetAsStartProj,cmdCompile,cmdNewFile});
Load ();
}
#endregion
public Solution solution;
public List<Crow.Command> Commands;
public CompilerResults CompilationResults;
public List<Project> dependantProjects = new List<Project>();
public Project ParentProject = null;
List<ProjectNode> rootItems;
List<ProjectNode> flattenNodes;
public string Name {
get { return solutionProject.ProjectName; }
}
public bool IsLoaded {
get { return isLoaded; }
set {
if (isLoaded == value)
return;
isLoaded = value;
NotifyValueChanged ("IsLoaded", isLoaded);
}
}
public bool IsExpanded
{
get { return isExpanded; }
set
{
if (value == isExpanded)
return;
isExpanded = value;
NotifyValueChanged ("IsExpanded", isExpanded);
}
}
public bool IsStartupProject {
get { return solution.StartupProject == this; }
}
public string Path {
get { return System.IO.Path.Combine (solution.SolutionFolder, solutionProject.RelativePath.Replace('\\','/')); }
}
public string RootDir {
get { return System.IO.Path.GetDirectoryName (Path); }
}
public List<ProjectNode> RootItems {
get { return rootItems; }
}
void buildTreeNodes(){
ProjectNode root = new ProjectNode (this, ItemType.VirtualGroup, RootNamespace);
List<ProjectItem> items = new List<ProjectItem> ();
foreach (XmlNode i in nodesItems) {
foreach (XmlNode f in i.ChildNodes) {
items.Add (new ProjectItem (this, f));
}
}
flattenNodes = new List<ProjectNode> ();
ProjectNode refs = new ProjectNode (this, ItemType.ReferenceGroup, "References");
root.AddChild (refs);
foreach (ProjectItem pn in items) {
switch (pn.Type) {
case ItemType.Reference:
refs.AddChild (pn);
flattenNodes.Add (pn);
break;
case ItemType.ProjectReference:
ProjectReference pr = new ProjectReference (pn);
refs.AddChild (pr);
flattenNodes.Add (pr);
break;
case ItemType.Compile:
case ItemType.None:
case ItemType.EmbeddedResource:
ProjectNode curNode = root;
string[] folds = pn.Path.Split ('/');
for (int i = 0; i < folds.Length - 1; i++) {
ProjectNode nextNode = curNode.ChildNodes.FirstOrDefault (n => n.DisplayName == folds [i] && n.Type == ItemType.VirtualGroup);
if (nextNode == null) {
nextNode = new ProjectNode (this, ItemType.VirtualGroup, folds [i]);
curNode.AddChild (nextNode);
}
curNode = nextNode;
}
ProjectNode f = null;
switch (pn.Extension) {
case ".crow":
case ".template":
case ".goml":
case ".itemp":
case ".imtl":
f = new ImlProjectItem (pn);
break;
case ".style":
f = new StyleProjectItem (pn);
break;
default:
f = new ProjectFile (pn);
break;
}
curNode.AddChild (f);
flattenNodes.Add (f);
break;
}
}
root.SortChilds ();
rootItems = root.ChildNodes;
}
#region Project properties
public string ToolsVersion {
get { return nodeProject?.Attributes ["ToolsVersion"]?.Value; }
}
public string DefaultTargets {
get { return nodeProject?.Attributes ["DefaultTargets"]?.Value; }
}
public string ProjectGuid {
get { return solutionProject.ProjectGuid; }
}
public string AssemblyName {
get { return nodeProps["AssemblyName"]?.InnerText; }
}
public string OutputType {
get { return nodeProps["OutputType"]?.InnerText; }
}
public string RootNamespace {
get { return nodeProps["RootNamespace"]?.InnerText; }
}
public bool AllowUnsafeBlocks {
get {return nodeProps["AllowUnsafeBlocks"] == null ? false :
bool.Parse (nodeProps["AllowUnsafeBlocks"]?.InnerText); }
}
public bool NoStdLib {
get { return nodeProps["NoStdLib"] == null ? false :
bool.Parse (nodeProps["NoStdLib"]?.InnerText); }
}
public bool TreatWarningsAsErrors {
get { return nodeProps["TreatWarningsAsErrors"] == null ? false:
bool.Parse (nodeProps["TreatWarningsAsErrors"]?.InnerText); }
}
public bool SignAssembly {
get { return bool.Parse (nodeProps["SignAssembly"]?.InnerText); }
}
public string TargetFrameworkVersion {
get { return nodeProps["TargetFrameworkVersion"]?.InnerText; }
}
public string Description {
get { return nodeProps["Description"]?.InnerText; }
}
public string OutputPath {
get { return nodeProps["OutputPath"]?.InnerText; }
}
public string IntermediateOutputPath {
get { return nodeProps["IntermediateOutputPath"]?.InnerText; }
}
public string StartupObject {
get { return nodeProps["StartupObject"]?.InnerText; }
}
public bool DebugSymbols {
get { return nodeProps["DebugSymbols"] == null ? false : bool.Parse( nodeProps["DebugSymbols"]?.InnerText); }
}
public int WarningLevel {
get { return nodeProps["WarningLevel"] == null ? 0 : int.Parse(nodeProps["WarningLevel"]?.InnerText); }
}
#endregion
public void AddNewFile () {
Window.Show(CrowIDE.MainIFace, "#Crow.Coding.ui.NewFile.crow",true).DataSource = this;
}
public void Load () {
xmlDoc = new XmlDocument();
using (Stream ins = new FileStream (this.Path, FileMode.Open)) {
xmlDoc.Load (new XmlTextReader(ins) { Namespaces = false });
}
nodeProject = xmlDoc.SelectSingleNode("Project");
XmlNodeList nodesProps = xmlDoc.SelectNodes ("/Project/PropertyGroup");
foreach (XmlNode n in nodesProps) {
if (n.Attributes ["Condition"] == null)
nodeProps = n;
}
nodesItems = xmlDoc.SelectNodes ("/Project/ItemGroup");
if (ProjectGuid != solutionProject.ProjectGuid)
throw new Exception ("Project GUID not matching with solution");
buildTreeNodes ();
IsLoaded = true;
}
public void Save () {
}
void setAsStartupProject () {
solution.StartupProject = this;
}
static Regex regexDirTokens = new Regex(@"\$\(([^\)]*)\)|([^\$]*)");
string getDirectoryWithTokens (string dir){
Match m = regexDirTokens.Match (dir);
string tmp = "";
while (m.Success)
{
if (m.Value == @"$(SolutionDir)")
tmp = System.IO.Path.Combine (tmp, solution.SolutionFolder);
else if (m.Value == @"$(Configuration)")
tmp = System.IO.Path.Combine (tmp, "Debug");
else
tmp = System.IO.Path.Combine (tmp, m.Value);
if (tmp.EndsWith (@"\")||tmp.EndsWith (@"/"))
tmp = tmp.Remove (tmp.Length - 1);
m = m.NextMatch();
}
return tmp;
}
public string Compile () {
if (ParentProject != null)
ParentProject.Compile ();
CSharpCodeProvider cp = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
foreach (ProjectReference pr in flattenNodes.OfType<ProjectReference>()) {
Project p = solution.Projects.FirstOrDefault (pp => pp.ProjectGuid == pr.ProjectGUID);
if (p == null)
throw new Exception ("referenced project not found");
parameters.ReferencedAssemblies.Add (p.Compile ());
}
string outputDir = getDirectoryWithTokens(this.OutputPath);
string objDir = getDirectoryWithTokens (this.IntermediateOutputPath);
Directory.CreateDirectory (outputDir);
Directory.CreateDirectory (objDir);
parameters.OutputAssembly = System.IO.Path.Combine (outputDir, this.AssemblyName);
// True - exe file generation, false - dll file generation
if (this.OutputType == "Library") {
parameters.GenerateExecutable = false;
parameters.CompilerOptions += " /target:library";
parameters.OutputAssembly += ".dll";
} else {
parameters.GenerateExecutable = true;
parameters.CompilerOptions += " /target:exe";
parameters.OutputAssembly += ".exe";
parameters.MainClass = this.StartupObject;
}
parameters.GenerateInMemory = false;
parameters.IncludeDebugInformation = this.DebugSymbols;
parameters.TreatWarningsAsErrors = this.TreatWarningsAsErrors;
parameters.WarningLevel = this.WarningLevel;
parameters.CompilerOptions += " /noconfig";
if (this.AllowUnsafeBlocks)
parameters.CompilerOptions += " /unsafe";
parameters.CompilerOptions += " /delaysign+";
parameters.CompilerOptions += " /debug:full /debug+";
parameters.CompilerOptions += " /optimize-";
parameters.CompilerOptions += " /define:\"DEBUG;TRACE\"";
parameters.CompilerOptions += " /nostdlib";
foreach (ProjectItem pi in flattenNodes.Where (p=>p.Type == ItemType.Reference)) {
if (string.IsNullOrEmpty (pi.HintPath)) {
parameters.CompilerOptions += " /reference:/usr/lib/mono/4.5/" + pi.Path + ".dll";
continue;
}
parameters.ReferencedAssemblies.Add (pi.Path);
string fullHintPath = System.IO.Path.GetFullPath(System.IO.Path.Combine (RootDir, pi.HintPath.Replace('\\','/')));
if (File.Exists (fullHintPath)) {
string outPath = System.IO.Path.Combine (outputDir, System.IO.Path.GetFileName (fullHintPath));
if (!File.Exists(outPath))
File.Copy (fullHintPath, outPath);
}
}
parameters.CompilerOptions += " /reference:/usr/lib/mono/4.5/System.Core.dll";
parameters.CompilerOptions += " /reference:/usr/lib/mono/4.5/mscorlib.dll";
//parameters.ReferencedAssemblies.Add ("System.Core");
//parameters.ReferencedAssemblies.Add ("mscorlib.dll");
IEnumerable<ProjectFile> pfs = flattenNodes.OfType<ProjectFile> ();
foreach (ProjectFile pi in pfs.Where (p => p.Type == ItemType.EmbeddedResource)) {
string absPath = pi.AbsolutePath;
string logicName = pi.LogicalName;
if (string.IsNullOrEmpty (logicName))
parameters.CompilerOptions += string.Format (" /resource:{0},{1}", absPath , this.Name + "." + pi.Path.Replace('/','.'));
else
parameters.CompilerOptions += string.Format (" /resource:{0},{1}", absPath, logicName);
}
foreach (ProjectFile pi in pfs.Where (p => p.Type == ItemType.None)) {
if (pi.CopyToOutputDirectory == CopyToOutputState.Never)
continue;
string source = pi.AbsolutePath;
string target = System.IO.Path.Combine (outputDir, pi.Path);
Directory.CreateDirectory (System.IO.Path.GetDirectoryName (target));
if (File.Exists (target)) {
if (pi.CopyToOutputDirectory == CopyToOutputState.PreserveNewest) {
if (DateTime.Compare (
System.IO.File.GetLastWriteTime (source),
System.IO.File.GetLastWriteTime (target)) < 0)
continue;
}
File.Delete (target);
}
System.Diagnostics.Debug.WriteLine ("copy " + source + " to " + target);
File.Copy (source, target);
}
string[] files = pfs.Where (p => p.Type == ItemType.Compile).Select (p => p.AbsolutePath).ToArray();
System.Diagnostics.Debug.WriteLine("---- start compilation of :" + parameters.OutputAssembly);
System.Diagnostics.Debug.WriteLine (parameters.CompilerOptions);
CompilationResults = cp.CompileAssemblyFromFile(parameters, files);
solution.UpdateErrorList ();
return parameters.OutputAssembly;
}
public bool TryGetProjectFileFromAbsolutePath (string absolutePath, out ProjectFile pi){
pi = flattenNodes.OfType<ProjectFile> ().FirstOrDefault
(pp => pp.AbsolutePath == absolutePath);
return pi != null;
}
public bool TryGetProjectFileFromPath (string path, out ProjectFile pi){
if (path.StartsWith ("#"))
pi = flattenNodes.OfType<ProjectFile> ().FirstOrDefault
(pp => pp.Type == ItemType.EmbeddedResource && pp.ResourceID == path.Substring (1));
else
pi = flattenNodes.OfType<ProjectFile> ().FirstOrDefault (pp => pp.Path == path);
if (pi != null)
return true;
foreach (ProjectReference pr in flattenNodes.OfType<ProjectReference>()) {
Project p = solution.Projects.FirstOrDefault (pp => pp.ProjectGuid == pr.ProjectGUID);
if (p == null)
throw new Exception ("referenced project not found");
if (p.TryGetProjectFileFromPath (path, out pi))
return true;
}
//TODO: search referenced assemblies
return false;
}
public void GetDefaultTemplates () {
IEnumerable<ProjectFile> tmpFiles =
flattenNodes.OfType<ProjectFile> ().Where (pp => pp.Extension == ".template" );
foreach (ProjectFile pi in tmpFiles.Where(
pp=>pp.Type == ItemType.None && pp.CopyToOutputDirectory != CopyToOutputState.Never)) {
string clsName = System.IO.Path.GetFileNameWithoutExtension(pi.Path);
if (solution.DefaultTemplates.ContainsKey (clsName))
continue;
solution.DefaultTemplates [clsName] = pi.AbsolutePath;
}
foreach (ProjectFile pi in tmpFiles.Where(pp=>pp.Type == ItemType.EmbeddedResource)) {
string resId = pi.ResourceID;
string clsName = resId.Substring (0, resId.Length - 9);
if (solution.DefaultTemplates.ContainsKey (clsName))
continue;
solution.DefaultTemplates [clsName] = pi.Path;
}
foreach (ProjectReference pr in flattenNodes.OfType<ProjectReference>()) {
Project p = solution.Projects.FirstOrDefault (pp => pp.ProjectGuid == pr.ProjectGUID);
if (p == null)
throw new Exception ("referenced project not found");
p.GetDefaultTemplates ();
}
}
// void searchTemplatesIn(Assembly assembly){
// if (assembly == null)
// return;
// foreach (string resId in assembly
// .GetManifestResourceNames ()
// .Where (r => r.EndsWith (".template", StringComparison.OrdinalIgnoreCase))) {
// string clsName = resId.Substring (0, resId.Length - 9);
// if (DefaultTemplates.ContainsKey (clsName))
// continue;
// DefaultTemplates[clsName] = "#" + resId;
// }
// }
public void GetStyling () {
try {
foreach (ProjectFile pi in flattenNodes.OfType<ProjectFile> ().Where (pp=>pp.Type == ItemType.EmbeddedResource && pp.Extension == ".style")) {
using (Stream s = new MemoryStream (System.Text.Encoding.UTF8.GetBytes(pi.Source))) {
new StyleReader (solution.Styling, s, pi.ResourceID);
}
}
} catch (Exception ex) {
Console.WriteLine (ex.ToString ());
}
foreach (ProjectReference pr in flattenNodes.OfType<ProjectReference>()) {
Project p = solution.Projects.FirstOrDefault (pp => pp.ProjectGuid == pr.ProjectGUID);
if (p == null)
throw new Exception ("referenced project not found");
p.GetStyling();
}
//TODO:get styling from referenced assemblies
}
}
}
|